minitest 5.11.3 → 5.27.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 +426 -4
- data/Manifest.txt +6 -0
- data/README.rdoc +119 -20
- data/Rakefile +28 -17
- data/design_rationale.rb +21 -19
- data/lib/hoe/minitest.rb +4 -6
- data/lib/minitest/assertions.rb +257 -100
- data/lib/minitest/autorun.rb +4 -11
- data/lib/minitest/benchmark.rb +15 -18
- data/lib/minitest/compress.rb +94 -0
- data/lib/minitest/error_on_warning.rb +11 -0
- data/lib/minitest/expectations.rb +72 -35
- data/lib/minitest/hell.rb +1 -1
- data/lib/minitest/manual_plugins.rb +16 -0
- data/lib/minitest/mock.rb +131 -44
- data/lib/minitest/parallel.rb +7 -5
- data/lib/minitest/pride.rb +1 -1
- data/lib/minitest/pride_plugin.rb +18 -25
- data/lib/minitest/spec.rb +47 -25
- data/lib/minitest/test.rb +54 -36
- data/lib/minitest/test_task.rb +324 -0
- data/lib/minitest/unit.rb +6 -9
- data/lib/minitest.rb +443 -180
- data/test/minitest/metametameta.rb +65 -17
- data/test/minitest/test_minitest_assertions.rb +1677 -0
- data/test/minitest/test_minitest_benchmark.rb +3 -3
- data/test/minitest/test_minitest_mock.rb +405 -66
- data/test/minitest/test_minitest_reporter.rb +174 -36
- data/test/minitest/test_minitest_spec.rb +368 -196
- data/test/minitest/test_minitest_test.rb +476 -1244
- data/test/minitest/test_minitest_test_task.rb +57 -0
- data.tar.gz.sig +0 -0
- metadata +50 -23
- metadata.gz.sig +0 -0
|
@@ -1,11 +1,18 @@
|
|
|
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
|
-
@mock = Minitest::Mock.new
|
|
8
|
-
|
|
13
|
+
@mock = Minitest::Mock.new
|
|
14
|
+
.expect(:foo, nil)
|
|
15
|
+
.expect(:meaning_of_life, 42)
|
|
9
16
|
end
|
|
10
17
|
|
|
11
18
|
def test_create_stub_method
|
|
@@ -19,7 +26,7 @@ class TestMinitestMock < Minitest::Test
|
|
|
19
26
|
def test_blow_up_if_not_called
|
|
20
27
|
@mock.foo
|
|
21
28
|
|
|
22
|
-
util_verify_bad "
|
|
29
|
+
util_verify_bad "Expected meaning_of_life() => 42"
|
|
23
30
|
end
|
|
24
31
|
|
|
25
32
|
def test_not_blow_up_if_everything_called
|
|
@@ -30,46 +37,44 @@ class TestMinitestMock < Minitest::Test
|
|
|
30
37
|
end
|
|
31
38
|
|
|
32
39
|
def test_allow_expectations_to_be_added_after_creation
|
|
33
|
-
@mock.expect
|
|
40
|
+
@mock.expect :bar, true
|
|
34
41
|
assert @mock.bar
|
|
35
42
|
end
|
|
36
43
|
|
|
37
44
|
def test_not_verify_if_new_expected_method_is_not_called
|
|
38
45
|
@mock.foo
|
|
39
46
|
@mock.meaning_of_life
|
|
40
|
-
@mock.expect
|
|
47
|
+
@mock.expect :bar, true
|
|
41
48
|
|
|
42
|
-
util_verify_bad "
|
|
49
|
+
util_verify_bad "Expected bar() => true"
|
|
43
50
|
end
|
|
44
51
|
|
|
45
52
|
def test_blow_up_on_wrong_number_of_arguments
|
|
46
53
|
@mock.foo
|
|
47
54
|
@mock.meaning_of_life
|
|
48
|
-
@mock.expect
|
|
55
|
+
@mock.expect :sum, 3, [1, 2]
|
|
49
56
|
|
|
50
57
|
e = assert_raises ArgumentError do
|
|
51
58
|
@mock.sum
|
|
52
59
|
end
|
|
53
60
|
|
|
54
|
-
assert_equal "mocked method :sum expects 2 arguments, got
|
|
61
|
+
assert_equal "mocked method :sum expects 2 arguments, got []", e.message
|
|
55
62
|
end
|
|
56
63
|
|
|
57
64
|
def test_return_mock_does_not_raise
|
|
58
65
|
retval = Minitest::Mock.new
|
|
59
66
|
mock = Minitest::Mock.new
|
|
60
|
-
mock.expect
|
|
67
|
+
mock.expect :foo, retval
|
|
61
68
|
mock.foo
|
|
62
69
|
|
|
63
70
|
assert_mock mock
|
|
64
71
|
end
|
|
65
72
|
|
|
66
73
|
def test_mock_args_does_not_raise
|
|
67
|
-
skip "non-opaque use of ==" if maglev?
|
|
68
|
-
|
|
69
74
|
arg = Minitest::Mock.new
|
|
70
75
|
mock = Minitest::Mock.new
|
|
71
|
-
mock.expect
|
|
72
|
-
mock.foo
|
|
76
|
+
mock.expect :foo, nil, [arg]
|
|
77
|
+
mock.foo arg
|
|
73
78
|
|
|
74
79
|
assert_mock mock
|
|
75
80
|
end
|
|
@@ -109,8 +114,6 @@ class TestMinitestMock < Minitest::Test
|
|
|
109
114
|
end
|
|
110
115
|
|
|
111
116
|
def test_expectations_can_be_satisfied_via_public_send
|
|
112
|
-
skip "Doesn't run on 1.8" if RUBY_VERSION < "1.9"
|
|
113
|
-
|
|
114
117
|
@mock.public_send :foo
|
|
115
118
|
@mock.public_send :meaning_of_life
|
|
116
119
|
|
|
@@ -120,10 +123,10 @@ class TestMinitestMock < Minitest::Test
|
|
|
120
123
|
def test_blow_up_on_wrong_arguments
|
|
121
124
|
@mock.foo
|
|
122
125
|
@mock.meaning_of_life
|
|
123
|
-
@mock.expect
|
|
126
|
+
@mock.expect :sum, 3, [1, 2]
|
|
124
127
|
|
|
125
128
|
e = assert_raises MockExpectationError do
|
|
126
|
-
@mock.sum
|
|
129
|
+
@mock.sum 2, 4
|
|
127
130
|
end
|
|
128
131
|
|
|
129
132
|
exp = "mocked method :sum called with unexpected arguments [2, 4]"
|
|
@@ -135,7 +138,7 @@ class TestMinitestMock < Minitest::Test
|
|
|
135
138
|
@mock.expect :blah, 3, false
|
|
136
139
|
end
|
|
137
140
|
|
|
138
|
-
|
|
141
|
+
assert_match "args must be an array", e.message
|
|
139
142
|
end
|
|
140
143
|
|
|
141
144
|
def test_respond_appropriately
|
|
@@ -152,15 +155,15 @@ class TestMinitestMock < Minitest::Test
|
|
|
152
155
|
|
|
153
156
|
expected = "unmocked method :bar, expected one of [:foo, :meaning_of_life]"
|
|
154
157
|
|
|
155
|
-
|
|
158
|
+
assert_match expected, e.message
|
|
156
159
|
end
|
|
157
160
|
|
|
158
161
|
def test_assign_per_mock_return_values
|
|
159
162
|
a = Minitest::Mock.new
|
|
160
163
|
b = Minitest::Mock.new
|
|
161
164
|
|
|
162
|
-
a.expect
|
|
163
|
-
b.expect
|
|
165
|
+
a.expect :foo, :a
|
|
166
|
+
b.expect :foo, :b
|
|
164
167
|
|
|
165
168
|
assert_equal :a, a.foo
|
|
166
169
|
assert_equal :b, b.foo
|
|
@@ -168,7 +171,7 @@ class TestMinitestMock < Minitest::Test
|
|
|
168
171
|
|
|
169
172
|
def test_do_not_create_stub_method_on_new_mocks
|
|
170
173
|
a = Minitest::Mock.new
|
|
171
|
-
a.expect
|
|
174
|
+
a.expect :foo, :a
|
|
172
175
|
|
|
173
176
|
assert !Minitest::Mock.new.respond_to?(:foo)
|
|
174
177
|
end
|
|
@@ -177,10 +180,77 @@ class TestMinitestMock < Minitest::Test
|
|
|
177
180
|
@mock.expect :kind_of?, true, [String]
|
|
178
181
|
@mock.expect :==, true, [1]
|
|
179
182
|
|
|
180
|
-
assert @mock.kind_of?(String), "didn't mock :kind_of
|
|
183
|
+
assert @mock.kind_of?(String), "didn't mock :kind_of?"
|
|
181
184
|
assert @mock == 1, "didn't mock :=="
|
|
182
185
|
end
|
|
183
186
|
|
|
187
|
+
def test_assert_mock__pass
|
|
188
|
+
mock = Minitest::Mock.new
|
|
189
|
+
mock.expect :loose_expectation, true, [Integer]
|
|
190
|
+
mock.loose_expectation 1
|
|
191
|
+
|
|
192
|
+
result = assert_mock mock
|
|
193
|
+
|
|
194
|
+
assert_equal true, result
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def assert_bad_mock klass, msg
|
|
198
|
+
mock = Minitest::Mock.new
|
|
199
|
+
mock.expect :foo, nil, [:bar]
|
|
200
|
+
mock.expect :foo, nil, [:baz]
|
|
201
|
+
|
|
202
|
+
mock.foo :bar
|
|
203
|
+
|
|
204
|
+
e = assert_raises klass do
|
|
205
|
+
yield mock
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
assert_equal msg, e.message
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def test_verify__error
|
|
212
|
+
exp = "Expected foo(:baz) => nil, got [foo(:bar) => nil]"
|
|
213
|
+
assert_bad_mock MockExpectationError, exp do |mock|
|
|
214
|
+
mock.verify
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def test_assert_mock__fail
|
|
219
|
+
exp = "Expected foo(:baz) => nil, got [foo(:bar) => nil]."
|
|
220
|
+
assert_bad_mock Minitest::Assertion, exp do |mock|
|
|
221
|
+
assert_mock mock
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def test_assert_mock__fail_msg
|
|
226
|
+
exp = "BLAH.\nExpected foo(:baz) => nil, got [foo(:bar) => nil]."
|
|
227
|
+
assert_bad_mock Minitest::Assertion, exp do |mock|
|
|
228
|
+
assert_mock mock, "BLAH"
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def test_assert_mock__fail_exp
|
|
233
|
+
exp = "Expected foo(:baz) => nil, got [foo(:bar) => nil]."
|
|
234
|
+
assert_bad_mock Minitest::Assertion, exp do |mock|
|
|
235
|
+
describe "X" do
|
|
236
|
+
it "y" do
|
|
237
|
+
_(mock).must_verify
|
|
238
|
+
end
|
|
239
|
+
end.new(:blah).send(:test_0001_y)
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def test_assert_mock__fail_exp_msg
|
|
244
|
+
exp = "BLAH.\nExpected foo(:baz) => nil, got [foo(:bar) => nil]."
|
|
245
|
+
assert_bad_mock Minitest::Assertion, exp do |mock|
|
|
246
|
+
describe "X" do
|
|
247
|
+
it "y" do
|
|
248
|
+
_(mock).must_verify "BLAH"
|
|
249
|
+
end
|
|
250
|
+
end.new(:blah).send(:test_0001_y)
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
|
|
184
254
|
def test_verify_allows_called_args_to_be_loosely_specified
|
|
185
255
|
mock = Minitest::Mock.new
|
|
186
256
|
mock.expect :loose_expectation, true, [Integer]
|
|
@@ -212,7 +282,7 @@ class TestMinitestMock < Minitest::Test
|
|
|
212
282
|
mock.a
|
|
213
283
|
end
|
|
214
284
|
|
|
215
|
-
assert_equal "No more expects available for :a: []", e.message
|
|
285
|
+
assert_equal "No more expects available for :a: [] {}", e.message
|
|
216
286
|
end
|
|
217
287
|
|
|
218
288
|
def test_same_method_expects_are_verified_when_all_called
|
|
@@ -235,7 +305,7 @@ class TestMinitestMock < Minitest::Test
|
|
|
235
305
|
|
|
236
306
|
e = assert_raises(MockExpectationError) { mock.verify }
|
|
237
307
|
|
|
238
|
-
exp = "
|
|
308
|
+
exp = "Expected foo(:baz) => nil, got [foo(:bar) => nil]"
|
|
239
309
|
|
|
240
310
|
assert_equal exp, e.message
|
|
241
311
|
end
|
|
@@ -249,11 +319,36 @@ class TestMinitestMock < Minitest::Test
|
|
|
249
319
|
|
|
250
320
|
e = assert_raises(MockExpectationError) { mock.verify }
|
|
251
321
|
|
|
252
|
-
exp = "
|
|
322
|
+
exp = "Expected foo(:bar) => nil, got [foo(:bar) => nil]"
|
|
253
323
|
|
|
254
324
|
assert_equal exp, e.message
|
|
255
325
|
end
|
|
256
326
|
|
|
327
|
+
def test_delegator_calls_are_propagated
|
|
328
|
+
delegator = Object.new
|
|
329
|
+
mock = Minitest::Mock.new delegator
|
|
330
|
+
|
|
331
|
+
refute delegator.nil?
|
|
332
|
+
refute mock.nil?
|
|
333
|
+
assert_mock mock
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
def test_handles_kwargs_in_error_message
|
|
337
|
+
mock = Minitest::Mock.new
|
|
338
|
+
|
|
339
|
+
mock.expect :foo, nil, [], kw: true
|
|
340
|
+
mock.expect :foo, nil, [], kw: false
|
|
341
|
+
|
|
342
|
+
mock.foo kw: true
|
|
343
|
+
|
|
344
|
+
e = assert_raises(MockExpectationError) { mock.verify }
|
|
345
|
+
|
|
346
|
+
exp = "Expected foo(%p) => nil, got [foo(%p) => nil]" \
|
|
347
|
+
% [{ :kw => false }, { :kw => true }]
|
|
348
|
+
|
|
349
|
+
assert_equal exp.delete("{}"), e.message
|
|
350
|
+
end
|
|
351
|
+
|
|
257
352
|
def test_verify_passes_when_mock_block_returns_true
|
|
258
353
|
mock = Minitest::Mock.new
|
|
259
354
|
mock.expect :foo, nil do
|
|
@@ -272,22 +367,205 @@ class TestMinitestMock < Minitest::Test
|
|
|
272
367
|
a1 == arg1 && a2 == arg2 && a3 == arg3
|
|
273
368
|
end
|
|
274
369
|
|
|
275
|
-
|
|
370
|
+
assert_silent do
|
|
371
|
+
mock.foo arg1, arg2, arg3
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
assert_mock mock
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
def test_mock_block_is_passed_keyword_args__block
|
|
378
|
+
arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
|
|
379
|
+
mock = Minitest::Mock.new
|
|
380
|
+
mock.expect :foo, nil do |k1:, k2:, k3:|
|
|
381
|
+
k1 == arg1 && k2 == arg2 && k3 == arg3
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
mock.foo k1: arg1, k2: arg2, k3: arg3
|
|
385
|
+
|
|
386
|
+
assert_mock mock
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
def test_mock_block_is_passed_keyword_args__block_bad_missing
|
|
390
|
+
arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
|
|
391
|
+
mock = Minitest::Mock.new
|
|
392
|
+
mock.expect :foo, nil do |k1:, k2:, k3:|
|
|
393
|
+
k1 == arg1 && k2 == arg2 && k3 == arg3
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
e = assert_raises ArgumentError do
|
|
397
|
+
mock.foo k1: arg1, k2: arg2
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
# basically testing ruby
|
|
401
|
+
assert_match(/missing keyword: :k3/, e.message)
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
def test_mock_block_is_passed_keyword_args__block_bad_extra
|
|
405
|
+
arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
|
|
406
|
+
mock = Minitest::Mock.new
|
|
407
|
+
mock.expect :foo, nil do |k1:, k2:|
|
|
408
|
+
k1 == arg1 && k2 == arg2 && k3 == arg3
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
e = assert_raises ArgumentError do
|
|
412
|
+
mock.foo k1: arg1, k2: arg2, k3: arg3
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
assert_match(/unknown keyword: :k3/, e.message)
|
|
416
|
+
end
|
|
417
|
+
|
|
418
|
+
def test_mock_block_is_passed_keyword_args__block_bad_value
|
|
419
|
+
arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
|
|
420
|
+
mock = Minitest::Mock.new
|
|
421
|
+
mock.expect :foo, nil do |k1:, k2:, k3:|
|
|
422
|
+
k1 == arg1 && k2 == arg2 && k3 == arg3
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
e = assert_raises MockExpectationError do
|
|
426
|
+
mock.foo k1: arg1, k2: arg2, k3: :BAD!
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
exp = "mocked method :foo failed block w/ [] %p" \
|
|
430
|
+
% [{ :k1 => :bar, :k2 => [1, 2, 3], :k3 => :BAD! }]
|
|
431
|
+
|
|
432
|
+
assert_equal exp, e.message
|
|
433
|
+
end
|
|
434
|
+
|
|
435
|
+
def test_mock_block_is_passed_keyword_args__args
|
|
436
|
+
arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
|
|
437
|
+
mock = Minitest::Mock.new
|
|
438
|
+
mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
|
|
439
|
+
|
|
440
|
+
mock.foo k1: arg1, k2: arg2, k3: arg3
|
|
276
441
|
|
|
277
442
|
assert_mock mock
|
|
278
443
|
end
|
|
279
444
|
|
|
445
|
+
def test_mock_allow_all_kwargs__old_style_env
|
|
446
|
+
with_kwargs_env do
|
|
447
|
+
mock = Minitest::Mock.new
|
|
448
|
+
mock.expect :foo, true, [Hash]
|
|
449
|
+
assert_equal true, mock.foo(bar: 42)
|
|
450
|
+
end
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
def test_mock_allow_all_kwargs__old_style_env__rewrite
|
|
454
|
+
with_kwargs_env do
|
|
455
|
+
mock = Minitest::Mock.new
|
|
456
|
+
mock.expect :foo, true, [], bar: Integer
|
|
457
|
+
assert_equal true, mock.foo(bar: 42)
|
|
458
|
+
end
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
def test_mock_block_is_passed_keyword_args__args__old_style_bad
|
|
462
|
+
arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
|
|
463
|
+
mock = Minitest::Mock.new
|
|
464
|
+
mock.expect :foo, nil, [{ k1: arg1, k2: arg2, k3: arg3 }]
|
|
465
|
+
|
|
466
|
+
e = assert_raises ArgumentError do
|
|
467
|
+
mock.foo k1: arg1, k2: arg2, k3: arg3
|
|
468
|
+
end
|
|
469
|
+
|
|
470
|
+
assert_equal "mocked method :foo expects 1 arguments, got []", e.message
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
def test_mock_block_is_passed_keyword_args__args__old_style_env
|
|
474
|
+
with_kwargs_env do
|
|
475
|
+
arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
|
|
476
|
+
mock = Minitest::Mock.new
|
|
477
|
+
mock.expect :foo, nil, [{ k1: arg1, k2: arg2, k3: arg3 }]
|
|
478
|
+
|
|
479
|
+
mock.foo k1: arg1, k2: arg2, k3: arg3
|
|
480
|
+
|
|
481
|
+
assert_mock mock
|
|
482
|
+
end
|
|
483
|
+
end
|
|
484
|
+
|
|
485
|
+
def test_mock_block_is_passed_keyword_args__args__old_style_both
|
|
486
|
+
with_kwargs_env do
|
|
487
|
+
arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
|
|
488
|
+
mock = Minitest::Mock.new
|
|
489
|
+
|
|
490
|
+
assert_deprecation(/Using MT_KWARGS_HAC. yet passing kwargs/) do
|
|
491
|
+
mock.expect :foo, nil, [{}], k1: arg1, k2: arg2, k3: arg3
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
skip "-Werror" if error_on_warn? # mock above raised, so this is dead
|
|
495
|
+
|
|
496
|
+
mock.foo({}, k1: arg1, k2: arg2, k3: arg3)
|
|
497
|
+
|
|
498
|
+
assert_mock mock
|
|
499
|
+
end
|
|
500
|
+
end
|
|
501
|
+
|
|
502
|
+
def test_mock_block_is_passed_keyword_args__args_bad_missing
|
|
503
|
+
arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
|
|
504
|
+
mock = Minitest::Mock.new
|
|
505
|
+
mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
|
|
506
|
+
|
|
507
|
+
e = assert_raises ArgumentError do
|
|
508
|
+
mock.foo k1: arg1, k2: arg2
|
|
509
|
+
end
|
|
510
|
+
|
|
511
|
+
assert_equal "mocked method :foo expects 3 keyword arguments, got %p" % { k1: arg1, k2: arg2 }, e.message
|
|
512
|
+
end
|
|
513
|
+
|
|
514
|
+
def test_mock_block_is_passed_keyword_args__args_bad_extra
|
|
515
|
+
arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
|
|
516
|
+
mock = Minitest::Mock.new
|
|
517
|
+
mock.expect :foo, nil, k1: arg1, k2: arg2
|
|
518
|
+
|
|
519
|
+
e = assert_raises ArgumentError do
|
|
520
|
+
mock.foo k1: arg1, k2: arg2, k3: arg3
|
|
521
|
+
end
|
|
522
|
+
|
|
523
|
+
assert_equal "mocked method :foo expects 2 keyword arguments, got %p" % { k1: arg1, k2: arg2, k3: arg3 }, e.message
|
|
524
|
+
end
|
|
525
|
+
|
|
526
|
+
def test_mock_block_is_passed_keyword_args__args_bad_key
|
|
527
|
+
arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
|
|
528
|
+
mock = Minitest::Mock.new
|
|
529
|
+
mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
|
|
530
|
+
|
|
531
|
+
e = assert_raises MockExpectationError do
|
|
532
|
+
mock.foo k1: arg1, k2: arg2, BAD: arg3
|
|
533
|
+
end
|
|
534
|
+
|
|
535
|
+
assert_includes e.message, "unexpected keywords [:k1, :k2, :k3]"
|
|
536
|
+
assert_includes e.message, "vs [:k1, :k2, :BAD]"
|
|
537
|
+
end
|
|
538
|
+
|
|
539
|
+
def test_mock_block_is_passed_keyword_args__args_bad_val
|
|
540
|
+
arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
|
|
541
|
+
mock = Minitest::Mock.new
|
|
542
|
+
mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
|
|
543
|
+
|
|
544
|
+
e = assert_raises MockExpectationError do
|
|
545
|
+
mock.foo k1: arg1, k2: :BAD!, k3: arg3
|
|
546
|
+
end
|
|
547
|
+
|
|
548
|
+
bad = { :k2 => :BAD! }.inspect.delete "{}"
|
|
549
|
+
assert_match(/unexpected keyword arguments.* vs .*#{bad}/, e.message)
|
|
550
|
+
end
|
|
551
|
+
|
|
280
552
|
def test_mock_block_is_passed_function_block
|
|
281
553
|
mock = Minitest::Mock.new
|
|
282
554
|
block = proc { "bar" }
|
|
283
555
|
mock.expect :foo, nil do |arg, &blk|
|
|
284
|
-
arg == "foo" &&
|
|
285
|
-
blk == block
|
|
556
|
+
arg == "foo" && blk == block
|
|
286
557
|
end
|
|
287
558
|
mock.foo "foo", &block
|
|
288
559
|
assert_mock mock
|
|
289
560
|
end
|
|
290
561
|
|
|
562
|
+
def test_mock_forward_keyword_arguments
|
|
563
|
+
mock = Minitest::Mock.new
|
|
564
|
+
mock.expect(:foo, nil) { |bar:| bar == "bar" }
|
|
565
|
+
mock.foo bar: "bar"
|
|
566
|
+
assert_mock mock
|
|
567
|
+
end
|
|
568
|
+
|
|
291
569
|
def test_verify_fails_when_mock_block_returns_false
|
|
292
570
|
mock = Minitest::Mock.new
|
|
293
571
|
mock.expect :foo, nil do
|
|
@@ -295,15 +573,15 @@ class TestMinitestMock < Minitest::Test
|
|
|
295
573
|
end
|
|
296
574
|
|
|
297
575
|
e = assert_raises(MockExpectationError) { mock.foo }
|
|
298
|
-
exp = "mocked method :foo failed block w/ []"
|
|
576
|
+
exp = "mocked method :foo failed block w/ [] {}"
|
|
299
577
|
|
|
300
578
|
assert_equal exp, e.message
|
|
301
579
|
end
|
|
302
580
|
|
|
303
|
-
def
|
|
581
|
+
def test_mock_block_raises_if_args_passed
|
|
304
582
|
mock = Minitest::Mock.new
|
|
305
583
|
|
|
306
|
-
e = assert_raises
|
|
584
|
+
e = assert_raises ArgumentError do
|
|
307
585
|
mock.expect :foo, nil, [:a, :b, :c] do
|
|
308
586
|
true
|
|
309
587
|
end
|
|
@@ -311,12 +589,26 @@ class TestMinitestMock < Minitest::Test
|
|
|
311
589
|
|
|
312
590
|
exp = "args ignored when block given"
|
|
313
591
|
|
|
314
|
-
|
|
592
|
+
assert_match exp, e.message
|
|
593
|
+
end
|
|
594
|
+
|
|
595
|
+
def test_mock_block_raises_if_kwargs_passed
|
|
596
|
+
mock = Minitest::Mock.new
|
|
597
|
+
|
|
598
|
+
e = assert_raises ArgumentError do
|
|
599
|
+
mock.expect :foo, nil, kwargs: 1 do
|
|
600
|
+
true
|
|
601
|
+
end
|
|
602
|
+
end
|
|
603
|
+
|
|
604
|
+
exp = "kwargs ignored when block given"
|
|
605
|
+
|
|
606
|
+
assert_match exp, e.message
|
|
315
607
|
end
|
|
316
608
|
|
|
317
609
|
def test_mock_returns_retval_when_called_with_block
|
|
318
610
|
mock = Minitest::Mock.new
|
|
319
|
-
mock.expect
|
|
611
|
+
mock.expect :foo, 32 do
|
|
320
612
|
true
|
|
321
613
|
end
|
|
322
614
|
|
|
@@ -335,7 +627,7 @@ class TestMinitestMock < Minitest::Test
|
|
|
335
627
|
|
|
336
628
|
def test_mock_called_via_send
|
|
337
629
|
mock = Minitest::Mock.new
|
|
338
|
-
mock.expect
|
|
630
|
+
mock.expect :foo, true
|
|
339
631
|
|
|
340
632
|
mock.send :foo
|
|
341
633
|
assert_mock mock
|
|
@@ -343,7 +635,7 @@ class TestMinitestMock < Minitest::Test
|
|
|
343
635
|
|
|
344
636
|
def test_mock_called_via___send__
|
|
345
637
|
mock = Minitest::Mock.new
|
|
346
|
-
mock.expect
|
|
638
|
+
mock.expect :foo, true
|
|
347
639
|
|
|
348
640
|
mock.__send__ :foo
|
|
349
641
|
assert_mock mock
|
|
@@ -351,18 +643,18 @@ class TestMinitestMock < Minitest::Test
|
|
|
351
643
|
|
|
352
644
|
def test_mock_called_via_send_with_args
|
|
353
645
|
mock = Minitest::Mock.new
|
|
354
|
-
mock.expect
|
|
646
|
+
mock.expect :foo, true, [1, 2, 3]
|
|
355
647
|
|
|
356
|
-
mock.send
|
|
648
|
+
mock.send :foo, 1, 2, 3
|
|
357
649
|
assert_mock mock
|
|
358
650
|
end
|
|
359
651
|
|
|
360
652
|
end
|
|
361
653
|
|
|
362
|
-
|
|
654
|
+
require_relative "metametameta"
|
|
363
655
|
|
|
364
656
|
class TestMinitestStub < Minitest::Test
|
|
365
|
-
|
|
657
|
+
# Do not parallelize since we're calling stub on class methods
|
|
366
658
|
|
|
367
659
|
def setup
|
|
368
660
|
super
|
|
@@ -374,7 +666,7 @@ class TestMinitestStub < Minitest::Test
|
|
|
374
666
|
|
|
375
667
|
def teardown
|
|
376
668
|
super
|
|
377
|
-
assert_equal @assertion_count, @tc.assertions
|
|
669
|
+
assert_equal @assertion_count, @tc.assertions if self.passed?
|
|
378
670
|
end
|
|
379
671
|
|
|
380
672
|
class Time
|
|
@@ -429,7 +721,7 @@ class TestMinitestStub < Minitest::Test
|
|
|
429
721
|
end
|
|
430
722
|
end
|
|
431
723
|
|
|
432
|
-
def
|
|
724
|
+
def test_stub_value__literal
|
|
433
725
|
assert_stub 42
|
|
434
726
|
end
|
|
435
727
|
|
|
@@ -460,7 +752,7 @@ class TestMinitestStub < Minitest::Test
|
|
|
460
752
|
end
|
|
461
753
|
|
|
462
754
|
def test_stub_yield_self
|
|
463
|
-
obj = "foo"
|
|
755
|
+
obj = +"foo"
|
|
464
756
|
|
|
465
757
|
val = obj.stub :to_s, "bar" do |s|
|
|
466
758
|
s.to_s
|
|
@@ -486,7 +778,7 @@ class TestMinitestStub < Minitest::Test
|
|
|
486
778
|
end
|
|
487
779
|
end
|
|
488
780
|
|
|
489
|
-
val = dynamic.stub
|
|
781
|
+
val = dynamic.stub :found, true do |s|
|
|
490
782
|
s.found
|
|
491
783
|
end
|
|
492
784
|
|
|
@@ -501,13 +793,17 @@ class TestMinitestStub < Minitest::Test
|
|
|
501
793
|
end
|
|
502
794
|
end
|
|
503
795
|
|
|
504
|
-
exp =
|
|
796
|
+
exp = if jruby? then
|
|
797
|
+
/Undefined method nope_nope_nope for '#{self.class}::Time'/
|
|
798
|
+
else
|
|
799
|
+
/undefined method [`']nope_nope_nope' for( class)? [`']#{self.class}::Time'/
|
|
800
|
+
end
|
|
505
801
|
assert_match exp, e.message
|
|
506
802
|
end
|
|
507
803
|
|
|
508
804
|
def test_mock_with_yield
|
|
509
805
|
mock = Minitest::Mock.new
|
|
510
|
-
mock.expect
|
|
806
|
+
mock.expect :write, true do
|
|
511
807
|
true
|
|
512
808
|
end
|
|
513
809
|
rs = nil
|
|
@@ -520,7 +816,18 @@ class TestMinitestStub < Minitest::Test
|
|
|
520
816
|
@tc.assert_equal true, rs
|
|
521
817
|
end
|
|
522
818
|
|
|
523
|
-
|
|
819
|
+
def test_mock_with_yield_kwargs
|
|
820
|
+
mock = Minitest::Mock.new
|
|
821
|
+
rs = nil
|
|
822
|
+
|
|
823
|
+
File.stub :open, true, mock, kw: 42 do
|
|
824
|
+
File.open "foo.txt", "r" do |f, kw:|
|
|
825
|
+
rs = kw
|
|
826
|
+
end
|
|
827
|
+
end
|
|
828
|
+
|
|
829
|
+
@tc.assert_equal 42, rs
|
|
830
|
+
end
|
|
524
831
|
|
|
525
832
|
## Permutation Sets:
|
|
526
833
|
|
|
@@ -576,7 +883,7 @@ class TestMinitestStub < Minitest::Test
|
|
|
576
883
|
# [:value, :block_call, :args] => N/A
|
|
577
884
|
|
|
578
885
|
class Bar
|
|
579
|
-
def call
|
|
886
|
+
def call &_ # to ignore unused block
|
|
580
887
|
puts "hi"
|
|
581
888
|
end
|
|
582
889
|
end
|
|
@@ -593,6 +900,38 @@ class TestMinitestStub < Minitest::Test
|
|
|
593
900
|
end
|
|
594
901
|
end
|
|
595
902
|
|
|
903
|
+
class Keywords
|
|
904
|
+
def self.args req, kw1:, kw2: 24
|
|
905
|
+
[req, kw1, kw2]
|
|
906
|
+
end
|
|
907
|
+
end
|
|
908
|
+
|
|
909
|
+
def test_stub_callable_keyword_args
|
|
910
|
+
Keywords.stub :args, ->(*args, **kws) { [args, kws] } do
|
|
911
|
+
@tc.assert_equal [["woot"], { kw1: 42 }], Keywords.args("woot", kw1: 42)
|
|
912
|
+
end
|
|
913
|
+
end
|
|
914
|
+
|
|
915
|
+
def test_stub__hash_as_last_real_arg
|
|
916
|
+
with_kwargs_env do
|
|
917
|
+
token = Object.new
|
|
918
|
+
def token.create_with_retry _u, _p; raise "shouldn't see this"; end
|
|
919
|
+
|
|
920
|
+
controller = Object.new
|
|
921
|
+
controller.define_singleton_method :create do |u, p|
|
|
922
|
+
token.create_with_retry u, p
|
|
923
|
+
end
|
|
924
|
+
|
|
925
|
+
params = Object.new
|
|
926
|
+
def params.to_hash; raise "nah"; end
|
|
927
|
+
|
|
928
|
+
token.stub(:create_with_retry, ->(u, p) { 42 }) do
|
|
929
|
+
act = controller.create :u, params
|
|
930
|
+
@tc.assert_equal 42, act
|
|
931
|
+
end
|
|
932
|
+
end
|
|
933
|
+
end
|
|
934
|
+
|
|
596
935
|
def test_stub_callable_block_5 # from tenderlove
|
|
597
936
|
@assertion_count += 1
|
|
598
937
|
Foo.stub5 :blocking, Bar.new do
|
|
@@ -684,7 +1023,7 @@ class TestMinitestStub < Minitest::Test
|
|
|
684
1023
|
def test_stub_lambda_block_call_5
|
|
685
1024
|
@assertion_count += 1
|
|
686
1025
|
rs = nil
|
|
687
|
-
io = StringIO.new
|
|
1026
|
+
io = StringIO.new(+"", "w")
|
|
688
1027
|
File.stub5 :open, lambda { |p, m, &blk| blk and blk.call io } do
|
|
689
1028
|
File.open "foo.txt", "r" do |f|
|
|
690
1029
|
rs = f && f.write("woot")
|
|
@@ -699,10 +1038,10 @@ class TestMinitestStub < Minitest::Test
|
|
|
699
1038
|
|
|
700
1039
|
@assertion_count += 1
|
|
701
1040
|
rs = nil
|
|
702
|
-
io = StringIO.new
|
|
1041
|
+
io = StringIO.new(+"", "w")
|
|
703
1042
|
File.stub6 :open, lambda { |p, m, &blk| blk.call io } do
|
|
704
1043
|
File.open "foo.txt", "r" do |f|
|
|
705
|
-
rs = f.write
|
|
1044
|
+
rs = f.write "woot"
|
|
706
1045
|
end
|
|
707
1046
|
end
|
|
708
1047
|
@tc.assert_equal 4, rs
|
|
@@ -712,10 +1051,10 @@ class TestMinitestStub < Minitest::Test
|
|
|
712
1051
|
def test_stub_lambda_block_call_args_5
|
|
713
1052
|
@assertion_count += 1
|
|
714
1053
|
rs = nil
|
|
715
|
-
io = StringIO.new
|
|
1054
|
+
io = StringIO.new(+"", "w")
|
|
716
1055
|
File.stub5(:open, lambda { |p, m, &blk| blk and blk.call io }, :WTF?) do
|
|
717
1056
|
File.open "foo.txt", "r" do |f|
|
|
718
|
-
rs = f.write
|
|
1057
|
+
rs = f.write "woot"
|
|
719
1058
|
end
|
|
720
1059
|
end
|
|
721
1060
|
@tc.assert_equal 4, rs
|
|
@@ -727,10 +1066,10 @@ class TestMinitestStub < Minitest::Test
|
|
|
727
1066
|
|
|
728
1067
|
@assertion_count += 1
|
|
729
1068
|
rs = nil
|
|
730
|
-
io = StringIO.new
|
|
1069
|
+
io = StringIO.new(+"", "w")
|
|
731
1070
|
File.stub6(:open, lambda { |p, m, &blk| blk.call io }, :WTF?) do
|
|
732
1071
|
File.open "foo.txt", "r" do |f|
|
|
733
|
-
rs = f.write
|
|
1072
|
+
rs = f.write "woot"
|
|
734
1073
|
end
|
|
735
1074
|
end
|
|
736
1075
|
@tc.assert_equal 4, rs
|
|
@@ -742,11 +1081,11 @@ class TestMinitestStub < Minitest::Test
|
|
|
742
1081
|
|
|
743
1082
|
@assertion_count += 2
|
|
744
1083
|
rs = nil
|
|
745
|
-
io = StringIO.new
|
|
1084
|
+
io = StringIO.new(+"", "w")
|
|
746
1085
|
@tc.assert_raises ArgumentError do
|
|
747
1086
|
File.stub6_2(:open, lambda { |p, m, &blk| blk.call io }, :WTF?) do
|
|
748
1087
|
File.open "foo.txt", "r" do |f|
|
|
749
|
-
rs = f.write
|
|
1088
|
+
rs = f.write "woot"
|
|
750
1089
|
end
|
|
751
1090
|
end
|
|
752
1091
|
end
|
|
@@ -792,10 +1131,10 @@ class TestMinitestStub < Minitest::Test
|
|
|
792
1131
|
def test_stub_value_block_args_5
|
|
793
1132
|
@assertion_count += 2
|
|
794
1133
|
rs = nil
|
|
795
|
-
io = StringIO.new
|
|
1134
|
+
io = StringIO.new(+"", "w")
|
|
796
1135
|
File.stub5 :open, :value, io do
|
|
797
1136
|
result = File.open "foo.txt", "r" do |f|
|
|
798
|
-
rs = f.write
|
|
1137
|
+
rs = f.write "woot"
|
|
799
1138
|
end
|
|
800
1139
|
@tc.assert_equal :value, result
|
|
801
1140
|
end
|
|
@@ -811,8 +1150,8 @@ class TestMinitestStub < Minitest::Test
|
|
|
811
1150
|
end
|
|
812
1151
|
end
|
|
813
1152
|
end
|
|
814
|
-
exp =
|
|
815
|
-
|
|
1153
|
+
exp = /undefined method [`']write' for nil/
|
|
1154
|
+
assert_match exp, e.message
|
|
816
1155
|
end
|
|
817
1156
|
|
|
818
1157
|
def test_stub_value_block_args_6
|
|
@@ -820,11 +1159,11 @@ class TestMinitestStub < Minitest::Test
|
|
|
820
1159
|
|
|
821
1160
|
@assertion_count += 2
|
|
822
1161
|
rs = nil
|
|
823
|
-
io = StringIO.new
|
|
1162
|
+
io = StringIO.new(+"", "w")
|
|
824
1163
|
assert_deprecated do
|
|
825
1164
|
File.stub6 :open, :value, io do
|
|
826
1165
|
result = File.open "foo.txt", "r" do |f|
|
|
827
|
-
rs = f.write
|
|
1166
|
+
rs = f.write "woot"
|
|
828
1167
|
end
|
|
829
1168
|
@tc.assert_equal :value, result
|
|
830
1169
|
end
|
|
@@ -838,7 +1177,7 @@ class TestMinitestStub < Minitest::Test
|
|
|
838
1177
|
|
|
839
1178
|
@assertion_count += 2
|
|
840
1179
|
rs = nil
|
|
841
|
-
io = StringIO.new
|
|
1180
|
+
io = StringIO.new(+"", "w")
|
|
842
1181
|
@tc.assert_raises ArgumentError do
|
|
843
1182
|
File.stub6_2 :open, :value, io do
|
|
844
1183
|
result = File.open "foo.txt", "r" do |f|
|