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,11 +1,18 @@
1
1
  require "minitest/autorun"
2
2
 
3
- class TestMinitestMock < Minitest::Test
4
- parallelize_me!
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.expect(:foo, nil)
8
- @mock.expect(:meaning_of_life, 42)
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 "expected meaning_of_life() => 42"
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,34 +37,34 @@ class TestMinitestMock < Minitest::Test
30
37
  end
31
38
 
32
39
  def test_allow_expectations_to_be_added_after_creation
33
- @mock.expect(:bar, true)
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(:bar, true)
47
+ @mock.expect :bar, true
41
48
 
42
- util_verify_bad "expected bar() => true"
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(:sum, 3, [1, 2])
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 0", e.message
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(:foo, retval)
67
+ mock.expect :foo, retval
61
68
  mock.foo
62
69
 
63
70
  assert_mock mock
@@ -66,8 +73,8 @@ class TestMinitestMock < Minitest::Test
66
73
  def test_mock_args_does_not_raise
67
74
  arg = Minitest::Mock.new
68
75
  mock = Minitest::Mock.new
69
- mock.expect(:foo, nil, [arg])
70
- mock.foo(arg)
76
+ mock.expect :foo, nil, [arg]
77
+ mock.foo arg
71
78
 
72
79
  assert_mock mock
73
80
  end
@@ -107,8 +114,6 @@ class TestMinitestMock < Minitest::Test
107
114
  end
108
115
 
109
116
  def test_expectations_can_be_satisfied_via_public_send
110
- skip "Doesn't run on 1.8" if RUBY_VERSION < "1.9"
111
-
112
117
  @mock.public_send :foo
113
118
  @mock.public_send :meaning_of_life
114
119
 
@@ -118,10 +123,10 @@ class TestMinitestMock < Minitest::Test
118
123
  def test_blow_up_on_wrong_arguments
119
124
  @mock.foo
120
125
  @mock.meaning_of_life
121
- @mock.expect(:sum, 3, [1, 2])
126
+ @mock.expect :sum, 3, [1, 2]
122
127
 
123
128
  e = assert_raises MockExpectationError do
124
- @mock.sum(2, 4)
129
+ @mock.sum 2, 4
125
130
  end
126
131
 
127
132
  exp = "mocked method :sum called with unexpected arguments [2, 4]"
@@ -133,7 +138,7 @@ class TestMinitestMock < Minitest::Test
133
138
  @mock.expect :blah, 3, false
134
139
  end
135
140
 
136
- assert_equal "args must be an array", e.message
141
+ assert_match "args must be an array", e.message
137
142
  end
138
143
 
139
144
  def test_respond_appropriately
@@ -150,15 +155,15 @@ class TestMinitestMock < Minitest::Test
150
155
 
151
156
  expected = "unmocked method :bar, expected one of [:foo, :meaning_of_life]"
152
157
 
153
- assert_equal expected, e.message
158
+ assert_match expected, e.message
154
159
  end
155
160
 
156
161
  def test_assign_per_mock_return_values
157
162
  a = Minitest::Mock.new
158
163
  b = Minitest::Mock.new
159
164
 
160
- a.expect(:foo, :a)
161
- b.expect(:foo, :b)
165
+ a.expect :foo, :a
166
+ b.expect :foo, :b
162
167
 
163
168
  assert_equal :a, a.foo
164
169
  assert_equal :b, b.foo
@@ -166,7 +171,7 @@ class TestMinitestMock < Minitest::Test
166
171
 
167
172
  def test_do_not_create_stub_method_on_new_mocks
168
173
  a = Minitest::Mock.new
169
- a.expect(:foo, :a)
174
+ a.expect :foo, :a
170
175
 
171
176
  assert !Minitest::Mock.new.respond_to?(:foo)
172
177
  end
@@ -175,10 +180,77 @@ class TestMinitestMock < Minitest::Test
175
180
  @mock.expect :kind_of?, true, [String]
176
181
  @mock.expect :==, true, [1]
177
182
 
178
- assert @mock.kind_of?(String), "didn't mock :kind_of\?"
183
+ assert @mock.kind_of?(String), "didn't mock :kind_of?"
179
184
  assert @mock == 1, "didn't mock :=="
180
185
  end
181
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
+
182
254
  def test_verify_allows_called_args_to_be_loosely_specified
183
255
  mock = Minitest::Mock.new
184
256
  mock.expect :loose_expectation, true, [Integer]
@@ -210,7 +282,7 @@ class TestMinitestMock < Minitest::Test
210
282
  mock.a
211
283
  end
212
284
 
213
- assert_equal "No more expects available for :a: []", e.message
285
+ assert_equal "No more expects available for :a: [] {}", e.message
214
286
  end
215
287
 
216
288
  def test_same_method_expects_are_verified_when_all_called
@@ -233,7 +305,7 @@ class TestMinitestMock < Minitest::Test
233
305
 
234
306
  e = assert_raises(MockExpectationError) { mock.verify }
235
307
 
236
- exp = "expected foo(:baz) => nil, got [foo(:bar) => nil]"
308
+ exp = "Expected foo(:baz) => nil, got [foo(:bar) => nil]"
237
309
 
238
310
  assert_equal exp, e.message
239
311
  end
@@ -247,11 +319,36 @@ class TestMinitestMock < Minitest::Test
247
319
 
248
320
  e = assert_raises(MockExpectationError) { mock.verify }
249
321
 
250
- exp = "expected foo(:bar) => nil, got [foo(:bar) => nil]"
322
+ exp = "Expected foo(:bar) => nil, got [foo(:bar) => nil]"
251
323
 
252
324
  assert_equal exp, e.message
253
325
  end
254
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
+
255
352
  def test_verify_passes_when_mock_block_returns_true
256
353
  mock = Minitest::Mock.new
257
354
  mock.expect :foo, nil do
@@ -270,22 +367,210 @@ class TestMinitestMock < Minitest::Test
270
367
  a1 == arg1 && a2 == arg2 && a3 == arg3
271
368
  end
272
369
 
273
- mock.foo arg1, arg2, arg3
370
+ assert_silent do
371
+ if RUBY_VERSION > "3" then
372
+ mock.foo arg1, arg2, arg3
373
+ else
374
+ mock.foo arg1, arg2, **arg3 # oddity just for ruby 2.7
375
+ end
376
+ end
377
+
378
+ assert_mock mock
379
+ end
380
+
381
+ def test_mock_block_is_passed_keyword_args__block
382
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
383
+ mock = Minitest::Mock.new
384
+ mock.expect :foo, nil do |k1:, k2:, k3:|
385
+ k1 == arg1 && k2 == arg2 && k3 == arg3
386
+ end
387
+
388
+ mock.foo k1: arg1, k2: arg2, k3: arg3
274
389
 
275
390
  assert_mock mock
276
391
  end
277
392
 
393
+ def test_mock_block_is_passed_keyword_args__block_bad_missing
394
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
395
+ mock = Minitest::Mock.new
396
+ mock.expect :foo, nil do |k1:, k2:, k3:|
397
+ k1 == arg1 && k2 == arg2 && k3 == arg3
398
+ end
399
+
400
+ e = assert_raises ArgumentError do
401
+ mock.foo k1: arg1, k2: arg2
402
+ end
403
+
404
+ # basically testing ruby ... need ? for ruby < 2.7 :(
405
+ assert_match(/missing keyword: :?k3/, e.message)
406
+ end
407
+
408
+ def test_mock_block_is_passed_keyword_args__block_bad_extra
409
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
410
+ mock = Minitest::Mock.new
411
+ mock.expect :foo, nil do |k1:, k2:|
412
+ k1 == arg1 && k2 == arg2 && k3 == arg3
413
+ end
414
+
415
+ e = assert_raises ArgumentError do
416
+ mock.foo k1: arg1, k2: arg2, k3: arg3
417
+ end
418
+
419
+ # basically testing ruby ... need ? for ruby < 2.7 :(
420
+ assert_match(/unknown keyword: :?k3/, e.message)
421
+ end
422
+
423
+ def test_mock_block_is_passed_keyword_args__block_bad_value
424
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
425
+ mock = Minitest::Mock.new
426
+ mock.expect :foo, nil do |k1:, k2:, k3:|
427
+ k1 == arg1 && k2 == arg2 && k3 == arg3
428
+ end
429
+
430
+ e = assert_raises MockExpectationError do
431
+ mock.foo k1: arg1, k2: arg2, k3: :BAD!
432
+ end
433
+
434
+ exp = "mocked method :foo failed block w/ [] %p" \
435
+ % [{ :k1 => :bar, :k2 => [1, 2, 3], :k3 => :BAD! }]
436
+
437
+ assert_equal exp, e.message
438
+ end
439
+
440
+ def test_mock_block_is_passed_keyword_args__args
441
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
442
+ mock = Minitest::Mock.new
443
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
444
+
445
+ mock.foo k1: arg1, k2: arg2, k3: arg3
446
+
447
+ assert_mock mock
448
+ end
449
+
450
+ def test_mock_allow_all_kwargs__old_style_env
451
+ with_kwargs_env do
452
+ mock = Minitest::Mock.new
453
+ mock.expect :foo, true, [Hash]
454
+ assert_equal true, mock.foo(bar: 42)
455
+ end
456
+ end
457
+
458
+ def test_mock_allow_all_kwargs__old_style_env__rewrite
459
+ with_kwargs_env do
460
+ mock = Minitest::Mock.new
461
+ mock.expect :foo, true, [], bar: Integer
462
+ assert_equal true, mock.foo(bar: 42)
463
+ end
464
+ end
465
+
466
+ def test_mock_block_is_passed_keyword_args__args__old_style_bad
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 ArgumentError do
472
+ mock.foo k1: arg1, k2: arg2, k3: arg3
473
+ end
474
+
475
+ assert_equal "mocked method :foo expects 1 arguments, got []", e.message
476
+ end
477
+
478
+ def test_mock_block_is_passed_keyword_args__args__old_style_env
479
+ with_kwargs_env do
480
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
481
+ mock = Minitest::Mock.new
482
+ mock.expect :foo, nil, [{ k1: arg1, k2: arg2, k3: arg3 }]
483
+
484
+ mock.foo k1: arg1, k2: arg2, k3: arg3
485
+
486
+ assert_mock mock
487
+ end
488
+ end
489
+
490
+ def test_mock_block_is_passed_keyword_args__args__old_style_both
491
+ with_kwargs_env do
492
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
493
+ mock = Minitest::Mock.new
494
+
495
+ assert_deprecation(/Using MT_KWARGS_HAC. yet passing kwargs/) do
496
+ mock.expect :foo, nil, [{}], k1: arg1, k2: arg2, k3: arg3
497
+ end
498
+
499
+ skip "-Werror" if error_on_warn? # mock above raised, so this is dead
500
+
501
+ mock.foo({}, k1: arg1, k2: arg2, k3: arg3)
502
+
503
+ assert_mock mock
504
+ end
505
+ end
506
+
507
+ def test_mock_block_is_passed_keyword_args__args_bad_missing
508
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
509
+ mock = Minitest::Mock.new
510
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
511
+
512
+ e = assert_raises ArgumentError do
513
+ mock.foo k1: arg1, k2: arg2
514
+ end
515
+
516
+ assert_equal "mocked method :foo expects 3 keyword arguments, got %p" % { k1: arg1, k2: arg2 }, e.message
517
+ end
518
+
519
+ def test_mock_block_is_passed_keyword_args__args_bad_extra
520
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
521
+ mock = Minitest::Mock.new
522
+ mock.expect :foo, nil, k1: arg1, k2: arg2
523
+
524
+ e = assert_raises ArgumentError do
525
+ mock.foo k1: arg1, k2: arg2, k3: arg3
526
+ end
527
+
528
+ assert_equal "mocked method :foo expects 2 keyword arguments, got %p" % { k1: arg1, k2: arg2, k3: arg3 }, e.message
529
+ end
530
+
531
+ def test_mock_block_is_passed_keyword_args__args_bad_key
532
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
533
+ mock = Minitest::Mock.new
534
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
535
+
536
+ e = assert_raises MockExpectationError do
537
+ mock.foo k1: arg1, k2: arg2, BAD: arg3
538
+ end
539
+
540
+ assert_includes e.message, "unexpected keywords [:k1, :k2, :k3]"
541
+ assert_includes e.message, "vs [:k1, :k2, :BAD]"
542
+ end
543
+
544
+ def test_mock_block_is_passed_keyword_args__args_bad_val
545
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
546
+ mock = Minitest::Mock.new
547
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
548
+
549
+ e = assert_raises MockExpectationError do
550
+ mock.foo k1: arg1, k2: :BAD!, k3: arg3
551
+ end
552
+
553
+ bad = { :k2 => :BAD! }.inspect.delete "{}"
554
+ assert_match(/unexpected keyword arguments.* vs .*#{bad}/, e.message)
555
+ end
556
+
278
557
  def test_mock_block_is_passed_function_block
279
558
  mock = Minitest::Mock.new
280
559
  block = proc { "bar" }
281
560
  mock.expect :foo, nil do |arg, &blk|
282
- arg == "foo" &&
283
- blk == block
561
+ arg == "foo" && blk == block
284
562
  end
285
563
  mock.foo "foo", &block
286
564
  assert_mock mock
287
565
  end
288
566
 
567
+ def test_mock_forward_keyword_arguments
568
+ mock = Minitest::Mock.new
569
+ mock.expect(:foo, nil) { |bar:| bar == "bar" }
570
+ mock.foo bar: "bar"
571
+ assert_mock mock
572
+ end
573
+
289
574
  def test_verify_fails_when_mock_block_returns_false
290
575
  mock = Minitest::Mock.new
291
576
  mock.expect :foo, nil do
@@ -293,15 +578,15 @@ class TestMinitestMock < Minitest::Test
293
578
  end
294
579
 
295
580
  e = assert_raises(MockExpectationError) { mock.foo }
296
- exp = "mocked method :foo failed block w/ []"
581
+ exp = "mocked method :foo failed block w/ [] {}"
297
582
 
298
583
  assert_equal exp, e.message
299
584
  end
300
585
 
301
- def test_mock_block_throws_if_args_passed
586
+ def test_mock_block_raises_if_args_passed
302
587
  mock = Minitest::Mock.new
303
588
 
304
- e = assert_raises(ArgumentError) do
589
+ e = assert_raises ArgumentError do
305
590
  mock.expect :foo, nil, [:a, :b, :c] do
306
591
  true
307
592
  end
@@ -309,12 +594,26 @@ class TestMinitestMock < Minitest::Test
309
594
 
310
595
  exp = "args ignored when block given"
311
596
 
312
- assert_equal exp, e.message
597
+ assert_match exp, e.message
598
+ end
599
+
600
+ def test_mock_block_raises_if_kwargs_passed
601
+ mock = Minitest::Mock.new
602
+
603
+ e = assert_raises ArgumentError do
604
+ mock.expect :foo, nil, kwargs: 1 do
605
+ true
606
+ end
607
+ end
608
+
609
+ exp = "kwargs ignored when block given"
610
+
611
+ assert_match exp, e.message
313
612
  end
314
613
 
315
614
  def test_mock_returns_retval_when_called_with_block
316
615
  mock = Minitest::Mock.new
317
- mock.expect(:foo, 32) do
616
+ mock.expect :foo, 32 do
318
617
  true
319
618
  end
320
619
 
@@ -333,7 +632,7 @@ class TestMinitestMock < Minitest::Test
333
632
 
334
633
  def test_mock_called_via_send
335
634
  mock = Minitest::Mock.new
336
- mock.expect(:foo, true)
635
+ mock.expect :foo, true
337
636
 
338
637
  mock.send :foo
339
638
  assert_mock mock
@@ -341,7 +640,7 @@ class TestMinitestMock < Minitest::Test
341
640
 
342
641
  def test_mock_called_via___send__
343
642
  mock = Minitest::Mock.new
344
- mock.expect(:foo, true)
643
+ mock.expect :foo, true
345
644
 
346
645
  mock.__send__ :foo
347
646
  assert_mock mock
@@ -349,9 +648,9 @@ class TestMinitestMock < Minitest::Test
349
648
 
350
649
  def test_mock_called_via_send_with_args
351
650
  mock = Minitest::Mock.new
352
- mock.expect(:foo, true, [1, 2, 3])
651
+ mock.expect :foo, true, [1, 2, 3]
353
652
 
354
- mock.send(:foo, 1, 2, 3)
653
+ mock.send :foo, 1, 2, 3
355
654
  assert_mock mock
356
655
  end
357
656
 
@@ -427,7 +726,7 @@ class TestMinitestStub < Minitest::Test
427
726
  end
428
727
  end
429
728
 
430
- def test_stub_value
729
+ def test_stub_value__literal
431
730
  assert_stub 42
432
731
  end
433
732
 
@@ -458,7 +757,7 @@ class TestMinitestStub < Minitest::Test
458
757
  end
459
758
 
460
759
  def test_stub_yield_self
461
- obj = "foo"
760
+ obj = +"foo"
462
761
 
463
762
  val = obj.stub :to_s, "bar" do |s|
464
763
  s.to_s
@@ -484,7 +783,7 @@ class TestMinitestStub < Minitest::Test
484
783
  end
485
784
  end
486
785
 
487
- val = dynamic.stub(:found, true) do |s|
786
+ val = dynamic.stub :found, true do |s|
488
787
  s.found
489
788
  end
490
789
 
@@ -499,13 +798,17 @@ class TestMinitestStub < Minitest::Test
499
798
  end
500
799
  end
501
800
 
502
- exp = /undefined method `nope_nope_nope' for( class)? `#{self.class}::Time'/
801
+ exp = if jruby? then
802
+ /Undefined method nope_nope_nope for '#{self.class}::Time'/
803
+ else
804
+ /undefined method [`']nope_nope_nope' for( class)? [`']#{self.class}::Time'/
805
+ end
503
806
  assert_match exp, e.message
504
807
  end
505
808
 
506
809
  def test_mock_with_yield
507
810
  mock = Minitest::Mock.new
508
- mock.expect(:write, true) do
811
+ mock.expect :write, true do
509
812
  true
510
813
  end
511
814
  rs = nil
@@ -518,7 +821,18 @@ class TestMinitestStub < Minitest::Test
518
821
  @tc.assert_equal true, rs
519
822
  end
520
823
 
521
- alias test_stub_value__old test_stub_value # TODO: remove/rename
824
+ def test_mock_with_yield_kwargs
825
+ mock = Minitest::Mock.new
826
+ rs = nil
827
+
828
+ File.stub :open, true, mock, kw: 42 do
829
+ File.open "foo.txt", "r" do |f, kw:|
830
+ rs = kw
831
+ end
832
+ end
833
+
834
+ @tc.assert_equal 42, rs
835
+ end
522
836
 
523
837
  ## Permutation Sets:
524
838
 
@@ -574,7 +888,7 @@ class TestMinitestStub < Minitest::Test
574
888
  # [:value, :block_call, :args] => N/A
575
889
 
576
890
  class Bar
577
- def call
891
+ def call &_ # to ignore unused block
578
892
  puts "hi"
579
893
  end
580
894
  end
@@ -592,7 +906,7 @@ class TestMinitestStub < Minitest::Test
592
906
  end
593
907
 
594
908
  class Keywords
595
- def self.args req, kw1:, kw2:24
909
+ def self.args req, kw1:, kw2: 24
596
910
  [req, kw1, kw2]
597
911
  end
598
912
  end
@@ -603,6 +917,26 @@ class TestMinitestStub < Minitest::Test
603
917
  end
604
918
  end
605
919
 
920
+ def test_stub__hash_as_last_real_arg
921
+ with_kwargs_env do
922
+ token = Object.new
923
+ def token.create_with_retry _u, _p; raise "shouldn't see this"; end
924
+
925
+ controller = Object.new
926
+ controller.define_singleton_method :create do |u, p|
927
+ token.create_with_retry u, p
928
+ end
929
+
930
+ params = Object.new
931
+ def params.to_hash; raise "nah"; end
932
+
933
+ token.stub(:create_with_retry, ->(u, p) { 42 }) do
934
+ act = controller.create :u, params
935
+ @tc.assert_equal 42, act
936
+ end
937
+ end
938
+ end
939
+
606
940
  def test_stub_callable_block_5 # from tenderlove
607
941
  @assertion_count += 1
608
942
  Foo.stub5 :blocking, Bar.new do
@@ -694,7 +1028,7 @@ class TestMinitestStub < Minitest::Test
694
1028
  def test_stub_lambda_block_call_5
695
1029
  @assertion_count += 1
696
1030
  rs = nil
697
- io = StringIO.new "", "w"
1031
+ io = StringIO.new(+"", "w")
698
1032
  File.stub5 :open, lambda { |p, m, &blk| blk and blk.call io } do
699
1033
  File.open "foo.txt", "r" do |f|
700
1034
  rs = f && f.write("woot")
@@ -709,10 +1043,10 @@ class TestMinitestStub < Minitest::Test
709
1043
 
710
1044
  @assertion_count += 1
711
1045
  rs = nil
712
- io = StringIO.new "", "w"
1046
+ io = StringIO.new(+"", "w")
713
1047
  File.stub6 :open, lambda { |p, m, &blk| blk.call io } do
714
1048
  File.open "foo.txt", "r" do |f|
715
- rs = f.write("woot")
1049
+ rs = f.write "woot"
716
1050
  end
717
1051
  end
718
1052
  @tc.assert_equal 4, rs
@@ -722,10 +1056,10 @@ class TestMinitestStub < Minitest::Test
722
1056
  def test_stub_lambda_block_call_args_5
723
1057
  @assertion_count += 1
724
1058
  rs = nil
725
- io = StringIO.new "", "w"
1059
+ io = StringIO.new(+"", "w")
726
1060
  File.stub5(:open, lambda { |p, m, &blk| blk and blk.call io }, :WTF?) do
727
1061
  File.open "foo.txt", "r" do |f|
728
- rs = f.write("woot")
1062
+ rs = f.write "woot"
729
1063
  end
730
1064
  end
731
1065
  @tc.assert_equal 4, rs
@@ -737,10 +1071,10 @@ class TestMinitestStub < Minitest::Test
737
1071
 
738
1072
  @assertion_count += 1
739
1073
  rs = nil
740
- io = StringIO.new "", "w"
1074
+ io = StringIO.new(+"", "w")
741
1075
  File.stub6(:open, lambda { |p, m, &blk| blk.call io }, :WTF?) do
742
1076
  File.open "foo.txt", "r" do |f|
743
- rs = f.write("woot")
1077
+ rs = f.write "woot"
744
1078
  end
745
1079
  end
746
1080
  @tc.assert_equal 4, rs
@@ -752,11 +1086,11 @@ class TestMinitestStub < Minitest::Test
752
1086
 
753
1087
  @assertion_count += 2
754
1088
  rs = nil
755
- io = StringIO.new "", "w"
1089
+ io = StringIO.new(+"", "w")
756
1090
  @tc.assert_raises ArgumentError do
757
1091
  File.stub6_2(:open, lambda { |p, m, &blk| blk.call io }, :WTF?) do
758
1092
  File.open "foo.txt", "r" do |f|
759
- rs = f.write("woot")
1093
+ rs = f.write "woot"
760
1094
  end
761
1095
  end
762
1096
  end
@@ -802,10 +1136,10 @@ class TestMinitestStub < Minitest::Test
802
1136
  def test_stub_value_block_args_5
803
1137
  @assertion_count += 2
804
1138
  rs = nil
805
- io = StringIO.new "", "w"
1139
+ io = StringIO.new(+"", "w")
806
1140
  File.stub5 :open, :value, io do
807
1141
  result = File.open "foo.txt", "r" do |f|
808
- rs = f.write("woot")
1142
+ rs = f.write "woot"
809
1143
  end
810
1144
  @tc.assert_equal :value, result
811
1145
  end
@@ -821,8 +1155,8 @@ class TestMinitestStub < Minitest::Test
821
1155
  end
822
1156
  end
823
1157
  end
824
- exp = "undefined method `write' for nil:NilClass"
825
- assert_equal exp, e.message
1158
+ exp = /undefined method [`']write' for nil/
1159
+ assert_match exp, e.message
826
1160
  end
827
1161
 
828
1162
  def test_stub_value_block_args_6
@@ -830,11 +1164,11 @@ class TestMinitestStub < Minitest::Test
830
1164
 
831
1165
  @assertion_count += 2
832
1166
  rs = nil
833
- io = StringIO.new "", "w"
1167
+ io = StringIO.new(+"", "w")
834
1168
  assert_deprecated do
835
1169
  File.stub6 :open, :value, io do
836
1170
  result = File.open "foo.txt", "r" do |f|
837
- rs = f.write("woot")
1171
+ rs = f.write "woot"
838
1172
  end
839
1173
  @tc.assert_equal :value, result
840
1174
  end
@@ -848,7 +1182,7 @@ class TestMinitestStub < Minitest::Test
848
1182
 
849
1183
  @assertion_count += 2
850
1184
  rs = nil
851
- io = StringIO.new "", "w"
1185
+ io = StringIO.new(+"", "w")
852
1186
  @tc.assert_raises ArgumentError do
853
1187
  File.stub6_2 :open, :value, io do
854
1188
  result = File.open "foo.txt", "r" do |f|