opal-rspec-cj 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/.gitmodules +15 -0
- data/.travis.yml +13 -0
- data/.yardopts +5 -0
- data/CHANGELOG.md +25 -0
- data/Gemfile +8 -0
- data/README.md +147 -0
- data/Rakefile +26 -0
- data/config.ru +10 -0
- data/example/Gemfile +4 -0
- data/example/README.md +13 -0
- data/example/Rakefile +8 -0
- data/example/opal/user.rb +11 -0
- data/example/spec/user_spec.rb +15 -0
- data/lib/opal-rspec.rb +2 -0
- data/lib/opal/rspec.rb +20 -0
- data/lib/opal/rspec/rake_task.rb +63 -0
- data/lib/opal/rspec/version.rb +5 -0
- data/opal-rspec.gemspec +21 -0
- data/opal/opal-rspec.rb +1 -0
- data/opal/opal/rspec.rb +25 -0
- data/opal/opal/rspec/async.rb +289 -0
- data/opal/opal/rspec/browser_formatter.rb +188 -0
- data/opal/opal/rspec/fixes.rb +116 -0
- data/opal/opal/rspec/requires.rb +45 -0
- data/opal/opal/rspec/runner.rb +69 -0
- data/opal/opal/rspec/sprockets_runner.rb.erb +11 -0
- data/opal/opal/rspec/text_formatter.rb +74 -0
- data/spec/async_spec.rb +38 -0
- data/spec/example_spec.rb +163 -0
- data/spec/matchers_spec.rb +201 -0
- data/spec/mock_spec.rb +63 -0
- data/spec/named_subject_spec.rb +11 -0
- data/spec/should_syntax_spec.rb +17 -0
- data/vendor/spec_runner.js +50 -0
- data/vendor_lib/rspec-expectations.rb +1 -0
- data/vendor_lib/rspec.rb +3 -0
- data/vendor_lib/rspec/autorun.rb +2 -0
- data/vendor_lib/rspec/core.rb +203 -0
- data/vendor_lib/rspec/core/backport_random.rb +302 -0
- data/vendor_lib/rspec/core/backtrace_formatter.rb +65 -0
- data/vendor_lib/rspec/core/command_line.rb +36 -0
- data/vendor_lib/rspec/core/configuration.rb +1129 -0
- data/vendor_lib/rspec/core/configuration_options.rb +143 -0
- data/vendor_lib/rspec/core/drb_command_line.rb +26 -0
- data/vendor_lib/rspec/core/drb_options.rb +87 -0
- data/vendor_lib/rspec/core/dsl.rb +26 -0
- data/vendor_lib/rspec/core/example.rb +312 -0
- data/vendor_lib/rspec/core/example_group.rb +540 -0
- data/vendor_lib/rspec/core/filter_manager.rb +224 -0
- data/vendor_lib/rspec/core/flat_map.rb +17 -0
- data/vendor_lib/rspec/core/formatters.rb +54 -0
- data/vendor_lib/rspec/core/formatters/base_formatter.rb +291 -0
- data/vendor_lib/rspec/core/formatters/base_text_formatter.rb +307 -0
- data/vendor_lib/rspec/core/formatters/deprecation_formatter.rb +193 -0
- data/vendor_lib/rspec/core/formatters/documentation_formatter.rb +67 -0
- data/vendor_lib/rspec/core/formatters/helpers.rb +82 -0
- data/vendor_lib/rspec/core/formatters/html_formatter.rb +155 -0
- data/vendor_lib/rspec/core/formatters/html_printer.rb +408 -0
- data/vendor_lib/rspec/core/formatters/json_formatter.rb +99 -0
- data/vendor_lib/rspec/core/formatters/progress_formatter.rb +32 -0
- data/vendor_lib/rspec/core/formatters/snippet_extractor.rb +101 -0
- data/vendor_lib/rspec/core/hooks.rb +535 -0
- data/vendor_lib/rspec/core/memoized_helpers.rb +431 -0
- data/vendor_lib/rspec/core/metadata.rb +313 -0
- data/vendor_lib/rspec/core/mocking/with_absolutely_nothing.rb +11 -0
- data/vendor_lib/rspec/core/mocking/with_flexmock.rb +27 -0
- data/vendor_lib/rspec/core/mocking/with_mocha.rb +52 -0
- data/vendor_lib/rspec/core/mocking/with_rr.rb +27 -0
- data/vendor_lib/rspec/core/mocking/with_rspec.rb +27 -0
- data/vendor_lib/rspec/core/option_parser.rb +234 -0
- data/vendor_lib/rspec/core/ordering.rb +154 -0
- data/vendor_lib/rspec/core/pending.rb +110 -0
- data/vendor_lib/rspec/core/project_initializer.rb +88 -0
- data/vendor_lib/rspec/core/rake_task.rb +128 -0
- data/vendor_lib/rspec/core/reporter.rb +132 -0
- data/vendor_lib/rspec/core/ruby_project.rb +44 -0
- data/vendor_lib/rspec/core/runner.rb +97 -0
- data/vendor_lib/rspec/core/shared_context.rb +53 -0
- data/vendor_lib/rspec/core/shared_example_group.rb +146 -0
- data/vendor_lib/rspec/core/shared_example_group/collection.rb +27 -0
- data/vendor_lib/rspec/core/version.rb +7 -0
- data/vendor_lib/rspec/core/warnings.rb +22 -0
- data/vendor_lib/rspec/core/world.rb +131 -0
- data/vendor_lib/rspec/expectations.rb +75 -0
- data/vendor_lib/rspec/expectations/differ.rb +154 -0
- data/vendor_lib/rspec/expectations/errors.rb +9 -0
- data/vendor_lib/rspec/expectations/expectation_target.rb +87 -0
- data/vendor_lib/rspec/expectations/extensions.rb +1 -0
- data/vendor_lib/rspec/expectations/extensions/object.rb +29 -0
- data/vendor_lib/rspec/expectations/fail_with.rb +79 -0
- data/vendor_lib/rspec/expectations/handler.rb +68 -0
- data/vendor_lib/rspec/expectations/syntax.rb +182 -0
- data/vendor_lib/rspec/expectations/version.rb +8 -0
- data/vendor_lib/rspec/matchers.rb +633 -0
- data/vendor_lib/rspec/matchers/built_in.rb +39 -0
- data/vendor_lib/rspec/matchers/built_in/base_matcher.rb +68 -0
- data/vendor_lib/rspec/matchers/built_in/be.rb +213 -0
- data/vendor_lib/rspec/matchers/built_in/be_instance_of.rb +15 -0
- data/vendor_lib/rspec/matchers/built_in/be_kind_of.rb +11 -0
- data/vendor_lib/rspec/matchers/built_in/be_within.rb +55 -0
- data/vendor_lib/rspec/matchers/built_in/change.rb +141 -0
- data/vendor_lib/rspec/matchers/built_in/cover.rb +21 -0
- data/vendor_lib/rspec/matchers/built_in/eq.rb +22 -0
- data/vendor_lib/rspec/matchers/built_in/eql.rb +23 -0
- data/vendor_lib/rspec/matchers/built_in/equal.rb +48 -0
- data/vendor_lib/rspec/matchers/built_in/exist.rb +26 -0
- data/vendor_lib/rspec/matchers/built_in/has.rb +48 -0
- data/vendor_lib/rspec/matchers/built_in/include.rb +61 -0
- data/vendor_lib/rspec/matchers/built_in/match.rb +17 -0
- data/vendor_lib/rspec/matchers/built_in/match_array.rb +51 -0
- data/vendor_lib/rspec/matchers/built_in/raise_error.rb +154 -0
- data/vendor_lib/rspec/matchers/built_in/respond_to.rb +74 -0
- data/vendor_lib/rspec/matchers/built_in/satisfy.rb +30 -0
- data/vendor_lib/rspec/matchers/built_in/start_and_end_with.rb +48 -0
- data/vendor_lib/rspec/matchers/built_in/throw_symbol.rb +94 -0
- data/vendor_lib/rspec/matchers/built_in/yield.rb +297 -0
- data/vendor_lib/rspec/matchers/compatibility.rb +14 -0
- data/vendor_lib/rspec/matchers/configuration.rb +113 -0
- data/vendor_lib/rspec/matchers/dsl.rb +23 -0
- data/vendor_lib/rspec/matchers/generated_descriptions.rb +35 -0
- data/vendor_lib/rspec/matchers/matcher.rb +301 -0
- data/vendor_lib/rspec/matchers/method_missing.rb +12 -0
- data/vendor_lib/rspec/matchers/operator_matcher.rb +99 -0
- data/vendor_lib/rspec/matchers/pretty.rb +70 -0
- data/vendor_lib/rspec/matchers/test_unit_integration.rb +11 -0
- data/vendor_lib/rspec/mocks.rb +100 -0
- data/vendor_lib/rspec/mocks/any_instance/chain.rb +92 -0
- data/vendor_lib/rspec/mocks/any_instance/expectation_chain.rb +47 -0
- data/vendor_lib/rspec/mocks/any_instance/message_chains.rb +75 -0
- data/vendor_lib/rspec/mocks/any_instance/recorder.rb +200 -0
- data/vendor_lib/rspec/mocks/any_instance/stub_chain.rb +45 -0
- data/vendor_lib/rspec/mocks/any_instance/stub_chain_chain.rb +23 -0
- data/vendor_lib/rspec/mocks/argument_list_matcher.rb +104 -0
- data/vendor_lib/rspec/mocks/argument_matchers.rb +264 -0
- data/vendor_lib/rspec/mocks/arity_calculator.rb +66 -0
- data/vendor_lib/rspec/mocks/configuration.rb +111 -0
- data/vendor_lib/rspec/mocks/error_generator.rb +203 -0
- data/vendor_lib/rspec/mocks/errors.rb +12 -0
- data/vendor_lib/rspec/mocks/example_methods.rb +201 -0
- data/vendor_lib/rspec/mocks/extensions/marshal.rb +17 -0
- data/vendor_lib/rspec/mocks/framework.rb +36 -0
- data/vendor_lib/rspec/mocks/instance_method_stasher.rb +112 -0
- data/vendor_lib/rspec/mocks/matchers/have_received.rb +99 -0
- data/vendor_lib/rspec/mocks/matchers/receive.rb +112 -0
- data/vendor_lib/rspec/mocks/matchers/receive_messages.rb +72 -0
- data/vendor_lib/rspec/mocks/message_expectation.rb +643 -0
- data/vendor_lib/rspec/mocks/method_double.rb +209 -0
- data/vendor_lib/rspec/mocks/method_reference.rb +95 -0
- data/vendor_lib/rspec/mocks/mock.rb +7 -0
- data/vendor_lib/rspec/mocks/mutate_const.rb +406 -0
- data/vendor_lib/rspec/mocks/object_reference.rb +90 -0
- data/vendor_lib/rspec/mocks/order_group.rb +82 -0
- data/vendor_lib/rspec/mocks/proxy.rb +269 -0
- data/vendor_lib/rspec/mocks/proxy_for_nil.rb +37 -0
- data/vendor_lib/rspec/mocks/space.rb +95 -0
- data/vendor_lib/rspec/mocks/standalone.rb +3 -0
- data/vendor_lib/rspec/mocks/stub_chain.rb +51 -0
- data/vendor_lib/rspec/mocks/syntax.rb +374 -0
- data/vendor_lib/rspec/mocks/targets.rb +90 -0
- data/vendor_lib/rspec/mocks/test_double.rb +109 -0
- data/vendor_lib/rspec/mocks/verifying_double.rb +77 -0
- data/vendor_lib/rspec/mocks/verifying_message_expecation.rb +60 -0
- data/vendor_lib/rspec/mocks/verifying_proxy.rb +151 -0
- data/vendor_lib/rspec/mocks/version.rb +7 -0
- data/vendor_lib/rspec/support.rb +6 -0
- data/vendor_lib/rspec/support/caller_filter.rb +56 -0
- data/vendor_lib/rspec/support/spec.rb +14 -0
- data/vendor_lib/rspec/support/spec/deprecation_helpers.rb +29 -0
- data/vendor_lib/rspec/support/spec/in_sub_process.rb +40 -0
- data/vendor_lib/rspec/support/spec/stderr_splitter.rb +50 -0
- data/vendor_lib/rspec/support/version.rb +7 -0
- data/vendor_lib/rspec/support/warnings.rb +41 -0
- data/vendor_lib/rspec/version.rb +5 -0
- metadata +268 -0
@@ -0,0 +1,302 @@
|
|
1
|
+
# This code was (mostly) ported from the backports gem (https://github.com/marcandre/backports).
|
2
|
+
# The goal is to provide a random number generator in Ruby versions that do not have one.
|
3
|
+
# This was added to support localization of random spec ordering.
|
4
|
+
#
|
5
|
+
# These were in multiple files in backports, but merged into one here.
|
6
|
+
|
7
|
+
module RSpec
|
8
|
+
module Core
|
9
|
+
# Methods used internally by the backports.
|
10
|
+
module Backports
|
11
|
+
# Helper method to coerce a value into a specific class.
|
12
|
+
# Raises a TypeError if the coercion fails or the returned value
|
13
|
+
# is not of the right class.
|
14
|
+
# (from Rubinius)
|
15
|
+
def self.coerce_to(obj, cls, meth)
|
16
|
+
return obj if obj.kind_of?(cls)
|
17
|
+
|
18
|
+
begin
|
19
|
+
ret = obj.__send__(meth)
|
20
|
+
rescue Exception => e
|
21
|
+
raise TypeError, "Coercion error: #{obj.inspect}.#{meth} => #{cls} failed:\n" \
|
22
|
+
"(#{e.message})"
|
23
|
+
end
|
24
|
+
raise TypeError, "Coercion error: obj.#{meth} did NOT return a #{cls} (was #{ret.class})" unless ret.kind_of? cls
|
25
|
+
ret
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.coerce_to_int(obj)
|
29
|
+
coerce_to(obj, Integer, :to_int)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Used internally to make it easy to deal with optional arguments
|
33
|
+
# (from Rubinius)
|
34
|
+
Undefined = Object.new
|
35
|
+
|
36
|
+
class Random
|
37
|
+
# An implementation of Mersenne Twister MT19937 in Ruby
|
38
|
+
class MT19937
|
39
|
+
STATE_SIZE = 624
|
40
|
+
LAST_STATE = STATE_SIZE - 1
|
41
|
+
PAD_32_BITS = 0xffffffff
|
42
|
+
|
43
|
+
# See seed=
|
44
|
+
def initialize(seed)
|
45
|
+
self.seed = seed
|
46
|
+
end
|
47
|
+
|
48
|
+
LAST_31_BITS = 0x7fffffff
|
49
|
+
OFFSET = 397
|
50
|
+
|
51
|
+
# Generates a completely new state out of the previous one.
|
52
|
+
def next_state
|
53
|
+
STATE_SIZE.times do |i|
|
54
|
+
mix = @state[i] & 0x80000000 | @state[i+1 - STATE_SIZE] & 0x7fffffff
|
55
|
+
@state[i] = @state[i+OFFSET - STATE_SIZE] ^ (mix >> 1)
|
56
|
+
@state[i] ^= 0x9908b0df if mix.odd?
|
57
|
+
end
|
58
|
+
@last_read = -1
|
59
|
+
end
|
60
|
+
|
61
|
+
# Seed must be either an Integer (only the first 32 bits will be used)
|
62
|
+
# or an Array of Integers (of which only the first 32 bits will be used)
|
63
|
+
#
|
64
|
+
# No conversion or type checking is done at this level
|
65
|
+
def seed=(seed)
|
66
|
+
case seed
|
67
|
+
when Integer
|
68
|
+
@state = Array.new(STATE_SIZE)
|
69
|
+
@state[0] = seed & PAD_32_BITS
|
70
|
+
(1..LAST_STATE).each do |i|
|
71
|
+
@state[i] = (1812433253 * (@state[i-1] ^ @state[i-1]>>30) + i)& PAD_32_BITS
|
72
|
+
end
|
73
|
+
@last_read = LAST_STATE
|
74
|
+
when Array
|
75
|
+
self.seed = 19650218
|
76
|
+
i=1
|
77
|
+
j=0
|
78
|
+
[STATE_SIZE, seed.size].max.times do
|
79
|
+
@state[i] = (@state[i] ^ (@state[i-1] ^ @state[i-1]>>30) * 1664525) + j + seed[j] & PAD_32_BITS
|
80
|
+
if (i+=1) >= STATE_SIZE
|
81
|
+
@state[0] = @state[-1]
|
82
|
+
i = 1
|
83
|
+
end
|
84
|
+
j = 0 if (j+=1) >= seed.size
|
85
|
+
end
|
86
|
+
(STATE_SIZE-1).times do
|
87
|
+
@state[i] = (@state[i] ^ (@state[i-1] ^ @state[i-1]>>30) * 1566083941) - i & PAD_32_BITS
|
88
|
+
if (i+=1) >= STATE_SIZE
|
89
|
+
@state[0] = @state[-1]
|
90
|
+
i = 1
|
91
|
+
end
|
92
|
+
end
|
93
|
+
@state[0] = 0x80000000
|
94
|
+
else
|
95
|
+
raise ArgumentError, "Seed must be an Integer or an Array"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# Returns a random Integer from the range 0 ... (1 << 32)
|
100
|
+
def random_32_bits
|
101
|
+
next_state if @last_read >= LAST_STATE
|
102
|
+
@last_read += 1
|
103
|
+
y = @state[@last_read]
|
104
|
+
# Tempering
|
105
|
+
y ^= (y >> 11)
|
106
|
+
y ^= (y << 7) & 0x9d2c5680
|
107
|
+
y ^= (y << 15) & 0xefc60000
|
108
|
+
y ^= (y >> 18)
|
109
|
+
end
|
110
|
+
|
111
|
+
# Supplement the MT19937 class with methods to do
|
112
|
+
# conversions the same way as MRI.
|
113
|
+
# No argument checking is done here either.
|
114
|
+
|
115
|
+
FLOAT_FACTOR = 1.0/9007199254740992.0
|
116
|
+
# generates a random number on [0,1) with 53-bit resolution
|
117
|
+
def random_float
|
118
|
+
((random_32_bits >> 5) * 67108864.0 + (random_32_bits >> 6)) * FLOAT_FACTOR;
|
119
|
+
end
|
120
|
+
|
121
|
+
# Returns an integer within 0...upto
|
122
|
+
def random_integer(upto)
|
123
|
+
n = upto - 1
|
124
|
+
nb_full_32 = 0
|
125
|
+
while n > PAD_32_BITS
|
126
|
+
n >>= 32
|
127
|
+
nb_full_32 += 1
|
128
|
+
end
|
129
|
+
mask = mask_32_bits(n)
|
130
|
+
begin
|
131
|
+
rand = random_32_bits & mask
|
132
|
+
nb_full_32.times do
|
133
|
+
rand <<= 32
|
134
|
+
rand |= random_32_bits
|
135
|
+
end
|
136
|
+
end until rand < upto
|
137
|
+
rand
|
138
|
+
end
|
139
|
+
|
140
|
+
def random_bytes(nb)
|
141
|
+
nb_32_bits = (nb + 3) / 4
|
142
|
+
random = nb_32_bits.times.map { random_32_bits }
|
143
|
+
random.pack("L" * nb_32_bits)[0, nb]
|
144
|
+
end
|
145
|
+
|
146
|
+
def state_as_bignum
|
147
|
+
b = 0
|
148
|
+
@state.each_with_index do |val, i|
|
149
|
+
b |= val << (32 * i)
|
150
|
+
end
|
151
|
+
b
|
152
|
+
end
|
153
|
+
|
154
|
+
def left # It's actually the number of words left + 1, as per MRI...
|
155
|
+
MT19937::STATE_SIZE - @last_read
|
156
|
+
end
|
157
|
+
|
158
|
+
def marshal_dump
|
159
|
+
[state_as_bignum, left]
|
160
|
+
end
|
161
|
+
|
162
|
+
def marshal_load(ary)
|
163
|
+
b, left = ary
|
164
|
+
@last_read = MT19937::STATE_SIZE - left
|
165
|
+
@state = Array.new(STATE_SIZE)
|
166
|
+
STATE_SIZE.times do |i|
|
167
|
+
@state[i] = b & PAD_32_BITS
|
168
|
+
b >>= 32
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
# Convert an Integer seed of arbitrary size to either a single 32 bit integer, or an Array of 32 bit integers
|
173
|
+
def self.convert_seed(seed)
|
174
|
+
seed = seed.abs
|
175
|
+
long_values = []
|
176
|
+
begin
|
177
|
+
long_values << (seed & PAD_32_BITS)
|
178
|
+
seed >>= 32
|
179
|
+
end until seed == 0
|
180
|
+
|
181
|
+
long_values.pop if long_values[-1] == 1 && long_values.size > 1 # Done to allow any kind of sequence of integers
|
182
|
+
|
183
|
+
long_values.size > 1 ? long_values : long_values.first
|
184
|
+
end
|
185
|
+
|
186
|
+
def self.[](seed)
|
187
|
+
new(convert_seed(seed))
|
188
|
+
end
|
189
|
+
|
190
|
+
private
|
191
|
+
|
192
|
+
MASK_BY = [1,2,4,8,16]
|
193
|
+
def mask_32_bits(n)
|
194
|
+
MASK_BY.each do |shift|
|
195
|
+
n |= n >> shift
|
196
|
+
end
|
197
|
+
n
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
# Implementation corresponding to the actual Random class of Ruby
|
202
|
+
# The actual random generator (mersenne twister) is in MT19937.
|
203
|
+
# Ruby specific conversions are handled in bits_and_bytes.
|
204
|
+
# The high level stuff (argument checking) is done here.
|
205
|
+
module Implementation
|
206
|
+
attr_reader :seed
|
207
|
+
|
208
|
+
def initialize(seed = 0)
|
209
|
+
super()
|
210
|
+
seed_rand seed
|
211
|
+
end
|
212
|
+
|
213
|
+
def seed_rand(new_seed = 0)
|
214
|
+
new_seed = Backports.coerce_to_int(new_seed)
|
215
|
+
@seed = nil unless defined?(@seed)
|
216
|
+
old, @seed = @seed, new_seed.nonzero? || Random.new_seed
|
217
|
+
@mt = MT19937[ @seed ]
|
218
|
+
old
|
219
|
+
end
|
220
|
+
|
221
|
+
def rand(limit = Backports::Undefined)
|
222
|
+
case limit
|
223
|
+
when Backports::Undefined
|
224
|
+
@mt.random_float
|
225
|
+
when Float
|
226
|
+
limit * @mt.random_float unless limit <= 0
|
227
|
+
when Range
|
228
|
+
_rand_range(limit)
|
229
|
+
else
|
230
|
+
limit = Backports.coerce_to_int(limit)
|
231
|
+
@mt.random_integer(limit) unless limit <= 0
|
232
|
+
end || raise(ArgumentError, "invalid argument #{limit}")
|
233
|
+
end
|
234
|
+
|
235
|
+
def bytes(nb)
|
236
|
+
nb = Backports.coerce_to_int(nb)
|
237
|
+
raise ArgumentError, "negative size" if nb < 0
|
238
|
+
@mt.random_bytes(nb)
|
239
|
+
end
|
240
|
+
|
241
|
+
def ==(other)
|
242
|
+
other.is_a?(Random) &&
|
243
|
+
seed == other.seed &&
|
244
|
+
left == other.send(:left) &&
|
245
|
+
state == other.send(:state)
|
246
|
+
end
|
247
|
+
|
248
|
+
def marshal_dump
|
249
|
+
@mt.marshal_dump << @seed
|
250
|
+
end
|
251
|
+
|
252
|
+
def marshal_load(ary)
|
253
|
+
@seed = ary.pop
|
254
|
+
@mt = MT19937.allocate
|
255
|
+
@mt.marshal_load(ary)
|
256
|
+
end
|
257
|
+
|
258
|
+
private
|
259
|
+
|
260
|
+
def state
|
261
|
+
@mt.state_as_bignum
|
262
|
+
end
|
263
|
+
|
264
|
+
def left
|
265
|
+
@mt.left
|
266
|
+
end
|
267
|
+
|
268
|
+
def _rand_range(limit)
|
269
|
+
range = limit.end - limit.begin
|
270
|
+
if (!range.is_a?(Float)) && range.respond_to?(:to_int) && range = Backports.coerce_to_int(range)
|
271
|
+
range += 1 unless limit.exclude_end?
|
272
|
+
limit.begin + @mt.random_integer(range) unless range <= 0
|
273
|
+
elsif range = Backports.coerce_to(range, Float, :to_f)
|
274
|
+
if range < 0
|
275
|
+
nil
|
276
|
+
elsif limit.exclude_end?
|
277
|
+
limit.begin + @mt.random_float * range unless range <= 0
|
278
|
+
else
|
279
|
+
# cheat a bit... this will reduce the nb of random bits
|
280
|
+
loop do
|
281
|
+
r = @mt.random_float * range * 1.0001
|
282
|
+
break limit.begin + r unless r > range
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
def self.new_seed
|
290
|
+
(2 ** 62) + Kernel.rand(2 ** 62)
|
291
|
+
end
|
292
|
+
end
|
293
|
+
|
294
|
+
class Random
|
295
|
+
include Implementation
|
296
|
+
class << self
|
297
|
+
include Implementation
|
298
|
+
end
|
299
|
+
end
|
300
|
+
end
|
301
|
+
end
|
302
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Core
|
3
|
+
class BacktraceFormatter
|
4
|
+
# This is only used externally by rspec-expectations. Can be removed once
|
5
|
+
# rspec-expectations uses
|
6
|
+
# RSpec.configuration.backtrace_formatter.format_backtrace instead.
|
7
|
+
def self.format_backtrace(backtrace, options = {})
|
8
|
+
RSpec.configuration.backtrace_formatter.format_backtrace(backtrace, options)
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_accessor :exclusion_patterns, :inclusion_patterns
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@full_backtrace = false
|
15
|
+
@exclusion_patterns = [] << Regexp.union(
|
16
|
+
*["/lib\d*/ruby/",
|
17
|
+
"org/jruby/",
|
18
|
+
"bin/",
|
19
|
+
"/gems/",
|
20
|
+
"lib/rspec/(core|expectations|matchers|mocks)"].
|
21
|
+
map {|s| Regexp.new(s.gsub("/", File::SEPARATOR))}
|
22
|
+
)
|
23
|
+
@inclusion_patterns = [Regexp.new(Dir.getwd)]
|
24
|
+
end
|
25
|
+
|
26
|
+
def full_backtrace=(full_backtrace)
|
27
|
+
@full_backtrace = full_backtrace
|
28
|
+
end
|
29
|
+
|
30
|
+
def full_backtrace?
|
31
|
+
@full_backtrace || @exclusion_patterns.empty?
|
32
|
+
end
|
33
|
+
|
34
|
+
def format_backtrace(backtrace, options = {})
|
35
|
+
return backtrace if options[:full_backtrace]
|
36
|
+
backtrace.
|
37
|
+
take_while {|l| l != RSpec::Core::Runner::AT_EXIT_HOOK_BACKTRACE_LINE}.
|
38
|
+
map {|l| backtrace_line(l)}.
|
39
|
+
compact.
|
40
|
+
tap do |filtered|
|
41
|
+
if filtered.empty?
|
42
|
+
filtered.concat backtrace
|
43
|
+
filtered << ""
|
44
|
+
filtered << " Showing full backtrace because every line was filtered out."
|
45
|
+
filtered << " See docs for RSpec::Configuration#backtrace_exclusion_patterns and"
|
46
|
+
filtered << " RSpec::Configuration#backtrace_inclusion_patterns for more information."
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# @api private
|
52
|
+
def backtrace_line(line)
|
53
|
+
RSpec::Core::Metadata::relative_path(line) unless exclude?(line)
|
54
|
+
rescue SecurityError
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
|
58
|
+
# @api private
|
59
|
+
def exclude?(line)
|
60
|
+
return false if @full_backtrace
|
61
|
+
@exclusion_patterns.any? {|p| p =~ line} && @inclusion_patterns.none? {|p| p =~ line}
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Core
|
3
|
+
class CommandLine
|
4
|
+
def initialize(options, configuration=RSpec::configuration, world=RSpec::world)
|
5
|
+
if Array === options
|
6
|
+
options = ConfigurationOptions.new(options)
|
7
|
+
options.parse_options
|
8
|
+
end
|
9
|
+
@options = options
|
10
|
+
@configuration = configuration
|
11
|
+
@world = world
|
12
|
+
end
|
13
|
+
|
14
|
+
# Configures and runs a suite
|
15
|
+
#
|
16
|
+
# @param [IO] err
|
17
|
+
# @param [IO] out
|
18
|
+
def run(err, out)
|
19
|
+
@configuration.error_stream = err
|
20
|
+
@configuration.output_stream = out if @configuration.output_stream == $stdout
|
21
|
+
@options.configure(@configuration)
|
22
|
+
@configuration.load_spec_files
|
23
|
+
@world.announce_filters
|
24
|
+
|
25
|
+
@configuration.reporter.report(@world.example_count) do |reporter|
|
26
|
+
begin
|
27
|
+
@configuration.run_hook(:before, :suite)
|
28
|
+
@world.ordered_example_groups.map {|g| g.run(reporter) }.all? ? 0 : @configuration.failure_exit_code
|
29
|
+
ensure
|
30
|
+
@configuration.run_hook(:after, :suite)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,1129 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'rspec/core/backtrace_formatter'
|
3
|
+
require 'rspec/core/ruby_project'
|
4
|
+
require 'rspec/core/formatters/deprecation_formatter'
|
5
|
+
|
6
|
+
module RSpec
|
7
|
+
module Core
|
8
|
+
# Stores runtime configuration information.
|
9
|
+
#
|
10
|
+
# Configuration options are loaded from `~/.rspec`, `.rspec`,
|
11
|
+
# `.rspec-local`, command line switches, and the `SPEC_OPTS` environment
|
12
|
+
# variable (listed in lowest to highest precedence; for example, an option
|
13
|
+
# in `~/.rspec` can be overridden by an option in `.rspec-local`).
|
14
|
+
#
|
15
|
+
# @example Standard settings
|
16
|
+
# RSpec.configure do |c|
|
17
|
+
# c.drb = true
|
18
|
+
# c.drb_port = 1234
|
19
|
+
# c.default_path = 'behavior'
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# @example Hooks
|
23
|
+
# RSpec.configure do |c|
|
24
|
+
# c.before(:suite) { establish_connection }
|
25
|
+
# c.before(:each) { log_in_as :authorized }
|
26
|
+
# c.around(:each) { |ex| Database.transaction(&ex) }
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# @see RSpec.configure
|
30
|
+
# @see Hooks
|
31
|
+
class Configuration
|
32
|
+
include RSpec::Core::Hooks
|
33
|
+
|
34
|
+
class MustBeConfiguredBeforeExampleGroupsError < StandardError; end
|
35
|
+
|
36
|
+
# @private
|
37
|
+
def self.define_reader(name)
|
38
|
+
define_method(name) do
|
39
|
+
variable = instance_variable_defined?("@#{name}") ? instance_variable_get("@#{name}") : nil
|
40
|
+
value_for(name, variable)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# @private
|
45
|
+
def self.deprecate_alias_key
|
46
|
+
RSpec.deprecate("add_setting with :alias option", :replacement => ":alias_with")
|
47
|
+
end
|
48
|
+
|
49
|
+
# @private
|
50
|
+
def self.define_aliases(name, alias_name)
|
51
|
+
alias_method alias_name, name
|
52
|
+
alias_method "#{alias_name}=", "#{name}="
|
53
|
+
define_predicate_for alias_name
|
54
|
+
end
|
55
|
+
|
56
|
+
# @private
|
57
|
+
def self.define_predicate_for(*names)
|
58
|
+
names.each {|name| alias_method "#{name}?", name}
|
59
|
+
end
|
60
|
+
|
61
|
+
# @private
|
62
|
+
#
|
63
|
+
# Invoked by the `add_setting` instance method. Use that method on a
|
64
|
+
# `Configuration` instance rather than this class method.
|
65
|
+
def self.add_setting(name, opts={})
|
66
|
+
raise "Use the instance add_setting method if you want to set a default" if opts.has_key?(:default)
|
67
|
+
if opts[:alias]
|
68
|
+
deprecate_alias_key
|
69
|
+
define_aliases(opts[:alias], name)
|
70
|
+
else
|
71
|
+
attr_writer name
|
72
|
+
define_reader name
|
73
|
+
define_predicate_for name
|
74
|
+
end
|
75
|
+
Array(opts[:alias_with]).each do |alias_name|
|
76
|
+
define_aliases(name, alias_name)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# @macro [attach] add_setting
|
81
|
+
# @attribute $1
|
82
|
+
#
|
83
|
+
# @macro [attach] define_reader
|
84
|
+
# @attribute $1
|
85
|
+
|
86
|
+
# @macro add_setting
|
87
|
+
# Path to use if no path is provided to the `rspec` command (default:
|
88
|
+
# `"spec"`). Allows you to just type `rspec` instead of `rspec spec` to
|
89
|
+
# run all the examples in the `spec` directory.
|
90
|
+
add_setting :default_path
|
91
|
+
|
92
|
+
# @macro add_setting
|
93
|
+
# Run examples over DRb (default: `false`). RSpec doesn't supply the DRb
|
94
|
+
# server, but you can use tools like spork.
|
95
|
+
add_setting :drb
|
96
|
+
|
97
|
+
# @macro add_setting
|
98
|
+
# The drb_port (default: nil).
|
99
|
+
add_setting :drb_port
|
100
|
+
|
101
|
+
# @macro add_setting
|
102
|
+
# Default: `$stderr`.
|
103
|
+
add_setting :error_stream
|
104
|
+
|
105
|
+
# @macro add_setting
|
106
|
+
# Default: `$stderr`.
|
107
|
+
add_setting :deprecation_stream
|
108
|
+
|
109
|
+
# @macro add_setting
|
110
|
+
# Clean up and exit after the first failure (default: `false`).
|
111
|
+
add_setting :fail_fast
|
112
|
+
|
113
|
+
# @macro add_setting
|
114
|
+
# Prints the formatter output of your suite without running any
|
115
|
+
# examples or hooks.
|
116
|
+
add_setting :dry_run
|
117
|
+
|
118
|
+
# @macro add_setting
|
119
|
+
# The exit code to return if there are any failures (default: 1).
|
120
|
+
add_setting :failure_exit_code
|
121
|
+
|
122
|
+
# @macro define_reader
|
123
|
+
# Indicates files configured to be required
|
124
|
+
define_reader :requires
|
125
|
+
|
126
|
+
# @macro define_reader
|
127
|
+
# Returns dirs that have been prepended to the load path by #lib=
|
128
|
+
define_reader :libs
|
129
|
+
|
130
|
+
# @macro add_setting
|
131
|
+
# Default: `$stdout`.
|
132
|
+
# Also known as `output` and `out`
|
133
|
+
define_reader :output_stream
|
134
|
+
def output_stream=(value)
|
135
|
+
if @reporter && !value.equal?(@output_stream)
|
136
|
+
warn "RSpec's reporter has already been initialized with " +
|
137
|
+
"#{output_stream.inspect} as the output stream, so your change to "+
|
138
|
+
"`output_stream` will be ignored. You should configure it earlier for " +
|
139
|
+
"it to take effect. (Called from #{CallerFilter.first_non_rspec_line})"
|
140
|
+
else
|
141
|
+
@output_stream = value
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
# @macro add_setting
|
146
|
+
# Load files matching this pattern (default: `'**/*_spec.rb'`)
|
147
|
+
add_setting :pattern, :alias_with => :filename_pattern
|
148
|
+
|
149
|
+
def pattern= value
|
150
|
+
if @spec_files_loaded
|
151
|
+
RSpec.warning "Configuring `pattern` to #{value} has no effect since RSpec has already loaded the spec files."
|
152
|
+
end
|
153
|
+
@pattern = value
|
154
|
+
end
|
155
|
+
alias :filename_pattern= :pattern=
|
156
|
+
|
157
|
+
# @macro add_setting
|
158
|
+
# Report the times for the slowest examples (default: `false`).
|
159
|
+
# Use this to specify the number of examples to include in the profile.
|
160
|
+
add_setting :profile_examples
|
161
|
+
|
162
|
+
# @macro add_setting
|
163
|
+
# Run all examples if none match the configured filters (default: `false`).
|
164
|
+
add_setting :run_all_when_everything_filtered
|
165
|
+
|
166
|
+
# @macro add_setting
|
167
|
+
# Color to use to indicate success.
|
168
|
+
# @param [Symbol] color one of the following: [:black, :white, :red, :green, :yellow, :blue, :magenta, :cyan]
|
169
|
+
add_setting :success_color
|
170
|
+
|
171
|
+
# @macro add_setting
|
172
|
+
# Color to use to print pending examples.
|
173
|
+
# @param [Symbol] color one of the following: [:black, :white, :red, :green, :yellow, :blue, :magenta, :cyan]
|
174
|
+
add_setting :pending_color
|
175
|
+
|
176
|
+
# @macro add_setting
|
177
|
+
# Color to use to indicate failure.
|
178
|
+
# @param [Symbol] color one of the following: [:black, :white, :red, :green, :yellow, :blue, :magenta, :cyan]
|
179
|
+
add_setting :failure_color
|
180
|
+
|
181
|
+
# @macro add_setting
|
182
|
+
# The default output color.
|
183
|
+
# @param [Symbol] color one of the following: [:black, :white, :red, :green, :yellow, :blue, :magenta, :cyan]
|
184
|
+
add_setting :default_color
|
185
|
+
|
186
|
+
# @macro add_setting
|
187
|
+
# Color used when a pending example is fixed.
|
188
|
+
# @param [Symbol] color one of the following: [:black, :white, :red, :green, :yellow, :blue, :magenta, :cyan]
|
189
|
+
add_setting :fixed_color
|
190
|
+
|
191
|
+
# @macro add_setting
|
192
|
+
# Color used to print details.
|
193
|
+
# @param [Symbol] color one of the following: [:black, :white, :red, :green, :yellow, :blue, :magenta, :cyan]
|
194
|
+
add_setting :detail_color
|
195
|
+
|
196
|
+
# @macro add_setting
|
197
|
+
# When a block passed to pending fails (as expected), display the failure
|
198
|
+
# without reporting it as a failure (default: false).
|
199
|
+
add_setting :show_failures_in_pending_blocks
|
200
|
+
|
201
|
+
# Deprecated. This config option was added in RSpec 2 to pave the way
|
202
|
+
# for this being the default behavior in RSpec 3. Now this option is
|
203
|
+
# a no-op.
|
204
|
+
def treat_symbols_as_metadata_keys_with_true_values=(value)
|
205
|
+
RSpec.deprecate("RSpec::Core::Configuration#treat_symbols_as_metadata_keys_with_true_values=")
|
206
|
+
end
|
207
|
+
|
208
|
+
# @private
|
209
|
+
add_setting :tty
|
210
|
+
# @private
|
211
|
+
add_setting :include_or_extend_modules
|
212
|
+
# @private
|
213
|
+
add_setting :files_to_run
|
214
|
+
# @private
|
215
|
+
add_setting :expecting_with_rspec
|
216
|
+
# @private
|
217
|
+
attr_accessor :filter_manager
|
218
|
+
# @private
|
219
|
+
attr_reader :backtrace_formatter, :ordering_manager
|
220
|
+
|
221
|
+
# Alias for rspec-2.x's backtrace_cleaner (now backtrace_formatter)
|
222
|
+
#
|
223
|
+
# TODO: consider deprecating and removing this rather than aliasing in rspec-3?
|
224
|
+
alias backtrace_cleaner backtrace_formatter
|
225
|
+
|
226
|
+
def initialize
|
227
|
+
@expectation_frameworks = []
|
228
|
+
@include_or_extend_modules = []
|
229
|
+
@mock_framework = nil
|
230
|
+
@files_to_run = []
|
231
|
+
@formatters = []
|
232
|
+
@color = false
|
233
|
+
@pattern = '**/*_spec.rb'
|
234
|
+
@failure_exit_code = 1
|
235
|
+
@spec_files_loaded = false
|
236
|
+
|
237
|
+
@backtrace_formatter = BacktraceFormatter.new
|
238
|
+
|
239
|
+
@default_path = 'spec'
|
240
|
+
@deprecation_stream = $stderr
|
241
|
+
@output_stream = $stdout
|
242
|
+
@reporter = nil
|
243
|
+
@filter_manager = FilterManager.new
|
244
|
+
@ordering_manager = Ordering::ConfigurationManager.new
|
245
|
+
@preferred_options = {}
|
246
|
+
@failure_color = :red
|
247
|
+
@success_color = :green
|
248
|
+
@pending_color = :yellow
|
249
|
+
@default_color = :white
|
250
|
+
@fixed_color = :blue
|
251
|
+
@detail_color = :cyan
|
252
|
+
@profile_examples = false
|
253
|
+
@requires = []
|
254
|
+
@libs = []
|
255
|
+
end
|
256
|
+
|
257
|
+
# @private
|
258
|
+
#
|
259
|
+
# Used to set higher priority option values from the command line.
|
260
|
+
def force(hash)
|
261
|
+
ordering_manager.force(hash)
|
262
|
+
@preferred_options.merge!(hash)
|
263
|
+
self.warnings = value_for :warnings, nil
|
264
|
+
end
|
265
|
+
|
266
|
+
# @private
|
267
|
+
def reset
|
268
|
+
@spec_files_loaded = false
|
269
|
+
@reporter = nil
|
270
|
+
@formatters.clear
|
271
|
+
end
|
272
|
+
|
273
|
+
# @overload add_setting(name)
|
274
|
+
# @overload add_setting(name, opts)
|
275
|
+
# @option opts [Symbol] :default
|
276
|
+
#
|
277
|
+
# set a default value for the generated getter and predicate methods:
|
278
|
+
#
|
279
|
+
# add_setting(:foo, :default => "default value")
|
280
|
+
#
|
281
|
+
# @option opts [Symbol] :alias_with
|
282
|
+
#
|
283
|
+
# Use `:alias_with` to alias the setter, getter, and predicate to another
|
284
|
+
# name, or names:
|
285
|
+
#
|
286
|
+
# add_setting(:foo, :alias_with => :bar)
|
287
|
+
# add_setting(:foo, :alias_with => [:bar, :baz])
|
288
|
+
#
|
289
|
+
# Adds a custom setting to the RSpec.configuration object.
|
290
|
+
#
|
291
|
+
# RSpec.configuration.add_setting :foo
|
292
|
+
#
|
293
|
+
# Used internally and by extension frameworks like rspec-rails, so they
|
294
|
+
# can add config settings that are domain specific. For example:
|
295
|
+
#
|
296
|
+
# RSpec.configure do |c|
|
297
|
+
# c.add_setting :use_transactional_fixtures,
|
298
|
+
# :default => true,
|
299
|
+
# :alias_with => :use_transactional_examples
|
300
|
+
# end
|
301
|
+
#
|
302
|
+
# `add_setting` creates three methods on the configuration object, a
|
303
|
+
# setter, a getter, and a predicate:
|
304
|
+
#
|
305
|
+
# RSpec.configuration.foo=(value)
|
306
|
+
# RSpec.configuration.foo
|
307
|
+
# RSpec.configuration.foo? # returns true if foo returns anything but nil or false
|
308
|
+
def add_setting(name, opts={})
|
309
|
+
default = opts.delete(:default)
|
310
|
+
(class << self; self; end).class_eval do
|
311
|
+
add_setting(name, opts)
|
312
|
+
end
|
313
|
+
send("#{name}=", default) if default
|
314
|
+
end
|
315
|
+
|
316
|
+
# Returns the configured mock framework adapter module
|
317
|
+
def mock_framework
|
318
|
+
mock_with :rspec unless @mock_framework
|
319
|
+
@mock_framework
|
320
|
+
end
|
321
|
+
|
322
|
+
# Delegates to mock_framework=(framework)
|
323
|
+
def mock_framework=(framework)
|
324
|
+
mock_with framework
|
325
|
+
end
|
326
|
+
|
327
|
+
# Regexps used to exclude lines from backtraces.
|
328
|
+
#
|
329
|
+
# Excludes lines from ruby (and jruby) source, installed gems, anything
|
330
|
+
# in any "bin" directory, and any of the rspec libs (outside gem
|
331
|
+
# installs) by default.
|
332
|
+
#
|
333
|
+
# You can modify the list via the getter, or replace it with the setter.
|
334
|
+
#
|
335
|
+
# To override this behaviour and display a full backtrace, use
|
336
|
+
# `--backtrace`on the command line, in a `.rspec` file, or in the
|
337
|
+
# `rspec_options` attribute of RSpec's rake task.
|
338
|
+
def backtrace_exclusion_patterns
|
339
|
+
@backtrace_formatter.exclusion_patterns
|
340
|
+
end
|
341
|
+
|
342
|
+
def backtrace_exclusion_patterns=(patterns)
|
343
|
+
@backtrace_formatter.exclusion_patterns = patterns
|
344
|
+
end
|
345
|
+
|
346
|
+
# Regexps used to include lines in backtraces.
|
347
|
+
#
|
348
|
+
# Defaults to [Regexp.new Dir.getwd].
|
349
|
+
#
|
350
|
+
# Lines that match an exclusion _and_ an inclusion pattern
|
351
|
+
# will be included.
|
352
|
+
#
|
353
|
+
# You can modify the list via the getter, or replace it with the setter.
|
354
|
+
def backtrace_inclusion_patterns
|
355
|
+
@backtrace_formatter.inclusion_patterns
|
356
|
+
end
|
357
|
+
|
358
|
+
def backtrace_inclusion_patterns=(patterns)
|
359
|
+
@backtrace_formatter.inclusion_patterns = patterns
|
360
|
+
end
|
361
|
+
|
362
|
+
# Sets the mock framework adapter module.
|
363
|
+
#
|
364
|
+
# `framework` can be a Symbol or a Module.
|
365
|
+
#
|
366
|
+
# Given any of `:rspec`, `:mocha`, `:flexmock`, or `:rr`, configures the
|
367
|
+
# named framework.
|
368
|
+
#
|
369
|
+
# Given `:nothing`, configures no framework. Use this if you don't use
|
370
|
+
# any mocking framework to save a little bit of overhead.
|
371
|
+
#
|
372
|
+
# Given a Module, includes that module in every example group. The module
|
373
|
+
# should adhere to RSpec's mock framework adapter API:
|
374
|
+
#
|
375
|
+
# setup_mocks_for_rspec
|
376
|
+
# - called before each example
|
377
|
+
#
|
378
|
+
# verify_mocks_for_rspec
|
379
|
+
# - called after each example. Framework should raise an exception
|
380
|
+
# when expectations fail
|
381
|
+
#
|
382
|
+
# teardown_mocks_for_rspec
|
383
|
+
# - called after verify_mocks_for_rspec (even if there are errors)
|
384
|
+
#
|
385
|
+
# If the module responds to `configuration` and `mock_with` receives a block,
|
386
|
+
# it will yield the configuration object to the block e.g.
|
387
|
+
#
|
388
|
+
# config.mock_with OtherMockFrameworkAdapter do |mod_config|
|
389
|
+
# mod_config.custom_setting = true
|
390
|
+
# end
|
391
|
+
def mock_with(framework)
|
392
|
+
framework_module = case framework
|
393
|
+
when Module
|
394
|
+
framework
|
395
|
+
when String, Symbol
|
396
|
+
require case framework.to_s
|
397
|
+
when /rspec/i
|
398
|
+
'rspec/core/mocking/with_rspec'
|
399
|
+
when /mocha/i
|
400
|
+
'rspec/core/mocking/with_mocha'
|
401
|
+
when /rr/i
|
402
|
+
'rspec/core/mocking/with_rr'
|
403
|
+
when /flexmock/i
|
404
|
+
'rspec/core/mocking/with_flexmock'
|
405
|
+
else
|
406
|
+
'rspec/core/mocking/with_absolutely_nothing'
|
407
|
+
end
|
408
|
+
RSpec::Core::MockFrameworkAdapter
|
409
|
+
end
|
410
|
+
|
411
|
+
new_name, old_name = [framework_module, @mock_framework].map do |mod|
|
412
|
+
mod.respond_to?(:framework_name) ? mod.framework_name : :unnamed
|
413
|
+
end
|
414
|
+
|
415
|
+
unless new_name == old_name
|
416
|
+
assert_no_example_groups_defined(:mock_framework)
|
417
|
+
end
|
418
|
+
|
419
|
+
if block_given?
|
420
|
+
raise "#{framework_module} must respond to `configuration` so that mock_with can yield it." unless framework_module.respond_to?(:configuration)
|
421
|
+
yield framework_module.configuration
|
422
|
+
end
|
423
|
+
|
424
|
+
@mock_framework = framework_module
|
425
|
+
end
|
426
|
+
|
427
|
+
# Returns the configured expectation framework adapter module(s)
|
428
|
+
def expectation_frameworks
|
429
|
+
expect_with :rspec if @expectation_frameworks.empty?
|
430
|
+
@expectation_frameworks
|
431
|
+
end
|
432
|
+
|
433
|
+
# Delegates to expect_with(framework)
|
434
|
+
def expectation_framework=(framework)
|
435
|
+
expect_with(framework)
|
436
|
+
end
|
437
|
+
|
438
|
+
# Sets the expectation framework module(s) to be included in each example
|
439
|
+
# group.
|
440
|
+
#
|
441
|
+
# `frameworks` can be `:rspec`, `:stdlib`, a custom module, or any
|
442
|
+
# combination thereof:
|
443
|
+
#
|
444
|
+
# config.expect_with :rspec
|
445
|
+
# config.expect_with :stdlib
|
446
|
+
# config.expect_with :rspec, :stdlib
|
447
|
+
# config.expect_with OtherExpectationFramework
|
448
|
+
#
|
449
|
+
# RSpec will translate `:rspec` and `:stdlib` into the appropriate
|
450
|
+
# modules.
|
451
|
+
#
|
452
|
+
# ## Configuration
|
453
|
+
#
|
454
|
+
# If the module responds to `configuration`, `expect_with` will
|
455
|
+
# yield the `configuration` object if given a block:
|
456
|
+
#
|
457
|
+
# config.expect_with OtherExpectationFramework do |custom_config|
|
458
|
+
# custom_config.custom_setting = true
|
459
|
+
# end
|
460
|
+
def expect_with(*frameworks)
|
461
|
+
modules = frameworks.map do |framework|
|
462
|
+
case framework
|
463
|
+
when Module
|
464
|
+
framework
|
465
|
+
when :rspec
|
466
|
+
require 'rspec/expectations'
|
467
|
+
self.expecting_with_rspec = true
|
468
|
+
::RSpec::Matchers
|
469
|
+
when :stdlib
|
470
|
+
require 'test/unit/assertions'
|
471
|
+
::Test::Unit::Assertions
|
472
|
+
else
|
473
|
+
raise ArgumentError, "#{framework.inspect} is not supported"
|
474
|
+
end
|
475
|
+
end
|
476
|
+
|
477
|
+
if (modules - @expectation_frameworks).any?
|
478
|
+
assert_no_example_groups_defined(:expect_with)
|
479
|
+
end
|
480
|
+
|
481
|
+
if block_given?
|
482
|
+
raise "expect_with only accepts a block with a single argument. Call expect_with #{modules.length} times, once with each argument, instead." if modules.length > 1
|
483
|
+
raise "#{modules.first} must respond to `configuration` so that expect_with can yield it." unless modules.first.respond_to?(:configuration)
|
484
|
+
yield modules.first.configuration
|
485
|
+
end
|
486
|
+
|
487
|
+
@expectation_frameworks.push(*modules)
|
488
|
+
end
|
489
|
+
|
490
|
+
def full_backtrace?
|
491
|
+
@backtrace_formatter.full_backtrace?
|
492
|
+
end
|
493
|
+
|
494
|
+
def full_backtrace=(true_or_false)
|
495
|
+
@backtrace_formatter.full_backtrace = true_or_false
|
496
|
+
end
|
497
|
+
|
498
|
+
def color(output=output_stream)
|
499
|
+
# rspec's built-in formatters all call this with the output argument,
|
500
|
+
# but defaulting to output_stream for backward compatibility with
|
501
|
+
# formatters in extension libs
|
502
|
+
return false unless output_to_tty?(output)
|
503
|
+
value_for(:color, @color)
|
504
|
+
end
|
505
|
+
|
506
|
+
def color=(bool)
|
507
|
+
if bool
|
508
|
+
if RSpec.windows_os? and not ENV['ANSICON']
|
509
|
+
RSpec.warning "You must use ANSICON 1.31 or later (http://adoxa.3eeweb.com/ansicon/) to use colour on Windows"
|
510
|
+
@color = false
|
511
|
+
else
|
512
|
+
@color = true
|
513
|
+
end
|
514
|
+
end
|
515
|
+
end
|
516
|
+
|
517
|
+
# TODO - deprecate color_enabled - probably not until the last 2.x
|
518
|
+
# release before 3.0
|
519
|
+
alias_method :color_enabled, :color
|
520
|
+
alias_method :color_enabled=, :color=
|
521
|
+
define_predicate_for :color_enabled, :color
|
522
|
+
|
523
|
+
def libs=(libs)
|
524
|
+
libs.map do |lib|
|
525
|
+
@libs.unshift lib
|
526
|
+
$LOAD_PATH.unshift lib
|
527
|
+
end
|
528
|
+
end
|
529
|
+
|
530
|
+
def requires=(paths)
|
531
|
+
RSpec.deprecate("RSpec::Core::Configuration#requires=(paths)",
|
532
|
+
:replacement => "paths.each {|path| require path}")
|
533
|
+
paths.map {|path| require path}
|
534
|
+
@requires += paths
|
535
|
+
end
|
536
|
+
|
537
|
+
# Run examples defined on `line_numbers` in all files to run.
|
538
|
+
def line_numbers=(line_numbers)
|
539
|
+
filter_run :line_numbers => line_numbers.map{|l| l.to_i}
|
540
|
+
end
|
541
|
+
|
542
|
+
def line_numbers
|
543
|
+
filter.fetch(:line_numbers,[])
|
544
|
+
end
|
545
|
+
|
546
|
+
def full_description=(description)
|
547
|
+
filter_run :full_description => Regexp.union(*Array(description).map {|d| Regexp.new(d) })
|
548
|
+
end
|
549
|
+
|
550
|
+
def full_description
|
551
|
+
filter.fetch :full_description, nil
|
552
|
+
end
|
553
|
+
|
554
|
+
# @overload add_formatter(formatter)
|
555
|
+
#
|
556
|
+
# Adds a formatter to the formatters collection. `formatter` can be a
|
557
|
+
# string representing any of the built-in formatters (see
|
558
|
+
# `built_in_formatter`), or a custom formatter class.
|
559
|
+
#
|
560
|
+
# ### Note
|
561
|
+
#
|
562
|
+
# For internal purposes, `add_formatter` also accepts the name of a class
|
563
|
+
# and paths to use for output streams, but you should consider that a
|
564
|
+
# private api that may change at any time without notice.
|
565
|
+
def add_formatter(formatter_to_use, *paths)
|
566
|
+
formatter_class =
|
567
|
+
built_in_formatter(formatter_to_use) ||
|
568
|
+
custom_formatter(formatter_to_use) ||
|
569
|
+
(raise ArgumentError, "Formatter '#{formatter_to_use}' unknown - maybe you meant 'documentation' or 'progress'?.")
|
570
|
+
|
571
|
+
paths << output_stream if paths.empty?
|
572
|
+
formatters << formatter_class.new(*paths.map {|p| String === p ? file_at(p) : p})
|
573
|
+
end
|
574
|
+
|
575
|
+
alias_method :formatter=, :add_formatter
|
576
|
+
|
577
|
+
def formatters
|
578
|
+
@formatters ||= []
|
579
|
+
end
|
580
|
+
|
581
|
+
def reporter
|
582
|
+
@reporter ||= begin
|
583
|
+
add_formatter('progress') if formatters.empty?
|
584
|
+
add_formatter(RSpec::Core::Formatters::DeprecationFormatter, deprecation_stream, output_stream)
|
585
|
+
Reporter.new(self, *formatters)
|
586
|
+
end
|
587
|
+
end
|
588
|
+
|
589
|
+
# @api private
|
590
|
+
#
|
591
|
+
# Defaults `profile_examples` to 10 examples when `@profile_examples` is `true`.
|
592
|
+
#
|
593
|
+
def profile_examples
|
594
|
+
profile = value_for(:profile_examples, @profile_examples)
|
595
|
+
if profile && !profile.is_a?(Integer)
|
596
|
+
10
|
597
|
+
else
|
598
|
+
profile
|
599
|
+
end
|
600
|
+
end
|
601
|
+
|
602
|
+
# @private
|
603
|
+
def files_or_directories_to_run=(*files)
|
604
|
+
files = files.flatten
|
605
|
+
files << default_path if (command == 'rspec' || Runner.running_in_drb?) && default_path && files.empty?
|
606
|
+
self.files_to_run = get_files_to_run(files)
|
607
|
+
end
|
608
|
+
|
609
|
+
# Creates a method that delegates to `example` including the submitted
|
610
|
+
# `args`. Used internally to add variants of `example` like `pending`:
|
611
|
+
#
|
612
|
+
# @example
|
613
|
+
# alias_example_to :pending, :pending => true
|
614
|
+
#
|
615
|
+
# # This lets you do this:
|
616
|
+
#
|
617
|
+
# describe Thing do
|
618
|
+
# pending "does something" do
|
619
|
+
# thing = Thing.new
|
620
|
+
# end
|
621
|
+
# end
|
622
|
+
#
|
623
|
+
# # ... which is the equivalent of
|
624
|
+
#
|
625
|
+
# describe Thing do
|
626
|
+
# it "does something", :pending => true do
|
627
|
+
# thing = Thing.new
|
628
|
+
# end
|
629
|
+
# end
|
630
|
+
def alias_example_to(new_name, *args)
|
631
|
+
extra_options = Metadata.build_hash_from(args)
|
632
|
+
RSpec::Core::ExampleGroup.alias_example_to(new_name, extra_options)
|
633
|
+
end
|
634
|
+
|
635
|
+
# Define an alias for it_should_behave_like that allows different
|
636
|
+
# language (like "it_has_behavior" or "it_behaves_like") to be
|
637
|
+
# employed when including shared examples.
|
638
|
+
#
|
639
|
+
# Example:
|
640
|
+
#
|
641
|
+
# alias_it_behaves_like_to(:it_has_behavior, 'has behavior:')
|
642
|
+
#
|
643
|
+
# allows the user to include a shared example group like:
|
644
|
+
#
|
645
|
+
# describe Entity do
|
646
|
+
# it_has_behavior 'sortability' do
|
647
|
+
# let(:sortable) { Entity.new }
|
648
|
+
# end
|
649
|
+
# end
|
650
|
+
#
|
651
|
+
# which is reported in the output as:
|
652
|
+
#
|
653
|
+
# Entity
|
654
|
+
# has behavior: sortability
|
655
|
+
# # sortability examples here
|
656
|
+
def alias_it_behaves_like_to(new_name, report_label = '')
|
657
|
+
RSpec::Core::ExampleGroup.alias_it_behaves_like_to(new_name, report_label)
|
658
|
+
end
|
659
|
+
|
660
|
+
alias_method :alias_it_should_behave_like_to, :alias_it_behaves_like_to
|
661
|
+
|
662
|
+
# Adds key/value pairs to the `inclusion_filter`. If `args`
|
663
|
+
# includes any symbols that are not part of the hash, each symbol
|
664
|
+
# is treated as a key in the hash with the value `true`.
|
665
|
+
#
|
666
|
+
# ### Note
|
667
|
+
#
|
668
|
+
# Filters set using this method can be overridden from the command line
|
669
|
+
# or config files (e.g. `.rspec`).
|
670
|
+
#
|
671
|
+
# @example
|
672
|
+
# # given this declaration
|
673
|
+
# describe "something", :foo => 'bar' do
|
674
|
+
# # ...
|
675
|
+
# end
|
676
|
+
#
|
677
|
+
# # any of the following will include that group
|
678
|
+
# config.filter_run_including :foo => 'bar'
|
679
|
+
# config.filter_run_including :foo => /^ba/
|
680
|
+
# config.filter_run_including :foo => lambda {|v| v == 'bar'}
|
681
|
+
# config.filter_run_including :foo => lambda {|v,m| m[:foo] == 'bar'}
|
682
|
+
#
|
683
|
+
# # given a proc with an arity of 1, the lambda is passed the value related to the key, e.g.
|
684
|
+
# config.filter_run_including :foo => lambda {|v| v == 'bar'}
|
685
|
+
#
|
686
|
+
# # given a proc with an arity of 2, the lambda is passed the value related to the key,
|
687
|
+
# # and the metadata itself e.g.
|
688
|
+
# config.filter_run_including :foo => lambda {|v,m| m[:foo] == 'bar'}
|
689
|
+
#
|
690
|
+
# filter_run_including :foo # same as filter_run_including :foo => true
|
691
|
+
def filter_run_including(*args)
|
692
|
+
filter_manager.include_with_low_priority Metadata.build_hash_from(args)
|
693
|
+
end
|
694
|
+
|
695
|
+
alias_method :filter_run, :filter_run_including
|
696
|
+
|
697
|
+
# Clears and reassigns the `inclusion_filter`. Set to `nil` if you don't
|
698
|
+
# want any inclusion filter at all.
|
699
|
+
#
|
700
|
+
# ### Warning
|
701
|
+
#
|
702
|
+
# This overrides any inclusion filters/tags set on the command line or in
|
703
|
+
# configuration files.
|
704
|
+
def inclusion_filter=(filter)
|
705
|
+
filter_manager.include! Metadata.build_hash_from([filter])
|
706
|
+
end
|
707
|
+
|
708
|
+
alias_method :filter=, :inclusion_filter=
|
709
|
+
|
710
|
+
# Returns the `inclusion_filter`. If none has been set, returns an empty
|
711
|
+
# hash.
|
712
|
+
def inclusion_filter
|
713
|
+
filter_manager.inclusions
|
714
|
+
end
|
715
|
+
|
716
|
+
alias_method :filter, :inclusion_filter
|
717
|
+
|
718
|
+
# Adds key/value pairs to the `exclusion_filter`. If `args`
|
719
|
+
# includes any symbols that are not part of the hash, each symbol
|
720
|
+
# is treated as a key in the hash with the value `true`.
|
721
|
+
#
|
722
|
+
# ### Note
|
723
|
+
#
|
724
|
+
# Filters set using this method can be overridden from the command line
|
725
|
+
# or config files (e.g. `.rspec`).
|
726
|
+
#
|
727
|
+
# @example
|
728
|
+
# # given this declaration
|
729
|
+
# describe "something", :foo => 'bar' do
|
730
|
+
# # ...
|
731
|
+
# end
|
732
|
+
#
|
733
|
+
# # any of the following will exclude that group
|
734
|
+
# config.filter_run_excluding :foo => 'bar'
|
735
|
+
# config.filter_run_excluding :foo => /^ba/
|
736
|
+
# config.filter_run_excluding :foo => lambda {|v| v == 'bar'}
|
737
|
+
# config.filter_run_excluding :foo => lambda {|v,m| m[:foo] == 'bar'}
|
738
|
+
#
|
739
|
+
# # given a proc with an arity of 1, the lambda is passed the value related to the key, e.g.
|
740
|
+
# config.filter_run_excluding :foo => lambda {|v| v == 'bar'}
|
741
|
+
#
|
742
|
+
# # given a proc with an arity of 2, the lambda is passed the value related to the key,
|
743
|
+
# # and the metadata itself e.g.
|
744
|
+
# config.filter_run_excluding :foo => lambda {|v,m| m[:foo] == 'bar'}
|
745
|
+
#
|
746
|
+
# filter_run_excluding :foo # same as filter_run_excluding :foo => true
|
747
|
+
def filter_run_excluding(*args)
|
748
|
+
filter_manager.exclude_with_low_priority Metadata.build_hash_from(args)
|
749
|
+
end
|
750
|
+
|
751
|
+
# Clears and reassigns the `exclusion_filter`. Set to `nil` if you don't
|
752
|
+
# want any exclusion filter at all.
|
753
|
+
#
|
754
|
+
# ### Warning
|
755
|
+
#
|
756
|
+
# This overrides any exclusion filters/tags set on the command line or in
|
757
|
+
# configuration files.
|
758
|
+
def exclusion_filter=(filter)
|
759
|
+
filter_manager.exclude! Metadata.build_hash_from([filter])
|
760
|
+
end
|
761
|
+
|
762
|
+
# Returns the `exclusion_filter`. If none has been set, returns an empty
|
763
|
+
# hash.
|
764
|
+
def exclusion_filter
|
765
|
+
filter_manager.exclusions
|
766
|
+
end
|
767
|
+
|
768
|
+
# Tells RSpec to include `mod` in example groups. Methods defined in
|
769
|
+
# `mod` are exposed to examples (not example groups). Use `filters` to
|
770
|
+
# constrain the groups in which to include the module.
|
771
|
+
#
|
772
|
+
# @example
|
773
|
+
#
|
774
|
+
# module AuthenticationHelpers
|
775
|
+
# def login_as(user)
|
776
|
+
# # ...
|
777
|
+
# end
|
778
|
+
# end
|
779
|
+
#
|
780
|
+
# module UserHelpers
|
781
|
+
# def users(username)
|
782
|
+
# # ...
|
783
|
+
# end
|
784
|
+
# end
|
785
|
+
#
|
786
|
+
# RSpec.configure do |config|
|
787
|
+
# config.include(UserHelpers) # included in all modules
|
788
|
+
# config.include(AuthenticationHelpers, :type => :request)
|
789
|
+
# end
|
790
|
+
#
|
791
|
+
# describe "edit profile", :type => :request do
|
792
|
+
# it "can be viewed by owning user" do
|
793
|
+
# login_as users(:jdoe)
|
794
|
+
# get "/profiles/jdoe"
|
795
|
+
# assert_select ".username", :text => 'jdoe'
|
796
|
+
# end
|
797
|
+
# end
|
798
|
+
#
|
799
|
+
# @see #extend
|
800
|
+
def include(mod, *filters)
|
801
|
+
include_or_extend_modules << [:include, mod, Metadata.build_hash_from(filters)]
|
802
|
+
end
|
803
|
+
|
804
|
+
# Tells RSpec to extend example groups with `mod`. Methods defined in
|
805
|
+
# `mod` are exposed to example groups (not examples). Use `filters` to
|
806
|
+
# constrain the groups to extend.
|
807
|
+
#
|
808
|
+
# Similar to `include`, but behavior is added to example groups, which
|
809
|
+
# are classes, rather than the examples, which are instances of those
|
810
|
+
# classes.
|
811
|
+
#
|
812
|
+
# @example
|
813
|
+
#
|
814
|
+
# module UiHelpers
|
815
|
+
# def run_in_browser
|
816
|
+
# # ...
|
817
|
+
# end
|
818
|
+
# end
|
819
|
+
#
|
820
|
+
# RSpec.configure do |config|
|
821
|
+
# config.extend(UiHelpers, :type => :request)
|
822
|
+
# end
|
823
|
+
#
|
824
|
+
# describe "edit profile", :type => :request do
|
825
|
+
# run_in_browser
|
826
|
+
#
|
827
|
+
# it "does stuff in the client" do
|
828
|
+
# # ...
|
829
|
+
# end
|
830
|
+
# end
|
831
|
+
#
|
832
|
+
# @see #include
|
833
|
+
def extend(mod, *filters)
|
834
|
+
include_or_extend_modules << [:extend, mod, Metadata.build_hash_from(filters)]
|
835
|
+
end
|
836
|
+
|
837
|
+
# @private
|
838
|
+
#
|
839
|
+
# Used internally to extend a group with modules using `include` and/or
|
840
|
+
# `extend`.
|
841
|
+
def configure_group(group)
|
842
|
+
include_or_extend_modules.each do |include_or_extend, mod, filters|
|
843
|
+
next unless filters.empty? || group.any_apply?(filters)
|
844
|
+
send("safe_#{include_or_extend}", mod, group)
|
845
|
+
end
|
846
|
+
end
|
847
|
+
|
848
|
+
# @private
|
849
|
+
def safe_include(mod, host)
|
850
|
+
host.send(:include,mod) unless host < mod
|
851
|
+
end
|
852
|
+
|
853
|
+
# @private
|
854
|
+
def setup_load_path_and_require(paths)
|
855
|
+
directories = ['lib', default_path].select { |p| File.directory? p }
|
856
|
+
RSpec::Core::RubyProject.add_to_load_path(*directories)
|
857
|
+
paths.each {|path| require path}
|
858
|
+
@requires += paths
|
859
|
+
end
|
860
|
+
|
861
|
+
# @private
|
862
|
+
if RUBY_VERSION.to_f >= 1.9
|
863
|
+
def safe_extend(mod, host)
|
864
|
+
host.extend(mod) unless (class << host; self; end) < mod
|
865
|
+
end
|
866
|
+
else
|
867
|
+
def safe_extend(mod, host)
|
868
|
+
host.extend(mod) unless (class << host; self; end).included_modules.include?(mod)
|
869
|
+
end
|
870
|
+
end
|
871
|
+
|
872
|
+
# @private
|
873
|
+
def configure_mock_framework
|
874
|
+
RSpec::Core::ExampleGroup.send(:include, mock_framework)
|
875
|
+
end
|
876
|
+
|
877
|
+
# @private
|
878
|
+
def configure_expectation_framework
|
879
|
+
expectation_frameworks.each do |framework|
|
880
|
+
RSpec::Core::ExampleGroup.send(:include, framework)
|
881
|
+
end
|
882
|
+
end
|
883
|
+
|
884
|
+
# @private
|
885
|
+
def load_spec_files
|
886
|
+
files_to_run.uniq.each {|f| load File.expand_path(f) }
|
887
|
+
@spec_files_loaded = true
|
888
|
+
end
|
889
|
+
|
890
|
+
# @private
|
891
|
+
DEFAULT_FORMATTER = lambda { |string| string }
|
892
|
+
|
893
|
+
# Formats the docstring output using the block provided.
|
894
|
+
#
|
895
|
+
# @example
|
896
|
+
# # This will strip the descriptions of both examples and example groups.
|
897
|
+
# RSpec.configure do |config|
|
898
|
+
# config.format_docstrings { |s| s.strip }
|
899
|
+
# end
|
900
|
+
def format_docstrings(&block)
|
901
|
+
@format_docstrings_block = block_given? ? block : DEFAULT_FORMATTER
|
902
|
+
end
|
903
|
+
|
904
|
+
# @private
|
905
|
+
def format_docstrings_block
|
906
|
+
@format_docstrings_block ||= DEFAULT_FORMATTER
|
907
|
+
end
|
908
|
+
|
909
|
+
# @private
|
910
|
+
def self.delegate_to_ordering_manager(*methods)
|
911
|
+
methods.each do |method|
|
912
|
+
define_method method do |*args, &block|
|
913
|
+
ordering_manager.__send__(method, *args, &block)
|
914
|
+
end
|
915
|
+
end
|
916
|
+
end
|
917
|
+
|
918
|
+
# @macro delegate_to_ordering_manager
|
919
|
+
#
|
920
|
+
# Sets the seed value and sets the default global ordering to random.
|
921
|
+
delegate_to_ordering_manager :seed=
|
922
|
+
|
923
|
+
# @macro delegate_to_ordering_manager
|
924
|
+
# Seed for random ordering (default: generated randomly each run).
|
925
|
+
#
|
926
|
+
# When you run specs with `--order random`, RSpec generates a random seed
|
927
|
+
# for the randomization and prints it to the `output_stream` (assuming
|
928
|
+
# you're using RSpec's built-in formatters). If you discover an ordering
|
929
|
+
# dependency (i.e. examples fail intermittently depending on order), set
|
930
|
+
# this (on Configuration or on the command line with `--seed`) to run
|
931
|
+
# using the same seed while you debug the issue.
|
932
|
+
#
|
933
|
+
# We recommend, actually, that you use the command line approach so you
|
934
|
+
# don't accidentally leave the seed encoded.
|
935
|
+
delegate_to_ordering_manager :seed
|
936
|
+
|
937
|
+
# @macro delegate_to_ordering_manager
|
938
|
+
#
|
939
|
+
# Sets the default global order and, if order is `'rand:<seed>'`, also sets the seed.
|
940
|
+
delegate_to_ordering_manager :order=
|
941
|
+
|
942
|
+
# @macro delegate_to_ordering_manager
|
943
|
+
# Registers a named ordering strategy that can later be
|
944
|
+
# used to order an example group's subgroups by adding
|
945
|
+
# `:order => <name>` metadata to the example group.
|
946
|
+
#
|
947
|
+
# @param name [Symbol] The name of the ordering.
|
948
|
+
# @yield Block that will order the given examples or example groups
|
949
|
+
# @yieldparam list [Array<RSpec::Core::Example>, Array<RSpec::Core::ExampleGroup>] The examples or groups to order
|
950
|
+
# @yieldreturn [Array<RSpec::Core::Example>, Array<RSpec::Core::ExampleGroup>] The re-ordered examples or groups
|
951
|
+
#
|
952
|
+
# @example
|
953
|
+
# RSpec.configure do |rspec|
|
954
|
+
# rspec.register_ordering :reverse do |list|
|
955
|
+
# list.reverse
|
956
|
+
# end
|
957
|
+
# end
|
958
|
+
#
|
959
|
+
# describe MyClass, :order => :reverse do
|
960
|
+
# # ...
|
961
|
+
# end
|
962
|
+
#
|
963
|
+
# @note Pass the symbol `:global` to set the ordering strategy that
|
964
|
+
# will be used to order the top-level example groups and any example
|
965
|
+
# groups that do not have declared `:order` metadata.
|
966
|
+
delegate_to_ordering_manager :register_ordering
|
967
|
+
|
968
|
+
# @private
|
969
|
+
delegate_to_ordering_manager :seed_used?, :ordering_registry
|
970
|
+
|
971
|
+
# Set Ruby warnings on or off
|
972
|
+
def warnings= value
|
973
|
+
$VERBOSE = !!value
|
974
|
+
end
|
975
|
+
|
976
|
+
def warnings
|
977
|
+
$VERBOSE
|
978
|
+
end
|
979
|
+
|
980
|
+
# Exposes the current running example via the named
|
981
|
+
# helper method. RSpec 2.x exposed this via `example`,
|
982
|
+
# but in RSpec 3.0, the example is instead exposed via
|
983
|
+
# an arg yielded to `it`, `before`, `let`, etc. However,
|
984
|
+
# some extension gems (such as Capybara) depend on the
|
985
|
+
# RSpec 2.x's `example` method, so this config option
|
986
|
+
# can be used to maintain compatibility.
|
987
|
+
#
|
988
|
+
# @param method_name [Symbol] the name of the helper method
|
989
|
+
#
|
990
|
+
# @example
|
991
|
+
#
|
992
|
+
# RSpec.configure do |rspec|
|
993
|
+
# rspec.expose_current_running_example_as :example
|
994
|
+
# end
|
995
|
+
#
|
996
|
+
# describe MyClass do
|
997
|
+
# before do
|
998
|
+
# # `example` can be used here because of the above config.
|
999
|
+
# do_something if example.metadata[:type] == "foo"
|
1000
|
+
# end
|
1001
|
+
# end
|
1002
|
+
def expose_current_running_example_as(method_name)
|
1003
|
+
ExposeCurrentExample.module_eval do
|
1004
|
+
extend RSpec::SharedContext
|
1005
|
+
let(method_name) { |ex| ex }
|
1006
|
+
end
|
1007
|
+
|
1008
|
+
include ExposeCurrentExample
|
1009
|
+
end
|
1010
|
+
|
1011
|
+
module ExposeCurrentExample; end
|
1012
|
+
|
1013
|
+
# Turns deprecation warnings into errors, in order to surface
|
1014
|
+
# the full backtrace of the call site. This can be useful when
|
1015
|
+
# you need more context to address a deprecation than the
|
1016
|
+
# single-line call site normally provided.
|
1017
|
+
#
|
1018
|
+
# @example
|
1019
|
+
#
|
1020
|
+
# RSpec.configure do |rspec|
|
1021
|
+
# rspec.raise_errors_for_deprecations!
|
1022
|
+
# end
|
1023
|
+
def raise_errors_for_deprecations!
|
1024
|
+
self.deprecation_stream = Formatters::DeprecationFormatter::RaiseErrorStream.new
|
1025
|
+
end
|
1026
|
+
|
1027
|
+
private
|
1028
|
+
|
1029
|
+
def get_files_to_run(paths)
|
1030
|
+
FlatMap.flat_map(paths) do |path|
|
1031
|
+
path = path.gsub(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
|
1032
|
+
File.directory?(path) ? gather_directories(path) : extract_location(path)
|
1033
|
+
end.sort
|
1034
|
+
end
|
1035
|
+
|
1036
|
+
def gather_directories(path)
|
1037
|
+
stripped = "{#{pattern.gsub(/\s*,\s*/, ',')}}"
|
1038
|
+
files = pattern =~ /^#{Regexp.escape path}/ ? Dir[stripped] : Dir["#{path}/#{stripped}"]
|
1039
|
+
files.sort
|
1040
|
+
end
|
1041
|
+
|
1042
|
+
def extract_location(path)
|
1043
|
+
if path =~ /^(.*?)((?:\:\d+)+)$/
|
1044
|
+
path, lines = $1, $2[1..-1].split(":").map{|n| n.to_i}
|
1045
|
+
filter_manager.add_location path, lines
|
1046
|
+
end
|
1047
|
+
path
|
1048
|
+
end
|
1049
|
+
|
1050
|
+
def command
|
1051
|
+
$0.split(File::SEPARATOR).last
|
1052
|
+
end
|
1053
|
+
|
1054
|
+
def value_for(key, default=nil)
|
1055
|
+
@preferred_options.has_key?(key) ? @preferred_options[key] : default
|
1056
|
+
end
|
1057
|
+
|
1058
|
+
def assert_no_example_groups_defined(config_option)
|
1059
|
+
if RSpec.world.example_groups.any?
|
1060
|
+
raise MustBeConfiguredBeforeExampleGroupsError.new(
|
1061
|
+
"RSpec's #{config_option} configuration option must be configured before " +
|
1062
|
+
"any example groups are defined, but you have already defined a group."
|
1063
|
+
)
|
1064
|
+
end
|
1065
|
+
end
|
1066
|
+
|
1067
|
+
def output_to_tty?(output=output_stream)
|
1068
|
+
tty? || (output.respond_to?(:tty?) && output.tty?)
|
1069
|
+
end
|
1070
|
+
|
1071
|
+
def built_in_formatter(key)
|
1072
|
+
case key.to_s
|
1073
|
+
when 'd', 'doc', 'documentation', 's', 'n', 'spec', 'nested'
|
1074
|
+
require 'rspec/core/formatters/documentation_formatter'
|
1075
|
+
RSpec::Core::Formatters::DocumentationFormatter
|
1076
|
+
when 'h', 'html'
|
1077
|
+
require 'rspec/core/formatters/html_formatter'
|
1078
|
+
RSpec::Core::Formatters::HtmlFormatter
|
1079
|
+
when 'p', 'progress'
|
1080
|
+
require 'rspec/core/formatters/progress_formatter'
|
1081
|
+
RSpec::Core::Formatters::ProgressFormatter
|
1082
|
+
when 'j', 'json'
|
1083
|
+
require 'rspec/core/formatters/json_formatter'
|
1084
|
+
RSpec::Core::Formatters::JsonFormatter
|
1085
|
+
end
|
1086
|
+
end
|
1087
|
+
|
1088
|
+
def custom_formatter(formatter_ref)
|
1089
|
+
if Class === formatter_ref
|
1090
|
+
formatter_ref
|
1091
|
+
elsif string_const?(formatter_ref)
|
1092
|
+
begin
|
1093
|
+
formatter_ref.gsub(/^::/,'').split('::').inject(Object) { |const,string| const.const_get string }
|
1094
|
+
rescue NameError
|
1095
|
+
require( path_for(formatter_ref) ) ? retry : raise
|
1096
|
+
end
|
1097
|
+
end
|
1098
|
+
end
|
1099
|
+
|
1100
|
+
def string_const?(str)
|
1101
|
+
str.is_a?(String) && /\A[A-Z][a-zA-Z0-9_:]*\z/ =~ str
|
1102
|
+
end
|
1103
|
+
|
1104
|
+
def path_for(const_ref)
|
1105
|
+
underscore_with_fix_for_non_standard_rspec_naming(const_ref)
|
1106
|
+
end
|
1107
|
+
|
1108
|
+
def underscore_with_fix_for_non_standard_rspec_naming(string)
|
1109
|
+
underscore(string).sub(%r{(^|/)r_spec($|/)}, '\\1rspec\\2')
|
1110
|
+
end
|
1111
|
+
|
1112
|
+
# activesupport/lib/active_support/inflector/methods.rb, line 48
|
1113
|
+
def underscore(camel_cased_word)
|
1114
|
+
word = camel_cased_word.to_s.dup
|
1115
|
+
word.gsub!(/::/, '/')
|
1116
|
+
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
1117
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
1118
|
+
word.tr!("-", "_")
|
1119
|
+
word.downcase!
|
1120
|
+
word
|
1121
|
+
end
|
1122
|
+
|
1123
|
+
def file_at(path)
|
1124
|
+
FileUtils.mkdir_p(File.dirname(path))
|
1125
|
+
File.new(path, 'w')
|
1126
|
+
end
|
1127
|
+
end
|
1128
|
+
end
|
1129
|
+
end
|