rspec-expectations 2.14.5 → 2.99.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -33,8 +33,8 @@ module RSpec::Matchers::DSL
33
33
  m1 = example_matcher(1)
34
34
  m2 = example_matcher(2)
35
35
 
36
- expect(m1.matches?(1)).to be_true
37
- expect(m2.matches?(2)).to be_true
36
+ expect(m1.matches?(1)).to be_truthy
37
+ expect(m2.matches?(2)).to be_truthy
38
38
  end
39
39
 
40
40
  context "with an included module" do
@@ -104,13 +104,13 @@ module RSpec::Matchers::DSL
104
104
  end
105
105
 
106
106
  it "invokes the match_for_should block for #matches?" do
107
- expect(matcher.matches?(77)).to be_true
108
- expect(matcher.matches?(18)).to be_false
107
+ expect(matcher.matches?(77)).to be_truthy
108
+ expect(matcher.matches?(18)).to be_falsey
109
109
  end
110
110
 
111
111
  it "invokes the match_for_should_not block for #does_not_match?" do
112
- expect(matcher.does_not_match?(77)).to be_false
113
- expect(matcher.does_not_match?(18)).to be_true
112
+ expect(matcher.does_not_match?(77)).to be_falsey
113
+ expect(matcher.does_not_match?(18)).to be_truthy
114
114
  end
115
115
 
116
116
  it "provides a default failure message for #should_not" do
@@ -159,7 +159,7 @@ module RSpec::Matchers::DSL
159
159
  expect(actual).to eq expected
160
160
  end
161
161
  end.for_expected('value')
162
- expect(matcher.matches?('value')).to be_true
162
+ expect(matcher.matches?('value')).to be_truthy
163
163
  end
164
164
 
165
165
  it "returns false if the wrapped expectation fails" do
@@ -168,7 +168,7 @@ module RSpec::Matchers::DSL
168
168
  expect(actual).to eq expected
169
169
  end
170
170
  end.for_expected('value')
171
- expect(matcher.matches?('other value')).to be_false
171
+ expect(matcher.matches?('other value')).to be_falsey
172
172
  end
173
173
  end
174
174
 
@@ -191,11 +191,11 @@ module RSpec::Matchers::DSL
191
191
  end
192
192
 
193
193
  it "does not hide result of match block when true" do
194
- expect(@matcher.matches?(true)).to be_true
194
+ expect(@matcher.matches?(true)).to be_truthy
195
195
  end
196
196
 
197
197
  it "does not hide result of match block when false" do
198
- expect(@matcher.matches?(false)).to be_false
198
+ expect(@matcher.matches?(false)).to be_falsey
199
199
  end
200
200
 
201
201
  it "overrides the description" do
@@ -220,7 +220,7 @@ module RSpec::Matchers::DSL
220
220
  actual == 5
221
221
  end
222
222
  end.for_expected
223
- expect(matcher.matches?(5)).to be_true
223
+ expect(matcher.matches?(5)).to be_truthy
224
224
  end
225
225
 
226
226
  it "exposes arg submitted through #new to matcher block" do
@@ -229,7 +229,7 @@ module RSpec::Matchers::DSL
229
229
  actual > expected
230
230
  end
231
231
  end.for_expected(4)
232
- expect(matcher.matches?(5)).to be_true
232
+ expect(matcher.matches?(5)).to be_truthy
233
233
  end
234
234
  end
235
235
 
@@ -243,7 +243,7 @@ module RSpec::Matchers::DSL
243
243
  end
244
244
 
245
245
  it "matches" do
246
- expect(@matcher.matches?(5)).to be_true
246
+ expect(@matcher.matches?(5)).to be_truthy
247
247
  end
248
248
 
249
249
  it "describes" do
@@ -261,7 +261,7 @@ module RSpec::Matchers::DSL
261
261
  end
262
262
 
263
263
  it "matches" do
264
- expect(@matcher.matches?(5)).to be_true
264
+ expect(@matcher.matches?(5)).to be_truthy
265
265
  end
266
266
 
267
267
  it "describes" do
@@ -279,7 +279,7 @@ module RSpec::Matchers::DSL
279
279
  end
280
280
 
281
281
  it "matches" do
282
- expect(@matcher.matches?(10)).to be_true
282
+ expect(@matcher.matches?(10)).to be_truthy
283
283
  end
284
284
 
285
285
  it "describes" do
@@ -298,7 +298,7 @@ module RSpec::Matchers::DSL
298
298
  end
299
299
  end.for_expected([1,2,3])
300
300
 
301
- expect(matcher.matches?([2,3,1])).to be_true
301
+ expect(matcher.matches?([2,3,1])).to be_truthy
302
302
  end
303
303
 
304
304
  it "supports fluent interface" do
@@ -324,7 +324,131 @@ module RSpec::Matchers::DSL
324
324
  end
325
325
  end.for_expected(3)
326
326
 
327
- expect(matcher.matches?(8)).to be_true
327
+ expect(matcher.matches?(8)).to be_truthy
328
+ end
329
+
330
+ shared_examples_for "accessing a singleton helper method" do
331
+ before { allow_deprecation }
332
+
333
+ it 'can access the helper method from `match`' do
334
+ expect([2, 3]).to matcher.for_expected(5)
335
+ expect([2, 3]).not_to matcher.for_expected(4)
336
+ end
337
+
338
+ it 'prints a deprecation warning when the helper method is accessed `match`' do
339
+ expect(RSpec).to receive(:deprecate).with(/sum_of/, an_instance_of(Hash))
340
+ matcher.for_expected(5).matches?([2, 3])
341
+ end
342
+
343
+ it 'includes the call site in the deprecation warning' do
344
+ expect_deprecation_with_call_site(__FILE__, line)
345
+ matcher.for_expected(5).matches?([2, 3])
346
+ end
347
+
348
+ it 'does not print a deprecation warning if the helper method is used as a macro' do
349
+ expect(RSpec).not_to receive(:deprecate)
350
+ matcher.for_expected(:use_as_macro).matches?([2, 3])
351
+ end
352
+ end
353
+
354
+ context "when a module of helper methods is extended" do
355
+ include_examples "accessing a singleton helper method" do
356
+ let(:matcher) do
357
+ RSpec::Matchers::DSL::Matcher.new(:sum_to) do |sum|
358
+ extend Module.new {
359
+ def sum_of(x, y) x + y end
360
+ def define_match() match {} end
361
+ }
362
+
363
+ if sum == :use_as_macro
364
+ define_match
365
+ else
366
+ match { |summands| sum_of(*summands) == sum }
367
+ end
368
+ end
369
+ end
370
+ let(:line) { __LINE__ - 4 }
371
+ end
372
+ end
373
+
374
+ context "when a helper method is defined using `self.`" do
375
+ include_examples "accessing a singleton helper method" do
376
+ let(:matcher) do
377
+ RSpec::Matchers::DSL::Matcher.new(:sum_to) do |sum|
378
+ def self.sum_of(x, y) x + y end
379
+ def self.define_match() match {} end
380
+
381
+ if sum == :use_as_macro
382
+ define_match
383
+ else
384
+ match { |summands| sum_of(*summands) == sum }
385
+ end
386
+ end
387
+ end
388
+ let(:line) { __LINE__ - 4 }
389
+ end
390
+ end
391
+
392
+ shared_examples_for "accessing an instance helper method" do
393
+ before { allow_deprecation }
394
+
395
+ it 'can access the helper method from `match`' do
396
+ expect([2, 3]).to matcher.for_expected(5)
397
+ expect([2, 3]).not_to matcher.for_expected(4)
398
+ end
399
+
400
+ it 'does not print a deprecation warning when the helper method is accessed from `match`' do
401
+ expect(RSpec).not_to receive(:deprecate)
402
+ matcher.for_expected(5).matches?([2, 3])
403
+ end
404
+
405
+ it 'prints a deprecation warning if the helper method is used as a macro' do
406
+ expect(RSpec).to receive(:deprecate).with(/define_match/, an_instance_of(Hash))
407
+ matcher.for_expected(:use_as_macro).matches?([2, 3])
408
+ end
409
+
410
+ it 'includes the call site in the deprecation warning' do
411
+ expect_deprecation_with_call_site(__FILE__, line)
412
+ matcher.for_expected(:use_as_macro).matches?([2, 3])
413
+ end
414
+ end
415
+
416
+ context "when a module of helper methods is included" do
417
+ include_examples "accessing an instance helper method" do
418
+ let(:matcher) do
419
+ RSpec::Matchers::DSL::Matcher.new(:sum_to) do |sum|
420
+ include Module.new {
421
+ def sum_of(x, y) x + y end
422
+ def define_match() match {} end
423
+ }
424
+
425
+ if sum == :use_as_macro
426
+ define_match
427
+ else
428
+ match { |summands| sum_of(*summands) == sum }
429
+ end
430
+ end
431
+ end
432
+ let(:line) { __LINE__ - 6 }
433
+ end
434
+ end
435
+
436
+ context "when a helper method is defined using `def foo`" do
437
+ include_examples "accessing an instance helper method" do
438
+ let(:matcher) do
439
+ RSpec::Matchers::DSL::Matcher.new(:sum_to) do |sum|
440
+ def sum_of(x, y) x + y end
441
+ def define_match() match {} end
442
+
443
+ if sum == :use_as_macro
444
+ define_match
445
+ else
446
+ match { |summands| sum_of(*summands) == sum }
447
+ end
448
+ end
449
+ end
450
+ let(:line) { __LINE__ - 6 }
451
+ end
328
452
  end
329
453
 
330
454
  context 'when multiple instances of the same matcher are used in the same example' do
@@ -377,7 +501,7 @@ module RSpec::Matchers::DSL
377
501
  let(:matcher) do
378
502
  m = mod
379
503
  RSpec::Matchers::DSL::Matcher.new :equal do |expected|
380
- extend m
504
+ include m
381
505
  match_unless_raises UnexpectedError do
382
506
  assert_equal expected, actual
383
507
  end
@@ -386,13 +510,13 @@ module RSpec::Matchers::DSL
386
510
 
387
511
  context "with passing assertion" do
388
512
  it "passes" do
389
- expect(matcher.matches?(4)).to be_true
513
+ expect(matcher.matches?(4)).to be_truthy
390
514
  end
391
515
  end
392
516
 
393
517
  context "with failing assertion" do
394
518
  it "fails" do
395
- expect(matcher.matches?(5)).to be_false
519
+ expect(matcher.matches?(5)).to be_falsey
396
520
  end
397
521
 
398
522
  it "provides the raised exception" do
@@ -428,8 +552,8 @@ module RSpec::Matchers::DSL
428
552
  match { |actual| actual == @expected_value }
429
553
  end.for_expected
430
554
 
431
- expect(matcher.expecting('value').matches?('value')).to be_true
432
- expect(matcher.expecting('value').matches?('other value')).to be_false
555
+ expect(matcher.expecting('value').matches?('value')).to be_truthy
556
+ expect(matcher.expecting('value').matches?('other value')).to be_falsey
433
557
  end
434
558
 
435
559
  it "prevents name collisions on chainable methods from different matchers" do
@@ -445,7 +569,7 @@ module RSpec::Matchers::DSL
445
569
  "method defined in the example"
446
570
  end
447
571
 
448
- it "can access methods in the running example" do
572
+ it "can access methods in the running example" do |example|
449
573
  RSpec::Matchers.define(:__access_running_example) do
450
574
  match do |actual|
451
575
  a_method_in_the_example == "method defined in the example"
@@ -454,7 +578,7 @@ module RSpec::Matchers::DSL
454
578
  expect(example).to __access_running_example
455
579
  end
456
580
 
457
- it "raises NoMethodError for methods not in the running_example" do
581
+ it "raises NoMethodError for methods not in the running_example" do |example|
458
582
  RSpec::Matchers.define(:__raise_no_method_error) do
459
583
  match do |actual|
460
584
  a_method_not_in_the_example == "method defined in the example"
@@ -24,7 +24,7 @@ describe "operator matchers", :uses_should do
24
24
 
25
25
  it "returns true on success" do
26
26
  subject = "apple"
27
- (subject.should == "apple").should be_true
27
+ (subject.should == "apple").should be_truthy
28
28
  end
29
29
 
30
30
  it "fails when target.==(actual) returns false" do
@@ -44,7 +44,7 @@ describe "operator matchers", :uses_should do
44
44
  obj = Object.new
45
45
 
46
46
  myobj = MethodMissingObject.new(obj)
47
- myobj.should == obj
47
+ (myobj.should == obj).nil? # just to avoid `useless use of == in void context` warning
48
48
  myobj.should_not == Object.new
49
49
  end
50
50
  end
@@ -84,7 +84,7 @@ describe "operator matchers", :uses_should do
84
84
 
85
85
  it "returns true on success" do
86
86
  subject = "apple"
87
- (subject.should_not == "orange").should be_false
87
+ (subject.should_not == "orange").should be_falsey
88
88
  end
89
89
 
90
90
  it "fails when target.==(actual) returns false" do
@@ -61,7 +61,7 @@ describe "expect { ... }.to raise_error {|err| ... }" do
61
61
  expect { non_existent_method }.to raise_error {|e|
62
62
  ran = true
63
63
  }
64
- expect(ran).to be_true
64
+ expect(ran).to be_truthy
65
65
  end
66
66
 
67
67
  it "passes the error to the block" do
@@ -409,8 +409,8 @@ describe "expect { ... }.to raise_error(NamedError, error_message) { |err| ... }
409
409
  }
410
410
  }.to fail_with(/expected: 4/m)
411
411
 
412
- expect(ran).to be_true
413
- expect(passed).to be_false
412
+ expect(ran).to be_truthy
413
+ expect(passed).to be_falsey
414
414
  end
415
415
 
416
416
  it "does NOT yield exception if no error was thrown" do
@@ -11,13 +11,13 @@ module RSpec::Matchers::BuiltIn
11
11
  before(:each) { @matcher = throw_symbol }
12
12
 
13
13
  it "matches if any Symbol is thrown" do
14
- expect(@matcher.matches?(lambda{ throw :sym })).to be_true
14
+ expect(@matcher.matches?(lambda{ throw :sym })).to be_truthy
15
15
  end
16
16
  it "matches if any Symbol is thrown with an arg" do
17
- expect(@matcher.matches?(lambda{ throw :sym, "argument" })).to be_true
17
+ expect(@matcher.matches?(lambda{ throw :sym, "argument" })).to be_truthy
18
18
  end
19
19
  it "does not match if no Symbol is thrown" do
20
- expect(@matcher.matches?(lambda{ })).to be_false
20
+ expect(@matcher.matches?(lambda{ })).to be_falsey
21
21
  end
22
22
  it "provides a failure message" do
23
23
  @matcher.matches?(lambda{})
@@ -33,16 +33,16 @@ module RSpec::Matchers::BuiltIn
33
33
  before(:each) { @matcher = throw_symbol(:sym) }
34
34
 
35
35
  it "matches if correct Symbol is thrown" do
36
- expect(@matcher.matches?(lambda{ throw :sym })).to be_true
36
+ expect(@matcher.matches?(lambda{ throw :sym })).to be_truthy
37
37
  end
38
38
  it "matches if correct Symbol is thrown with an arg" do
39
- expect(@matcher.matches?(lambda{ throw :sym, "argument" })).to be_true
39
+ expect(@matcher.matches?(lambda{ throw :sym, "argument" })).to be_truthy
40
40
  end
41
41
  it "does not match if no Symbol is thrown" do
42
- expect(@matcher.matches?(lambda{ })).to be_false
42
+ expect(@matcher.matches?(lambda{ })).to be_falsey
43
43
  end
44
44
  it "does not match if correct Symbol is thrown" do
45
- expect(@matcher.matches?(lambda{ throw :other_sym })).to be_false
45
+ expect(@matcher.matches?(lambda{ throw :other_sym })).to be_falsey
46
46
  end
47
47
  it "provides a failure message when no Symbol is thrown" do
48
48
  @matcher.matches?(lambda{})
@@ -58,7 +58,7 @@ module RSpec::Matchers::BuiltIn
58
58
  end
59
59
  it "only matches NameErrors raised by uncaught throws" do
60
60
  expect {
61
- expect(@matcher.matches?(lambda{ sym })).to be_false
61
+ expect(@matcher.matches?(lambda{ sym })).to be_falsey
62
62
  }.to raise_error(NameError)
63
63
  end
64
64
  end
@@ -67,19 +67,19 @@ module RSpec::Matchers::BuiltIn
67
67
  before(:each) { @matcher = throw_symbol(:sym, "a") }
68
68
 
69
69
  it "matches if correct Symbol and args are thrown" do
70
- expect(@matcher.matches?(lambda{ throw :sym, "a" })).to be_true
70
+ expect(@matcher.matches?(lambda{ throw :sym, "a" })).to be_truthy
71
71
  end
72
72
  it "does not match if nothing is thrown" do
73
- expect(@matcher.matches?(lambda{ })).to be_false
73
+ expect(@matcher.matches?(lambda{ })).to be_falsey
74
74
  end
75
75
  it "does not match if other Symbol is thrown" do
76
- expect(@matcher.matches?(lambda{ throw :other_sym, "a" })).to be_false
76
+ expect(@matcher.matches?(lambda{ throw :other_sym, "a" })).to be_falsey
77
77
  end
78
78
  it "does not match if no arg is thrown" do
79
- expect(@matcher.matches?(lambda{ throw :sym })).to be_false
79
+ expect(@matcher.matches?(lambda{ throw :sym })).to be_falsey
80
80
  end
81
81
  it "does not match if wrong arg is thrown" do
82
- expect(@matcher.matches?(lambda{ throw :sym, "b" })).to be_false
82
+ expect(@matcher.matches?(lambda{ throw :sym, "b" })).to be_falsey
83
83
  end
84
84
  it "provides a failure message when no Symbol is thrown" do
85
85
  @matcher.matches?(lambda{})
@@ -103,7 +103,7 @@ module RSpec::Matchers::BuiltIn
103
103
  end
104
104
  it "only matches NameErrors raised by uncaught throws" do
105
105
  expect {
106
- expect(@matcher.matches?(lambda{ sym })).to be_false
106
+ expect(@matcher.matches?(lambda{ sym })).to be_falsey
107
107
  }.to raise_error(NameError)
108
108
  end
109
109
  it "raises other errors" do
@@ -1,5 +1,25 @@
1
1
  Dir['./spec/support/**/*'].each {|f| require f}
2
2
 
3
+ module DeprecationHelpers
4
+ def allow_deprecation
5
+ allow(RSpec.configuration.reporter).to receive(:deprecation)
6
+ end
7
+
8
+ def expect_deprecation_with_call_site(file, line)
9
+ expect(RSpec.configuration.reporter).to receive(:deprecation) do |options|
10
+ matcher = include([file, line].join(':'))
11
+ unless matcher.matches?(options[:call_site])
12
+ # RSpec::Expectations::ExpectationNotMetError is rescued in the `match` block
13
+ # of a custom matcher and returned as `false` from `matches?`. This would
14
+ # prevent an expectation failure here from surfacing in the test suite if
15
+ # it's triggered from within a `match` block, so we need to raise
16
+ # a different error class instead.
17
+ raise matcher.failure_message_for_should
18
+ end
19
+ end
20
+ end
21
+ end
22
+
3
23
  RSpec::configure do |config|
4
24
  config.treat_symbols_as_metadata_keys_with_true_values = true
5
25
  config.color_enabled = true
@@ -11,6 +31,9 @@ RSpec::configure do |config|
11
31
  $default_expectation_syntax = expectations.syntax
12
32
  expectations.syntax = :expect
13
33
  end
34
+
35
+ config.include SpecHelperMethods
36
+ config.include DeprecationHelpers
14
37
  end
15
38
 
16
39
  shared_context "with #should enabled", :uses_should do