minitest 5.14.4 → 5.25.5

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,4 +1,3 @@
1
- # encoding: utf-8
2
1
  require "minitest/metametameta"
3
2
  require "stringio"
4
3
 
@@ -12,15 +11,12 @@ class ExampleA; end
12
11
  class ExampleB < ExampleA; end
13
12
 
14
13
  describe Minitest::Spec do
15
- # helps to deal with 2.4 deprecation of Fixnum for Integer
16
- Int = 1.class
17
-
18
14
  # do not parallelize this suite... it just can"t handle it.
19
15
 
20
16
  def assert_triggered expected = "blah", klass = Minitest::Assertion
21
17
  @assertion_count += 1
22
18
 
23
- e = assert_raises(klass) do
19
+ e = assert_raises klass do
24
20
  yield
25
21
  end
26
22
 
@@ -29,17 +25,17 @@ describe Minitest::Spec do
29
25
  msg.gsub!(/(\d\.\d{6})\d+/, '\1xxx') # normalize: ruby version, impl, platform
30
26
  msg.gsub!(/:0x[Xa-fA-F0-9]{4,}[ @].+?>/, ":0xXXXXXX@PATH>")
31
27
 
32
- if expected
28
+ return unless expected
29
+
30
+ @assertion_count += 1
31
+ case expected
32
+ when String then
33
+ assert_equal expected, msg
34
+ when Regexp then
33
35
  @assertion_count += 1
34
- case expected
35
- when String then
36
- assert_equal expected, msg
37
- when Regexp then
38
- @assertion_count += 1
39
- assert_match expected, msg
40
- else
41
- flunk "Unknown: #{expected.inspect}"
42
- end
36
+ assert_match expected, msg
37
+ else
38
+ flunk "Unknown: #{expected.inspect}"
43
39
  end
44
40
  end
45
41
 
@@ -137,6 +133,46 @@ describe Minitest::Spec do
137
133
  end
138
134
  end
139
135
 
136
+ def good_pattern
137
+ capture_io do # 3.0 is noisy
138
+ eval "[1,2,3] => [Integer, Integer, Integer]" # eval to escape parser for ruby<3
139
+ end
140
+ end
141
+
142
+ def bad_pattern
143
+ capture_io do # 3.0 is noisy
144
+ eval "[1,2,3] => [Integer, Integer]" # eval to escape parser for ruby<3
145
+ end
146
+ end
147
+
148
+ it "needs to pattern match" do
149
+ @assertion_count = 1
150
+
151
+ if RUBY_VERSION > "3" then
152
+ expect { good_pattern }.must_pattern_match
153
+ else
154
+ assert_raises NotImplementedError do
155
+ expect {}.must_pattern_match
156
+ end
157
+ end
158
+ end
159
+
160
+ it "needs to error on bad pattern match" do
161
+ skip unless RUBY_VERSION > "3"
162
+
163
+ @assertion_count = 1
164
+
165
+ exp = if RUBY_VERSION.start_with? "3.0"
166
+ "[1, 2, 3]" # terrible error message!
167
+ else
168
+ /length mismatch/
169
+ end
170
+
171
+ assert_triggered exp do
172
+ expect { bad_pattern }.must_pattern_match
173
+ end
174
+ end
175
+
140
176
  it "needs to ensure silence" do
141
177
  @assertion_count -= 1 # no msg
142
178
  @assertion_count += 2 # assert_output is 2 assertions
@@ -156,7 +192,7 @@ describe Minitest::Spec do
156
192
  methods = Minitest::Expectations.public_instance_methods.grep(/must|wont/)
157
193
  methods.map!(&:to_s) if Symbol === methods.first
158
194
 
159
- musts, wonts = methods.sort.partition { |m| m =~ /must/ }
195
+ musts, wonts = methods.sort.partition { |m| m.include? "must" }
160
196
 
161
197
  expected_musts = %w[must_be
162
198
  must_be_close_to
@@ -172,14 +208,16 @@ describe Minitest::Spec do
172
208
  must_include
173
209
  must_match
174
210
  must_output
211
+ must_pattern_match
175
212
  must_raise
176
213
  must_respond_to
177
214
  must_throw
215
+ must_verify
178
216
  path_must_exist]
179
217
 
180
- bad = %w[not raise throw send output be_silent]
218
+ bad = %w[not raise throw send output be_silent verify]
181
219
 
182
- expected_wonts = expected_musts.map { |m| m.sub(/must/, "wont") }.sort
220
+ expected_wonts = expected_musts.map { |m| m.sub("must", "wont") }.sort
183
221
  expected_wonts.reject! { |m| m =~ /wont_#{Regexp.union(*bad)}/ }
184
222
 
185
223
  _(musts).must_equal expected_musts
@@ -243,18 +281,14 @@ describe Minitest::Spec do
243
281
  end
244
282
 
245
283
  it "needs to warn on equality with nil" do
246
- @assertion_count += 1 # extra test
284
+ @assertion_count = 3
285
+ @assertion_count += 2 unless error_on_warn? # 2 extra assertions
286
+
287
+ exp = /DEPRECATED: Use assert_nil if expecting nil from .* This will fail in Minitest 6./
247
288
 
248
- out, err = capture_io do
289
+ assert_deprecation exp do
249
290
  assert_success _(nil).must_equal(nil)
250
291
  end
251
-
252
- exp = "DEPRECATED: Use assert_nil if expecting nil from #{__FILE__}:#{__LINE__-3}. " \
253
- "This will fail in Minitest 6.\n"
254
- exp = "" if $-w.nil?
255
-
256
- assert_empty out
257
- assert_equal exp, err
258
292
  end
259
293
 
260
294
  it "needs to verify floats outside a delta" do
@@ -362,12 +396,12 @@ describe Minitest::Spec do
362
396
  it "needs to verify instances of a class" do
363
397
  assert_success _(42).wont_be_instance_of(String)
364
398
 
365
- assert_triggered "Expected 42 to not be a kind of #{Int.name}." do
366
- _(42).wont_be_kind_of Int
399
+ assert_triggered "Expected 42 to not be a kind of Integer." do
400
+ _(42).wont_be_kind_of Integer
367
401
  end
368
402
 
369
- assert_triggered "msg.\nExpected 42 to not be an instance of #{Int.name}." do
370
- _(42).wont_be_instance_of Int, "msg"
403
+ assert_triggered "msg.\nExpected 42 to not be an instance of Integer." do
404
+ _(42).wont_be_instance_of Integer, "msg"
371
405
  end
372
406
  end
373
407
 
@@ -377,26 +411,26 @@ describe Minitest::Spec do
377
411
  assert_success _(42).wont_be_kind_of(String)
378
412
  assert_success _(proc {}).wont_be_kind_of(String)
379
413
 
380
- assert_triggered "Expected 42 to not be a kind of #{Int.name}." do
381
- _(42).wont_be_kind_of Int
414
+ assert_triggered "Expected 42 to not be a kind of Integer." do
415
+ _(42).wont_be_kind_of Integer
382
416
  end
383
417
 
384
- assert_triggered "msg.\nExpected 42 to not be a kind of #{Int.name}." do
385
- _(42).wont_be_kind_of Int, "msg"
418
+ assert_triggered "msg.\nExpected 42 to not be a kind of Integer." do
419
+ _(42).wont_be_kind_of Integer, "msg"
386
420
  end
387
421
  end
388
422
 
389
423
  it "needs to verify kinds of objects" do
390
424
  @assertion_count += 3 # extra test
391
425
 
392
- assert_success _(6 * 7).must_be_kind_of(Int)
426
+ assert_success _(6 * 7).must_be_kind_of(Integer)
393
427
  assert_success _(6 * 7).must_be_kind_of(Numeric)
394
428
 
395
- assert_triggered "Expected 42 to be a kind of String, not #{Int.name}." do
429
+ assert_triggered "Expected 42 to be a kind of String, not Integer." do
396
430
  _(6 * 7).must_be_kind_of String
397
431
  end
398
432
 
399
- assert_triggered "msg.\nExpected 42 to be a kind of String, not #{Int.name}." do
433
+ assert_triggered "msg.\nExpected 42 to be a kind of String, not Integer." do
400
434
  _(6 * 7).must_be_kind_of String, "msg"
401
435
  end
402
436
 
@@ -505,7 +539,7 @@ describe Minitest::Spec do
505
539
  it "needs to verify regexp matches" do
506
540
  @assertion_count += 3 # must_match is 2 assertions
507
541
 
508
- assert_success _("blah").must_match(/\w+/)
542
+ assert_kind_of MatchData, _("blah").must_match(/\w+/)
509
543
 
510
544
  assert_triggered "Expected /\\d+/ to match \"blah\"." do
511
545
  _("blah").must_match(/\d+/)
@@ -535,7 +569,8 @@ describe Minitest::Spec do
535
569
 
536
570
  it "can NOT use must_equal in a thread. It must use expect in a thread" do
537
571
  skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
538
- assert_raises RuntimeError do
572
+
573
+ assert_raises RuntimeError, Minitest::UnexpectedWarning do
539
574
  capture_io do
540
575
  Thread.new { (1 + 1).must_equal 2 }.join
541
576
  end
@@ -545,29 +580,33 @@ describe Minitest::Spec do
545
580
  it "fails gracefully when expectation used outside of `it`" do
546
581
  skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
547
582
 
548
- @assertion_count += 1
583
+ @assertion_count += 2 # assert_match is compound
549
584
 
550
- e = assert_raises RuntimeError do
585
+ e = assert_raises RuntimeError, Minitest::UnexpectedWarning do
551
586
  capture_io do
552
587
  Thread.new { # forces ctx to be nil
553
- describe("woot") do
588
+ describe "woot" do
554
589
  (1 + 1).must_equal 2
555
590
  end
556
591
  }.join
557
592
  end
558
593
  end
559
594
 
560
- assert_equal "Calling #must_equal outside of test.", e.message
595
+ exp = "Calling #must_equal outside of test."
596
+ exp = "DEPRECATED: global use of must_equal from" if error_on_warn?
597
+
598
+ assert_match exp, e.message
561
599
  end
562
600
 
563
601
  it "deprecates expectation used without _" do
564
602
  skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
565
603
 
566
- @assertion_count += 3
604
+ @assertion_count += 1
605
+ @assertion_count += 2 unless error_on_warn?
567
606
 
568
607
  exp = /DEPRECATED: global use of must_equal from/
569
608
 
570
- assert_output "", exp do
609
+ assert_deprecation exp do
571
610
  (1 + 1).must_equal 2
572
611
  end
573
612
  end
@@ -577,12 +616,13 @@ describe Minitest::Spec do
577
616
  it "deprecates expectation used without _ with empty backtrace_filter" do
578
617
  skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
579
618
 
580
- @assertion_count += 3
619
+ @assertion_count += 1
620
+ @assertion_count += 2 unless error_on_warn?
581
621
 
582
622
  exp = /DEPRECATED: global use of must_equal from/
583
623
 
584
624
  with_empty_backtrace_filter do
585
- assert_output "", exp do
625
+ assert_deprecation exp do
586
626
  (1 + 1).must_equal 2
587
627
  end
588
628
  end
@@ -590,9 +630,10 @@ describe Minitest::Spec do
590
630
  end
591
631
 
592
632
  it "needs to verify throw" do
593
- @assertion_count += 2 # 2 extra tests
633
+ @assertion_count += 4 # 2 extra tests
594
634
 
595
- assert_success expect { throw :blah }.must_throw(:blah)
635
+ assert_nil expect { throw :blah }.must_throw(:blah)
636
+ assert_equal 42, expect { throw :blah, 42 }.must_throw(:blah)
596
637
 
597
638
  assert_triggered "Expected :blah to have been thrown." do
598
639
  expect {}.must_throw :blah
@@ -612,9 +653,9 @@ describe Minitest::Spec do
612
653
  end
613
654
 
614
655
  it "needs to verify types of objects" do
615
- assert_success _(6 * 7).must_be_instance_of(Int)
656
+ assert_success _(6 * 7).must_be_instance_of(Integer)
616
657
 
617
- exp = "Expected 42 to be an instance of String, not #{Int.name}."
658
+ exp = "Expected 42 to be an instance of String, not Integer."
618
659
 
619
660
  assert_triggered exp do
620
661
  _(6 * 7).must_be_instance_of String
@@ -641,7 +682,7 @@ describe Minitest::Spec do
641
682
  assert_success _(41).must_be(:<, 42)
642
683
 
643
684
  assert_triggered "Expected 42 to be < 41." do
644
- _(42).must_be(:<, 41)
685
+ _(42).must_be :<, 41
645
686
  end
646
687
  end
647
688
 
@@ -658,11 +699,11 @@ describe Minitest::Spec do
658
699
  it "needs to verify using respond_to" do
659
700
  assert_success _(42).must_respond_to(:+)
660
701
 
661
- assert_triggered "Expected 42 (#{Int.name}) to respond to #clear." do
702
+ assert_triggered "Expected 42 (Integer) to respond to #clear." do
662
703
  _(42).must_respond_to :clear
663
704
  end
664
705
 
665
- assert_triggered "msg.\nExpected 42 (#{Int.name}) to respond to #clear." do
706
+ assert_triggered "msg.\nExpected 42 (Integer) to respond to #clear." do
666
707
  _(42).must_respond_to :clear, "msg"
667
708
  end
668
709
  end
@@ -709,9 +750,9 @@ describe Minitest::Spec, :let do
709
750
  it "doesn't raise an error if it is just another let" do
710
751
  v = proc do
711
752
  describe :outer do
712
- let(:bar)
753
+ let :bar
713
754
  describe :inner do
714
- let(:bar)
755
+ let :bar
715
756
  end
716
757
  end
717
758
  :good
@@ -743,6 +784,10 @@ describe Minitest::Spec, :subject do
743
784
  end
744
785
 
745
786
  class TestMetaStatic < Minitest::Test
787
+ def assert_method_count expected, klass
788
+ assert_equal expected, klass.public_instance_methods.grep(/^test_/).count
789
+ end
790
+
746
791
  def test_children
747
792
  Minitest::Spec.children.clear # prevents parallel run
748
793
 
@@ -776,8 +821,8 @@ class TestMetaStatic < Minitest::Test
776
821
  end
777
822
  end
778
823
 
779
- assert_equal 1, outer.public_instance_methods.grep(/^test_/).count
780
- assert_equal 1, inner.public_instance_methods.grep(/^test_/).count
824
+ assert_method_count 1, outer
825
+ assert_method_count 1, inner
781
826
  end
782
827
 
783
828
  def test_it_wont_add_test_methods_to_children
@@ -791,14 +836,18 @@ class TestMetaStatic < Minitest::Test
791
836
  end
792
837
  end
793
838
 
794
- assert_equal 1, outer.public_instance_methods.grep(/^test_/).count
795
- assert_equal 0, inner.public_instance_methods.grep(/^test_/).count
839
+ assert_method_count 1, outer
840
+ assert_method_count 0, inner
796
841
  end
797
842
  end
798
843
 
799
844
  class TestMeta < MetaMetaMetaTestCase
800
845
  # do not call parallelize_me! here because specs use register_spec_type globally
801
846
 
847
+ def assert_defined_methods expected, klass
848
+ assert_equal expected, klass.instance_methods(false).sort.map(&:to_s)
849
+ end
850
+
802
851
  def util_structure
803
852
  y = z = nil
804
853
  before_list = []
@@ -871,7 +920,7 @@ class TestMeta < MetaMetaMetaTestCase
871
920
  end
872
921
  end
873
922
 
874
- test_name = spec_class.instance_methods.sort.grep(/test/).first
923
+ test_name = spec_class.instance_methods.sort.grep(/test_/).first
875
924
 
876
925
  spec = spec_class.new test_name
877
926
 
@@ -904,6 +953,23 @@ class TestMeta < MetaMetaMetaTestCase
904
953
  assert_equal "ExampleB::random_method", spec_b.name
905
954
  end
906
955
 
956
+ def test_name_inside_class
957
+ spec_a = nil
958
+ spec_b = nil
959
+ inside_class_example = Class.new Minitest::Spec
960
+ Object.const_set :InsideClassExample, inside_class_example
961
+ inside_class_example.class_eval do
962
+ spec_a = describe "a" do
963
+ spec_b = describe "b" do; end
964
+ end
965
+ end
966
+
967
+ assert_equal "InsideClassExample::a", spec_a.name
968
+ assert_equal "InsideClassExample::a::b", spec_b.name
969
+ ensure
970
+ Object.send :remove_const, :InsideClassExample
971
+ end
972
+
907
973
  def test_structure
908
974
  x, y, z, * = util_structure
909
975
 
@@ -920,9 +986,9 @@ class TestMeta < MetaMetaMetaTestCase
920
986
  inner_methods2 = inner_methods1 +
921
987
  %w[test_0002_anonymous test_0003_anonymous]
922
988
 
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)
989
+ assert_defined_methods top_methods, x
990
+ assert_defined_methods inner_methods1, y
991
+ assert_defined_methods inner_methods2, z
926
992
  end
927
993
 
928
994
  def test_structure_postfix_it
@@ -939,8 +1005,8 @@ class TestMeta < MetaMetaMetaTestCase
939
1005
  it "inner-it" do end
940
1006
  end
941
1007
 
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)
1008
+ assert_defined_methods %w[test_0001_inner-it], y
1009
+ assert_defined_methods %w[test_0001_inner-it], z
944
1010
  end
945
1011
 
946
1012
  def test_setup_teardown_behavior
@@ -966,14 +1032,15 @@ class TestMeta < MetaMetaMetaTestCase
966
1032
  z = describe "second thingy" do end
967
1033
  end
968
1034
 
969
- test_methods = ["test_0001_top level it",
970
- "test_0002_не латинские &いった α, β, γ, δ, ε hello!!! world",
1035
+ test_methods = [
1036
+ "test_0001_top level it",
1037
+ "test_0002_не латинские &いった α, β, γ, δ, ε hello!!! world",
971
1038
  ].sort
972
1039
 
973
1040
  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/)
1041
+ assert_defined_methods test_methods, x
1042
+ assert_defined_methods [], y
1043
+ assert_defined_methods [], z
977
1044
  end
978
1045
 
979
1046
  def test_structure_subclasses
@@ -1059,3 +1126,38 @@ class ValueMonadTest < Minitest::Test
1059
1126
  assert_equal "c", struct.expect
1060
1127
  end
1061
1128
  end
1129
+
1130
+ describe Minitest::Spec, :infect_an_assertion do
1131
+ class << self
1132
+ attr_accessor :infect_mock
1133
+ end
1134
+
1135
+ def assert_infects exp, act, msg = nil, foo: nil, bar: nil
1136
+ self.class.infect_mock.assert_infects exp, act, msg, foo: foo, bar: bar
1137
+ end
1138
+
1139
+ infect_an_assertion :assert_infects, :must_infect
1140
+ infect_an_assertion :assert_infects, :must_infect_without_flipping, :dont_flip
1141
+
1142
+ it "infects assertions with kwargs" do
1143
+ mock = Minitest::Mock.new
1144
+ mock.expect :assert_infects, true, [:exp, :act, nil], foo: :foo, bar: :bar
1145
+
1146
+ self.class.infect_mock = mock
1147
+
1148
+ _(:act).must_infect :exp, foo: :foo, bar: :bar
1149
+
1150
+ assert_mock mock
1151
+ end
1152
+
1153
+ it "infects assertions with kwargs (dont_flip)" do
1154
+ mock = Minitest::Mock.new
1155
+ mock.expect :assert_infects, true, [:act, :exp, nil], foo: :foo, bar: :bar
1156
+
1157
+ self.class.infect_mock = mock
1158
+
1159
+ _(:act).must_infect_without_flipping :exp, foo: :foo, bar: :bar
1160
+
1161
+ assert_mock mock
1162
+ end
1163
+ end