minitest 5.14.4 → 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.
@@ -3,7 +3,7 @@ require "minitest/benchmark"
3
3
 
4
4
  ##
5
5
  # Used to verify data:
6
- # http://www.wolframalpha.com/examples/RegressionAnalysis.html
6
+ # https://www.wolframalpha.com/examples/RegressionAnalysis.html
7
7
 
8
8
  class TestMinitestBenchmark < Minitest::Test
9
9
  def test_cls_bench_exp
@@ -110,7 +110,7 @@ class TestMinitestBenchmark < Minitest::Test
110
110
  assert_fit :power, x, y, 0.90, 2.6217, 1.4556
111
111
 
112
112
  # income to % of households below income amount
113
- # http://library.wolfram.com/infocenter/Conferences/6461/PowerLaws.nb
113
+ # https://library.wolfram.com/infocenter/Conferences/6461/PowerLaws.nb
114
114
  x = [15_000, 25_000, 35_000, 50_000, 75_000, 100_000]
115
115
  y = [0.154, 0.283, 0.402, 0.55, 0.733, 0.843]
116
116
 
@@ -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
@@ -133,7 +141,7 @@ class TestMinitestMock < Minitest::Test
133
141
  @mock.expect :blah, 3, false
134
142
  end
135
143
 
136
- assert_equal "args must be an array", e.message
144
+ assert_match "args must be an array", e.message
137
145
  end
138
146
 
139
147
  def test_respond_appropriately
@@ -150,7 +158,7 @@ class TestMinitestMock < Minitest::Test
150
158
 
151
159
  expected = "unmocked method :bar, expected one of [:foo, :meaning_of_life]"
152
160
 
153
- assert_equal expected, e.message
161
+ assert_match expected, e.message
154
162
  end
155
163
 
156
164
  def test_assign_per_mock_return_values
@@ -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
@@ -309,7 +516,21 @@ class TestMinitestMock < Minitest::Test
309
516
 
310
517
  exp = "args ignored when block given"
311
518
 
312
- assert_equal exp, e.message
519
+ assert_match exp, e.message
520
+ end
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
313
534
  end
314
535
 
315
536
  def test_mock_returns_retval_when_called_with_block
@@ -499,7 +720,8 @@ class TestMinitestStub < Minitest::Test
499
720
  end
500
721
  end
501
722
 
502
- exp = /undefined method `nope_nope_nope' for( class)? `#{self.class}::Time'/
723
+ exp = jruby? ? /Undefined method nope_nope_nope for '#{self.class}::Time'/ :
724
+ /undefined method `nope_nope_nope' for( class)? `#{self.class}::Time'/
503
725
  assert_match exp, e.message
504
726
  end
505
727
 
@@ -518,6 +740,19 @@ class TestMinitestStub < Minitest::Test
518
740
  @tc.assert_equal true, rs
519
741
  end
520
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
+
521
756
  alias test_stub_value__old test_stub_value # TODO: remove/rename
522
757
 
523
758
  ## Permutation Sets:
@@ -603,6 +838,26 @@ class TestMinitestStub < Minitest::Test
603
838
  end
604
839
  end
605
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
+
606
861
  def test_stub_callable_block_5 # from tenderlove
607
862
  @assertion_count += 1
608
863
  Foo.stub5 :blocking, Bar.new do
@@ -822,7 +1077,7 @@ class TestMinitestStub < Minitest::Test
822
1077
  end
823
1078
  end
824
1079
  exp = "undefined method `write' for nil:NilClass"
825
- assert_equal exp, e.message
1080
+ assert_match exp, e.message
826
1081
  end
827
1082
 
828
1083
  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