minitest 5.15.0 → 5.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,14 @@
1
1
  require "minitest/autorun"
2
2
 
3
- class TestMinitestMock < Minitest::Test
4
- parallelize_me!
3
+ def with_kwargs_env
4
+ ENV["MT_KWARGS_HAC\K"] = "1"
5
+
6
+ yield
7
+ ensure
8
+ ENV.delete "MT_KWARGS_HAC\K"
9
+ end
5
10
 
11
+ class TestMinitestMock < Minitest::Test
6
12
  def setup
7
13
  @mock = Minitest::Mock.new.expect(:foo, nil)
8
14
  @mock.expect(:meaning_of_life, 42)
@@ -51,7 +57,7 @@ class TestMinitestMock < Minitest::Test
51
57
  @mock.sum
52
58
  end
53
59
 
54
- assert_equal "mocked method :sum expects 2 arguments, got 0", e.message
60
+ assert_equal "mocked method :sum expects 2 arguments, got []", e.message
55
61
  end
56
62
 
57
63
  def test_return_mock_does_not_raise
@@ -210,7 +216,7 @@ class TestMinitestMock < Minitest::Test
210
216
  mock.a
211
217
  end
212
218
 
213
- assert_equal "No more expects available for :a: []", e.message
219
+ assert_equal "No more expects available for :a: [] {}", e.message
214
220
  end
215
221
 
216
222
  def test_same_method_expects_are_verified_when_all_called
@@ -252,6 +258,30 @@ class TestMinitestMock < Minitest::Test
252
258
  assert_equal exp, e.message
253
259
  end
254
260
 
261
+ def test_delegator_calls_are_propagated
262
+ delegator = Object.new
263
+ mock = Minitest::Mock.new delegator
264
+
265
+ refute delegator.nil?
266
+ refute mock.nil?
267
+ assert_mock mock
268
+ end
269
+
270
+ def test_handles_kwargs_in_error_message
271
+ mock = Minitest::Mock.new
272
+
273
+ mock.expect :foo, nil, [], kw: true
274
+ mock.expect :foo, nil, [], kw: false
275
+
276
+ mock.foo kw: true
277
+
278
+ e = assert_raises(MockExpectationError) { mock.verify }
279
+
280
+ exp = "expected foo(:kw=>false) => nil, got [foo(:kw=>true) => nil]"
281
+
282
+ assert_equal exp, e.message
283
+ end
284
+
255
285
  def test_verify_passes_when_mock_block_returns_true
256
286
  mock = Minitest::Mock.new
257
287
  mock.expect :foo, nil do
@@ -270,11 +300,188 @@ class TestMinitestMock < Minitest::Test
270
300
  a1 == arg1 && a2 == arg2 && a3 == arg3
271
301
  end
272
302
 
273
- mock.foo arg1, arg2, arg3
303
+ assert_silent do
304
+ if RUBY_VERSION > "3" then
305
+ mock.foo arg1, arg2, arg3
306
+ else
307
+ mock.foo arg1, arg2, **arg3 # oddity just for ruby 2.7
308
+ end
309
+ end
310
+
311
+ assert_mock mock
312
+ end
313
+
314
+ def test_mock_block_is_passed_keyword_args__block
315
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
316
+ mock = Minitest::Mock.new
317
+ mock.expect :foo, nil do |k1:, k2:, k3:|
318
+ k1 == arg1 && k2 == arg2 && k3 == arg3
319
+ end
320
+
321
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
322
+
323
+ assert_mock mock
324
+ end
325
+
326
+ def test_mock_block_is_passed_keyword_args__block_bad_missing
327
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
328
+ mock = Minitest::Mock.new
329
+ mock.expect :foo, nil do |k1:, k2:, k3:|
330
+ k1 == arg1 && k2 == arg2 && k3 == arg3
331
+ end
332
+
333
+ e = assert_raises ArgumentError do
334
+ mock.foo(k1: arg1, k2: arg2)
335
+ end
336
+
337
+ # basically testing ruby ... need ? for ruby < 2.7 :(
338
+ assert_match(/missing keyword: :?k3/, e.message)
339
+ end
340
+
341
+ def test_mock_block_is_passed_keyword_args__block_bad_extra
342
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
343
+ mock = Minitest::Mock.new
344
+ mock.expect :foo, nil do |k1:, k2:|
345
+ k1 == arg1 && k2 == arg2 && k3 == arg3
346
+ end
347
+
348
+ e = assert_raises ArgumentError do
349
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
350
+ end
351
+
352
+ # basically testing ruby ... need ? for ruby < 2.7 :(
353
+ assert_match(/unknown keyword: :?k3/, e.message)
354
+ end
355
+
356
+ def test_mock_block_is_passed_keyword_args__block_bad_value
357
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
358
+ mock = Minitest::Mock.new
359
+ mock.expect :foo, nil do |k1:, k2:, k3:|
360
+ k1 == arg1 && k2 == arg2 && k3 == arg3
361
+ end
362
+
363
+ e = assert_raises MockExpectationError do
364
+ mock.foo(k1: arg1, k2: arg2, k3: :BAD!)
365
+ end
366
+
367
+ exp = "mocked method :foo failed block w/ [] {:k1=>:bar, :k2=>[1, 2, 3], :k3=>:BAD!}"
368
+ assert_equal exp, e.message
369
+ end
370
+
371
+ def test_mock_block_is_passed_keyword_args__args
372
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
373
+ mock = Minitest::Mock.new
374
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
375
+
376
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
274
377
 
275
378
  assert_mock mock
276
379
  end
277
380
 
381
+ def test_mock_allow_all_kwargs__old_style_env
382
+ with_kwargs_env do
383
+ mock = Minitest::Mock.new
384
+ mock.expect :foo, true, [Hash]
385
+ assert_equal true, mock.foo(bar: 42)
386
+ end
387
+ end
388
+
389
+ def test_mock_allow_all_kwargs__old_style_env__rewrite
390
+ with_kwargs_env do
391
+ mock = Minitest::Mock.new
392
+ mock.expect :foo, true, [], bar: Integer
393
+ assert_equal true, mock.foo(bar: 42)
394
+ end
395
+ end
396
+
397
+ def test_mock_block_is_passed_keyword_args__args__old_style_bad
398
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
399
+ mock = Minitest::Mock.new
400
+ mock.expect :foo, nil, [{k1: arg1, k2: arg2, k3: arg3}]
401
+
402
+ e = assert_raises ArgumentError do
403
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
404
+ end
405
+
406
+ assert_equal "mocked method :foo expects 1 arguments, got []", e.message
407
+ end
408
+
409
+ def test_mock_block_is_passed_keyword_args__args__old_style_env
410
+ with_kwargs_env do
411
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
412
+ mock = Minitest::Mock.new
413
+ mock.expect :foo, nil, [{k1: arg1, k2: arg2, k3: arg3}]
414
+
415
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
416
+
417
+ assert_mock mock
418
+ end
419
+ end
420
+
421
+ def test_mock_block_is_passed_keyword_args__args__old_style_both
422
+ with_kwargs_env do
423
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
424
+ mock = Minitest::Mock.new
425
+
426
+ assert_output nil, /Using MT_KWARGS_HAC. yet passing kwargs/ do
427
+ mock.expect :foo, nil, [{}], k1: arg1, k2: arg2, k3: arg3
428
+ end
429
+
430
+ mock.foo({}, k1: arg1, k2: arg2, k3: arg3)
431
+
432
+ assert_mock mock
433
+ end
434
+ end
435
+
436
+ def test_mock_block_is_passed_keyword_args__args_bad_missing
437
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
438
+ mock = Minitest::Mock.new
439
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
440
+
441
+ e = assert_raises ArgumentError do
442
+ mock.foo(k1: arg1, k2: arg2)
443
+ end
444
+
445
+ assert_equal "mocked method :foo expects 3 keyword arguments, got %p" % {k1: arg1, k2: arg2}, e.message
446
+ end
447
+
448
+ def test_mock_block_is_passed_keyword_args__args_bad_extra
449
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
450
+ mock = Minitest::Mock.new
451
+ mock.expect :foo, nil, k1: arg1, k2: arg2
452
+
453
+ e = assert_raises ArgumentError do
454
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
455
+ end
456
+
457
+ assert_equal "mocked method :foo expects 2 keyword arguments, got %p" % {k1: arg1, k2: arg2, k3: arg3}, e.message
458
+ end
459
+
460
+ def test_mock_block_is_passed_keyword_args__args_bad_key
461
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
462
+ mock = Minitest::Mock.new
463
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
464
+
465
+ e = assert_raises MockExpectationError do
466
+ mock.foo(k1: arg1, k2: arg2, BAD: arg3)
467
+ end
468
+
469
+ assert_includes e.message, "unexpected keywords [:k1, :k2, :k3]"
470
+ assert_includes e.message, "vs [:k1, :k2, :BAD]"
471
+ end
472
+
473
+ def test_mock_block_is_passed_keyword_args__args_bad_val
474
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
475
+ mock = Minitest::Mock.new
476
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
477
+
478
+ e = assert_raises MockExpectationError do
479
+ mock.foo(k1: arg1, k2: :BAD!, k3: arg3)
480
+ end
481
+
482
+ assert_match(/unexpected keyword arguments.* vs .*:k2=>:BAD!/, e.message)
483
+ end
484
+
278
485
  def test_mock_block_is_passed_function_block
279
486
  mock = Minitest::Mock.new
280
487
  block = proc { "bar" }
@@ -286,6 +493,13 @@ class TestMinitestMock < Minitest::Test
286
493
  assert_mock mock
287
494
  end
288
495
 
496
+ def test_mock_forward_keyword_arguments
497
+ mock = Minitest::Mock.new
498
+ mock.expect(:foo, nil) { |bar:| bar == 'bar' }
499
+ mock.foo(bar: 'bar')
500
+ assert_mock mock
501
+ end
502
+
289
503
  def test_verify_fails_when_mock_block_returns_false
290
504
  mock = Minitest::Mock.new
291
505
  mock.expect :foo, nil do
@@ -293,12 +507,12 @@ class TestMinitestMock < Minitest::Test
293
507
  end
294
508
 
295
509
  e = assert_raises(MockExpectationError) { mock.foo }
296
- exp = "mocked method :foo failed block w/ []"
510
+ exp = "mocked method :foo failed block w/ [] {}"
297
511
 
298
512
  assert_equal exp, e.message
299
513
  end
300
514
 
301
- def test_mock_block_throws_if_args_passed
515
+ def test_mock_block_raises_if_args_passed
302
516
  mock = Minitest::Mock.new
303
517
 
304
518
  e = assert_raises(ArgumentError) do
@@ -312,6 +526,20 @@ class TestMinitestMock < Minitest::Test
312
526
  assert_match exp, e.message
313
527
  end
314
528
 
529
+ def test_mock_block_raises_if_kwargs_passed
530
+ mock = Minitest::Mock.new
531
+
532
+ e = assert_raises(ArgumentError) do
533
+ mock.expect :foo, nil, kwargs:1 do
534
+ true
535
+ end
536
+ end
537
+
538
+ exp = "kwargs ignored when block given"
539
+
540
+ assert_match exp, e.message
541
+ end
542
+
315
543
  def test_mock_returns_retval_when_called_with_block
316
544
  mock = Minitest::Mock.new
317
545
  mock.expect(:foo, 32) do
@@ -519,6 +747,19 @@ class TestMinitestStub < Minitest::Test
519
747
  @tc.assert_equal true, rs
520
748
  end
521
749
 
750
+ def test_mock_with_yield_kwargs
751
+ mock = Minitest::Mock.new
752
+ rs = nil
753
+
754
+ File.stub :open, true, mock, kw:42 do
755
+ File.open "foo.txt", "r" do |f, kw:|
756
+ rs = kw
757
+ end
758
+ end
759
+
760
+ @tc.assert_equal 42, rs
761
+ end
762
+
522
763
  alias test_stub_value__old test_stub_value # TODO: remove/rename
523
764
 
524
765
  ## Permutation Sets:
@@ -604,6 +845,26 @@ class TestMinitestStub < Minitest::Test
604
845
  end
605
846
  end
606
847
 
848
+ def test_stub__hash_as_last_real_arg
849
+ with_kwargs_env do
850
+ token = Object.new
851
+ def token.create_with_retry u, p; raise "shouldn't see this"; end
852
+
853
+ controller = Object.new
854
+ controller.define_singleton_method :create do |u, p|
855
+ token.create_with_retry u, p
856
+ end
857
+
858
+ params = Object.new
859
+ def params.to_hash; raise "nah"; end
860
+
861
+ token.stub(:create_with_retry, ->(u, p) { 42 }) do
862
+ act = controller.create :u, params
863
+ @tc.assert_equal 42, act
864
+ end
865
+ end
866
+ end
867
+
607
868
  def test_stub_callable_block_5 # from tenderlove
608
869
  @assertion_count += 1
609
870
  Foo.stub5 :blocking, Bar.new do
@@ -822,7 +1083,7 @@ class TestMinitestStub < Minitest::Test
822
1083
  end
823
1084
  end
824
1085
  end
825
- exp = "undefined method `write' for nil:NilClass"
1086
+ exp = /undefined method `write' for nil/
826
1087
  assert_match exp, e.message
827
1088
  end
828
1089
 
@@ -30,6 +30,7 @@ class TestMinitestReporter < MetaMetaMetaTestCase
30
30
  end
31
31
 
32
32
  def setup
33
+ super
33
34
  self.io = StringIO.new("")
34
35
  self.r = new_composite_reporter
35
36
  end
@@ -64,6 +65,12 @@ class TestMinitestReporter < MetaMetaMetaTestCase
64
65
  @pt ||= Minitest::Result.from Minitest::Test.new(:woot)
65
66
  end
66
67
 
68
+ def passing_test_with_metadata
69
+ test = Minitest::Test.new(:woot)
70
+ test.metadata[:meta] = :data
71
+ @pt ||= Minitest::Result.from test
72
+ end
73
+
67
74
  def skip_test
68
75
  unless defined? @st then
69
76
  @st = Minitest::Test.new(:woot)
@@ -165,6 +172,29 @@ class TestMinitestReporter < MetaMetaMetaTestCase
165
172
  assert_equal 0, r.assertions
166
173
  end
167
174
 
175
+ def test_record_pass_with_metadata
176
+ reporter = self.r
177
+
178
+ def reporter.metadata
179
+ @metadata
180
+ end
181
+
182
+ def reporter.record result
183
+ super
184
+ @metadata = result.metadata if result.metadata?
185
+ end
186
+
187
+ r.record passing_test_with_metadata
188
+
189
+ exp = { :meta => :data }
190
+ assert_equal exp, reporter.metadata
191
+
192
+ assert_equal ".", io.string
193
+ assert_empty r.results
194
+ assert_equal 1, r.count
195
+ assert_equal 0, r.assertions
196
+ end
197
+
168
198
  def test_record_fail
169
199
  fail_test = self.fail_test
170
200
  r.record fail_test
@@ -137,6 +137,46 @@ describe Minitest::Spec do
137
137
  end
138
138
  end
139
139
 
140
+ def good_pattern
141
+ capture_io do # 3.0 is noisy
142
+ eval "[1,2,3] => [Integer, Integer, Integer]" # eval to escape parser for ruby<3
143
+ end
144
+ end
145
+
146
+ def bad_pattern
147
+ capture_io do # 3.0 is noisy
148
+ eval "[1,2,3] => [Integer, Integer]" # eval to escape parser for ruby<3
149
+ end
150
+ end
151
+
152
+ it "needs to pattern match" do
153
+ @assertion_count = 1
154
+
155
+ if RUBY_VERSION > "3" then
156
+ expect { good_pattern }.must_pattern_match
157
+ else
158
+ assert_raises NotImplementedError do
159
+ expect {}.must_pattern_match
160
+ end
161
+ end
162
+ end
163
+
164
+ it "needs to error on bad pattern match" do
165
+ skip unless RUBY_VERSION > "3"
166
+
167
+ @assertion_count = 1
168
+
169
+ exp = if RUBY_VERSION.start_with? "3.0"
170
+ "[1, 2, 3]" # terrible error message!
171
+ else
172
+ /length mismatch/
173
+ end
174
+
175
+ assert_triggered exp do
176
+ expect { bad_pattern }.must_pattern_match
177
+ end
178
+ end
179
+
140
180
  it "needs to ensure silence" do
141
181
  @assertion_count -= 1 # no msg
142
182
  @assertion_count += 2 # assert_output is 2 assertions
@@ -172,6 +212,7 @@ describe Minitest::Spec do
172
212
  must_include
173
213
  must_match
174
214
  must_output
215
+ must_pattern_match
175
216
  must_raise
176
217
  must_respond_to
177
218
  must_throw
@@ -505,7 +546,7 @@ describe Minitest::Spec do
505
546
  it "needs to verify regexp matches" do
506
547
  @assertion_count += 3 # must_match is 2 assertions
507
548
 
508
- assert_success _("blah").must_match(/\w+/)
549
+ assert_kind_of MatchData, _("blah").must_match(/\w+/)
509
550
 
510
551
  assert_triggered "Expected /\\d+/ to match \"blah\"." do
511
552
  _("blah").must_match(/\d+/)
@@ -744,6 +785,10 @@ describe Minitest::Spec, :subject do
744
785
  end
745
786
 
746
787
  class TestMetaStatic < Minitest::Test
788
+ def assert_method_count expected, klass
789
+ assert_equal expected, klass.public_instance_methods.grep(/^test_/).count
790
+ end
791
+
747
792
  def test_children
748
793
  Minitest::Spec.children.clear # prevents parallel run
749
794
 
@@ -777,8 +822,8 @@ class TestMetaStatic < Minitest::Test
777
822
  end
778
823
  end
779
824
 
780
- assert_equal 1, outer.public_instance_methods.grep(/^test_/).count
781
- assert_equal 1, inner.public_instance_methods.grep(/^test_/).count
825
+ assert_method_count 1, outer
826
+ assert_method_count 1, inner
782
827
  end
783
828
 
784
829
  def test_it_wont_add_test_methods_to_children
@@ -792,14 +837,18 @@ class TestMetaStatic < Minitest::Test
792
837
  end
793
838
  end
794
839
 
795
- assert_equal 1, outer.public_instance_methods.grep(/^test_/).count
796
- assert_equal 0, inner.public_instance_methods.grep(/^test_/).count
840
+ assert_method_count 1, outer
841
+ assert_method_count 0, inner
797
842
  end
798
843
  end
799
844
 
800
845
  class TestMeta < MetaMetaMetaTestCase
801
846
  # do not call parallelize_me! here because specs use register_spec_type globally
802
847
 
848
+ def assert_defined_methods expected, klass
849
+ assert_equal expected, klass.instance_methods(false).sort.map(&:to_s)
850
+ end
851
+
803
852
  def util_structure
804
853
  y = z = nil
805
854
  before_list = []
@@ -872,7 +921,7 @@ class TestMeta < MetaMetaMetaTestCase
872
921
  end
873
922
  end
874
923
 
875
- test_name = spec_class.instance_methods.sort.grep(/test/).first
924
+ test_name = spec_class.instance_methods.sort.grep(/test_/).first
876
925
 
877
926
  spec = spec_class.new test_name
878
927
 
@@ -921,9 +970,9 @@ class TestMeta < MetaMetaMetaTestCase
921
970
  inner_methods2 = inner_methods1 +
922
971
  %w[test_0002_anonymous test_0003_anonymous]
923
972
 
924
- assert_equal top_methods, x.instance_methods(false).sort.map(&:to_s)
925
- assert_equal inner_methods1, y.instance_methods(false).sort.map(&:to_s)
926
- assert_equal inner_methods2, z.instance_methods(false).sort.map(&:to_s)
973
+ assert_defined_methods top_methods, x
974
+ assert_defined_methods inner_methods1, y
975
+ assert_defined_methods inner_methods2, z
927
976
  end
928
977
 
929
978
  def test_structure_postfix_it
@@ -940,8 +989,8 @@ class TestMeta < MetaMetaMetaTestCase
940
989
  it "inner-it" do end
941
990
  end
942
991
 
943
- assert_equal %w[test_0001_inner-it], y.instance_methods(false).map(&:to_s)
944
- assert_equal %w[test_0001_inner-it], z.instance_methods(false).map(&:to_s)
992
+ assert_defined_methods %w[test_0001_inner-it], y
993
+ assert_defined_methods %w[test_0001_inner-it], z
945
994
  end
946
995
 
947
996
  def test_setup_teardown_behavior
@@ -972,9 +1021,9 @@ class TestMeta < MetaMetaMetaTestCase
972
1021
  ].sort
973
1022
 
974
1023
  assert_equal test_methods, [x1, x2]
975
- assert_equal test_methods, x.instance_methods.grep(/^test/).map(&:to_s).sort
976
- assert_equal [], y.instance_methods.grep(/^test/)
977
- assert_equal [], z.instance_methods.grep(/^test/)
1024
+ assert_defined_methods test_methods, x
1025
+ assert_defined_methods [], y
1026
+ assert_defined_methods [], z
978
1027
  end
979
1028
 
980
1029
  def test_structure_subclasses
@@ -1060,3 +1109,38 @@ class ValueMonadTest < Minitest::Test
1060
1109
  assert_equal "c", struct.expect
1061
1110
  end
1062
1111
  end
1112
+
1113
+ describe Minitest::Spec, :infect_an_assertion do
1114
+ class << self
1115
+ attr_accessor :infect_mock
1116
+ end
1117
+
1118
+ def assert_infects exp, act, msg = nil, foo: nil, bar: nil
1119
+ self.class.infect_mock.assert_infects exp, act, msg, foo: foo, bar: bar
1120
+ end
1121
+
1122
+ infect_an_assertion :assert_infects, :must_infect
1123
+ infect_an_assertion :assert_infects, :must_infect_without_flipping, :dont_flip
1124
+
1125
+ it "infects assertions with kwargs" do
1126
+ mock = Minitest::Mock.new
1127
+ mock.expect :assert_infects, true, [:exp, :act, nil], foo: :foo, bar: :bar
1128
+
1129
+ self.class.infect_mock = mock
1130
+
1131
+ _(:act).must_infect :exp, foo: :foo, bar: :bar
1132
+
1133
+ assert_mock mock
1134
+ end
1135
+
1136
+ it "infects assertions with kwargs (dont_flip)" do
1137
+ mock = Minitest::Mock.new
1138
+ mock.expect :assert_infects, true, [:act, :exp, nil], foo: :foo, bar: :bar
1139
+
1140
+ self.class.infect_mock = mock
1141
+
1142
+ _(:act).must_infect_without_flipping :exp, foo: :foo, bar: :bar
1143
+
1144
+ assert_mock mock
1145
+ end
1146
+ end