minitest 5.14.4 → 5.16.1

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,180 @@ 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
274
295
 
275
296
  assert_mock mock
276
297
  end
277
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)
362
+
363
+ assert_mock mock
364
+ end
365
+
366
+ def with_kwargs_env
367
+ ENV["MT_KWARGS_HAC\K"] = "1"
368
+
369
+ yield
370
+ ensure
371
+ ENV.delete "MT_KWARGS_HAC\K"
372
+ end
373
+
374
+ def test_mock_block_is_passed_keyword_args__args__old_style_bad
375
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
376
+ mock = Minitest::Mock.new
377
+ mock.expect :foo, nil, [{k1: arg1, k2: arg2, k3: arg3}]
378
+
379
+ e = assert_raises ArgumentError do
380
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
381
+ end
382
+
383
+ assert_equal "mocked method :foo expects 1 arguments, got []", e.message
384
+ end
385
+
386
+ def test_mock_block_is_passed_keyword_args__args__old_style_env
387
+ with_kwargs_env do
388
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
389
+ mock = Minitest::Mock.new
390
+ mock.expect :foo, nil, [{k1: arg1, k2: arg2, k3: arg3}]
391
+
392
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
393
+
394
+ assert_mock mock
395
+ end
396
+ end
397
+
398
+ def test_mock_block_is_passed_keyword_args__args__old_style_both
399
+ with_kwargs_env do
400
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
401
+ mock = Minitest::Mock.new
402
+
403
+ assert_output nil, /Using MT_KWARGS_HAC. yet passing kwargs/ do
404
+ mock.expect :foo, nil, [{}], k1: arg1, k2: arg2, k3: arg3
405
+ end
406
+
407
+ mock.foo({}, k1: arg1, k2: arg2, k3: arg3)
408
+
409
+ assert_mock mock
410
+ end
411
+ end
412
+
413
+ def test_mock_block_is_passed_keyword_args__args_bad_missing
414
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
415
+ mock = Minitest::Mock.new
416
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
417
+
418
+ e = assert_raises ArgumentError do
419
+ mock.foo(k1: arg1, k2: arg2)
420
+ end
421
+
422
+ assert_equal "mocked method :foo expects 3 keyword arguments, got %p" % {k1: arg1, k2: arg2}, e.message
423
+ end
424
+
425
+ def test_mock_block_is_passed_keyword_args__args_bad_extra
426
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
427
+ mock = Minitest::Mock.new
428
+ mock.expect :foo, nil, k1: arg1, k2: arg2
429
+
430
+ e = assert_raises ArgumentError do
431
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
432
+ end
433
+
434
+ assert_equal "mocked method :foo expects 2 keyword arguments, got %p" % {k1: arg1, k2: arg2, k3: arg3}, e.message
435
+ end
436
+
437
+ def test_mock_block_is_passed_keyword_args__args_bad_key
438
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
439
+ mock = Minitest::Mock.new
440
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
441
+
442
+ e = assert_raises MockExpectationError do
443
+ mock.foo(k1: arg1, k2: arg2, BAD: arg3)
444
+ end
445
+
446
+ assert_includes e.message, "unexpected keywords [:k1, :k2, :k3]"
447
+ assert_includes e.message, "vs [:k1, :k2, :BAD]"
448
+ end
449
+
450
+ def test_mock_block_is_passed_keyword_args__args_bad_val
451
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
452
+ mock = Minitest::Mock.new
453
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
454
+
455
+ e = assert_raises MockExpectationError do
456
+ mock.foo(k1: arg1, k2: :BAD!, k3: arg3)
457
+ end
458
+
459
+ assert_match(/unexpected keyword arguments.* vs .*:k2=>:BAD!/, e.message)
460
+ end
461
+
278
462
  def test_mock_block_is_passed_function_block
279
463
  mock = Minitest::Mock.new
280
464
  block = proc { "bar" }
@@ -286,6 +470,13 @@ class TestMinitestMock < Minitest::Test
286
470
  assert_mock mock
287
471
  end
288
472
 
473
+ def test_mock_forward_keyword_arguments
474
+ mock = Minitest::Mock.new
475
+ mock.expect(:foo, nil) { |bar:| bar == 'bar' }
476
+ mock.foo(bar: 'bar')
477
+ assert_mock mock
478
+ end
479
+
289
480
  def test_verify_fails_when_mock_block_returns_false
290
481
  mock = Minitest::Mock.new
291
482
  mock.expect :foo, nil do
@@ -293,12 +484,12 @@ class TestMinitestMock < Minitest::Test
293
484
  end
294
485
 
295
486
  e = assert_raises(MockExpectationError) { mock.foo }
296
- exp = "mocked method :foo failed block w/ []"
487
+ exp = "mocked method :foo failed block w/ [] {}"
297
488
 
298
489
  assert_equal exp, e.message
299
490
  end
300
491
 
301
- def test_mock_block_throws_if_args_passed
492
+ def test_mock_block_raises_if_args_passed
302
493
  mock = Minitest::Mock.new
303
494
 
304
495
  e = assert_raises(ArgumentError) do
@@ -309,7 +500,21 @@ class TestMinitestMock < Minitest::Test
309
500
 
310
501
  exp = "args ignored when block given"
311
502
 
312
- assert_equal exp, e.message
503
+ assert_match exp, e.message
504
+ end
505
+
506
+ def test_mock_block_raises_if_kwargs_passed
507
+ mock = Minitest::Mock.new
508
+
509
+ e = assert_raises(ArgumentError) do
510
+ mock.expect :foo, nil, kwargs:1 do
511
+ true
512
+ end
513
+ end
514
+
515
+ exp = "kwargs ignored when block given"
516
+
517
+ assert_match exp, e.message
313
518
  end
314
519
 
315
520
  def test_mock_returns_retval_when_called_with_block
@@ -499,7 +704,8 @@ class TestMinitestStub < Minitest::Test
499
704
  end
500
705
  end
501
706
 
502
- exp = /undefined method `nope_nope_nope' for( class)? `#{self.class}::Time'/
707
+ exp = jruby? ? /Undefined method nope_nope_nope for '#{self.class}::Time'/ :
708
+ /undefined method `nope_nope_nope' for( class)? `#{self.class}::Time'/
503
709
  assert_match exp, e.message
504
710
  end
505
711
 
@@ -518,6 +724,19 @@ class TestMinitestStub < Minitest::Test
518
724
  @tc.assert_equal true, rs
519
725
  end
520
726
 
727
+ def test_mock_with_yield_kwargs
728
+ mock = Minitest::Mock.new
729
+ rs = nil
730
+
731
+ File.stub :open, true, mock, kw:42 do
732
+ File.open "foo.txt", "r" do |f, kw:|
733
+ rs = kw
734
+ end
735
+ end
736
+
737
+ @tc.assert_equal 42, rs
738
+ end
739
+
521
740
  alias test_stub_value__old test_stub_value # TODO: remove/rename
522
741
 
523
742
  ## Permutation Sets:
@@ -822,7 +1041,7 @@ class TestMinitestStub < Minitest::Test
822
1041
  end
823
1042
  end
824
1043
  exp = "undefined method `write' for nil:NilClass"
825
- assert_equal exp, e.message
1044
+ assert_match exp, e.message
826
1045
  end
827
1046
 
828
1047
  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