minitest 5.27.0 → 6.0.0.a1

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,1213 +0,0 @@
1
- require "minitest/autorun"
2
-
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
10
-
11
- class TestMinitestMock < Minitest::Test
12
- def setup
13
- @mock = Minitest::Mock.new
14
- .expect(:foo, nil)
15
- .expect(:meaning_of_life, 42)
16
- end
17
-
18
- def test_create_stub_method
19
- assert_nil @mock.foo
20
- end
21
-
22
- def test_allow_return_value_specification
23
- assert_equal 42, @mock.meaning_of_life
24
- end
25
-
26
- def test_blow_up_if_not_called
27
- @mock.foo
28
-
29
- util_verify_bad "Expected meaning_of_life() => 42"
30
- end
31
-
32
- def test_not_blow_up_if_everything_called
33
- @mock.foo
34
- @mock.meaning_of_life
35
-
36
- assert_mock @mock
37
- end
38
-
39
- def test_allow_expectations_to_be_added_after_creation
40
- @mock.expect :bar, true
41
- assert @mock.bar
42
- end
43
-
44
- def test_not_verify_if_new_expected_method_is_not_called
45
- @mock.foo
46
- @mock.meaning_of_life
47
- @mock.expect :bar, true
48
-
49
- util_verify_bad "Expected bar() => true"
50
- end
51
-
52
- def test_blow_up_on_wrong_number_of_arguments
53
- @mock.foo
54
- @mock.meaning_of_life
55
- @mock.expect :sum, 3, [1, 2]
56
-
57
- e = assert_raises ArgumentError do
58
- @mock.sum
59
- end
60
-
61
- assert_equal "mocked method :sum expects 2 arguments, got []", e.message
62
- end
63
-
64
- def test_return_mock_does_not_raise
65
- retval = Minitest::Mock.new
66
- mock = Minitest::Mock.new
67
- mock.expect :foo, retval
68
- mock.foo
69
-
70
- assert_mock mock
71
- end
72
-
73
- def test_mock_args_does_not_raise
74
- arg = Minitest::Mock.new
75
- mock = Minitest::Mock.new
76
- mock.expect :foo, nil, [arg]
77
- mock.foo arg
78
-
79
- assert_mock mock
80
- end
81
-
82
- def test_set_expectation_on_special_methods
83
- mock = Minitest::Mock.new
84
-
85
- mock.expect :object_id, "received object_id"
86
- assert_equal "received object_id", mock.object_id
87
-
88
- mock.expect :respond_to_missing?, "received respond_to_missing?"
89
- assert_equal "received respond_to_missing?", mock.respond_to_missing?
90
-
91
- mock.expect :===, "received ==="
92
- assert_equal "received ===", mock.===
93
-
94
- mock.expect :inspect, "received inspect"
95
- assert_equal "received inspect", mock.inspect
96
-
97
- mock.expect :to_s, "received to_s"
98
- assert_equal "received to_s", mock.to_s
99
-
100
- mock.expect :public_send, "received public_send"
101
- assert_equal "received public_send", mock.public_send
102
-
103
- mock.expect :send, "received send"
104
- assert_equal "received send", mock.send
105
-
106
- assert_mock mock
107
- end
108
-
109
- def test_expectations_can_be_satisfied_via_send
110
- @mock.send :foo
111
- @mock.send :meaning_of_life
112
-
113
- assert_mock @mock
114
- end
115
-
116
- def test_expectations_can_be_satisfied_via_public_send
117
- @mock.public_send :foo
118
- @mock.public_send :meaning_of_life
119
-
120
- assert_mock @mock
121
- end
122
-
123
- def test_blow_up_on_wrong_arguments
124
- @mock.foo
125
- @mock.meaning_of_life
126
- @mock.expect :sum, 3, [1, 2]
127
-
128
- e = assert_raises MockExpectationError do
129
- @mock.sum 2, 4
130
- end
131
-
132
- exp = "mocked method :sum called with unexpected arguments [2, 4]"
133
- assert_equal exp, e.message
134
- end
135
-
136
- def test_expect_with_non_array_args
137
- e = assert_raises ArgumentError do
138
- @mock.expect :blah, 3, false
139
- end
140
-
141
- assert_match "args must be an array", e.message
142
- end
143
-
144
- def test_respond_appropriately
145
- assert @mock.respond_to?(:foo)
146
- assert @mock.respond_to?(:foo, true)
147
- assert @mock.respond_to?("foo")
148
- assert !@mock.respond_to?(:bar)
149
- end
150
-
151
- def test_no_method_error_on_unexpected_methods
152
- e = assert_raises NoMethodError do
153
- @mock.bar
154
- end
155
-
156
- expected = "unmocked method :bar, expected one of [:foo, :meaning_of_life]"
157
-
158
- assert_match expected, e.message
159
- end
160
-
161
- def test_assign_per_mock_return_values
162
- a = Minitest::Mock.new
163
- b = Minitest::Mock.new
164
-
165
- a.expect :foo, :a
166
- b.expect :foo, :b
167
-
168
- assert_equal :a, a.foo
169
- assert_equal :b, b.foo
170
- end
171
-
172
- def test_do_not_create_stub_method_on_new_mocks
173
- a = Minitest::Mock.new
174
- a.expect :foo, :a
175
-
176
- assert !Minitest::Mock.new.respond_to?(:foo)
177
- end
178
-
179
- def test_mock_is_a_blank_slate
180
- @mock.expect :kind_of?, true, [String]
181
- @mock.expect :==, true, [1]
182
-
183
- assert @mock.kind_of?(String), "didn't mock :kind_of?"
184
- assert @mock == 1, "didn't mock :=="
185
- end
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
-
254
- def test_verify_allows_called_args_to_be_loosely_specified
255
- mock = Minitest::Mock.new
256
- mock.expect :loose_expectation, true, [Integer]
257
- mock.loose_expectation 1
258
-
259
- assert_mock mock
260
- end
261
-
262
- def test_verify_raises_with_strict_args
263
- mock = Minitest::Mock.new
264
- mock.expect :strict_expectation, true, [2]
265
-
266
- e = assert_raises MockExpectationError do
267
- mock.strict_expectation 1
268
- end
269
-
270
- exp = "mocked method :strict_expectation called with unexpected arguments [1]"
271
- assert_equal exp, e.message
272
- end
273
-
274
- def test_method_missing_empty
275
- mock = Minitest::Mock.new
276
-
277
- mock.expect :a, nil
278
-
279
- mock.a
280
-
281
- e = assert_raises MockExpectationError do
282
- mock.a
283
- end
284
-
285
- assert_equal "No more expects available for :a: [] {}", e.message
286
- end
287
-
288
- def test_same_method_expects_are_verified_when_all_called
289
- mock = Minitest::Mock.new
290
- mock.expect :foo, nil, [:bar]
291
- mock.expect :foo, nil, [:baz]
292
-
293
- mock.foo :bar
294
- mock.foo :baz
295
-
296
- assert_mock mock
297
- end
298
-
299
- def test_same_method_expects_blow_up_when_not_all_called
300
- mock = Minitest::Mock.new
301
- mock.expect :foo, nil, [:bar]
302
- mock.expect :foo, nil, [:baz]
303
-
304
- mock.foo :bar
305
-
306
- e = assert_raises(MockExpectationError) { mock.verify }
307
-
308
- exp = "Expected foo(:baz) => nil, got [foo(:bar) => nil]"
309
-
310
- assert_equal exp, e.message
311
- end
312
-
313
- def test_same_method_expects_with_same_args_blow_up_when_not_all_called
314
- mock = Minitest::Mock.new
315
- mock.expect :foo, nil, [:bar]
316
- mock.expect :foo, nil, [:bar]
317
-
318
- mock.foo :bar
319
-
320
- e = assert_raises(MockExpectationError) { mock.verify }
321
-
322
- exp = "Expected foo(:bar) => nil, got [foo(:bar) => nil]"
323
-
324
- assert_equal exp, e.message
325
- end
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
-
352
- def test_verify_passes_when_mock_block_returns_true
353
- mock = Minitest::Mock.new
354
- mock.expect :foo, nil do
355
- true
356
- end
357
-
358
- mock.foo
359
-
360
- assert_mock mock
361
- end
362
-
363
- def test_mock_block_is_passed_function_params
364
- arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
365
- mock = Minitest::Mock.new
366
- mock.expect :foo, nil do |a1, a2, a3|
367
- a1 == arg1 && a2 == arg2 && a3 == arg3
368
- end
369
-
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
441
-
442
- assert_mock mock
443
- end
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
-
552
- def test_mock_block_is_passed_function_block
553
- mock = Minitest::Mock.new
554
- block = proc { "bar" }
555
- mock.expect :foo, nil do |arg, &blk|
556
- arg == "foo" && blk == block
557
- end
558
- mock.foo "foo", &block
559
- assert_mock mock
560
- end
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
-
569
- def test_verify_fails_when_mock_block_returns_false
570
- mock = Minitest::Mock.new
571
- mock.expect :foo, nil do
572
- false
573
- end
574
-
575
- e = assert_raises(MockExpectationError) { mock.foo }
576
- exp = "mocked method :foo failed block w/ [] {}"
577
-
578
- assert_equal exp, e.message
579
- end
580
-
581
- def test_mock_block_raises_if_args_passed
582
- mock = Minitest::Mock.new
583
-
584
- e = assert_raises ArgumentError do
585
- mock.expect :foo, nil, [:a, :b, :c] do
586
- true
587
- end
588
- end
589
-
590
- exp = "args ignored when block given"
591
-
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
607
- end
608
-
609
- def test_mock_returns_retval_when_called_with_block
610
- mock = Minitest::Mock.new
611
- mock.expect :foo, 32 do
612
- true
613
- end
614
-
615
- rs = mock.foo
616
-
617
- assert_equal rs, 32
618
- end
619
-
620
- def util_verify_bad exp
621
- e = assert_raises MockExpectationError do
622
- @mock.verify
623
- end
624
-
625
- assert_equal exp, e.message
626
- end
627
-
628
- def test_mock_called_via_send
629
- mock = Minitest::Mock.new
630
- mock.expect :foo, true
631
-
632
- mock.send :foo
633
- assert_mock mock
634
- end
635
-
636
- def test_mock_called_via___send__
637
- mock = Minitest::Mock.new
638
- mock.expect :foo, true
639
-
640
- mock.__send__ :foo
641
- assert_mock mock
642
- end
643
-
644
- def test_mock_called_via_send_with_args
645
- mock = Minitest::Mock.new
646
- mock.expect :foo, true, [1, 2, 3]
647
-
648
- mock.send :foo, 1, 2, 3
649
- assert_mock mock
650
- end
651
-
652
- end
653
-
654
- require_relative "metametameta"
655
-
656
- class TestMinitestStub < Minitest::Test
657
- # Do not parallelize since we're calling stub on class methods
658
-
659
- def setup
660
- super
661
- Minitest::Test.reset
662
-
663
- @tc = Minitest::Test.new "fake tc"
664
- @assertion_count = 1
665
- end
666
-
667
- def teardown
668
- super
669
- assert_equal @assertion_count, @tc.assertions if self.passed?
670
- end
671
-
672
- class Time
673
- def self.now
674
- 24
675
- end
676
- end
677
-
678
- def assert_stub val_or_callable
679
- @assertion_count += 1
680
-
681
- t = Time.now.to_i
682
-
683
- Time.stub :now, val_or_callable do
684
- @tc.assert_equal 42, Time.now
685
- end
686
-
687
- @tc.assert_operator Time.now.to_i, :>=, t
688
- end
689
-
690
- def test_stub_private_module_method
691
- @assertion_count += 1
692
-
693
- t0 = Time.now
694
-
695
- self.stub :sleep, nil do
696
- @tc.assert_nil sleep(10)
697
- end
698
-
699
- @tc.assert_operator Time.now - t0, :<=, 1
700
- end
701
-
702
- def test_stub_private_module_method_indirect
703
- @assertion_count += 1
704
-
705
- fail_clapper = Class.new do
706
- def fail_clap
707
- raise
708
- :clap
709
- end
710
- end.new
711
-
712
- fail_clapper.stub :raise, nil do |safe_clapper|
713
- @tc.assert_equal :clap, safe_clapper.fail_clap # either form works
714
- @tc.assert_equal :clap, fail_clapper.fail_clap # yay closures
715
- end
716
- end
717
-
718
- def test_stub_public_module_method
719
- Math.stub :log10, :stubbed do
720
- @tc.assert_equal :stubbed, Math.log10(1000)
721
- end
722
- end
723
-
724
- def test_stub_value__literal
725
- assert_stub 42
726
- end
727
-
728
- def test_stub_block
729
- assert_stub lambda { 42 }
730
- end
731
-
732
- def test_stub_block_args
733
- @assertion_count += 1
734
-
735
- t = Time.now.to_i
736
-
737
- Time.stub :now, lambda { |n| n * 2 } do
738
- @tc.assert_equal 42, Time.now(21)
739
- end
740
-
741
- @tc.assert_operator Time.now.to_i, :>=, t
742
- end
743
-
744
- def test_stub_callable
745
- obj = Object.new
746
-
747
- def obj.call
748
- 42
749
- end
750
-
751
- assert_stub obj
752
- end
753
-
754
- def test_stub_yield_self
755
- obj = +"foo"
756
-
757
- val = obj.stub :to_s, "bar" do |s|
758
- s.to_s
759
- end
760
-
761
- @tc.assert_equal "bar", val
762
- end
763
-
764
- def test_dynamic_method
765
- @assertion_count = 2
766
-
767
- dynamic = Class.new do
768
- def self.respond_to? meth
769
- meth == :found
770
- end
771
-
772
- def self.method_missing meth, *args, &block
773
- if meth == :found
774
- false
775
- else
776
- super
777
- end
778
- end
779
- end
780
-
781
- val = dynamic.stub :found, true do |s|
782
- s.found
783
- end
784
-
785
- @tc.assert_equal true, val
786
- @tc.assert_equal false, dynamic.found
787
- end
788
-
789
- def test_stub_NameError
790
- e = @tc.assert_raises NameError do
791
- Time.stub :nope_nope_nope, 42 do
792
- # do nothing
793
- end
794
- end
795
-
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
801
- assert_match exp, e.message
802
- end
803
-
804
- def test_mock_with_yield
805
- mock = Minitest::Mock.new
806
- mock.expect :write, true do
807
- true
808
- end
809
- rs = nil
810
-
811
- File.stub :open, true, mock do
812
- File.open "foo.txt", "r" do |f|
813
- rs = f.write
814
- end
815
- end
816
- @tc.assert_equal true, rs
817
- end
818
-
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
831
-
832
- ## Permutation Sets:
833
-
834
- # [:value, :lambda]
835
- # [:*, :block, :block_call]
836
- # [:**, :block_args]
837
- #
838
- # Where:
839
- #
840
- # :value = a normal value
841
- # :lambda = callable or lambda
842
- # :* = no block
843
- # :block = normal block
844
- # :block_call = :lambda invokes the block (N/A for :value)
845
- # :** = no args
846
- # :args = args passed to stub
847
-
848
- ## Permutations
849
-
850
- # [:call, :*, :**] =>5 callable+block FIX: CALL BOTH (bug)
851
- # [:call, :*, :**] =>6 callable
852
-
853
- # [:lambda, :*, :**] => lambda result
854
-
855
- # [:lambda, :*, :args] => lambda result NO ARGS
856
-
857
- # [:lambda, :block, :**] =>5 lambda result FIX: CALL BOTH (bug)
858
- # [:lambda, :block, :**] =>6 lambda result
859
-
860
- # [:lambda, :block, :args] =>5 lambda result FIX: CALL BOTH (bug)
861
- # [:lambda, :block, :args] =>6 lambda result
862
- # [:lambda, :block, :args] =>7 raise ArgumentError
863
-
864
- # [:lambda, :block_call, :**] =>5 lambda FIX: BUG!-not passed block to lambda
865
- # [:lambda, :block_call, :**] =>6 lambda+block result
866
-
867
- # [:lambda, :block_call, :args] =>5 lambda FIX: BUG!-not passed block to lambda
868
- # [:lambda, :block_call, :args] =>6 lambda+block result
869
-
870
- # [:value, :*, :**] => value
871
-
872
- # [:value, :*, :args] => value, ignore args
873
-
874
- # [:value, :block, :**] =>5 value, call block
875
- # [:value, :block, :**] =>6 value
876
-
877
- # [:value, :block, :args] =>5 value, call block w/ args
878
- # [:value, :block, :args] =>6 value, call block w/ args, deprecated
879
- # [:value, :block, :args] =>7 raise ArgumentError
880
-
881
- # [:value, :block_call, :**] => N/A
882
-
883
- # [:value, :block_call, :args] => N/A
884
-
885
- class Bar
886
- def call &_ # to ignore unused block
887
- puts "hi"
888
- end
889
- end
890
-
891
- class Foo
892
- def self.blocking
893
- yield
894
- end
895
- end
896
-
897
- class Thingy
898
- def self.identity arg
899
- arg
900
- end
901
- end
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
-
935
- def test_stub_callable_block_5 # from tenderlove
936
- @assertion_count += 1
937
- Foo.stub5 :blocking, Bar.new do
938
- @tc.assert_output "hi\n", "" do
939
- Foo.blocking do
940
- @tc.flunk "shouldn't ever hit this"
941
- end
942
- end
943
- end
944
- end
945
-
946
- def test_stub_callable_block_6 # from tenderlove
947
- skip_stub6
948
-
949
- @assertion_count += 1
950
- Foo.stub6 :blocking, Bar.new do
951
- @tc.assert_output "hi\n", "" do
952
- Foo.blocking do
953
- @tc.flunk "shouldn't ever hit this"
954
- end
955
- end
956
- end
957
- end
958
-
959
- def test_stub_lambda
960
- Thread.stub :new, lambda { 21+21 } do
961
- @tc.assert_equal 42, Thread.new
962
- end
963
- end
964
-
965
- def test_stub_lambda_args
966
- Thread.stub :new, lambda { 21+21 }, :wtf do
967
- @tc.assert_equal 42, Thread.new
968
- end
969
- end
970
-
971
- def test_stub_lambda_block_5
972
- Thread.stub5 :new, lambda { 21+21 } do
973
- result = Thread.new do
974
- @tc.flunk "shouldn't ever hit this"
975
- end
976
- @tc.assert_equal 42, result
977
- end
978
- end
979
-
980
- def test_stub_lambda_block_6
981
- skip_stub6
982
-
983
- Thread.stub6 :new, lambda { 21+21 } do
984
- result = Thread.new do
985
- @tc.flunk "shouldn't ever hit this"
986
- end
987
- @tc.assert_equal 42, result
988
- end
989
- end
990
-
991
- def test_stub_lambda_block_args_5
992
- @assertion_count += 1
993
- Thingy.stub5 :identity, lambda { |y| @tc.assert_equal :nope, y; 21+21 }, :WTF? do
994
- result = Thingy.identity :nope do |x|
995
- @tc.flunk "shouldn't reach this"
996
- end
997
- @tc.assert_equal 42, result
998
- end
999
- end
1000
-
1001
- def test_stub_lambda_block_args_6
1002
- skip_stub6
1003
-
1004
- @assertion_count += 1
1005
- Thingy.stub6 :identity, lambda { |y| @tc.assert_equal :nope, y; 21+21 }, :WTF? do
1006
- result = Thingy.identity :nope do |x|
1007
- @tc.flunk "shouldn't reach this"
1008
- end
1009
- @tc.assert_equal 42, result
1010
- end
1011
- end
1012
-
1013
- def test_stub_lambda_block_args_6_2
1014
- skip_stub6
1015
-
1016
- @tc.assert_raises ArgumentError do
1017
- Thingy.stub6_2 :identity, lambda { |y| :__not_run__ }, :WTF? do
1018
- # doesn't matter
1019
- end
1020
- end
1021
- end
1022
-
1023
- def test_stub_lambda_block_call_5
1024
- @assertion_count += 1
1025
- rs = nil
1026
- io = StringIO.new(+"", "w")
1027
- File.stub5 :open, lambda { |p, m, &blk| blk and blk.call io } do
1028
- File.open "foo.txt", "r" do |f|
1029
- rs = f && f.write("woot")
1030
- end
1031
- end
1032
- @tc.assert_equal 4, rs
1033
- @tc.assert_equal "woot", io.string
1034
- end
1035
-
1036
- def test_stub_lambda_block_call_6
1037
- skip_stub6
1038
-
1039
- @assertion_count += 1
1040
- rs = nil
1041
- io = StringIO.new(+"", "w")
1042
- File.stub6 :open, lambda { |p, m, &blk| blk.call io } do
1043
- File.open "foo.txt", "r" do |f|
1044
- rs = f.write "woot"
1045
- end
1046
- end
1047
- @tc.assert_equal 4, rs
1048
- @tc.assert_equal "woot", io.string
1049
- end
1050
-
1051
- def test_stub_lambda_block_call_args_5
1052
- @assertion_count += 1
1053
- rs = nil
1054
- io = StringIO.new(+"", "w")
1055
- File.stub5(:open, lambda { |p, m, &blk| blk and blk.call io }, :WTF?) do
1056
- File.open "foo.txt", "r" do |f|
1057
- rs = f.write "woot"
1058
- end
1059
- end
1060
- @tc.assert_equal 4, rs
1061
- @tc.assert_equal "woot", io.string
1062
- end
1063
-
1064
- def test_stub_lambda_block_call_args_6
1065
- skip_stub6
1066
-
1067
- @assertion_count += 1
1068
- rs = nil
1069
- io = StringIO.new(+"", "w")
1070
- File.stub6(:open, lambda { |p, m, &blk| blk.call io }, :WTF?) do
1071
- File.open "foo.txt", "r" do |f|
1072
- rs = f.write "woot"
1073
- end
1074
- end
1075
- @tc.assert_equal 4, rs
1076
- @tc.assert_equal "woot", io.string
1077
- end
1078
-
1079
- def test_stub_lambda_block_call_args_6_2
1080
- skip_stub6
1081
-
1082
- @assertion_count += 2
1083
- rs = nil
1084
- io = StringIO.new(+"", "w")
1085
- @tc.assert_raises ArgumentError do
1086
- File.stub6_2(:open, lambda { |p, m, &blk| blk.call io }, :WTF?) do
1087
- File.open "foo.txt", "r" do |f|
1088
- rs = f.write "woot"
1089
- end
1090
- end
1091
- end
1092
- @tc.assert_nil rs
1093
- @tc.assert_equal "", io.string
1094
- end
1095
-
1096
- def test_stub_value
1097
- Thread.stub :new, 42 do
1098
- result = Thread.new
1099
- @tc.assert_equal 42, result
1100
- end
1101
- end
1102
-
1103
- def test_stub_value_args
1104
- Thread.stub :new, 42, :WTF? do
1105
- result = Thread.new
1106
- @tc.assert_equal 42, result
1107
- end
1108
- end
1109
-
1110
- def test_stub_value_block_5
1111
- @assertion_count += 1
1112
- Thread.stub5 :new, 42 do
1113
- result = Thread.new do
1114
- @tc.assert true
1115
- end
1116
- @tc.assert_equal 42, result
1117
- end
1118
- end
1119
-
1120
- def test_stub_value_block_6
1121
- skip_stub6
1122
-
1123
- Thread.stub6 :new, 42 do
1124
- result = Thread.new do
1125
- @tc.flunk "shouldn't hit this"
1126
- end
1127
- @tc.assert_equal 42, result
1128
- end
1129
- end
1130
-
1131
- def test_stub_value_block_args_5
1132
- @assertion_count += 2
1133
- rs = nil
1134
- io = StringIO.new(+"", "w")
1135
- File.stub5 :open, :value, io do
1136
- result = File.open "foo.txt", "r" do |f|
1137
- rs = f.write "woot"
1138
- end
1139
- @tc.assert_equal :value, result
1140
- end
1141
- @tc.assert_equal 4, rs
1142
- @tc.assert_equal "woot", io.string
1143
- end
1144
-
1145
- def test_stub_value_block_args_5__break_if_not_passed
1146
- e = @tc.assert_raises NoMethodError do
1147
- File.stub5 :open, :return_value do # intentionally bad setup w/ no args
1148
- File.open "foo.txt", "r" do |f|
1149
- f.write "woot"
1150
- end
1151
- end
1152
- end
1153
- exp = /undefined method [`']write' for nil/
1154
- assert_match exp, e.message
1155
- end
1156
-
1157
- def test_stub_value_block_args_6
1158
- skip_stub6
1159
-
1160
- @assertion_count += 2
1161
- rs = nil
1162
- io = StringIO.new(+"", "w")
1163
- assert_deprecated do
1164
- File.stub6 :open, :value, io do
1165
- result = File.open "foo.txt", "r" do |f|
1166
- rs = f.write "woot"
1167
- end
1168
- @tc.assert_equal :value, result
1169
- end
1170
- end
1171
- @tc.assert_equal 4, rs
1172
- @tc.assert_equal "woot", io.string
1173
- end
1174
-
1175
- def test_stub_value_block_args_6_2
1176
- skip_stub6
1177
-
1178
- @assertion_count += 2
1179
- rs = nil
1180
- io = StringIO.new(+"", "w")
1181
- @tc.assert_raises ArgumentError do
1182
- File.stub6_2 :open, :value, io do
1183
- result = File.open "foo.txt", "r" do |f|
1184
- @tc.flunk "shouldn't hit this"
1185
- end
1186
- @tc.assert_equal :value, result
1187
- end
1188
- end
1189
- @tc.assert_nil rs
1190
- @tc.assert_equal "", io.string
1191
- end
1192
-
1193
- def assert_deprecated re = /deprecated/
1194
- assert_output "", re do
1195
- yield
1196
- end
1197
- end
1198
-
1199
- def skip_stub6
1200
- skip "not yet" unless STUB6
1201
- end
1202
- end
1203
-
1204
- STUB6 = ENV["STUB6"]
1205
-
1206
- if STUB6 then
1207
- require "minitest/mock6" if STUB6
1208
- else
1209
- class Object
1210
- alias stub5 stub
1211
- alias stub6 stub
1212
- end
1213
- end