rspec-expectations 2.13.0 → 2.14.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.md +34 -0
- data/README.md +43 -87
- data/features/README.md +8 -9
- data/features/built_in_matchers/README.md +41 -41
- data/features/built_in_matchers/be_within.feature +3 -3
- data/features/built_in_matchers/expect_change.feature +6 -6
- data/features/built_in_matchers/expect_error.feature +2 -2
- data/features/built_in_matchers/start_with.feature +1 -1
- data/features/built_in_matchers/throw_symbol.feature +11 -11
- data/features/built_in_matchers/yield.feature +18 -3
- data/features/custom_matchers/define_diffable_matcher.feature +1 -1
- data/features/custom_matchers/define_matcher_outside_rspec.feature +6 -6
- data/features/custom_matchers/define_matcher_with_fluent_interface.feature +1 -1
- data/features/customized_message.feature +1 -1
- data/features/support/env.rb +10 -1
- data/features/syntax_configuration.feature +3 -0
- data/features/test_frameworks/test_unit.feature +15 -17
- data/lib/rspec/expectations.rb +1 -1
- data/lib/rspec/expectations/deprecation.rb +12 -33
- data/lib/rspec/expectations/differ.rb +25 -7
- data/lib/rspec/expectations/expectation_target.rb +7 -8
- data/lib/rspec/expectations/extensions/object.rb +2 -12
- data/lib/rspec/expectations/fail_with.rb +11 -1
- data/lib/rspec/expectations/handler.rb +11 -6
- data/lib/rspec/expectations/syntax.rb +2 -2
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/matchers.rb +134 -144
- data/lib/rspec/matchers/be_close.rb +1 -1
- data/lib/rspec/matchers/built_in/be_within.rb +1 -1
- data/lib/rspec/matchers/built_in/have.rb +20 -4
- data/lib/rspec/matchers/built_in/raise_error.rb +23 -7
- data/lib/rspec/matchers/built_in/yield.rb +78 -3
- data/lib/rspec/matchers/operator_matcher.rb +1 -1
- data/lib/rspec/matchers/test_unit_integration.rb +11 -0
- data/spec/rspec/expectations/differ_spec.rb +27 -5
- data/spec/rspec/expectations/expectation_target_spec.rb +10 -3
- data/spec/rspec/expectations/extensions/kernel_spec.rb +5 -5
- data/spec/rspec/expectations/fail_with_spec.rb +19 -0
- data/spec/rspec/expectations/handler_spec.rb +42 -21
- data/spec/rspec/expectations/syntax_spec.rb +45 -3
- data/spec/rspec/matchers/be_close_spec.rb +6 -6
- data/spec/rspec/matchers/be_spec.rb +36 -36
- data/spec/rspec/matchers/be_within_spec.rb +4 -0
- data/spec/rspec/matchers/change_spec.rb +6 -6
- data/spec/rspec/matchers/configuration_spec.rb +57 -89
- data/spec/rspec/matchers/description_generation_spec.rb +1 -1
- data/spec/rspec/matchers/exist_spec.rb +9 -9
- data/spec/rspec/matchers/has_spec.rb +1 -1
- data/spec/rspec/matchers/have_spec.rb +12 -2
- data/spec/rspec/matchers/include_matcher_integration_spec.rb +2 -2
- data/spec/rspec/matchers/include_spec.rb +4 -4
- data/spec/rspec/matchers/match_array_spec.rb +1 -1
- data/spec/rspec/matchers/match_spec.rb +1 -1
- data/spec/rspec/matchers/raise_error_spec.rb +189 -99
- data/spec/rspec/matchers/respond_to_spec.rb +4 -4
- data/spec/rspec/matchers/satisfy_spec.rb +1 -1
- data/spec/rspec/matchers/start_with_end_with_spec.rb +2 -2
- data/spec/rspec/matchers/yield_spec.rb +81 -4
- data/spec/spec_helper.rb +1 -1
- metadata +10 -12
@@ -52,7 +52,17 @@ module RSpec
|
|
52
52
|
|
53
53
|
def coerce_to_string(string_or_array)
|
54
54
|
return string_or_array unless Array === string_or_array
|
55
|
-
string_or_array.join(
|
55
|
+
diffably_stringify(string_or_array).join("\n")
|
56
|
+
end
|
57
|
+
|
58
|
+
def diffably_stringify(array)
|
59
|
+
array.map do |entry|
|
60
|
+
if Array === entry
|
61
|
+
entry.inspect
|
62
|
+
else
|
63
|
+
entry.to_s.gsub("\n", "\\n")
|
64
|
+
end
|
65
|
+
end
|
56
66
|
end
|
57
67
|
|
58
68
|
if String.method_defined?(:encoding)
|
@@ -2,13 +2,14 @@ module RSpec
|
|
2
2
|
module Expectations
|
3
3
|
|
4
4
|
class ExpectationHandler
|
5
|
-
def self.message_must_be_string(msg)
|
6
|
-
"WARNING: ignoring the provided expectation message argument " +
|
7
|
-
"(#{msg.inspect}) since it is not a string."
|
8
|
-
end
|
9
|
-
|
10
5
|
def self.check_message(msg)
|
11
|
-
|
6
|
+
unless msg.nil? || msg.respond_to?(:to_str) || msg.respond_to?(:call)
|
7
|
+
::Kernel.warn [
|
8
|
+
"WARNING: ignoring the provided expectation message argument (",
|
9
|
+
msg.inspect,
|
10
|
+
") since it is not a string or a proc."
|
11
|
+
].join
|
12
|
+
end
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
@@ -23,6 +24,8 @@ module RSpec
|
|
23
24
|
match = matcher.matches?(actual, &block)
|
24
25
|
return match if match
|
25
26
|
|
27
|
+
message = message.call if message.respond_to?(:call)
|
28
|
+
|
26
29
|
message ||= matcher.respond_to?(:failure_message_for_should) ?
|
27
30
|
matcher.failure_message_for_should :
|
28
31
|
matcher.failure_message
|
@@ -47,6 +50,8 @@ module RSpec
|
|
47
50
|
matcher.matches?(actual, &block)
|
48
51
|
return match unless match
|
49
52
|
|
53
|
+
message = message.call if message.respond_to?(:call)
|
54
|
+
|
50
55
|
message ||= matcher.respond_to?(:failure_message_for_should_not) ?
|
51
56
|
matcher.failure_message_for_should_not :
|
52
57
|
matcher.negative_failure_message
|
@@ -32,10 +32,10 @@ module RSpec
|
|
32
32
|
# `ExpectationTarget`.
|
33
33
|
# @example
|
34
34
|
# expect(actual).to eq(expected)
|
35
|
-
# expect(actual).
|
35
|
+
# expect(actual).not_to eq(expected)
|
36
36
|
# @return [ExpectationTarget]
|
37
37
|
# @see ExpectationTarget#to
|
38
|
-
# @see ExpectationTarget#
|
38
|
+
# @see ExpectationTarget#not_to
|
39
39
|
|
40
40
|
# @api private
|
41
41
|
# Determines where we add `should` and `should_not`.
|
data/lib/rspec/matchers.rb
CHANGED
@@ -1,5 +1,19 @@
|
|
1
|
+
require 'rspec/matchers/extensions/instance_eval_with_args'
|
2
|
+
require 'rspec/matchers/pretty'
|
3
|
+
|
4
|
+
require 'rspec/matchers/built_in'
|
5
|
+
require 'rspec/matchers/matcher'
|
6
|
+
require 'rspec/matchers/operator_matcher'
|
7
|
+
require 'rspec/matchers/be_close'
|
8
|
+
|
9
|
+
require 'rspec/matchers/generated_descriptions'
|
10
|
+
require 'rspec/matchers/method_missing'
|
11
|
+
require 'rspec/matchers/compatibility'
|
12
|
+
require 'rspec/matchers/dsl'
|
13
|
+
require 'rspec/matchers/test_unit_integration'
|
14
|
+
|
1
15
|
module RSpec
|
2
|
-
# RSpec::Matchers provides a number of useful matchers we use to
|
16
|
+
# RSpec::Matchers provides a number of useful matchers we use to define
|
3
17
|
# expectations. A matcher is any object that responds to the following:
|
4
18
|
#
|
5
19
|
# matches?(actual)
|
@@ -20,28 +34,29 @@ module RSpec
|
|
20
34
|
# A Ruby predicate is a method that ends with a "?" and returns true or false.
|
21
35
|
# Common examples are `empty?`, `nil?`, and `instance_of?`.
|
22
36
|
#
|
23
|
-
# All you need to do is write `
|
24
|
-
# the question mark, and RSpec will figure it out from there.
|
37
|
+
# All you need to do is write `expect(..).to be_` followed by the predicate
|
38
|
+
# without the question mark, and RSpec will figure it out from there.
|
39
|
+
# For example:
|
25
40
|
#
|
26
|
-
# [].
|
27
|
-
# [].
|
41
|
+
# expect([]).to be_empty # => [].empty?() | passes
|
42
|
+
# expect([]).not_to be_empty # => [].empty?() | fails
|
28
43
|
#
|
29
44
|
# In addtion to prefixing the predicate matchers with "be_", you can also use "be_a_"
|
30
45
|
# and "be_an_", making your specs read much more naturally:
|
31
46
|
#
|
32
|
-
# "a string".
|
47
|
+
# expect("a string").to be_an_instance_of(String) # =>"a string".instance_of?(String) # passes
|
33
48
|
#
|
34
|
-
# 3.
|
35
|
-
# 3.
|
36
|
-
# 3.
|
37
|
-
# 3.
|
49
|
+
# expect(3).to be_a_kind_of(Fixnum) # => 3.kind_of?(Numeric) | passes
|
50
|
+
# expect(3).to be_a_kind_of(Numeric) # => 3.kind_of?(Numeric) | passes
|
51
|
+
# expect(3).to be_an_instance_of(Fixnum) # => 3.instance_of?(Fixnum) | passes
|
52
|
+
# expect(3).not_to be_an_instance_of(Numeric) # => 3.instance_of?(Numeric) | fails
|
38
53
|
#
|
39
54
|
# RSpec will also create custom matchers for predicates like `has_key?`. To
|
40
55
|
# use this feature, just state that the object should have_key(:key) and RSpec will
|
41
56
|
# call has_key?(:key) on the target. For example:
|
42
57
|
#
|
43
|
-
#
|
44
|
-
#
|
58
|
+
# expect(:a => "A").to have_key(:a)
|
59
|
+
# expect(:a => "A").to have_key(:b) # fails
|
45
60
|
#
|
46
61
|
# You can use this feature to invoke any predicate that begins with "has_", whether it is
|
47
62
|
# part of the Ruby libraries (like `Hash#has_key?`) or a method you wrote on your own class.
|
@@ -58,15 +73,15 @@ module RSpec
|
|
58
73
|
# zones on a virtual board. To specify that bob should be in zone 4, you
|
59
74
|
# could say:
|
60
75
|
#
|
61
|
-
# bob.current_zone.
|
76
|
+
# expect(bob.current_zone).to eql(Zone.new("4"))
|
62
77
|
#
|
63
78
|
# But you might find it more expressive to say:
|
64
79
|
#
|
65
|
-
# bob.
|
80
|
+
# expect(bob).to be_in_zone("4")
|
66
81
|
#
|
67
82
|
# and/or
|
68
83
|
#
|
69
|
-
# bob.
|
84
|
+
# expect(bob).not_to be_in_zone("3")
|
70
85
|
#
|
71
86
|
# You can create such a matcher like so:
|
72
87
|
#
|
@@ -102,7 +117,7 @@ module RSpec
|
|
102
117
|
# passed to the <tt>create</tt> method (in this case, <tt>zone</tt>). The
|
103
118
|
# failure message methods (<tt>failure_message_for_should</tt> and
|
104
119
|
# <tt>failure_message_for_should_not</tt>) are passed the actual value (the
|
105
|
-
# receiver of <tt>
|
120
|
+
# receiver of <tt>expect(..)</tt> or <tt>expect(..).not_to</tt>).
|
106
121
|
#
|
107
122
|
# ### Custom Matcher from scratch
|
108
123
|
#
|
@@ -158,35 +173,6 @@ module RSpec
|
|
158
173
|
# RSpec::configure do |config|
|
159
174
|
# config.include(CustomGameMatchers)
|
160
175
|
# end
|
161
|
-
module Matchers
|
162
|
-
# Include Matchers for other test frameworks. Note that MiniTest _must_
|
163
|
-
# come before TU because on ruby 1.9, T::U::TC is a subclass of MT::U::TC
|
164
|
-
# and a 1.9 bug can lead to infinite recursion from the `super` call in our
|
165
|
-
# method_missing hook. See this gist for more info:
|
166
|
-
# https://gist.github.com/845896
|
167
|
-
if defined?(MiniTest::Unit::TestCase)
|
168
|
-
MiniTest::Unit::TestCase.send(:include, self)
|
169
|
-
end
|
170
|
-
if defined?(Test::Unit::TestCase)
|
171
|
-
Test::Unit::TestCase.send(:include, self)
|
172
|
-
end
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
require 'rspec/matchers/extensions/instance_eval_with_args'
|
177
|
-
require 'rspec/matchers/pretty'
|
178
|
-
|
179
|
-
require 'rspec/matchers/built_in'
|
180
|
-
require 'rspec/matchers/matcher'
|
181
|
-
require 'rspec/matchers/operator_matcher'
|
182
|
-
require 'rspec/matchers/be_close'
|
183
|
-
|
184
|
-
require 'rspec/matchers/generated_descriptions'
|
185
|
-
require 'rspec/matchers/method_missing'
|
186
|
-
require 'rspec/matchers/compatibility'
|
187
|
-
require 'rspec/matchers/dsl'
|
188
|
-
|
189
|
-
module RSpec
|
190
176
|
module Matchers
|
191
177
|
|
192
178
|
# Passes if actual is truthy (anything but false or nil)
|
@@ -205,12 +191,12 @@ module RSpec
|
|
205
191
|
end
|
206
192
|
|
207
193
|
# @example
|
208
|
-
# actual.
|
209
|
-
# actual.
|
210
|
-
# actual.
|
211
|
-
# actual.
|
212
|
-
# actual.
|
213
|
-
# actual.
|
194
|
+
# expect(actual).to be_true
|
195
|
+
# expect(actual).to be_false
|
196
|
+
# expect(actual).to be_nil
|
197
|
+
# expect(actual).to be_[arbitrary_predicate](*args)
|
198
|
+
# expect(actual).not_to be_nil
|
199
|
+
# expect(actual).not_to be_[arbitrary_predicate](*args)
|
214
200
|
#
|
215
201
|
# Given true, false, or nil, will pass if actual value is true, false or
|
216
202
|
# nil (respectively). Given no args means the caller should satisfy an if
|
@@ -240,9 +226,9 @@ module RSpec
|
|
240
226
|
#
|
241
227
|
# @example
|
242
228
|
#
|
243
|
-
# 5.
|
244
|
-
# 5.
|
245
|
-
# 5.
|
229
|
+
# expect(5).to be_an_instance_of(Fixnum)
|
230
|
+
# expect(5).not_to be_an_instance_of(Numeric)
|
231
|
+
# expect(5).not_to be_an_instance_of(Float)
|
246
232
|
def be_an_instance_of(expected)
|
247
233
|
BuiltIn::BeAnInstanceOf.new(expected)
|
248
234
|
end
|
@@ -253,9 +239,9 @@ module RSpec
|
|
253
239
|
#
|
254
240
|
# @example
|
255
241
|
#
|
256
|
-
# 5.
|
257
|
-
# 5.
|
258
|
-
# 5.
|
242
|
+
# expect(5).to be_a_kind_of(Fixnum)
|
243
|
+
# expect(5).to be_a_kind_of(Numeric)
|
244
|
+
# expect(5).not_to be_a_kind_of(Float)
|
259
245
|
def be_a_kind_of(expected)
|
260
246
|
BuiltIn::BeAKindOf.new(expected)
|
261
247
|
end
|
@@ -266,8 +252,8 @@ module RSpec
|
|
266
252
|
#
|
267
253
|
# @example
|
268
254
|
#
|
269
|
-
# result.
|
270
|
-
# result.
|
255
|
+
# expect(result).to be_within(0.5).of(3.0)
|
256
|
+
# expect(result).not_to be_within(0.5).of(3.0)
|
271
257
|
def be_within(delta)
|
272
258
|
BuiltIn::BeWithin.new(delta)
|
273
259
|
end
|
@@ -283,55 +269,59 @@ module RSpec
|
|
283
269
|
#
|
284
270
|
# When passing a block, it must use the <tt>{ ... }</tt> format, not
|
285
271
|
# do/end, as <tt>{ ... }</tt> binds to the +change+ method, whereas do/end
|
286
|
-
# would errantly bind to the +
|
272
|
+
# would errantly bind to the +expect(..)+ or +expect(..).not_to+ method.
|
287
273
|
#
|
288
274
|
# @example
|
289
275
|
#
|
290
|
-
#
|
276
|
+
# expect {
|
291
277
|
# team.add_player(player)
|
292
|
-
# }.
|
278
|
+
# }.to change(roster, :count)
|
293
279
|
#
|
294
|
-
#
|
280
|
+
# expect {
|
295
281
|
# team.add_player(player)
|
296
|
-
# }.
|
282
|
+
# }.to change(roster, :count).by(1)
|
297
283
|
#
|
298
|
-
#
|
284
|
+
# expect {
|
299
285
|
# team.add_player(player)
|
300
|
-
# }.
|
286
|
+
# }.to change(roster, :count).by_at_least(1)
|
301
287
|
#
|
302
|
-
#
|
288
|
+
# expect {
|
303
289
|
# team.add_player(player)
|
304
|
-
# }.
|
290
|
+
# }.to change(roster, :count).by_at_most(1)
|
305
291
|
#
|
306
292
|
# string = "string"
|
307
|
-
#
|
293
|
+
# expect {
|
308
294
|
# string.reverse!
|
309
|
-
# }.
|
295
|
+
# }.to change { string }.from("string").to("gnirts")
|
310
296
|
#
|
311
|
-
#
|
297
|
+
# string = "string"
|
298
|
+
# expect {
|
299
|
+
# string
|
300
|
+
# }.not_to change { string }
|
301
|
+
#
|
302
|
+
# expect {
|
312
303
|
# person.happy_birthday
|
313
|
-
# }.
|
304
|
+
# }.to change(person, :birthday).from(32).to(33)
|
314
305
|
#
|
315
|
-
#
|
306
|
+
# expect {
|
316
307
|
# employee.develop_great_new_social_networking_app
|
317
|
-
# }.
|
308
|
+
# }.to change(employee, :title).from("Mail Clerk").to("CEO")
|
318
309
|
#
|
319
|
-
#
|
310
|
+
# expect {
|
320
311
|
# doctor.leave_office
|
321
|
-
# }.
|
312
|
+
# }.to change(doctor, :sign).from(/is in/).to(/is out/)
|
322
313
|
#
|
323
314
|
# user = User.new(:type => "admin")
|
324
|
-
#
|
315
|
+
# expect {
|
325
316
|
# user.symbolize_type
|
326
|
-
# }.
|
317
|
+
# }.to change(user, :type).from(String).to(Symbol)
|
327
318
|
#
|
328
319
|
# == Notes
|
329
320
|
#
|
330
321
|
# Evaluates <tt>receiver.message</tt> or <tt>block</tt> before and after it
|
331
|
-
# evaluates the
|
332
|
-
# above).
|
322
|
+
# evaluates the block passed to <tt>expect</tt>.
|
333
323
|
#
|
334
|
-
# <tt>
|
324
|
+
# <tt>expect( ... ).not_to change</tt> only supports the form with no subsequent
|
335
325
|
# calls to <tt>by</tt>, <tt>by_at_least</tt>, <tt>by_at_most</tt>,
|
336
326
|
# <tt>to</tt> or <tt>from</tt>.
|
337
327
|
def change(receiver=nil, message=nil, &block)
|
@@ -343,11 +333,11 @@ module RSpec
|
|
343
333
|
# and it will only pass if all args are found in Range.
|
344
334
|
#
|
345
335
|
# @example
|
346
|
-
# (1..10).
|
347
|
-
# (1..10).
|
348
|
-
# (1..10).
|
349
|
-
# (1..10).
|
350
|
-
# (1..10).
|
336
|
+
# expect(1..10).to cover(5)
|
337
|
+
# expect(1..10).to cover(4, 6)
|
338
|
+
# expect(1..10).to cover(4, 6, 11) # fails
|
339
|
+
# expect(1..10).not_to cover(11)
|
340
|
+
# expect(1..10).not_to cover(5) # fails
|
351
341
|
#
|
352
342
|
# ### Warning:: Ruby >= 1.9 only
|
353
343
|
def cover(*values)
|
@@ -361,45 +351,48 @@ module RSpec
|
|
361
351
|
#
|
362
352
|
# @example
|
363
353
|
#
|
364
|
-
# "this string".
|
365
|
-
# [0, 1, 2, 3, 4].
|
366
|
-
# [0, 2, 3, 4, 4].
|
354
|
+
# expect("this string").to end_with "string"
|
355
|
+
# expect([0, 1, 2, 3, 4]).to end_with 4
|
356
|
+
# expect([0, 2, 3, 4, 4]).to end_with 3, 4
|
367
357
|
def end_with(*expected)
|
368
358
|
BuiltIn::EndWith.new(*expected)
|
369
359
|
end
|
370
360
|
|
371
361
|
# Passes if <tt>actual == expected</tt>.
|
372
362
|
#
|
373
|
-
# See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more
|
363
|
+
# See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more
|
364
|
+
# information about equality in Ruby.
|
374
365
|
#
|
375
366
|
# @example
|
376
367
|
#
|
377
|
-
# 5.
|
378
|
-
# 5.
|
368
|
+
# expect(5).to eq(5)
|
369
|
+
# expect(5).not_to eq(3)
|
379
370
|
def eq(expected)
|
380
371
|
BuiltIn::Eq.new(expected)
|
381
372
|
end
|
382
373
|
|
383
374
|
# Passes if +actual.eql?(expected)+
|
384
375
|
#
|
385
|
-
# See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more
|
376
|
+
# See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more
|
377
|
+
# information about equality in Ruby.
|
386
378
|
#
|
387
379
|
# @example
|
388
380
|
#
|
389
|
-
# 5.
|
390
|
-
# 5.
|
381
|
+
# expect(5).to eql(5)
|
382
|
+
# expect(5).not_to eql(3)
|
391
383
|
def eql(expected)
|
392
384
|
BuiltIn::Eql.new(expected)
|
393
385
|
end
|
394
386
|
|
395
387
|
# Passes if <tt>actual.equal?(expected)</tt> (object identity).
|
396
388
|
#
|
397
|
-
# See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more
|
389
|
+
# See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more
|
390
|
+
# information about equality in Ruby.
|
398
391
|
#
|
399
392
|
# @example
|
400
393
|
#
|
401
|
-
# 5.
|
402
|
-
# "5".
|
394
|
+
# expect(5).to equal(5) # Fixnums are equal
|
395
|
+
# expect("5").not_to equal("5") # Strings that look the same are not the same object
|
403
396
|
def equal(expected)
|
404
397
|
BuiltIn::Equal.new(expected)
|
405
398
|
end
|
@@ -407,7 +400,7 @@ module RSpec
|
|
407
400
|
# Passes if `actual.exist?` or `actual.exists?`
|
408
401
|
#
|
409
402
|
# @example
|
410
|
-
# File.
|
403
|
+
# expect(File).to exist("path/to/file")
|
411
404
|
def exist(*args)
|
412
405
|
BuiltIn::Exist.new(*args)
|
413
406
|
end
|
@@ -430,16 +423,16 @@ module RSpec
|
|
430
423
|
# @example
|
431
424
|
#
|
432
425
|
# # Passes if team.players.size == 11
|
433
|
-
# team.
|
426
|
+
# expect(team).to have(11).players
|
434
427
|
#
|
435
428
|
# # Passes if [1,2,3].length == 3
|
436
|
-
# [1,2,3].
|
429
|
+
# expect([1,2,3]).to have(3).items #"items" is pure sugar
|
437
430
|
#
|
438
431
|
# # Passes if ['a', 'b', 'c'].count == 3
|
439
|
-
# [1,2,3].
|
432
|
+
# expect([1,2,3]).to have(3).items #"items" is pure sugar
|
440
433
|
#
|
441
434
|
# # Passes if "this string".length == 11
|
442
|
-
# "this string".
|
435
|
+
# expect("this string").to have(11).characters #"characters" is pure sugar
|
443
436
|
def have(n)
|
444
437
|
BuiltIn::Have.new(n)
|
445
438
|
end
|
@@ -448,11 +441,11 @@ module RSpec
|
|
448
441
|
# Exactly like have() with >=.
|
449
442
|
#
|
450
443
|
# @example
|
451
|
-
# "this".
|
444
|
+
# expect("this").to have_at_least(3).letters
|
452
445
|
#
|
453
446
|
# ### Warning:
|
454
447
|
#
|
455
|
-
# `
|
448
|
+
# `expect(..).not_to have_at_least` is not supported
|
456
449
|
def have_at_least(n)
|
457
450
|
BuiltIn::Have.new(n, :at_least)
|
458
451
|
end
|
@@ -460,11 +453,11 @@ module RSpec
|
|
460
453
|
# Exactly like have() with <=.
|
461
454
|
#
|
462
455
|
# @example
|
463
|
-
#
|
456
|
+
# expect("this").to have_at_most(4).letters
|
464
457
|
#
|
465
458
|
# ### Warning:
|
466
459
|
#
|
467
|
-
# `
|
460
|
+
# `expect(..).not_to have_at_most` is not supported
|
468
461
|
def have_at_most(n)
|
469
462
|
BuiltIn::Have.new(n, :at_most)
|
470
463
|
end
|
@@ -475,12 +468,12 @@ module RSpec
|
|
475
468
|
#
|
476
469
|
# @example
|
477
470
|
#
|
478
|
-
# [1,2,3].
|
479
|
-
# [1,2,3].
|
480
|
-
# [1,2,3].
|
481
|
-
# [1,2,3].
|
482
|
-
# "spread".
|
483
|
-
# "spread".
|
471
|
+
# expect([1,2,3]).to include(3)
|
472
|
+
# expect([1,2,3]).to include(2,3)
|
473
|
+
# expect([1,2,3]).to include(2,3,4) # fails
|
474
|
+
# expect([1,2,3]).not_to include(4)
|
475
|
+
# expect("spread").to include("read")
|
476
|
+
# expect("spread").not_to include("red")
|
484
477
|
def include(*expected)
|
485
478
|
BuiltIn::Include.new(*expected)
|
486
479
|
end
|
@@ -489,10 +482,10 @@ module RSpec
|
|
489
482
|
#
|
490
483
|
# @example
|
491
484
|
#
|
492
|
-
# email.
|
493
|
-
# email.
|
494
|
-
# zipcode.
|
495
|
-
# zipcode.
|
485
|
+
# expect(email).to match(/^([^\s]+)((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
|
486
|
+
# expect(email).to match("@example.com")
|
487
|
+
# expect(zipcode).to match_regex(/\A\d{5}(-\d{4})?\z/)
|
488
|
+
# expect(zipcode).to match_regex("90210")
|
496
489
|
#
|
497
490
|
# @note Due to Ruby's method dispatch mechanism, using the `#match` matcher
|
498
491
|
# within a custom matcher defined via the matcher DSL
|
@@ -513,16 +506,16 @@ module RSpec
|
|
513
506
|
#
|
514
507
|
# @example
|
515
508
|
#
|
516
|
-
#
|
517
|
-
#
|
518
|
-
#
|
519
|
-
#
|
520
|
-
#
|
509
|
+
# expect { do_something_risky }.to raise_error
|
510
|
+
# expect { do_something_risky }.to raise_error(PoorRiskDecisionError)
|
511
|
+
# expect { do_something_risky }.to raise_error(PoorRiskDecisionError) { |error| expect(error.data).to eq 42 }
|
512
|
+
# expect { do_something_risky }.to raise_error(PoorRiskDecisionError, "that was too risky")
|
513
|
+
# expect { do_something_risky }.to raise_error(PoorRiskDecisionError, /oo ri/)
|
521
514
|
#
|
522
|
-
#
|
523
|
-
#
|
524
|
-
#
|
525
|
-
#
|
515
|
+
# expect { do_something_risky }.not_to raise_error
|
516
|
+
# expect { do_something_risky }.not_to raise_error(PoorRiskDecisionError)
|
517
|
+
# expect { do_something_risky }.not_to raise_error(PoorRiskDecisionError, "that was too risky")
|
518
|
+
# expect { do_something_risky }.not_to raise_error(PoorRiskDecisionError, /oo ri/)
|
526
519
|
def raise_error(error=Exception, message=nil, &block)
|
527
520
|
BuiltIn::RaiseError.new(error, message, &block)
|
528
521
|
end
|
@@ -534,6 +527,8 @@ module RSpec
|
|
534
527
|
#
|
535
528
|
# @example
|
536
529
|
#
|
530
|
+
# expect("string").to respond_to(:length)
|
531
|
+
#
|
537
532
|
def respond_to(*names)
|
538
533
|
BuiltIn::RespondTo.new(*names)
|
539
534
|
end
|
@@ -550,9 +545,7 @@ module RSpec
|
|
550
545
|
#
|
551
546
|
# @example
|
552
547
|
#
|
553
|
-
# 5.
|
554
|
-
# n > 3
|
555
|
-
# }
|
548
|
+
# expect(5).to satisfy { |n| n > 3 }
|
556
549
|
def satisfy(&block)
|
557
550
|
BuiltIn::Satisfy.new(&block)
|
558
551
|
end
|
@@ -564,9 +557,9 @@ module RSpec
|
|
564
557
|
#
|
565
558
|
# @example
|
566
559
|
#
|
567
|
-
# "this string".
|
568
|
-
# [0, 1, 2, 3, 4].
|
569
|
-
# [0, 2, 3, 4, 4].
|
560
|
+
# expect("this string").to start_with "this s"
|
561
|
+
# expect([0, 1, 2, 3, 4]).to start_with 0
|
562
|
+
# expect([0, 2, 3, 4, 4]).to start_with 0, 1
|
570
563
|
def start_with(*expected)
|
571
564
|
BuiltIn::StartWith.new(*expected)
|
572
565
|
end
|
@@ -580,13 +573,13 @@ module RSpec
|
|
580
573
|
#
|
581
574
|
# @example
|
582
575
|
#
|
583
|
-
#
|
584
|
-
#
|
585
|
-
#
|
576
|
+
# expect { do_something_risky }.to throw_symbol
|
577
|
+
# expect { do_something_risky }.to throw_symbol(:that_was_risky)
|
578
|
+
# expect { do_something_risky }.to throw_symbol(:that_was_risky, 'culprit')
|
586
579
|
#
|
587
|
-
#
|
588
|
-
#
|
589
|
-
#
|
580
|
+
# expect { do_something_risky }.not_to throw_symbol
|
581
|
+
# expect { do_something_risky }.not_to throw_symbol(:that_was_risky)
|
582
|
+
# expect { do_something_risky }.not_to throw_symbol(:that_was_risky, 'culprit')
|
590
583
|
def throw_symbol(expected_symbol=nil, expected_arg=nil)
|
591
584
|
BuiltIn::ThrowSymbol.new(expected_symbol, expected_arg)
|
592
585
|
end
|
@@ -679,17 +672,14 @@ module RSpec
|
|
679
672
|
#
|
680
673
|
# @note This is also available using the `=~` operator with `should`,
|
681
674
|
# but `=~` is not supported with `expect`.
|
682
|
-
#
|
675
|
+
#
|
676
|
+
# @note This matcher only supports positive expectations.
|
677
|
+
# expect(..).not_to match_array(other_array) is not supported.
|
683
678
|
#
|
684
679
|
# @example
|
685
680
|
#
|
686
681
|
# expect([1,2,3]).to match_array([1,2,3])
|
687
682
|
# expect([1,2,3]).to match_array([1,3,2])
|
688
|
-
# [1,2,3].should =~ [1,2,3] # => would pass
|
689
|
-
# [1,2,3].should =~ [2,3,1] # => would pass
|
690
|
-
# [1,2,3,4].should =~ [1,2,3] # => would fail
|
691
|
-
# [1,2,2,3].should =~ [1,2,3] # => would fail
|
692
|
-
# [1,2,3].should =~ [1,2,3,4] # => would fail
|
693
683
|
def match_array(array)
|
694
684
|
BuiltIn::MatchArray.new(array)
|
695
685
|
end
|