minitest 5.14.3 → 5.16.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -51,7 +51,7 @@ class TestMinitestMock < Minitest::Test
51
51
  @mock.sum
52
52
  end
53
53
 
54
- assert_equal "mocked method :sum expects 2 arguments, got 0", e.message
54
+ assert_equal "mocked method :sum expects 2 arguments, got []", e.message
55
55
  end
56
56
 
57
57
  def test_return_mock_does_not_raise
@@ -133,7 +133,7 @@ class TestMinitestMock < Minitest::Test
133
133
  @mock.expect :blah, 3, false
134
134
  end
135
135
 
136
- assert_equal "args must be an array", e.message
136
+ assert_match "args must be an array", e.message
137
137
  end
138
138
 
139
139
  def test_respond_appropriately
@@ -150,7 +150,7 @@ class TestMinitestMock < Minitest::Test
150
150
 
151
151
  expected = "unmocked method :bar, expected one of [:foo, :meaning_of_life]"
152
152
 
153
- assert_equal expected, e.message
153
+ assert_match expected, e.message
154
154
  end
155
155
 
156
156
  def test_assign_per_mock_return_values
@@ -210,7 +210,7 @@ class TestMinitestMock < Minitest::Test
210
210
  mock.a
211
211
  end
212
212
 
213
- assert_equal "No more expects available for :a: []", e.message
213
+ assert_equal "No more expects available for :a: [] {}", e.message
214
214
  end
215
215
 
216
216
  def test_same_method_expects_are_verified_when_all_called
@@ -252,6 +252,21 @@ class TestMinitestMock < Minitest::Test
252
252
  assert_equal exp, e.message
253
253
  end
254
254
 
255
+ def test_handles_kwargs_in_error_message
256
+ mock = Minitest::Mock.new
257
+
258
+ mock.expect :foo, nil, [], kw: true
259
+ mock.expect :foo, nil, [], kw: false
260
+
261
+ mock.foo kw: true
262
+
263
+ e = assert_raises(MockExpectationError) { mock.verify }
264
+
265
+ exp = "expected foo(:kw=>false) => nil, got [foo(:kw=>true) => nil]"
266
+
267
+ assert_equal exp, e.message
268
+ end
269
+
255
270
  def test_verify_passes_when_mock_block_returns_true
256
271
  mock = Minitest::Mock.new
257
272
  mock.expect :foo, nil do
@@ -270,11 +285,133 @@ class TestMinitestMock < Minitest::Test
270
285
  a1 == arg1 && a2 == arg2 && a3 == arg3
271
286
  end
272
287
 
273
- mock.foo arg1, arg2, arg3
288
+ assert_silent do
289
+ if RUBY_VERSION > "3" then
290
+ mock.foo arg1, arg2, arg3
291
+ else
292
+ mock.foo arg1, arg2, **arg3 # oddity just for ruby 2.7
293
+ end
294
+ end
295
+
296
+ assert_mock mock
297
+ end
298
+
299
+ def test_mock_block_is_passed_keyword_args__block
300
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
301
+ mock = Minitest::Mock.new
302
+ mock.expect :foo, nil do |k1:, k2:, k3:|
303
+ k1 == arg1 && k2 == arg2 && k3 == arg3
304
+ end
305
+
306
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
307
+
308
+ assert_mock mock
309
+ end
310
+
311
+ def test_mock_block_is_passed_keyword_args__block_bad_missing
312
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
313
+ mock = Minitest::Mock.new
314
+ mock.expect :foo, nil do |k1:, k2:, k3:|
315
+ k1 == arg1 && k2 == arg2 && k3 == arg3
316
+ end
317
+
318
+ e = assert_raises ArgumentError do
319
+ mock.foo(k1: arg1, k2: arg2)
320
+ end
321
+
322
+ # basically testing ruby ... need ? for ruby < 2.7 :(
323
+ assert_match(/missing keyword: :?k3/, e.message)
324
+ end
325
+
326
+ def test_mock_block_is_passed_keyword_args__block_bad_extra
327
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
328
+ mock = Minitest::Mock.new
329
+ mock.expect :foo, nil do |k1:, k2:|
330
+ k1 == arg1 && k2 == arg2 && k3 == arg3
331
+ end
332
+
333
+ e = assert_raises ArgumentError do
334
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
335
+ end
336
+
337
+ # basically testing ruby ... need ? for ruby < 2.7 :(
338
+ assert_match(/unknown keyword: :?k3/, e.message)
339
+ end
340
+
341
+ def test_mock_block_is_passed_keyword_args__block_bad_value
342
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
343
+ mock = Minitest::Mock.new
344
+ mock.expect :foo, nil do |k1:, k2:, k3:|
345
+ k1 == arg1 && k2 == arg2 && k3 == arg3
346
+ end
347
+
348
+ e = assert_raises MockExpectationError do
349
+ mock.foo(k1: arg1, k2: arg2, k3: :BAD!)
350
+ end
351
+
352
+ exp = "mocked method :foo failed block w/ [] {:k1=>:bar, :k2=>[1, 2, 3], :k3=>:BAD!}"
353
+ assert_equal exp, e.message
354
+ end
355
+
356
+ def test_mock_block_is_passed_keyword_args__args
357
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
358
+ mock = Minitest::Mock.new
359
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
360
+
361
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
274
362
 
275
363
  assert_mock mock
276
364
  end
277
365
 
366
+ def test_mock_block_is_passed_keyword_args__args_bad_missing
367
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
368
+ mock = Minitest::Mock.new
369
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
370
+
371
+ e = assert_raises ArgumentError do
372
+ mock.foo(k1: arg1, k2: arg2)
373
+ end
374
+
375
+ assert_equal "mocked method :foo expects 3 keyword arguments, got %p" % {k1: arg1, k2: arg2}, e.message
376
+ end
377
+
378
+ def test_mock_block_is_passed_keyword_args__args_bad_extra
379
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
380
+ mock = Minitest::Mock.new
381
+ mock.expect :foo, nil, k1: arg1, k2: arg2
382
+
383
+ e = assert_raises ArgumentError do
384
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
385
+ end
386
+
387
+ assert_equal "mocked method :foo expects 2 keyword arguments, got %p" % {k1: arg1, k2: arg2, k3: arg3}, e.message
388
+ end
389
+
390
+ def test_mock_block_is_passed_keyword_args__args_bad_key
391
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
392
+ mock = Minitest::Mock.new
393
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
394
+
395
+ e = assert_raises MockExpectationError do
396
+ mock.foo(k1: arg1, k2: arg2, BAD: arg3)
397
+ end
398
+
399
+ assert_includes e.message, "unexpected keywords [:k1, :k2, :k3]"
400
+ assert_includes e.message, "vs [:k1, :k2, :BAD]"
401
+ end
402
+
403
+ def test_mock_block_is_passed_keyword_args__args_bad_val
404
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
405
+ mock = Minitest::Mock.new
406
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
407
+
408
+ e = assert_raises MockExpectationError do
409
+ mock.foo(k1: arg1, k2: :BAD!, k3: arg3)
410
+ end
411
+
412
+ assert_match(/unexpected keyword arguments.* vs .*:k2=>:BAD!/, e.message)
413
+ end
414
+
278
415
  def test_mock_block_is_passed_function_block
279
416
  mock = Minitest::Mock.new
280
417
  block = proc { "bar" }
@@ -286,6 +423,13 @@ class TestMinitestMock < Minitest::Test
286
423
  assert_mock mock
287
424
  end
288
425
 
426
+ def test_mock_forward_keyword_arguments
427
+ mock = Minitest::Mock.new
428
+ mock.expect(:foo, nil) { |bar:| bar == 'bar' }
429
+ mock.foo(bar: 'bar')
430
+ assert_mock mock
431
+ end
432
+
289
433
  def test_verify_fails_when_mock_block_returns_false
290
434
  mock = Minitest::Mock.new
291
435
  mock.expect :foo, nil do
@@ -293,7 +437,7 @@ class TestMinitestMock < Minitest::Test
293
437
  end
294
438
 
295
439
  e = assert_raises(MockExpectationError) { mock.foo }
296
- exp = "mocked method :foo failed block w/ []"
440
+ exp = "mocked method :foo failed block w/ [] {}"
297
441
 
298
442
  assert_equal exp, e.message
299
443
  end
@@ -309,7 +453,7 @@ class TestMinitestMock < Minitest::Test
309
453
 
310
454
  exp = "args ignored when block given"
311
455
 
312
- assert_equal exp, e.message
456
+ assert_match exp, e.message
313
457
  end
314
458
 
315
459
  def test_mock_returns_retval_when_called_with_block
@@ -499,7 +643,8 @@ class TestMinitestStub < Minitest::Test
499
643
  end
500
644
  end
501
645
 
502
- exp = /undefined method `nope_nope_nope' for( class)? `#{self.class}::Time'/
646
+ exp = jruby? ? /Undefined method nope_nope_nope for '#{self.class}::Time'/ :
647
+ /undefined method `nope_nope_nope' for( class)? `#{self.class}::Time'/
503
648
  assert_match exp, e.message
504
649
  end
505
650
 
@@ -518,6 +663,19 @@ class TestMinitestStub < Minitest::Test
518
663
  @tc.assert_equal true, rs
519
664
  end
520
665
 
666
+ def test_mock_with_yield_kwargs
667
+ mock = Minitest::Mock.new
668
+ rs = nil
669
+
670
+ File.stub :open, true, mock, kw:42 do
671
+ File.open "foo.txt", "r" do |f, kw:|
672
+ rs = kw
673
+ end
674
+ end
675
+
676
+ @tc.assert_equal 42, rs
677
+ end
678
+
521
679
  alias test_stub_value__old test_stub_value # TODO: remove/rename
522
680
 
523
681
  ## Permutation Sets:
@@ -591,6 +749,18 @@ class TestMinitestStub < Minitest::Test
591
749
  end
592
750
  end
593
751
 
752
+ class Keywords
753
+ def self.args req, kw1:, kw2:24
754
+ [req, kw1, kw2]
755
+ end
756
+ end
757
+
758
+ def test_stub_callable_keyword_args
759
+ Keywords.stub :args, ->(*args, **kws) { [args, kws] } do
760
+ @tc.assert_equal [["woot"], { kw1: 42 }], Keywords.args("woot", kw1: 42)
761
+ end
762
+ end
763
+
594
764
  def test_stub_callable_block_5 # from tenderlove
595
765
  @assertion_count += 1
596
766
  Foo.stub5 :blocking, Bar.new do
@@ -810,7 +980,7 @@ class TestMinitestStub < Minitest::Test
810
980
  end
811
981
  end
812
982
  exp = "undefined method `write' for nil:NilClass"
813
- assert_equal exp, e.message
983
+ assert_match exp, e.message
814
984
  end
815
985
 
816
986
  def test_stub_value_block_args_6
@@ -1,5 +1,6 @@
1
1
  require "minitest/autorun"
2
2
  require "minitest/metametameta"
3
+ require "forwardable"
3
4
 
4
5
  class Runnable
5
6
  def woot
@@ -12,30 +13,24 @@ class TestMinitestReporter < MetaMetaMetaTestCase
12
13
  attr_accessor :r, :io
13
14
 
14
15
  def new_composite_reporter
16
+ # Ruby bug in older versions of 2.2 & 2.3 on all platforms
17
+ # Latest Windows builds were 2.2.6 and 2.3.3. Latest Ruby releases were
18
+ # 2.2.10 and 2.3.8.
19
+ skip if windows? && RUBY_VERSION < '2.4'
15
20
  reporter = Minitest::CompositeReporter.new
16
21
  reporter << Minitest::SummaryReporter.new(self.io)
17
22
  reporter << Minitest::ProgressReporter.new(self.io)
18
23
 
19
- def reporter.first
20
- reporters.first
21
- end
22
-
23
- def reporter.results
24
- first.results
25
- end
26
-
27
- def reporter.count
28
- first.count
29
- end
30
-
31
- def reporter.assertions
32
- first.assertions
33
- end
24
+ # eg reporter.results -> reporters.first.results
25
+ reporter.extend Forwardable
26
+ reporter.delegate :first => :reporters
27
+ reporter.delegate %i[results count assertions options to_s] => :first
34
28
 
35
29
  reporter
36
30
  end
37
31
 
38
32
  def setup
33
+ super
39
34
  self.io = StringIO.new("")
40
35
  self.r = new_composite_reporter
41
36
  end
@@ -86,7 +81,25 @@ class TestMinitestReporter < MetaMetaMetaTestCase
86
81
  def test_to_s
87
82
  r.record passing_test
88
83
  r.record fail_test
89
- assert_match "woot", r.first.to_s
84
+ assert_match "woot", r.to_s
85
+ end
86
+
87
+ def test_options_skip_F
88
+ r.options[:skip] = "F"
89
+
90
+ r.record passing_test
91
+ r.record fail_test
92
+
93
+ refute_match "woot", r.to_s
94
+ end
95
+
96
+ def test_options_skip_E
97
+ r.options[:skip] = "E"
98
+
99
+ r.record passing_test
100
+ r.record error_test
101
+
102
+ refute_match "RuntimeError: no", r.to_s
90
103
  end
91
104
 
92
105
  def test_passed_eh_empty
@@ -126,7 +139,7 @@ class TestMinitestReporter < MetaMetaMetaTestCase
126
139
  end
127
140
 
128
141
  def test_passed_eh_skipped_verbose
129
- r.first.options[:verbose] = true
142
+ r.options[:verbose] = true
130
143
 
131
144
  r.start
132
145
  r.results << skip_test
@@ -505,7 +505,7 @@ describe Minitest::Spec do
505
505
  it "needs to verify regexp matches" do
506
506
  @assertion_count += 3 # must_match is 2 assertions
507
507
 
508
- assert_success _("blah").must_match(/\w+/)
508
+ assert_kind_of MatchData, _("blah").must_match(/\w+/)
509
509
 
510
510
  assert_triggered "Expected /\\d+/ to match \"blah\"." do
511
511
  _("blah").must_match(/\d+/)
@@ -590,9 +590,10 @@ describe Minitest::Spec do
590
590
  end
591
591
 
592
592
  it "needs to verify throw" do
593
- @assertion_count += 2 # 2 extra tests
593
+ @assertion_count += 4 # 2 extra tests
594
594
 
595
- assert_success expect { throw :blah }.must_throw(:blah)
595
+ assert_nil expect { throw :blah }.must_throw(:blah)
596
+ assert_equal 42, expect { throw :blah, 42 }.must_throw(:blah)
596
597
 
597
598
  assert_triggered "Expected :blah to have been thrown." do
598
599
  expect {}.must_throw :blah
@@ -743,6 +744,10 @@ describe Minitest::Spec, :subject do
743
744
  end
744
745
 
745
746
  class TestMetaStatic < Minitest::Test
747
+ def assert_method_count expected, klass
748
+ assert_equal expected, klass.public_instance_methods.grep(/^test_/).count
749
+ end
750
+
746
751
  def test_children
747
752
  Minitest::Spec.children.clear # prevents parallel run
748
753
 
@@ -776,8 +781,8 @@ class TestMetaStatic < Minitest::Test
776
781
  end
777
782
  end
778
783
 
779
- assert_equal 1, outer.public_instance_methods.grep(/^test_/).count
780
- assert_equal 1, inner.public_instance_methods.grep(/^test_/).count
784
+ assert_method_count 1, outer
785
+ assert_method_count 1, inner
781
786
  end
782
787
 
783
788
  def test_it_wont_add_test_methods_to_children
@@ -791,14 +796,18 @@ class TestMetaStatic < Minitest::Test
791
796
  end
792
797
  end
793
798
 
794
- assert_equal 1, outer.public_instance_methods.grep(/^test_/).count
795
- assert_equal 0, inner.public_instance_methods.grep(/^test_/).count
799
+ assert_method_count 1, outer
800
+ assert_method_count 0, inner
796
801
  end
797
802
  end
798
803
 
799
804
  class TestMeta < MetaMetaMetaTestCase
800
805
  # do not call parallelize_me! here because specs use register_spec_type globally
801
806
 
807
+ def assert_defined_methods expected, klass
808
+ assert_equal expected, klass.instance_methods(false).sort.map(&:to_s)
809
+ end
810
+
802
811
  def util_structure
803
812
  y = z = nil
804
813
  before_list = []
@@ -871,7 +880,7 @@ class TestMeta < MetaMetaMetaTestCase
871
880
  end
872
881
  end
873
882
 
874
- test_name = spec_class.instance_methods.sort.grep(/test/).first
883
+ test_name = spec_class.instance_methods.sort.grep(/test_/).first
875
884
 
876
885
  spec = spec_class.new test_name
877
886
 
@@ -920,9 +929,9 @@ class TestMeta < MetaMetaMetaTestCase
920
929
  inner_methods2 = inner_methods1 +
921
930
  %w[test_0002_anonymous test_0003_anonymous]
922
931
 
923
- assert_equal top_methods, x.instance_methods(false).sort.map(&:to_s)
924
- assert_equal inner_methods1, y.instance_methods(false).sort.map(&:to_s)
925
- assert_equal inner_methods2, z.instance_methods(false).sort.map(&:to_s)
932
+ assert_defined_methods top_methods, x
933
+ assert_defined_methods inner_methods1, y
934
+ assert_defined_methods inner_methods2, z
926
935
  end
927
936
 
928
937
  def test_structure_postfix_it
@@ -939,8 +948,8 @@ class TestMeta < MetaMetaMetaTestCase
939
948
  it "inner-it" do end
940
949
  end
941
950
 
942
- assert_equal %w[test_0001_inner-it], y.instance_methods(false).map(&:to_s)
943
- assert_equal %w[test_0001_inner-it], z.instance_methods(false).map(&:to_s)
951
+ assert_defined_methods %w[test_0001_inner-it], y
952
+ assert_defined_methods %w[test_0001_inner-it], z
944
953
  end
945
954
 
946
955
  def test_setup_teardown_behavior
@@ -971,9 +980,9 @@ class TestMeta < MetaMetaMetaTestCase
971
980
  ].sort
972
981
 
973
982
  assert_equal test_methods, [x1, x2]
974
- assert_equal test_methods, x.instance_methods.grep(/^test/).map(&:to_s).sort
975
- assert_equal [], y.instance_methods.grep(/^test/)
976
- assert_equal [], z.instance_methods.grep(/^test/)
983
+ assert_defined_methods test_methods, x
984
+ assert_defined_methods [], y
985
+ assert_defined_methods [], z
977
986
  end
978
987
 
979
988
  def test_structure_subclasses