minitest 5.15.0 → 5.16.3

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,5 +1,13 @@
1
1
  require "minitest/autorun"
2
2
 
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
10
+
3
11
  class TestMinitestMock < Minitest::Test
4
12
  parallelize_me!
5
13
 
@@ -51,7 +59,7 @@ class TestMinitestMock < Minitest::Test
51
59
  @mock.sum
52
60
  end
53
61
 
54
- assert_equal "mocked method :sum expects 2 arguments, got 0", e.message
62
+ assert_equal "mocked method :sum expects 2 arguments, got []", e.message
55
63
  end
56
64
 
57
65
  def test_return_mock_does_not_raise
@@ -210,7 +218,7 @@ class TestMinitestMock < Minitest::Test
210
218
  mock.a
211
219
  end
212
220
 
213
- assert_equal "No more expects available for :a: []", e.message
221
+ assert_equal "No more expects available for :a: [] {}", e.message
214
222
  end
215
223
 
216
224
  def test_same_method_expects_are_verified_when_all_called
@@ -252,6 +260,21 @@ class TestMinitestMock < Minitest::Test
252
260
  assert_equal exp, e.message
253
261
  end
254
262
 
263
+ def test_handles_kwargs_in_error_message
264
+ mock = Minitest::Mock.new
265
+
266
+ mock.expect :foo, nil, [], kw: true
267
+ mock.expect :foo, nil, [], kw: false
268
+
269
+ mock.foo kw: true
270
+
271
+ e = assert_raises(MockExpectationError) { mock.verify }
272
+
273
+ exp = "expected foo(:kw=>false) => nil, got [foo(:kw=>true) => nil]"
274
+
275
+ assert_equal exp, e.message
276
+ end
277
+
255
278
  def test_verify_passes_when_mock_block_returns_true
256
279
  mock = Minitest::Mock.new
257
280
  mock.expect :foo, nil do
@@ -270,11 +293,188 @@ class TestMinitestMock < Minitest::Test
270
293
  a1 == arg1 && a2 == arg2 && a3 == arg3
271
294
  end
272
295
 
273
- mock.foo arg1, arg2, arg3
296
+ assert_silent do
297
+ if RUBY_VERSION > "3" then
298
+ mock.foo arg1, arg2, arg3
299
+ else
300
+ mock.foo arg1, arg2, **arg3 # oddity just for ruby 2.7
301
+ end
302
+ end
303
+
304
+ assert_mock mock
305
+ end
306
+
307
+ def test_mock_block_is_passed_keyword_args__block
308
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
309
+ mock = Minitest::Mock.new
310
+ mock.expect :foo, nil do |k1:, k2:, k3:|
311
+ k1 == arg1 && k2 == arg2 && k3 == arg3
312
+ end
313
+
314
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
315
+
316
+ assert_mock mock
317
+ end
318
+
319
+ def test_mock_block_is_passed_keyword_args__block_bad_missing
320
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
321
+ mock = Minitest::Mock.new
322
+ mock.expect :foo, nil do |k1:, k2:, k3:|
323
+ k1 == arg1 && k2 == arg2 && k3 == arg3
324
+ end
325
+
326
+ e = assert_raises ArgumentError do
327
+ mock.foo(k1: arg1, k2: arg2)
328
+ end
329
+
330
+ # basically testing ruby ... need ? for ruby < 2.7 :(
331
+ assert_match(/missing keyword: :?k3/, e.message)
332
+ end
333
+
334
+ def test_mock_block_is_passed_keyword_args__block_bad_extra
335
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
336
+ mock = Minitest::Mock.new
337
+ mock.expect :foo, nil do |k1:, k2:|
338
+ k1 == arg1 && k2 == arg2 && k3 == arg3
339
+ end
340
+
341
+ e = assert_raises ArgumentError do
342
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
343
+ end
344
+
345
+ # basically testing ruby ... need ? for ruby < 2.7 :(
346
+ assert_match(/unknown keyword: :?k3/, e.message)
347
+ end
348
+
349
+ def test_mock_block_is_passed_keyword_args__block_bad_value
350
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
351
+ mock = Minitest::Mock.new
352
+ mock.expect :foo, nil do |k1:, k2:, k3:|
353
+ k1 == arg1 && k2 == arg2 && k3 == arg3
354
+ end
355
+
356
+ e = assert_raises MockExpectationError do
357
+ mock.foo(k1: arg1, k2: arg2, k3: :BAD!)
358
+ end
359
+
360
+ exp = "mocked method :foo failed block w/ [] {:k1=>:bar, :k2=>[1, 2, 3], :k3=>:BAD!}"
361
+ assert_equal exp, e.message
362
+ end
363
+
364
+ def test_mock_block_is_passed_keyword_args__args
365
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
366
+ mock = Minitest::Mock.new
367
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
368
+
369
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
274
370
 
275
371
  assert_mock mock
276
372
  end
277
373
 
374
+ def test_mock_allow_all_kwargs__old_style_env
375
+ with_kwargs_env do
376
+ mock = Minitest::Mock.new
377
+ mock.expect :foo, true, [Hash]
378
+ assert_equal true, mock.foo(bar: 42)
379
+ end
380
+ end
381
+
382
+ def test_mock_allow_all_kwargs__old_style_env__rewrite
383
+ with_kwargs_env do
384
+ mock = Minitest::Mock.new
385
+ mock.expect :foo, true, [], bar: Integer
386
+ assert_equal true, mock.foo(bar: 42)
387
+ end
388
+ end
389
+
390
+ def test_mock_block_is_passed_keyword_args__args__old_style_bad
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 ArgumentError do
396
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
397
+ end
398
+
399
+ assert_equal "mocked method :foo expects 1 arguments, got []", e.message
400
+ end
401
+
402
+ def test_mock_block_is_passed_keyword_args__args__old_style_env
403
+ with_kwargs_env do
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
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
409
+
410
+ assert_mock mock
411
+ end
412
+ end
413
+
414
+ def test_mock_block_is_passed_keyword_args__args__old_style_both
415
+ with_kwargs_env do
416
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
417
+ mock = Minitest::Mock.new
418
+
419
+ assert_output nil, /Using MT_KWARGS_HAC. yet passing kwargs/ do
420
+ mock.expect :foo, nil, [{}], k1: arg1, k2: arg2, k3: arg3
421
+ end
422
+
423
+ mock.foo({}, k1: arg1, k2: arg2, k3: arg3)
424
+
425
+ assert_mock mock
426
+ end
427
+ end
428
+
429
+ def test_mock_block_is_passed_keyword_args__args_bad_missing
430
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
431
+ mock = Minitest::Mock.new
432
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
433
+
434
+ e = assert_raises ArgumentError do
435
+ mock.foo(k1: arg1, k2: arg2)
436
+ end
437
+
438
+ assert_equal "mocked method :foo expects 3 keyword arguments, got %p" % {k1: arg1, k2: arg2}, e.message
439
+ end
440
+
441
+ def test_mock_block_is_passed_keyword_args__args_bad_extra
442
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
443
+ mock = Minitest::Mock.new
444
+ mock.expect :foo, nil, k1: arg1, k2: arg2
445
+
446
+ e = assert_raises ArgumentError do
447
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
448
+ end
449
+
450
+ assert_equal "mocked method :foo expects 2 keyword arguments, got %p" % {k1: arg1, k2: arg2, k3: arg3}, e.message
451
+ end
452
+
453
+ def test_mock_block_is_passed_keyword_args__args_bad_key
454
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
455
+ mock = Minitest::Mock.new
456
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
457
+
458
+ e = assert_raises MockExpectationError do
459
+ mock.foo(k1: arg1, k2: arg2, BAD: arg3)
460
+ end
461
+
462
+ assert_includes e.message, "unexpected keywords [:k1, :k2, :k3]"
463
+ assert_includes e.message, "vs [:k1, :k2, :BAD]"
464
+ end
465
+
466
+ def test_mock_block_is_passed_keyword_args__args_bad_val
467
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
468
+ mock = Minitest::Mock.new
469
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
470
+
471
+ e = assert_raises MockExpectationError do
472
+ mock.foo(k1: arg1, k2: :BAD!, k3: arg3)
473
+ end
474
+
475
+ assert_match(/unexpected keyword arguments.* vs .*:k2=>:BAD!/, e.message)
476
+ end
477
+
278
478
  def test_mock_block_is_passed_function_block
279
479
  mock = Minitest::Mock.new
280
480
  block = proc { "bar" }
@@ -286,6 +486,13 @@ class TestMinitestMock < Minitest::Test
286
486
  assert_mock mock
287
487
  end
288
488
 
489
+ def test_mock_forward_keyword_arguments
490
+ mock = Minitest::Mock.new
491
+ mock.expect(:foo, nil) { |bar:| bar == 'bar' }
492
+ mock.foo(bar: 'bar')
493
+ assert_mock mock
494
+ end
495
+
289
496
  def test_verify_fails_when_mock_block_returns_false
290
497
  mock = Minitest::Mock.new
291
498
  mock.expect :foo, nil do
@@ -293,12 +500,12 @@ class TestMinitestMock < Minitest::Test
293
500
  end
294
501
 
295
502
  e = assert_raises(MockExpectationError) { mock.foo }
296
- exp = "mocked method :foo failed block w/ []"
503
+ exp = "mocked method :foo failed block w/ [] {}"
297
504
 
298
505
  assert_equal exp, e.message
299
506
  end
300
507
 
301
- def test_mock_block_throws_if_args_passed
508
+ def test_mock_block_raises_if_args_passed
302
509
  mock = Minitest::Mock.new
303
510
 
304
511
  e = assert_raises(ArgumentError) do
@@ -312,6 +519,20 @@ class TestMinitestMock < Minitest::Test
312
519
  assert_match exp, e.message
313
520
  end
314
521
 
522
+ def test_mock_block_raises_if_kwargs_passed
523
+ mock = Minitest::Mock.new
524
+
525
+ e = assert_raises(ArgumentError) do
526
+ mock.expect :foo, nil, kwargs:1 do
527
+ true
528
+ end
529
+ end
530
+
531
+ exp = "kwargs ignored when block given"
532
+
533
+ assert_match exp, e.message
534
+ end
535
+
315
536
  def test_mock_returns_retval_when_called_with_block
316
537
  mock = Minitest::Mock.new
317
538
  mock.expect(:foo, 32) do
@@ -519,6 +740,19 @@ class TestMinitestStub < Minitest::Test
519
740
  @tc.assert_equal true, rs
520
741
  end
521
742
 
743
+ def test_mock_with_yield_kwargs
744
+ mock = Minitest::Mock.new
745
+ rs = nil
746
+
747
+ File.stub :open, true, mock, kw:42 do
748
+ File.open "foo.txt", "r" do |f, kw:|
749
+ rs = kw
750
+ end
751
+ end
752
+
753
+ @tc.assert_equal 42, rs
754
+ end
755
+
522
756
  alias test_stub_value__old test_stub_value # TODO: remove/rename
523
757
 
524
758
  ## Permutation Sets:
@@ -604,6 +838,26 @@ class TestMinitestStub < Minitest::Test
604
838
  end
605
839
  end
606
840
 
841
+ def test_stub__hash_as_last_real_arg
842
+ with_kwargs_env do
843
+ token = Object.new
844
+ def token.create_with_retry u, p; raise "shouldn't see this"; end
845
+
846
+ controller = Object.new
847
+ controller.define_singleton_method :create do |u, p|
848
+ token.create_with_retry u, p
849
+ end
850
+
851
+ params = Object.new
852
+ def params.to_hash; raise "nah"; end
853
+
854
+ token.stub(:create_with_retry, ->(u, p) { 42 }) do
855
+ act = controller.create :u, params
856
+ @tc.assert_equal 42, act
857
+ end
858
+ end
859
+ end
860
+
607
861
  def test_stub_callable_block_5 # from tenderlove
608
862
  @assertion_count += 1
609
863
  Foo.stub5 :blocking, Bar.new do
@@ -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
@@ -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+/)
@@ -744,6 +744,10 @@ describe Minitest::Spec, :subject do
744
744
  end
745
745
 
746
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
+
747
751
  def test_children
748
752
  Minitest::Spec.children.clear # prevents parallel run
749
753
 
@@ -777,8 +781,8 @@ class TestMetaStatic < Minitest::Test
777
781
  end
778
782
  end
779
783
 
780
- assert_equal 1, outer.public_instance_methods.grep(/^test_/).count
781
- assert_equal 1, inner.public_instance_methods.grep(/^test_/).count
784
+ assert_method_count 1, outer
785
+ assert_method_count 1, inner
782
786
  end
783
787
 
784
788
  def test_it_wont_add_test_methods_to_children
@@ -792,14 +796,18 @@ class TestMetaStatic < Minitest::Test
792
796
  end
793
797
  end
794
798
 
795
- assert_equal 1, outer.public_instance_methods.grep(/^test_/).count
796
- assert_equal 0, inner.public_instance_methods.grep(/^test_/).count
799
+ assert_method_count 1, outer
800
+ assert_method_count 0, inner
797
801
  end
798
802
  end
799
803
 
800
804
  class TestMeta < MetaMetaMetaTestCase
801
805
  # do not call parallelize_me! here because specs use register_spec_type globally
802
806
 
807
+ def assert_defined_methods expected, klass
808
+ assert_equal expected, klass.instance_methods(false).sort.map(&:to_s)
809
+ end
810
+
803
811
  def util_structure
804
812
  y = z = nil
805
813
  before_list = []
@@ -872,7 +880,7 @@ class TestMeta < MetaMetaMetaTestCase
872
880
  end
873
881
  end
874
882
 
875
- test_name = spec_class.instance_methods.sort.grep(/test/).first
883
+ test_name = spec_class.instance_methods.sort.grep(/test_/).first
876
884
 
877
885
  spec = spec_class.new test_name
878
886
 
@@ -921,9 +929,9 @@ class TestMeta < MetaMetaMetaTestCase
921
929
  inner_methods2 = inner_methods1 +
922
930
  %w[test_0002_anonymous test_0003_anonymous]
923
931
 
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)
932
+ assert_defined_methods top_methods, x
933
+ assert_defined_methods inner_methods1, y
934
+ assert_defined_methods inner_methods2, z
927
935
  end
928
936
 
929
937
  def test_structure_postfix_it
@@ -940,8 +948,8 @@ class TestMeta < MetaMetaMetaTestCase
940
948
  it "inner-it" do end
941
949
  end
942
950
 
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)
951
+ assert_defined_methods %w[test_0001_inner-it], y
952
+ assert_defined_methods %w[test_0001_inner-it], z
945
953
  end
946
954
 
947
955
  def test_setup_teardown_behavior
@@ -972,9 +980,9 @@ class TestMeta < MetaMetaMetaTestCase
972
980
  ].sort
973
981
 
974
982
  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/)
983
+ assert_defined_methods test_methods, x
984
+ assert_defined_methods [], y
985
+ assert_defined_methods [], z
978
986
  end
979
987
 
980
988
  def test_structure_subclasses