minitest 5.14.0 → 5.17.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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/History.rdoc +117 -4
- data/Manifest.txt +2 -0
- data/README.rdoc +78 -11
- data/Rakefile +4 -2
- data/lib/hoe/minitest.rb +0 -4
- data/lib/minitest/assertions.rb +8 -7
- data/lib/minitest/benchmark.rb +5 -5
- data/lib/minitest/expectations.rb +29 -29
- data/lib/minitest/mock.rb +119 -32
- data/lib/minitest/pride_plugin.rb +1 -1
- data/lib/minitest/spec.rb +9 -2
- data/lib/minitest/test.rb +42 -6
- data/lib/minitest/test_task.rb +305 -0
- data/lib/minitest/unit.rb +5 -8
- data/lib/minitest.rb +51 -26
- data/test/minitest/metametameta.rb +20 -1
- data/test/minitest/test_minitest_assertions.rb +49 -25
- data/test/minitest/test_minitest_benchmark.rb +2 -2
- data/test/minitest/test_minitest_mock.rb +286 -12
- data/test/minitest/test_minitest_reporter.rb +30 -17
- data/test/minitest/test_minitest_spec.rb +114 -50
- data/test/minitest/test_minitest_test.rb +225 -33
- data/test/minitest/test_minitest_test_task.rb +46 -0
- data.tar.gz.sig +0 -0
- metadata +23 -18
- metadata.gz.sig +0 -0
@@ -28,6 +28,9 @@ class TestMinitestAssertions < Minitest::Test
|
|
28
28
|
|
29
29
|
RUBY18 = !defined? Encoding
|
30
30
|
|
31
|
+
# not included in JRuby
|
32
|
+
RE_LEVELS = /\(\d+ levels\) /
|
33
|
+
|
31
34
|
class DummyTest
|
32
35
|
include Minitest::Assertions
|
33
36
|
# include Minitest::Reportable # TODO: why do I really need this?
|
@@ -351,7 +354,7 @@ class TestMinitestAssertions < Minitest::Test
|
|
351
354
|
@@ -1,3 +1,3 @@
|
352
355
|
-# encoding: UTF-8
|
353
356
|
-# valid: false
|
354
|
-
+# encoding:
|
357
|
+
+# encoding: #{Encoding::BINARY.name}
|
355
358
|
+# valid: true
|
356
359
|
"bad-utf8-\\xF1.txt"
|
357
360
|
EOM
|
@@ -370,7 +373,7 @@ class TestMinitestAssertions < Minitest::Test
|
|
370
373
|
@@ -1,3 +1,3 @@
|
371
374
|
-# encoding: US-ASCII
|
372
375
|
-# valid: false
|
373
|
-
+# encoding:
|
376
|
+
+# encoding: #{Encoding::BINARY.name}
|
374
377
|
+# valid: true
|
375
378
|
"bad-utf8-\\xF1.txt"
|
376
379
|
EOM
|
@@ -481,7 +484,10 @@ class TestMinitestAssertions < Minitest::Test
|
|
481
484
|
|
482
485
|
def test_assert_match
|
483
486
|
@assertion_count = 2
|
484
|
-
@tc.assert_match(/\w+/, "blah blah blah")
|
487
|
+
m = @tc.assert_match(/\w+/, "blah blah blah")
|
488
|
+
|
489
|
+
assert_kind_of MatchData, m
|
490
|
+
assert_equal "blah", m[0]
|
485
491
|
end
|
486
492
|
|
487
493
|
def test_assert_match_matchee_to_str
|
@@ -756,12 +762,12 @@ class TestMinitestAssertions < Minitest::Test
|
|
756
762
|
Class: <SomeError>
|
757
763
|
Message: <\"blah\">
|
758
764
|
---Backtrace---
|
759
|
-
FILE:LINE:in \`test_assert_raises_default_triggered\'
|
765
|
+
FILE:LINE:in \`block in test_assert_raises_default_triggered\'
|
760
766
|
---------------
|
761
767
|
EOM
|
762
768
|
|
763
769
|
actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
|
764
|
-
actual.gsub!(
|
770
|
+
actual.gsub!(RE_LEVELS, "") unless jruby?
|
765
771
|
|
766
772
|
assert_equal expected, actual
|
767
773
|
end
|
@@ -801,7 +807,7 @@ class TestMinitestAssertions < Minitest::Test
|
|
801
807
|
# *sigh* This is quite an odd scenario, but it is from real (albeit
|
802
808
|
# ugly) test code in ruby-core:
|
803
809
|
|
804
|
-
#
|
810
|
+
# https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29259
|
805
811
|
|
806
812
|
def test_assert_raises_skip
|
807
813
|
@assertion_count = 0
|
@@ -835,12 +841,12 @@ class TestMinitestAssertions < Minitest::Test
|
|
835
841
|
Class: <AnError>
|
836
842
|
Message: <\"some message\">
|
837
843
|
---Backtrace---
|
838
|
-
FILE:LINE:in \`test_assert_raises_subclass_triggered\'
|
844
|
+
FILE:LINE:in \`block in test_assert_raises_subclass_triggered\'
|
839
845
|
---------------
|
840
846
|
EOM
|
841
847
|
|
842
848
|
actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
|
843
|
-
actual.gsub!(
|
849
|
+
actual.gsub!(RE_LEVELS, "") unless jruby?
|
844
850
|
|
845
851
|
assert_equal expected.chomp, actual
|
846
852
|
end
|
@@ -857,12 +863,12 @@ class TestMinitestAssertions < Minitest::Test
|
|
857
863
|
Class: <SyntaxError>
|
858
864
|
Message: <\"icky\">
|
859
865
|
---Backtrace---
|
860
|
-
FILE:LINE:in \`test_assert_raises_triggered_different\'
|
866
|
+
FILE:LINE:in \`block in test_assert_raises_triggered_different\'
|
861
867
|
---------------
|
862
868
|
EOM
|
863
869
|
|
864
870
|
actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
|
865
|
-
actual.gsub!(
|
871
|
+
actual.gsub!(RE_LEVELS, "") unless jruby?
|
866
872
|
|
867
873
|
assert_equal expected, actual
|
868
874
|
end
|
@@ -880,12 +886,12 @@ class TestMinitestAssertions < Minitest::Test
|
|
880
886
|
Class: <SyntaxError>
|
881
887
|
Message: <\"icky\">
|
882
888
|
---Backtrace---
|
883
|
-
FILE:LINE:in \`test_assert_raises_triggered_different_msg\'
|
889
|
+
FILE:LINE:in \`block in test_assert_raises_triggered_different_msg\'
|
884
890
|
---------------
|
885
891
|
EOM
|
886
892
|
|
887
893
|
actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
|
888
|
-
actual.gsub!(
|
894
|
+
actual.gsub!(RE_LEVELS, "") unless jruby?
|
889
895
|
|
890
896
|
assert_equal expected.chomp, actual
|
891
897
|
end
|
@@ -995,9 +1001,19 @@ class TestMinitestAssertions < Minitest::Test
|
|
995
1001
|
end
|
996
1002
|
|
997
1003
|
def test_assert_throws
|
998
|
-
@tc.assert_throws :blah do
|
1004
|
+
v = @tc.assert_throws :blah do
|
999
1005
|
throw :blah
|
1000
1006
|
end
|
1007
|
+
|
1008
|
+
assert_nil v
|
1009
|
+
end
|
1010
|
+
|
1011
|
+
def test_assert_throws_value
|
1012
|
+
v = @tc.assert_throws :blah do
|
1013
|
+
throw :blah, 42
|
1014
|
+
end
|
1015
|
+
|
1016
|
+
assert_equal 42, v
|
1001
1017
|
end
|
1002
1018
|
|
1003
1019
|
def test_assert_throws_argument_exception
|
@@ -1118,16 +1134,20 @@ class TestMinitestAssertions < Minitest::Test
|
|
1118
1134
|
end
|
1119
1135
|
end
|
1120
1136
|
|
1137
|
+
def assert_fail_after t
|
1138
|
+
@tc.fail_after t.year, t.month, t.day, "remove the deprecations"
|
1139
|
+
end
|
1140
|
+
|
1121
1141
|
def test_fail_after
|
1122
|
-
|
1123
|
-
|
1142
|
+
d0 = Time.now
|
1143
|
+
d1 = d0 + 86_400 # I am an idiot
|
1124
1144
|
|
1125
1145
|
assert_silent do
|
1126
|
-
|
1146
|
+
assert_fail_after d1
|
1127
1147
|
end
|
1128
1148
|
|
1129
1149
|
assert_triggered "remove the deprecations" do
|
1130
|
-
|
1150
|
+
assert_fail_after d0
|
1131
1151
|
end
|
1132
1152
|
end
|
1133
1153
|
|
@@ -1150,7 +1170,7 @@ class TestMinitestAssertions < Minitest::Test
|
|
1150
1170
|
def test_refute
|
1151
1171
|
@assertion_count = 2
|
1152
1172
|
|
1153
|
-
@tc.assert_equal
|
1173
|
+
@tc.assert_equal true, @tc.refute(false), "returns true on success"
|
1154
1174
|
end
|
1155
1175
|
|
1156
1176
|
def test_refute_empty
|
@@ -1342,18 +1362,22 @@ class TestMinitestAssertions < Minitest::Test
|
|
1342
1362
|
end
|
1343
1363
|
end
|
1344
1364
|
|
1365
|
+
def assert_skip_until t, msg
|
1366
|
+
@tc.skip_until t.year, t.month, t.day, msg
|
1367
|
+
end
|
1368
|
+
|
1345
1369
|
def test_skip_until
|
1346
1370
|
@assertion_count = 0
|
1347
1371
|
|
1348
|
-
|
1349
|
-
|
1372
|
+
d0 = Time.now
|
1373
|
+
d1 = d0 + 86_400 # I am an idiot
|
1350
1374
|
|
1351
1375
|
assert_output "", /Stale skip_until \"not yet\" at .*?:\d+$/ do
|
1352
|
-
|
1376
|
+
assert_skip_until d0, "not yet"
|
1353
1377
|
end
|
1354
1378
|
|
1355
1379
|
assert_triggered "not ready yet", Minitest::Skip do
|
1356
|
-
|
1380
|
+
assert_skip_until d1, "not ready yet"
|
1357
1381
|
end
|
1358
1382
|
end
|
1359
1383
|
|
@@ -1507,14 +1531,14 @@ class TestMinitestAssertionHelpers < Minitest::Test
|
|
1507
1531
|
|
1508
1532
|
def test_mu_pp_for_diff_str_encoding
|
1509
1533
|
str = "A\nB".b
|
1510
|
-
exp = "# encoding:
|
1534
|
+
exp = "# encoding: #{Encoding::BINARY.name}\n# valid: true\n\"A\nB\""
|
1511
1535
|
|
1512
1536
|
assert_mu_pp_for_diff exp, str, :raw
|
1513
1537
|
end
|
1514
1538
|
|
1515
1539
|
def test_mu_pp_for_diff_str_encoding_both
|
1516
1540
|
str = "A\\n\nB".b
|
1517
|
-
exp = "# encoding:
|
1541
|
+
exp = "# encoding: #{Encoding::BINARY.name}\n# valid: true\n\"A\\\\n\\nB\""
|
1518
1542
|
|
1519
1543
|
assert_mu_pp_for_diff exp, str, :raw
|
1520
1544
|
end
|
@@ -1554,7 +1578,7 @@ class TestMinitestAssertionHelpers < Minitest::Test
|
|
1554
1578
|
|
1555
1579
|
def test_mu_pp_str_encoding
|
1556
1580
|
str = "A\nB".b
|
1557
|
-
exp = "# encoding:
|
1581
|
+
exp = "# encoding: #{Encoding::BINARY.name}\n# valid: true\n\"A\\nB\""
|
1558
1582
|
|
1559
1583
|
assert_mu_pp exp, str, :raw
|
1560
1584
|
end
|
@@ -3,7 +3,7 @@ require "minitest/benchmark"
|
|
3
3
|
|
4
4
|
##
|
5
5
|
# Used to verify data:
|
6
|
-
#
|
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
|
-
#
|
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,8 +1,14 @@
|
|
1
1
|
require "minitest/autorun"
|
2
2
|
|
3
|
-
|
4
|
-
|
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
|
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
|
@@ -133,7 +139,7 @@ class TestMinitestMock < Minitest::Test
|
|
133
139
|
@mock.expect :blah, 3, false
|
134
140
|
end
|
135
141
|
|
136
|
-
|
142
|
+
assert_match "args must be an array", e.message
|
137
143
|
end
|
138
144
|
|
139
145
|
def test_respond_appropriately
|
@@ -150,7 +156,7 @@ class TestMinitestMock < Minitest::Test
|
|
150
156
|
|
151
157
|
expected = "unmocked method :bar, expected one of [:foo, :meaning_of_life]"
|
152
158
|
|
153
|
-
|
159
|
+
assert_match expected, e.message
|
154
160
|
end
|
155
161
|
|
156
162
|
def test_assign_per_mock_return_values
|
@@ -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
|
-
|
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
|
274
310
|
|
275
311
|
assert_mock mock
|
276
312
|
end
|
277
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)
|
377
|
+
|
378
|
+
assert_mock mock
|
379
|
+
end
|
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
|
515
|
+
def test_mock_block_raises_if_args_passed
|
302
516
|
mock = Minitest::Mock.new
|
303
517
|
|
304
518
|
e = assert_raises(ArgumentError) do
|
@@ -309,7 +523,21 @@ class TestMinitestMock < Minitest::Test
|
|
309
523
|
|
310
524
|
exp = "args ignored when block given"
|
311
525
|
|
312
|
-
|
526
|
+
assert_match exp, e.message
|
527
|
+
end
|
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
|
313
541
|
end
|
314
542
|
|
315
543
|
def test_mock_returns_retval_when_called_with_block
|
@@ -499,7 +727,8 @@ class TestMinitestStub < Minitest::Test
|
|
499
727
|
end
|
500
728
|
end
|
501
729
|
|
502
|
-
exp = /
|
730
|
+
exp = jruby? ? /Undefined method nope_nope_nope for '#{self.class}::Time'/ :
|
731
|
+
/undefined method `nope_nope_nope' for( class)? `#{self.class}::Time'/
|
503
732
|
assert_match exp, e.message
|
504
733
|
end
|
505
734
|
|
@@ -518,6 +747,19 @@ class TestMinitestStub < Minitest::Test
|
|
518
747
|
@tc.assert_equal true, rs
|
519
748
|
end
|
520
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
|
+
|
521
763
|
alias test_stub_value__old test_stub_value # TODO: remove/rename
|
522
764
|
|
523
765
|
## Permutation Sets:
|
@@ -591,6 +833,38 @@ class TestMinitestStub < Minitest::Test
|
|
591
833
|
end
|
592
834
|
end
|
593
835
|
|
836
|
+
class Keywords
|
837
|
+
def self.args req, kw1:, kw2:24
|
838
|
+
[req, kw1, kw2]
|
839
|
+
end
|
840
|
+
end
|
841
|
+
|
842
|
+
def test_stub_callable_keyword_args
|
843
|
+
Keywords.stub :args, ->(*args, **kws) { [args, kws] } do
|
844
|
+
@tc.assert_equal [["woot"], { kw1: 42 }], Keywords.args("woot", kw1: 42)
|
845
|
+
end
|
846
|
+
end
|
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
|
+
|
594
868
|
def test_stub_callable_block_5 # from tenderlove
|
595
869
|
@assertion_count += 1
|
596
870
|
Foo.stub5 :blocking, Bar.new do
|
@@ -810,7 +1084,7 @@ class TestMinitestStub < Minitest::Test
|
|
810
1084
|
end
|
811
1085
|
end
|
812
1086
|
exp = "undefined method `write' for nil:NilClass"
|
813
|
-
|
1087
|
+
assert_match exp, e.message
|
814
1088
|
end
|
815
1089
|
|
816
1090
|
def test_stub_value_block_args_6
|