minitest 5.26.0 → 6.0.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.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/History.rdoc +95 -0
  4. data/Manifest.txt +13 -4
  5. data/README.rdoc +18 -98
  6. data/Rakefile +7 -1
  7. data/bin/minitest +5 -0
  8. data/design_rationale.rb +21 -19
  9. data/lib/hoe/minitest.rb +2 -1
  10. data/lib/minitest/assertions.rb +34 -66
  11. data/lib/minitest/autorun.rb +3 -4
  12. data/lib/minitest/benchmark.rb +2 -2
  13. data/lib/minitest/bisect.rb +306 -0
  14. data/lib/minitest/complete.rb +56 -0
  15. data/lib/minitest/find_minimal_combination.rb +127 -0
  16. data/lib/minitest/hell.rb +1 -1
  17. data/lib/minitest/manual_plugins.rb +4 -16
  18. data/lib/minitest/parallel.rb +5 -3
  19. data/lib/minitest/path_expander.rb +418 -0
  20. data/lib/minitest/pride.rb +2 -2
  21. data/lib/minitest/pride_plugin.rb +1 -1
  22. data/lib/minitest/server.rb +45 -0
  23. data/lib/minitest/server_plugin.rb +84 -0
  24. data/lib/minitest/spec.rb +5 -33
  25. data/lib/minitest/sprint.rb +104 -0
  26. data/lib/minitest/sprint_plugin.rb +39 -0
  27. data/lib/minitest/test.rb +7 -13
  28. data/lib/minitest/test_task.rb +6 -13
  29. data/lib/minitest.rb +86 -101
  30. data/test/minitest/metametameta.rb +1 -1
  31. data/test/minitest/test_bisect.rb +235 -0
  32. data/test/minitest/test_find_minimal_combination.rb +138 -0
  33. data/test/minitest/test_minitest_assertions.rb +48 -105
  34. data/test/minitest/test_minitest_reporter.rb +6 -5
  35. data/test/minitest/test_minitest_spec.rb +52 -118
  36. data/test/minitest/test_minitest_test.rb +21 -100
  37. data/test/minitest/test_path_expander.rb +229 -0
  38. data/test/minitest/test_server.rb +149 -0
  39. data.tar.gz.sig +1 -2
  40. metadata +50 -17
  41. metadata.gz.sig +2 -2
  42. data/.autotest +0 -34
  43. data/lib/minitest/mock.rb +0 -347
  44. data/lib/minitest/unit.rb +0 -42
  45. data/test/minitest/test_minitest_mock.rb +0 -1218
@@ -1,1218 +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
- 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
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
-
557
- def test_mock_block_is_passed_function_block
558
- mock = Minitest::Mock.new
559
- block = proc { "bar" }
560
- mock.expect :foo, nil do |arg, &blk|
561
- arg == "foo" && blk == block
562
- end
563
- mock.foo "foo", &block
564
- assert_mock mock
565
- end
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
-
574
- def test_verify_fails_when_mock_block_returns_false
575
- mock = Minitest::Mock.new
576
- mock.expect :foo, nil do
577
- false
578
- end
579
-
580
- e = assert_raises(MockExpectationError) { mock.foo }
581
- exp = "mocked method :foo failed block w/ [] {}"
582
-
583
- assert_equal exp, e.message
584
- end
585
-
586
- def test_mock_block_raises_if_args_passed
587
- mock = Minitest::Mock.new
588
-
589
- e = assert_raises ArgumentError do
590
- mock.expect :foo, nil, [:a, :b, :c] do
591
- true
592
- end
593
- end
594
-
595
- exp = "args ignored when block given"
596
-
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
612
- end
613
-
614
- def test_mock_returns_retval_when_called_with_block
615
- mock = Minitest::Mock.new
616
- mock.expect :foo, 32 do
617
- true
618
- end
619
-
620
- rs = mock.foo
621
-
622
- assert_equal rs, 32
623
- end
624
-
625
- def util_verify_bad exp
626
- e = assert_raises MockExpectationError do
627
- @mock.verify
628
- end
629
-
630
- assert_equal exp, e.message
631
- end
632
-
633
- def test_mock_called_via_send
634
- mock = Minitest::Mock.new
635
- mock.expect :foo, true
636
-
637
- mock.send :foo
638
- assert_mock mock
639
- end
640
-
641
- def test_mock_called_via___send__
642
- mock = Minitest::Mock.new
643
- mock.expect :foo, true
644
-
645
- mock.__send__ :foo
646
- assert_mock mock
647
- end
648
-
649
- def test_mock_called_via_send_with_args
650
- mock = Minitest::Mock.new
651
- mock.expect :foo, true, [1, 2, 3]
652
-
653
- mock.send :foo, 1, 2, 3
654
- assert_mock mock
655
- end
656
-
657
- end
658
-
659
- require "minitest/metametameta"
660
-
661
- class TestMinitestStub < Minitest::Test
662
- # Do not parallelize since we're calling stub on class methods
663
-
664
- def setup
665
- super
666
- Minitest::Test.reset
667
-
668
- @tc = Minitest::Test.new "fake tc"
669
- @assertion_count = 1
670
- end
671
-
672
- def teardown
673
- super
674
- assert_equal @assertion_count, @tc.assertions if self.passed?
675
- end
676
-
677
- class Time
678
- def self.now
679
- 24
680
- end
681
- end
682
-
683
- def assert_stub val_or_callable
684
- @assertion_count += 1
685
-
686
- t = Time.now.to_i
687
-
688
- Time.stub :now, val_or_callable do
689
- @tc.assert_equal 42, Time.now
690
- end
691
-
692
- @tc.assert_operator Time.now.to_i, :>=, t
693
- end
694
-
695
- def test_stub_private_module_method
696
- @assertion_count += 1
697
-
698
- t0 = Time.now
699
-
700
- self.stub :sleep, nil do
701
- @tc.assert_nil sleep(10)
702
- end
703
-
704
- @tc.assert_operator Time.now - t0, :<=, 1
705
- end
706
-
707
- def test_stub_private_module_method_indirect
708
- @assertion_count += 1
709
-
710
- fail_clapper = Class.new do
711
- def fail_clap
712
- raise
713
- :clap
714
- end
715
- end.new
716
-
717
- fail_clapper.stub :raise, nil do |safe_clapper|
718
- @tc.assert_equal :clap, safe_clapper.fail_clap # either form works
719
- @tc.assert_equal :clap, fail_clapper.fail_clap # yay closures
720
- end
721
- end
722
-
723
- def test_stub_public_module_method
724
- Math.stub :log10, :stubbed do
725
- @tc.assert_equal :stubbed, Math.log10(1000)
726
- end
727
- end
728
-
729
- def test_stub_value__literal
730
- assert_stub 42
731
- end
732
-
733
- def test_stub_block
734
- assert_stub lambda { 42 }
735
- end
736
-
737
- def test_stub_block_args
738
- @assertion_count += 1
739
-
740
- t = Time.now.to_i
741
-
742
- Time.stub :now, lambda { |n| n * 2 } do
743
- @tc.assert_equal 42, Time.now(21)
744
- end
745
-
746
- @tc.assert_operator Time.now.to_i, :>=, t
747
- end
748
-
749
- def test_stub_callable
750
- obj = Object.new
751
-
752
- def obj.call
753
- 42
754
- end
755
-
756
- assert_stub obj
757
- end
758
-
759
- def test_stub_yield_self
760
- obj = +"foo"
761
-
762
- val = obj.stub :to_s, "bar" do |s|
763
- s.to_s
764
- end
765
-
766
- @tc.assert_equal "bar", val
767
- end
768
-
769
- def test_dynamic_method
770
- @assertion_count = 2
771
-
772
- dynamic = Class.new do
773
- def self.respond_to? meth
774
- meth == :found
775
- end
776
-
777
- def self.method_missing meth, *args, &block
778
- if meth == :found
779
- false
780
- else
781
- super
782
- end
783
- end
784
- end
785
-
786
- val = dynamic.stub :found, true do |s|
787
- s.found
788
- end
789
-
790
- @tc.assert_equal true, val
791
- @tc.assert_equal false, dynamic.found
792
- end
793
-
794
- def test_stub_NameError
795
- e = @tc.assert_raises NameError do
796
- Time.stub :nope_nope_nope, 42 do
797
- # do nothing
798
- end
799
- end
800
-
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
806
- assert_match exp, e.message
807
- end
808
-
809
- def test_mock_with_yield
810
- mock = Minitest::Mock.new
811
- mock.expect :write, true do
812
- true
813
- end
814
- rs = nil
815
-
816
- File.stub :open, true, mock do
817
- File.open "foo.txt", "r" do |f|
818
- rs = f.write
819
- end
820
- end
821
- @tc.assert_equal true, rs
822
- end
823
-
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
836
-
837
- ## Permutation Sets:
838
-
839
- # [:value, :lambda]
840
- # [:*, :block, :block_call]
841
- # [:**, :block_args]
842
- #
843
- # Where:
844
- #
845
- # :value = a normal value
846
- # :lambda = callable or lambda
847
- # :* = no block
848
- # :block = normal block
849
- # :block_call = :lambda invokes the block (N/A for :value)
850
- # :** = no args
851
- # :args = args passed to stub
852
-
853
- ## Permutations
854
-
855
- # [:call, :*, :**] =>5 callable+block FIX: CALL BOTH (bug)
856
- # [:call, :*, :**] =>6 callable
857
-
858
- # [:lambda, :*, :**] => lambda result
859
-
860
- # [:lambda, :*, :args] => lambda result NO ARGS
861
-
862
- # [:lambda, :block, :**] =>5 lambda result FIX: CALL BOTH (bug)
863
- # [:lambda, :block, :**] =>6 lambda result
864
-
865
- # [:lambda, :block, :args] =>5 lambda result FIX: CALL BOTH (bug)
866
- # [:lambda, :block, :args] =>6 lambda result
867
- # [:lambda, :block, :args] =>7 raise ArgumentError
868
-
869
- # [:lambda, :block_call, :**] =>5 lambda FIX: BUG!-not passed block to lambda
870
- # [:lambda, :block_call, :**] =>6 lambda+block result
871
-
872
- # [:lambda, :block_call, :args] =>5 lambda FIX: BUG!-not passed block to lambda
873
- # [:lambda, :block_call, :args] =>6 lambda+block result
874
-
875
- # [:value, :*, :**] => value
876
-
877
- # [:value, :*, :args] => value, ignore args
878
-
879
- # [:value, :block, :**] =>5 value, call block
880
- # [:value, :block, :**] =>6 value
881
-
882
- # [:value, :block, :args] =>5 value, call block w/ args
883
- # [:value, :block, :args] =>6 value, call block w/ args, deprecated
884
- # [:value, :block, :args] =>7 raise ArgumentError
885
-
886
- # [:value, :block_call, :**] => N/A
887
-
888
- # [:value, :block_call, :args] => N/A
889
-
890
- class Bar
891
- def call &_ # to ignore unused block
892
- puts "hi"
893
- end
894
- end
895
-
896
- class Foo
897
- def self.blocking
898
- yield
899
- end
900
- end
901
-
902
- class Thingy
903
- def self.identity arg
904
- arg
905
- end
906
- end
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
-
940
- def test_stub_callable_block_5 # from tenderlove
941
- @assertion_count += 1
942
- Foo.stub5 :blocking, Bar.new do
943
- @tc.assert_output "hi\n", "" do
944
- Foo.blocking do
945
- @tc.flunk "shouldn't ever hit this"
946
- end
947
- end
948
- end
949
- end
950
-
951
- def test_stub_callable_block_6 # from tenderlove
952
- skip_stub6
953
-
954
- @assertion_count += 1
955
- Foo.stub6 :blocking, Bar.new do
956
- @tc.assert_output "hi\n", "" do
957
- Foo.blocking do
958
- @tc.flunk "shouldn't ever hit this"
959
- end
960
- end
961
- end
962
- end
963
-
964
- def test_stub_lambda
965
- Thread.stub :new, lambda { 21+21 } do
966
- @tc.assert_equal 42, Thread.new
967
- end
968
- end
969
-
970
- def test_stub_lambda_args
971
- Thread.stub :new, lambda { 21+21 }, :wtf do
972
- @tc.assert_equal 42, Thread.new
973
- end
974
- end
975
-
976
- def test_stub_lambda_block_5
977
- Thread.stub5 :new, lambda { 21+21 } do
978
- result = Thread.new do
979
- @tc.flunk "shouldn't ever hit this"
980
- end
981
- @tc.assert_equal 42, result
982
- end
983
- end
984
-
985
- def test_stub_lambda_block_6
986
- skip_stub6
987
-
988
- Thread.stub6 :new, lambda { 21+21 } do
989
- result = Thread.new do
990
- @tc.flunk "shouldn't ever hit this"
991
- end
992
- @tc.assert_equal 42, result
993
- end
994
- end
995
-
996
- def test_stub_lambda_block_args_5
997
- @assertion_count += 1
998
- Thingy.stub5 :identity, lambda { |y| @tc.assert_equal :nope, y; 21+21 }, :WTF? do
999
- result = Thingy.identity :nope do |x|
1000
- @tc.flunk "shouldn't reach this"
1001
- end
1002
- @tc.assert_equal 42, result
1003
- end
1004
- end
1005
-
1006
- def test_stub_lambda_block_args_6
1007
- skip_stub6
1008
-
1009
- @assertion_count += 1
1010
- Thingy.stub6 :identity, lambda { |y| @tc.assert_equal :nope, y; 21+21 }, :WTF? do
1011
- result = Thingy.identity :nope do |x|
1012
- @tc.flunk "shouldn't reach this"
1013
- end
1014
- @tc.assert_equal 42, result
1015
- end
1016
- end
1017
-
1018
- def test_stub_lambda_block_args_6_2
1019
- skip_stub6
1020
-
1021
- @tc.assert_raises ArgumentError do
1022
- Thingy.stub6_2 :identity, lambda { |y| :__not_run__ }, :WTF? do
1023
- # doesn't matter
1024
- end
1025
- end
1026
- end
1027
-
1028
- def test_stub_lambda_block_call_5
1029
- @assertion_count += 1
1030
- rs = nil
1031
- io = StringIO.new(+"", "w")
1032
- File.stub5 :open, lambda { |p, m, &blk| blk and blk.call io } do
1033
- File.open "foo.txt", "r" do |f|
1034
- rs = f && f.write("woot")
1035
- end
1036
- end
1037
- @tc.assert_equal 4, rs
1038
- @tc.assert_equal "woot", io.string
1039
- end
1040
-
1041
- def test_stub_lambda_block_call_6
1042
- skip_stub6
1043
-
1044
- @assertion_count += 1
1045
- rs = nil
1046
- io = StringIO.new(+"", "w")
1047
- File.stub6 :open, lambda { |p, m, &blk| blk.call io } do
1048
- File.open "foo.txt", "r" do |f|
1049
- rs = f.write "woot"
1050
- end
1051
- end
1052
- @tc.assert_equal 4, rs
1053
- @tc.assert_equal "woot", io.string
1054
- end
1055
-
1056
- def test_stub_lambda_block_call_args_5
1057
- @assertion_count += 1
1058
- rs = nil
1059
- io = StringIO.new(+"", "w")
1060
- File.stub5(:open, lambda { |p, m, &blk| blk and blk.call io }, :WTF?) do
1061
- File.open "foo.txt", "r" do |f|
1062
- rs = f.write "woot"
1063
- end
1064
- end
1065
- @tc.assert_equal 4, rs
1066
- @tc.assert_equal "woot", io.string
1067
- end
1068
-
1069
- def test_stub_lambda_block_call_args_6
1070
- skip_stub6
1071
-
1072
- @assertion_count += 1
1073
- rs = nil
1074
- io = StringIO.new(+"", "w")
1075
- File.stub6(:open, lambda { |p, m, &blk| blk.call io }, :WTF?) do
1076
- File.open "foo.txt", "r" do |f|
1077
- rs = f.write "woot"
1078
- end
1079
- end
1080
- @tc.assert_equal 4, rs
1081
- @tc.assert_equal "woot", io.string
1082
- end
1083
-
1084
- def test_stub_lambda_block_call_args_6_2
1085
- skip_stub6
1086
-
1087
- @assertion_count += 2
1088
- rs = nil
1089
- io = StringIO.new(+"", "w")
1090
- @tc.assert_raises ArgumentError do
1091
- File.stub6_2(:open, lambda { |p, m, &blk| blk.call io }, :WTF?) do
1092
- File.open "foo.txt", "r" do |f|
1093
- rs = f.write "woot"
1094
- end
1095
- end
1096
- end
1097
- @tc.assert_nil rs
1098
- @tc.assert_equal "", io.string
1099
- end
1100
-
1101
- def test_stub_value
1102
- Thread.stub :new, 42 do
1103
- result = Thread.new
1104
- @tc.assert_equal 42, result
1105
- end
1106
- end
1107
-
1108
- def test_stub_value_args
1109
- Thread.stub :new, 42, :WTF? do
1110
- result = Thread.new
1111
- @tc.assert_equal 42, result
1112
- end
1113
- end
1114
-
1115
- def test_stub_value_block_5
1116
- @assertion_count += 1
1117
- Thread.stub5 :new, 42 do
1118
- result = Thread.new do
1119
- @tc.assert true
1120
- end
1121
- @tc.assert_equal 42, result
1122
- end
1123
- end
1124
-
1125
- def test_stub_value_block_6
1126
- skip_stub6
1127
-
1128
- Thread.stub6 :new, 42 do
1129
- result = Thread.new do
1130
- @tc.flunk "shouldn't hit this"
1131
- end
1132
- @tc.assert_equal 42, result
1133
- end
1134
- end
1135
-
1136
- def test_stub_value_block_args_5
1137
- @assertion_count += 2
1138
- rs = nil
1139
- io = StringIO.new(+"", "w")
1140
- File.stub5 :open, :value, io do
1141
- result = File.open "foo.txt", "r" do |f|
1142
- rs = f.write "woot"
1143
- end
1144
- @tc.assert_equal :value, result
1145
- end
1146
- @tc.assert_equal 4, rs
1147
- @tc.assert_equal "woot", io.string
1148
- end
1149
-
1150
- def test_stub_value_block_args_5__break_if_not_passed
1151
- e = @tc.assert_raises NoMethodError do
1152
- File.stub5 :open, :return_value do # intentionally bad setup w/ no args
1153
- File.open "foo.txt", "r" do |f|
1154
- f.write "woot"
1155
- end
1156
- end
1157
- end
1158
- exp = /undefined method [`']write' for nil/
1159
- assert_match exp, e.message
1160
- end
1161
-
1162
- def test_stub_value_block_args_6
1163
- skip_stub6
1164
-
1165
- @assertion_count += 2
1166
- rs = nil
1167
- io = StringIO.new(+"", "w")
1168
- assert_deprecated do
1169
- File.stub6 :open, :value, io do
1170
- result = File.open "foo.txt", "r" do |f|
1171
- rs = f.write "woot"
1172
- end
1173
- @tc.assert_equal :value, result
1174
- end
1175
- end
1176
- @tc.assert_equal 4, rs
1177
- @tc.assert_equal "woot", io.string
1178
- end
1179
-
1180
- def test_stub_value_block_args_6_2
1181
- skip_stub6
1182
-
1183
- @assertion_count += 2
1184
- rs = nil
1185
- io = StringIO.new(+"", "w")
1186
- @tc.assert_raises ArgumentError do
1187
- File.stub6_2 :open, :value, io do
1188
- result = File.open "foo.txt", "r" do |f|
1189
- @tc.flunk "shouldn't hit this"
1190
- end
1191
- @tc.assert_equal :value, result
1192
- end
1193
- end
1194
- @tc.assert_nil rs
1195
- @tc.assert_equal "", io.string
1196
- end
1197
-
1198
- def assert_deprecated re = /deprecated/
1199
- assert_output "", re do
1200
- yield
1201
- end
1202
- end
1203
-
1204
- def skip_stub6
1205
- skip "not yet" unless STUB6
1206
- end
1207
- end
1208
-
1209
- STUB6 = ENV["STUB6"]
1210
-
1211
- if STUB6 then
1212
- require "minitest/mock6" if STUB6
1213
- else
1214
- class Object
1215
- alias stub5 stub
1216
- alias stub6 stub
1217
- end
1218
- end