minitest 5.11.3 → 5.25.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,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(: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
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(:foo, nil, [arg])
72
- mock.foo(arg)
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(:sum, 3, [1, 2])
126
+ @mock.expect :sum, 3, [1, 2]
124
127
 
125
128
  e = assert_raises MockExpectationError do
126
- @mock.sum(2, 4)
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
- assert_equal "args must be an array", e.message
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
- assert_equal expected, e.message
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(:foo, :a)
163
- b.expect(:foo, :b)
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(:foo, :a)
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 = "expected foo(:baz) => nil, got [foo(:bar) => nil]"
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 = "expected foo(:bar) => nil, got [foo(:bar) => nil]"
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,210 @@ class TestMinitestMock < Minitest::Test
272
367
  a1 == arg1 && a2 == arg2 && a3 == arg3
273
368
  end
274
369
 
275
- 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
389
+
390
+ assert_mock mock
391
+ end
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
276
446
 
277
447
  assert_mock mock
278
448
  end
279
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
+
280
557
  def test_mock_block_is_passed_function_block
281
558
  mock = Minitest::Mock.new
282
559
  block = proc { "bar" }
283
560
  mock.expect :foo, nil do |arg, &blk|
284
- arg == "foo" &&
285
- blk == block
561
+ arg == "foo" && blk == block
286
562
  end
287
563
  mock.foo "foo", &block
288
564
  assert_mock mock
289
565
  end
290
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
+
291
574
  def test_verify_fails_when_mock_block_returns_false
292
575
  mock = Minitest::Mock.new
293
576
  mock.expect :foo, nil do
@@ -295,15 +578,15 @@ class TestMinitestMock < Minitest::Test
295
578
  end
296
579
 
297
580
  e = assert_raises(MockExpectationError) { mock.foo }
298
- exp = "mocked method :foo failed block w/ []"
581
+ exp = "mocked method :foo failed block w/ [] {}"
299
582
 
300
583
  assert_equal exp, e.message
301
584
  end
302
585
 
303
- def test_mock_block_throws_if_args_passed
586
+ def test_mock_block_raises_if_args_passed
304
587
  mock = Minitest::Mock.new
305
588
 
306
- e = assert_raises(ArgumentError) do
589
+ e = assert_raises ArgumentError do
307
590
  mock.expect :foo, nil, [:a, :b, :c] do
308
591
  true
309
592
  end
@@ -311,12 +594,26 @@ class TestMinitestMock < Minitest::Test
311
594
 
312
595
  exp = "args ignored when block given"
313
596
 
314
- 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
315
612
  end
316
613
 
317
614
  def test_mock_returns_retval_when_called_with_block
318
615
  mock = Minitest::Mock.new
319
- mock.expect(:foo, 32) do
616
+ mock.expect :foo, 32 do
320
617
  true
321
618
  end
322
619
 
@@ -335,7 +632,7 @@ class TestMinitestMock < Minitest::Test
335
632
 
336
633
  def test_mock_called_via_send
337
634
  mock = Minitest::Mock.new
338
- mock.expect(:foo, true)
635
+ mock.expect :foo, true
339
636
 
340
637
  mock.send :foo
341
638
  assert_mock mock
@@ -343,7 +640,7 @@ class TestMinitestMock < Minitest::Test
343
640
 
344
641
  def test_mock_called_via___send__
345
642
  mock = Minitest::Mock.new
346
- mock.expect(:foo, true)
643
+ mock.expect :foo, true
347
644
 
348
645
  mock.__send__ :foo
349
646
  assert_mock mock
@@ -351,9 +648,9 @@ class TestMinitestMock < Minitest::Test
351
648
 
352
649
  def test_mock_called_via_send_with_args
353
650
  mock = Minitest::Mock.new
354
- mock.expect(:foo, true, [1, 2, 3])
651
+ mock.expect :foo, true, [1, 2, 3]
355
652
 
356
- mock.send(:foo, 1, 2, 3)
653
+ mock.send :foo, 1, 2, 3
357
654
  assert_mock mock
358
655
  end
359
656
 
@@ -362,7 +659,7 @@ end
362
659
  require "minitest/metametameta"
363
660
 
364
661
  class TestMinitestStub < Minitest::Test
365
- parallelize_me!
662
+ # Do not parallelize since we're calling stub on class methods
366
663
 
367
664
  def setup
368
665
  super
@@ -374,7 +671,7 @@ class TestMinitestStub < Minitest::Test
374
671
 
375
672
  def teardown
376
673
  super
377
- assert_equal @assertion_count, @tc.assertions
674
+ assert_equal @assertion_count, @tc.assertions if self.passed?
378
675
  end
379
676
 
380
677
  class Time
@@ -429,7 +726,7 @@ class TestMinitestStub < Minitest::Test
429
726
  end
430
727
  end
431
728
 
432
- def test_stub_value
729
+ def test_stub_value__literal
433
730
  assert_stub 42
434
731
  end
435
732
 
@@ -460,7 +757,7 @@ class TestMinitestStub < Minitest::Test
460
757
  end
461
758
 
462
759
  def test_stub_yield_self
463
- obj = "foo"
760
+ obj = +"foo"
464
761
 
465
762
  val = obj.stub :to_s, "bar" do |s|
466
763
  s.to_s
@@ -486,7 +783,7 @@ class TestMinitestStub < Minitest::Test
486
783
  end
487
784
  end
488
785
 
489
- val = dynamic.stub(:found, true) do |s|
786
+ val = dynamic.stub :found, true do |s|
490
787
  s.found
491
788
  end
492
789
 
@@ -501,13 +798,17 @@ class TestMinitestStub < Minitest::Test
501
798
  end
502
799
  end
503
800
 
504
- 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
505
806
  assert_match exp, e.message
506
807
  end
507
808
 
508
809
  def test_mock_with_yield
509
810
  mock = Minitest::Mock.new
510
- mock.expect(:write, true) do
811
+ mock.expect :write, true do
511
812
  true
512
813
  end
513
814
  rs = nil
@@ -520,7 +821,18 @@ class TestMinitestStub < Minitest::Test
520
821
  @tc.assert_equal true, rs
521
822
  end
522
823
 
523
- 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
524
836
 
525
837
  ## Permutation Sets:
526
838
 
@@ -576,7 +888,7 @@ class TestMinitestStub < Minitest::Test
576
888
  # [:value, :block_call, :args] => N/A
577
889
 
578
890
  class Bar
579
- def call
891
+ def call &_ # to ignore unused block
580
892
  puts "hi"
581
893
  end
582
894
  end
@@ -593,6 +905,38 @@ class TestMinitestStub < Minitest::Test
593
905
  end
594
906
  end
595
907
 
908
+ class Keywords
909
+ def self.args req, kw1:, kw2: 24
910
+ [req, kw1, kw2]
911
+ end
912
+ end
913
+
914
+ def test_stub_callable_keyword_args
915
+ Keywords.stub :args, ->(*args, **kws) { [args, kws] } do
916
+ @tc.assert_equal [["woot"], { kw1: 42 }], Keywords.args("woot", kw1: 42)
917
+ end
918
+ end
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
+
596
940
  def test_stub_callable_block_5 # from tenderlove
597
941
  @assertion_count += 1
598
942
  Foo.stub5 :blocking, Bar.new do
@@ -684,7 +1028,7 @@ class TestMinitestStub < Minitest::Test
684
1028
  def test_stub_lambda_block_call_5
685
1029
  @assertion_count += 1
686
1030
  rs = nil
687
- io = StringIO.new "", "w"
1031
+ io = StringIO.new(+"", "w")
688
1032
  File.stub5 :open, lambda { |p, m, &blk| blk and blk.call io } do
689
1033
  File.open "foo.txt", "r" do |f|
690
1034
  rs = f && f.write("woot")
@@ -699,10 +1043,10 @@ class TestMinitestStub < Minitest::Test
699
1043
 
700
1044
  @assertion_count += 1
701
1045
  rs = nil
702
- io = StringIO.new "", "w"
1046
+ io = StringIO.new(+"", "w")
703
1047
  File.stub6 :open, lambda { |p, m, &blk| blk.call io } do
704
1048
  File.open "foo.txt", "r" do |f|
705
- rs = f.write("woot")
1049
+ rs = f.write "woot"
706
1050
  end
707
1051
  end
708
1052
  @tc.assert_equal 4, rs
@@ -712,10 +1056,10 @@ class TestMinitestStub < Minitest::Test
712
1056
  def test_stub_lambda_block_call_args_5
713
1057
  @assertion_count += 1
714
1058
  rs = nil
715
- io = StringIO.new "", "w"
1059
+ io = StringIO.new(+"", "w")
716
1060
  File.stub5(:open, lambda { |p, m, &blk| blk and blk.call io }, :WTF?) do
717
1061
  File.open "foo.txt", "r" do |f|
718
- rs = f.write("woot")
1062
+ rs = f.write "woot"
719
1063
  end
720
1064
  end
721
1065
  @tc.assert_equal 4, rs
@@ -727,10 +1071,10 @@ class TestMinitestStub < Minitest::Test
727
1071
 
728
1072
  @assertion_count += 1
729
1073
  rs = nil
730
- io = StringIO.new "", "w"
1074
+ io = StringIO.new(+"", "w")
731
1075
  File.stub6(:open, lambda { |p, m, &blk| blk.call io }, :WTF?) do
732
1076
  File.open "foo.txt", "r" do |f|
733
- rs = f.write("woot")
1077
+ rs = f.write "woot"
734
1078
  end
735
1079
  end
736
1080
  @tc.assert_equal 4, rs
@@ -742,11 +1086,11 @@ class TestMinitestStub < Minitest::Test
742
1086
 
743
1087
  @assertion_count += 2
744
1088
  rs = nil
745
- io = StringIO.new "", "w"
1089
+ io = StringIO.new(+"", "w")
746
1090
  @tc.assert_raises ArgumentError do
747
1091
  File.stub6_2(:open, lambda { |p, m, &blk| blk.call io }, :WTF?) do
748
1092
  File.open "foo.txt", "r" do |f|
749
- rs = f.write("woot")
1093
+ rs = f.write "woot"
750
1094
  end
751
1095
  end
752
1096
  end
@@ -792,10 +1136,10 @@ class TestMinitestStub < Minitest::Test
792
1136
  def test_stub_value_block_args_5
793
1137
  @assertion_count += 2
794
1138
  rs = nil
795
- io = StringIO.new "", "w"
1139
+ io = StringIO.new(+"", "w")
796
1140
  File.stub5 :open, :value, io do
797
1141
  result = File.open "foo.txt", "r" do |f|
798
- rs = f.write("woot")
1142
+ rs = f.write "woot"
799
1143
  end
800
1144
  @tc.assert_equal :value, result
801
1145
  end
@@ -811,8 +1155,8 @@ class TestMinitestStub < Minitest::Test
811
1155
  end
812
1156
  end
813
1157
  end
814
- exp = "undefined method `write' for nil:NilClass"
815
- assert_equal exp, e.message
1158
+ exp = /undefined method [`']write' for nil/
1159
+ assert_match exp, e.message
816
1160
  end
817
1161
 
818
1162
  def test_stub_value_block_args_6
@@ -820,11 +1164,11 @@ class TestMinitestStub < Minitest::Test
820
1164
 
821
1165
  @assertion_count += 2
822
1166
  rs = nil
823
- io = StringIO.new "", "w"
1167
+ io = StringIO.new(+"", "w")
824
1168
  assert_deprecated do
825
1169
  File.stub6 :open, :value, io do
826
1170
  result = File.open "foo.txt", "r" do |f|
827
- rs = f.write("woot")
1171
+ rs = f.write "woot"
828
1172
  end
829
1173
  @tc.assert_equal :value, result
830
1174
  end
@@ -838,7 +1182,7 @@ class TestMinitestStub < Minitest::Test
838
1182
 
839
1183
  @assertion_count += 2
840
1184
  rs = nil
841
- io = StringIO.new "", "w"
1185
+ io = StringIO.new(+"", "w")
842
1186
  @tc.assert_raises ArgumentError do
843
1187
  File.stub6_2 :open, :value, io do
844
1188
  result = File.open "foo.txt", "r" do |f|