minitest 4.7.5 → 5.0.7

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.
@@ -2,27 +2,57 @@ require 'tempfile'
2
2
  require 'stringio'
3
3
  require 'minitest/autorun'
4
4
 
5
- class MiniTest::Unit::TestCase
5
+ class Minitest::Test
6
6
  def clean s
7
7
  s.gsub(/^ {6}/, '')
8
8
  end
9
9
  end
10
10
 
11
- class MetaMetaMetaTestCase < MiniTest::Unit::TestCase
11
+ class MetaMetaMetaTestCase < Minitest::Test
12
+ attr_accessor :reporter, :output, :tu
13
+
14
+ def run_tu_with_fresh_reporter flags = %w[--seed 42]
15
+ options = Minitest.process_args flags
16
+
17
+ @output = StringIO.new("")
18
+
19
+ self.reporter = Minitest::CompositeReporter.new
20
+ reporter << Minitest::SummaryReporter.new(@output, options)
21
+ reporter << Minitest::ProgressReporter.new(@output, options)
22
+
23
+ reporter.start
24
+
25
+ @tus ||= [@tu]
26
+ @tus.each do |tu|
27
+ Minitest::Runnable.runnables.delete tu
28
+
29
+ tu.run reporter, options
30
+ end
31
+
32
+ reporter.report
33
+ end
34
+
35
+ def first_reporter
36
+ reporter.reporters.first
37
+ end
38
+
12
39
  def assert_report expected, flags = %w[--seed 42]
13
40
  header = clean <<-EOM
14
41
  Run options: #{flags.map { |s| s =~ /\|/ ? s.inspect : s }.join " "}
15
42
 
16
- # Running tests:
43
+ # Running:
17
44
 
18
45
  EOM
19
46
 
20
- with_output do
21
- @tu.run flags
22
- end
47
+ run_tu_with_fresh_reporter flags
23
48
 
24
- output = @output.string.dup
25
- output.sub!(/Finished tests in .*/, "Finished tests in 0.00")
49
+ output = normalize_output @output.string.dup
50
+
51
+ assert_equal header + expected, output
52
+ end
53
+
54
+ def normalize_output output
55
+ output.sub!(/Finished in .*/, "Finished in 0.00")
26
56
  output.sub!(/Loaded suite .*/, 'Loaded suite blah')
27
57
 
28
58
  output.gsub!(/ = \d+.\d\d s = /, ' = 0.00 s = ')
@@ -36,32 +66,13 @@ class MetaMetaMetaTestCase < MiniTest::Unit::TestCase
36
66
  output.gsub!(/^(\s+)[^:]+:\d+:in/, '\1FILE:LINE:in')
37
67
  end
38
68
 
39
- assert_equal header + expected, output
69
+ output
40
70
  end
41
71
 
42
72
  def setup
43
73
  super
44
74
  srand 42
45
- MiniTest::Unit::TestCase.reset
46
- @tu = MiniTest::Unit.new
47
-
48
- MiniTest::Unit.runner = nil # protect the outer runner from the inner tests
49
- end
50
-
51
- def teardown
52
- super
53
- end
54
-
55
- def with_output
56
- synchronize do
57
- begin
58
- @output = StringIO.new("")
59
- MiniTest::Unit.output = @output
60
-
61
- yield
62
- ensure
63
- MiniTest::Unit.output = STDOUT
64
- end
65
- end
75
+ Minitest::Test.reset
76
+ @tu = nil
66
77
  end
67
78
  end
@@ -5,28 +5,28 @@ require 'minitest/benchmark'
5
5
  # Used to verify data:
6
6
  # http://www.wolframalpha.com/examples/RegressionAnalysis.html
7
7
 
8
- class TestMiniTestBenchmark < MiniTest::Unit::TestCase
8
+ class TestMinitestBenchmark < Minitest::Test
9
9
  def test_cls_bench_exp
10
- assert_equal [2, 4, 8, 16, 32], self.class.bench_exp(2, 32, 2)
10
+ assert_equal [2, 4, 8, 16, 32], Minitest::Benchmark.bench_exp(2, 32, 2)
11
11
  end
12
12
 
13
13
  def test_cls_bench_linear
14
- assert_equal [2, 4, 6, 8, 10], self.class.bench_linear(2, 10, 2)
14
+ assert_equal [2, 4, 6, 8, 10], Minitest::Benchmark.bench_linear(2, 10, 2)
15
15
  end
16
16
 
17
- def test_cls_benchmark_methods
18
- assert_equal [], self.class.benchmark_methods
17
+ def test_cls_runnable_methods
18
+ assert_equal [], Minitest::Benchmark.runnable_methods
19
19
 
20
- c = Class.new(MiniTest::Unit::TestCase) do
20
+ c = Class.new(Minitest::Benchmark) do
21
21
  def bench_blah
22
22
  end
23
23
  end
24
24
 
25
- assert_equal ["bench_blah"], c.benchmark_methods
25
+ assert_equal ["bench_blah"], c.runnable_methods
26
26
  end
27
27
 
28
28
  def test_cls_bench_range
29
- assert_equal [1, 10, 100, 1_000, 10_000], self.class.bench_range
29
+ assert_equal [1, 10, 100, 1_000, 10_000], Minitest::Benchmark.bench_range
30
30
  end
31
31
 
32
32
  def test_fit_exponential_clean
@@ -119,7 +119,9 @@ class TestMiniTestBenchmark < MiniTest::Unit::TestCase
119
119
  end
120
120
 
121
121
  def assert_fit msg, x, y, fit, exp_a, exp_b
122
- a, b, rr = send "fit_#{msg}", x, y
122
+ bench = Minitest::Benchmark.new :blah
123
+
124
+ a, b, rr = bench.send "fit_#{msg}", x, y
123
125
 
124
126
  assert_operator rr, :>=, fit if fit
125
127
  assert_in_delta exp_a, a
@@ -1,10 +1,10 @@
1
1
  require 'minitest/autorun'
2
2
 
3
- class TestMiniTestMock < MiniTest::Unit::TestCase
3
+ class TestMinitestMock < Minitest::Test
4
4
  parallelize_me!
5
5
 
6
6
  def setup
7
- @mock = MiniTest::Mock.new.expect(:foo, nil)
7
+ @mock = Minitest::Mock.new.expect(:foo, nil)
8
8
  @mock.expect(:meaning_of_life, 42)
9
9
  end
10
10
 
@@ -55,8 +55,8 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
55
55
  end
56
56
 
57
57
  def test_return_mock_does_not_raise
58
- retval = MiniTest::Mock.new
59
- mock = MiniTest::Mock.new
58
+ retval = Minitest::Mock.new
59
+ mock = Minitest::Mock.new
60
60
  mock.expect(:foo, retval)
61
61
  mock.foo
62
62
 
@@ -66,14 +66,57 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
66
66
  def test_mock_args_does_not_raise
67
67
  skip "non-opaque use of ==" if maglev?
68
68
 
69
- arg = MiniTest::Mock.new
70
- mock = MiniTest::Mock.new
69
+ arg = Minitest::Mock.new
70
+ mock = Minitest::Mock.new
71
71
  mock.expect(:foo, nil, [arg])
72
72
  mock.foo(arg)
73
73
 
74
74
  assert mock.verify
75
75
  end
76
76
 
77
+ def test_set_expectation_on_special_methods
78
+ mock = Minitest::Mock.new
79
+
80
+ mock.expect :object_id, "received object_id"
81
+ assert_equal "received object_id", mock.object_id
82
+
83
+ mock.expect :respond_to_missing?, "received respond_to_missing?"
84
+ assert_equal "received respond_to_missing?", mock.respond_to_missing?
85
+
86
+ mock.expect :===, "received ==="
87
+ assert_equal "received ===", mock.===
88
+
89
+ mock.expect :inspect, "received inspect"
90
+ assert_equal "received inspect", mock.inspect
91
+
92
+ mock.expect :to_s, "received to_s"
93
+ assert_equal "received to_s", mock.to_s
94
+
95
+ mock.expect :public_send, "received public_send"
96
+ assert_equal "received public_send", mock.public_send
97
+
98
+ mock.expect :send, "received send"
99
+ assert_equal "received send", mock.send
100
+
101
+ assert mock.verify
102
+ end
103
+
104
+ def test_expectations_can_be_satisfied_via_send
105
+ @mock.send :foo
106
+ @mock.send :meaning_of_life
107
+
108
+ assert @mock.verify
109
+ end
110
+
111
+ def test_expectations_can_be_satisfied_via_public_send
112
+ skip "Doesn't run on 1.8" if RUBY_VERSION < "1.9"
113
+
114
+ @mock.public_send :foo
115
+ @mock.public_send :meaning_of_life
116
+
117
+ assert @mock.verify
118
+ end
119
+
77
120
  def test_blow_up_on_wrong_arguments
78
121
  @mock.foo
79
122
  @mock.meaning_of_life
@@ -113,8 +156,8 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
113
156
  end
114
157
 
115
158
  def test_assign_per_mock_return_values
116
- a = MiniTest::Mock.new
117
- b = MiniTest::Mock.new
159
+ a = Minitest::Mock.new
160
+ b = Minitest::Mock.new
118
161
 
119
162
  a.expect(:foo, :a)
120
163
  b.expect(:foo, :b)
@@ -124,10 +167,10 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
124
167
  end
125
168
 
126
169
  def test_do_not_create_stub_method_on_new_mocks
127
- a = MiniTest::Mock.new
170
+ a = Minitest::Mock.new
128
171
  a.expect(:foo, :a)
129
172
 
130
- assert !MiniTest::Mock.new.respond_to?(:foo)
173
+ assert !Minitest::Mock.new.respond_to?(:foo)
131
174
  end
132
175
 
133
176
  def test_mock_is_a_blank_slate
@@ -139,7 +182,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
139
182
  end
140
183
 
141
184
  def test_verify_allows_called_args_to_be_loosely_specified
142
- mock = MiniTest::Mock.new
185
+ mock = Minitest::Mock.new
143
186
  mock.expect :loose_expectation, true, [Integer]
144
187
  mock.loose_expectation 1
145
188
 
@@ -147,7 +190,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
147
190
  end
148
191
 
149
192
  def test_verify_raises_with_strict_args
150
- mock = MiniTest::Mock.new
193
+ mock = Minitest::Mock.new
151
194
  mock.expect :strict_expectation, true, [2]
152
195
 
153
196
  e = assert_raises MockExpectationError do
@@ -159,7 +202,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
159
202
  end
160
203
 
161
204
  def test_method_missing_empty
162
- mock = MiniTest::Mock.new
205
+ mock = Minitest::Mock.new
163
206
 
164
207
  mock.expect :a, nil
165
208
 
@@ -173,7 +216,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
173
216
  end
174
217
 
175
218
  def test_same_method_expects_are_verified_when_all_called
176
- mock = MiniTest::Mock.new
219
+ mock = Minitest::Mock.new
177
220
  mock.expect :foo, nil, [:bar]
178
221
  mock.expect :foo, nil, [:baz]
179
222
 
@@ -184,7 +227,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
184
227
  end
185
228
 
186
229
  def test_same_method_expects_blow_up_when_not_all_called
187
- mock = MiniTest::Mock.new
230
+ mock = Minitest::Mock.new
188
231
  mock.expect :foo, nil, [:bar]
189
232
  mock.expect :foo, nil, [:baz]
190
233
 
@@ -198,7 +241,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
198
241
  end
199
242
 
200
243
  def test_verify_passes_when_mock_block_returns_true
201
- mock = MiniTest::Mock.new
244
+ mock = Minitest::Mock.new
202
245
  mock.expect :foo, nil do
203
246
  true
204
247
  end
@@ -210,7 +253,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
210
253
 
211
254
  def test_mock_block_is_passed_function_params
212
255
  arg1, arg2, arg3 = :bar, [1,2,3], {:a => 'a'}
213
- mock = MiniTest::Mock.new
256
+ mock = Minitest::Mock.new
214
257
  mock.expect :foo, nil do |a1, a2, a3|
215
258
  a1 == arg1 &&
216
259
  a2 == arg2 &&
@@ -223,7 +266,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
223
266
  end
224
267
 
225
268
  def test_verify_fails_when_mock_block_returns_false
226
- mock = MiniTest::Mock.new
269
+ mock = Minitest::Mock.new
227
270
  mock.expect :foo, nil do
228
271
  false
229
272
  end
@@ -235,7 +278,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
235
278
  end
236
279
 
237
280
  def test_mock_block_throws_if_args_passed
238
- mock = MiniTest::Mock.new
281
+ mock = Minitest::Mock.new
239
282
 
240
283
  e = assert_raises(ArgumentError) do
241
284
  mock.expect :foo, nil, [:a, :b, :c] do
@@ -249,7 +292,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
249
292
  end
250
293
 
251
294
  def test_mock_returns_retval_when_called_with_block
252
- mock = MiniTest::Mock.new
295
+ mock = Minitest::Mock.new
253
296
  mock.expect(:foo, 32) do
254
297
  true
255
298
  end
@@ -266,24 +309,49 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
266
309
 
267
310
  assert_equal exp, e.message
268
311
  end
312
+
313
+ def test_mock_called_via_send
314
+ mock = Minitest::Mock.new
315
+ mock.expect(:foo, true)
316
+
317
+ mock.send :foo
318
+ mock.verify
319
+ end
320
+
321
+ def test_mock_called_via___send__
322
+ mock = Minitest::Mock.new
323
+ mock.expect(:foo, true)
324
+
325
+ mock.__send__ :foo
326
+ mock.verify
327
+ end
328
+
329
+ def test_mock_called_via_send_with_args
330
+ mock = Minitest::Mock.new
331
+ mock.expect(:foo, true, [1,2,3])
332
+
333
+ mock.send(:foo, 1, 2, 3)
334
+ mock.verify
335
+ end
336
+
269
337
  end
270
338
 
271
339
  require "minitest/metametameta"
272
340
 
273
- class TestMiniTestStub < MiniTest::Unit::TestCase
341
+ class TestMinitestStub < Minitest::Test
274
342
  parallelize_me!
275
343
 
276
344
  def setup
277
345
  super
278
- MiniTest::Unit::TestCase.reset
346
+ Minitest::Test.reset
279
347
 
280
- @tc = MiniTest::Unit::TestCase.new 'fake tc'
348
+ @tc = Minitest::Test.new 'fake tc'
281
349
  @assertion_count = 1
282
350
  end
283
351
 
284
352
  def teardown
285
353
  super
286
- assert_equal @assertion_count, @tc._assertions
354
+ assert_equal @assertion_count, @tc.assertions
287
355
  end
288
356
 
289
357
  class Time
@@ -319,22 +387,22 @@ class TestMiniTestStub < MiniTest::Unit::TestCase
319
387
  def test_stub_private_module_method_indirect
320
388
  @assertion_count += 1
321
389
 
322
- slow_clapper = Class.new do
323
- def slow_clap
324
- sleep 3
390
+ fail_clapper = Class.new do
391
+ def fail_clap
392
+ fail
325
393
  :clap
326
394
  end
327
395
  end.new
328
396
 
329
- slow_clapper.stub :sleep, nil do |fast_clapper|
330
- @tc.assert_equal :clap, fast_clapper.slow_clap # either form works
331
- @tc.assert_equal :clap, slow_clapper.slow_clap # yay closures
397
+ fail_clapper.stub :fail, nil do |safe_clapper|
398
+ @tc.assert_equal :clap, safe_clapper.fail_clap # either form works
399
+ @tc.assert_equal :clap, fail_clapper.fail_clap # yay closures
332
400
  end
333
401
  end
334
402
 
335
403
  def test_stub_public_module_method
336
- Math.stub(:log10, 42.0) do
337
- @tc.assert_in_delta 42.0, Math.log10(1000)
404
+ Math.stub :log10, :stubbed do
405
+ @tc.assert_equal :stubbed, Math.log10(1000)
338
406
  end
339
407
  end
340
408
 
@@ -402,4 +470,20 @@ class TestMiniTestStub < MiniTest::Unit::TestCase
402
470
  @tc.assert_equal true, val
403
471
  @tc.assert_equal false, dynamic.found
404
472
  end
473
+
474
+ def test_mock_with_yield
475
+ mock = Minitest::Mock.new
476
+ mock.expect(:write, true) do
477
+ true
478
+ end
479
+ rs = nil
480
+
481
+ File.stub(:open, true, mock) do
482
+ File.open("foo.txt", "r") do |f|
483
+ rs = f.write
484
+ end
485
+ end
486
+ @tc.assert_equal true, rs
487
+ end
488
+
405
489
  end