minitest 4.7.5 → 5.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.
@@ -2,27 +2,49 @@ 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
+ self.reporter = Minitest::Reporter.new @output, options
19
+ reporter.start
20
+
21
+ @tus ||= [@tu]
22
+ @tus.each do |tu|
23
+ Minitest::Runnable.runnables.delete tu
24
+
25
+ tu.run reporter, options
26
+ end
27
+
28
+ reporter.report
29
+ end
30
+
12
31
  def assert_report expected, flags = %w[--seed 42]
13
32
  header = clean <<-EOM
14
33
  Run options: #{flags.map { |s| s =~ /\|/ ? s.inspect : s }.join " "}
15
34
 
16
- # Running tests:
35
+ # Running:
17
36
 
18
37
  EOM
19
38
 
20
- with_output do
21
- @tu.run flags
22
- end
39
+ run_tu_with_fresh_reporter flags
40
+
41
+ output = normalize_output @output.string.dup
42
+
43
+ assert_equal header + expected, output
44
+ end
23
45
 
24
- output = @output.string.dup
25
- output.sub!(/Finished tests in .*/, "Finished tests in 0.00")
46
+ def normalize_output output
47
+ output.sub!(/Finished in .*/, "Finished in 0.00")
26
48
  output.sub!(/Loaded suite .*/, 'Loaded suite blah')
27
49
 
28
50
  output.gsub!(/ = \d+.\d\d s = /, ' = 0.00 s = ')
@@ -36,32 +58,13 @@ class MetaMetaMetaTestCase < MiniTest::Unit::TestCase
36
58
  output.gsub!(/^(\s+)[^:]+:\d+:in/, '\1FILE:LINE:in')
37
59
  end
38
60
 
39
- assert_equal header + expected, output
61
+ output
40
62
  end
41
63
 
42
64
  def setup
43
65
  super
44
66
  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
67
+ Minitest::Test.reset
68
+ @tu = nil
66
69
  end
67
70
  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,8 +66,8 @@ 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
 
@@ -113,8 +113,8 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
113
113
  end
114
114
 
115
115
  def test_assign_per_mock_return_values
116
- a = MiniTest::Mock.new
117
- b = MiniTest::Mock.new
116
+ a = Minitest::Mock.new
117
+ b = Minitest::Mock.new
118
118
 
119
119
  a.expect(:foo, :a)
120
120
  b.expect(:foo, :b)
@@ -124,10 +124,10 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
124
124
  end
125
125
 
126
126
  def test_do_not_create_stub_method_on_new_mocks
127
- a = MiniTest::Mock.new
127
+ a = Minitest::Mock.new
128
128
  a.expect(:foo, :a)
129
129
 
130
- assert !MiniTest::Mock.new.respond_to?(:foo)
130
+ assert !Minitest::Mock.new.respond_to?(:foo)
131
131
  end
132
132
 
133
133
  def test_mock_is_a_blank_slate
@@ -139,7 +139,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
139
139
  end
140
140
 
141
141
  def test_verify_allows_called_args_to_be_loosely_specified
142
- mock = MiniTest::Mock.new
142
+ mock = Minitest::Mock.new
143
143
  mock.expect :loose_expectation, true, [Integer]
144
144
  mock.loose_expectation 1
145
145
 
@@ -147,7 +147,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
147
147
  end
148
148
 
149
149
  def test_verify_raises_with_strict_args
150
- mock = MiniTest::Mock.new
150
+ mock = Minitest::Mock.new
151
151
  mock.expect :strict_expectation, true, [2]
152
152
 
153
153
  e = assert_raises MockExpectationError do
@@ -159,7 +159,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
159
159
  end
160
160
 
161
161
  def test_method_missing_empty
162
- mock = MiniTest::Mock.new
162
+ mock = Minitest::Mock.new
163
163
 
164
164
  mock.expect :a, nil
165
165
 
@@ -173,7 +173,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
173
173
  end
174
174
 
175
175
  def test_same_method_expects_are_verified_when_all_called
176
- mock = MiniTest::Mock.new
176
+ mock = Minitest::Mock.new
177
177
  mock.expect :foo, nil, [:bar]
178
178
  mock.expect :foo, nil, [:baz]
179
179
 
@@ -184,7 +184,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
184
184
  end
185
185
 
186
186
  def test_same_method_expects_blow_up_when_not_all_called
187
- mock = MiniTest::Mock.new
187
+ mock = Minitest::Mock.new
188
188
  mock.expect :foo, nil, [:bar]
189
189
  mock.expect :foo, nil, [:baz]
190
190
 
@@ -198,7 +198,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
198
198
  end
199
199
 
200
200
  def test_verify_passes_when_mock_block_returns_true
201
- mock = MiniTest::Mock.new
201
+ mock = Minitest::Mock.new
202
202
  mock.expect :foo, nil do
203
203
  true
204
204
  end
@@ -210,7 +210,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
210
210
 
211
211
  def test_mock_block_is_passed_function_params
212
212
  arg1, arg2, arg3 = :bar, [1,2,3], {:a => 'a'}
213
- mock = MiniTest::Mock.new
213
+ mock = Minitest::Mock.new
214
214
  mock.expect :foo, nil do |a1, a2, a3|
215
215
  a1 == arg1 &&
216
216
  a2 == arg2 &&
@@ -223,7 +223,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
223
223
  end
224
224
 
225
225
  def test_verify_fails_when_mock_block_returns_false
226
- mock = MiniTest::Mock.new
226
+ mock = Minitest::Mock.new
227
227
  mock.expect :foo, nil do
228
228
  false
229
229
  end
@@ -235,7 +235,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
235
235
  end
236
236
 
237
237
  def test_mock_block_throws_if_args_passed
238
- mock = MiniTest::Mock.new
238
+ mock = Minitest::Mock.new
239
239
 
240
240
  e = assert_raises(ArgumentError) do
241
241
  mock.expect :foo, nil, [:a, :b, :c] do
@@ -249,7 +249,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
249
249
  end
250
250
 
251
251
  def test_mock_returns_retval_when_called_with_block
252
- mock = MiniTest::Mock.new
252
+ mock = Minitest::Mock.new
253
253
  mock.expect(:foo, 32) do
254
254
  true
255
255
  end
@@ -270,38 +270,34 @@ end
270
270
 
271
271
  require "minitest/metametameta"
272
272
 
273
- class TestMiniTestStub < MiniTest::Unit::TestCase
273
+ class TestMinitestStub < Minitest::Test
274
274
  parallelize_me!
275
275
 
276
276
  def setup
277
277
  super
278
- MiniTest::Unit::TestCase.reset
278
+ Minitest::Test.reset
279
279
 
280
- @tc = MiniTest::Unit::TestCase.new 'fake tc'
280
+ @tc = Minitest::Test.new 'fake tc'
281
281
  @assertion_count = 1
282
282
  end
283
283
 
284
284
  def teardown
285
285
  super
286
- assert_equal @assertion_count, @tc._assertions
287
- end
288
-
289
- class Time
290
- def self.now
291
- 24
292
- end
286
+ assert_equal @assertion_count, @tc.assertions
293
287
  end
294
288
 
295
289
  def assert_stub val_or_callable
296
290
  @assertion_count += 1
297
291
 
298
- t = Time.now.to_i
292
+ Minitest::Test.synchronize do
293
+ t = Time.now.to_i
299
294
 
300
- Time.stub :now, val_or_callable do
301
- @tc.assert_equal 42, Time.now
302
- end
295
+ Time.stub :now, val_or_callable do
296
+ @tc.assert_equal 42, Time.now
297
+ end
303
298
 
304
- @tc.assert_operator Time.now.to_i, :>=, t
299
+ @tc.assert_operator Time.now.to_i, :>=, t
300
+ end
305
301
  end
306
302
 
307
303
  def test_stub_private_module_method
@@ -319,22 +315,22 @@ class TestMiniTestStub < MiniTest::Unit::TestCase
319
315
  def test_stub_private_module_method_indirect
320
316
  @assertion_count += 1
321
317
 
322
- slow_clapper = Class.new do
323
- def slow_clap
324
- sleep 3
318
+ fail_clapper = Class.new do
319
+ def fail_clap
320
+ fail
325
321
  :clap
326
322
  end
327
323
  end.new
328
324
 
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
325
+ fail_clapper.stub :fail, nil do |safe_clapper|
326
+ @tc.assert_equal :clap, safe_clapper.fail_clap # either form works
327
+ @tc.assert_equal :clap, fail_clapper.fail_clap # yay closures
332
328
  end
333
329
  end
334
330
 
335
331
  def test_stub_public_module_method
336
- Math.stub(:log10, 42.0) do
337
- @tc.assert_in_delta 42.0, Math.log10(1000)
332
+ Math.stub :log10, :stubbed do
333
+ @tc.assert_equal :stubbed, Math.log10(1000)
338
334
  end
339
335
  end
340
336
 
@@ -349,13 +345,15 @@ class TestMiniTestStub < MiniTest::Unit::TestCase
349
345
  def test_stub_block_args
350
346
  @assertion_count += 1
351
347
 
352
- t = Time.now.to_i
348
+ Minitest::Test.synchronize do
349
+ t = Time.now.to_i
353
350
 
354
- Time.stub :now, lambda { |n| n * 2 } do
355
- @tc.assert_equal 42, Time.now(21)
356
- end
351
+ Time.stub :now, lambda { |n| n * 2 } do
352
+ @tc.assert_equal 42, Time.now(21)
353
+ end
357
354
 
358
- @tc.assert_operator Time.now.to_i, :>=, t
355
+ @tc.assert_operator Time.now.to_i, :>=, t
356
+ end
359
357
  end
360
358
 
361
359
  def test_stub_callable
@@ -0,0 +1,245 @@
1
+ require "minitest/autorun"
2
+ require "minitest/metametameta"
3
+
4
+ class TestMinitestReporter < Minitest::Test
5
+
6
+ attr_accessor :r, :io
7
+
8
+ def setup
9
+ self.io = StringIO.new("")
10
+ self.r = Minitest::Reporter.new io
11
+ end
12
+
13
+ def error_test
14
+ unless defined? @et then
15
+ @et = Minitest::Test.new(:woot)
16
+ @et.failures << Minitest::UnexpectedError.new(begin
17
+ raise "no"
18
+ rescue => e
19
+ e
20
+ end)
21
+ end
22
+ @et
23
+ end
24
+
25
+ def fail_test
26
+ unless defined? @ft then
27
+ @ft = Minitest::Test.new(:woot)
28
+ @ft.failures << begin
29
+ raise Minitest::Assertion, "boo"
30
+ rescue Minitest::Assertion => e
31
+ e
32
+ end
33
+ end
34
+ @ft
35
+ end
36
+
37
+ def passing_test
38
+ @pt ||= Minitest::Test.new(:woot)
39
+ end
40
+
41
+ def skip_test
42
+ unless defined? @st then
43
+ @st = Minitest::Test.new(:woot)
44
+ @st.failures << Minitest::Skip.new
45
+ end
46
+ @st
47
+ end
48
+
49
+ def test_passed_eh_empty
50
+ assert r.passed?
51
+ end
52
+
53
+ def test_passed_eh_failure
54
+ r.results << fail_test
55
+
56
+ refute r.passed?
57
+ end
58
+
59
+ def test_passed_eh_error
60
+ r.results << error_test
61
+
62
+ refute r.passed?
63
+ end
64
+
65
+ def test_passed_eh_skipped
66
+ r.results << skip_test
67
+
68
+ assert r.passed?
69
+ end
70
+
71
+ def test_start
72
+ r.start
73
+
74
+ exp = "Run options: \n\n# Running:\n\n"
75
+
76
+ assert_equal exp, io.string
77
+ end
78
+
79
+ def test_record_pass
80
+ r.record passing_test
81
+
82
+ assert_equal ".", io.string
83
+ assert_empty r.results
84
+ assert_equal 1, r.count
85
+ assert_equal 0, r.assertions
86
+ end
87
+
88
+ def test_record_fail
89
+ r.record fail_test
90
+
91
+ assert_equal "F", io.string
92
+ assert_equal [fail_test], r.results
93
+ assert_equal 1, r.count
94
+ assert_equal 0, r.assertions
95
+ end
96
+
97
+ def test_record_error
98
+ r.record error_test
99
+
100
+ assert_equal "E", io.string
101
+ assert_equal [error_test], r.results
102
+ assert_equal 1, r.count
103
+ assert_equal 0, r.assertions
104
+ end
105
+
106
+ def test_record_skip
107
+ r.record skip_test
108
+
109
+ assert_equal "S", io.string
110
+ assert_equal [skip_test], r.results
111
+ assert_equal 1, r.count
112
+ assert_equal 0, r.assertions
113
+ end
114
+
115
+ def normalize_output output
116
+ output.sub!(/Finished in .*/, "Finished in 0.00")
117
+ output.sub!(/Loaded suite .*/, 'Loaded suite blah')
118
+
119
+ output.gsub!(/ = \d+.\d\d s = /, ' = 0.00 s = ')
120
+ output.gsub!(/0x[A-Fa-f0-9]+/, '0xXXX')
121
+ output.gsub!(/ +$/, '')
122
+
123
+ if windows? then
124
+ output.gsub!(/\[(?:[A-Za-z]:)?[^\]:]+:\d+\]/, '[FILE:LINE]')
125
+ output.gsub!(/^(\s+)(?:[A-Za-z]:)?[^:]+:\d+:in/, '\1FILE:LINE:in')
126
+ else
127
+ output.gsub!(/\[[^\]:]+:\d+\]/, '[FILE:LINE]')
128
+ output.gsub!(/^(\s+)[^:]+:\d+:in/, '\1FILE:LINE:in')
129
+ end
130
+
131
+ output
132
+ end
133
+
134
+ def test_report_empty
135
+ r.start
136
+ r.report
137
+
138
+ exp = clean <<-EOM
139
+ Run options:
140
+
141
+ # Running:
142
+
143
+
144
+
145
+ Finished in 0.00
146
+
147
+ 0 runs, 0 assertions, 0 failures, 0 errors, 0 skips
148
+ EOM
149
+
150
+
151
+ assert_equal exp, normalize_output(io.string)
152
+ end
153
+
154
+ def test_report_passing
155
+ r.start
156
+ r.record passing_test
157
+ r.report
158
+
159
+ exp = clean <<-EOM
160
+ Run options:
161
+
162
+ # Running:
163
+
164
+ .
165
+
166
+ Finished in 0.00
167
+
168
+ 1 runs, 0 assertions, 0 failures, 0 errors, 0 skips
169
+ EOM
170
+
171
+
172
+ assert_equal exp, normalize_output(io.string)
173
+ end
174
+
175
+ def test_report_failure
176
+ r.start
177
+ r.record fail_test
178
+ r.report
179
+
180
+ exp = clean <<-EOM
181
+ Run options:
182
+
183
+ # Running:
184
+
185
+ F
186
+
187
+ Finished in 0.00
188
+
189
+ 1) Failure:
190
+ Minitest::Test#woot [FILE:LINE]:
191
+ boo
192
+
193
+ 1 runs, 0 assertions, 1 failures, 0 errors, 0 skips
194
+ EOM
195
+
196
+
197
+ assert_equal exp, normalize_output(io.string)
198
+ end
199
+
200
+ def test_report_error
201
+ r.start
202
+ r.record error_test
203
+ r.report
204
+
205
+ exp = clean <<-EOM
206
+ Run options:
207
+
208
+ # Running:
209
+
210
+ E
211
+
212
+ Finished in 0.00
213
+
214
+ 1) Error:
215
+ Minitest::Test#woot:
216
+ RuntimeError: no
217
+ FILE:LINE:in `error_test'
218
+ FILE:LINE:in `test_report_error'
219
+
220
+ 1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
221
+ EOM
222
+
223
+ assert_equal exp, normalize_output(io.string)
224
+ end
225
+
226
+ def test_report_skipped
227
+ r.start
228
+ r.record skip_test
229
+ r.report
230
+
231
+ exp = clean <<-EOM
232
+ Run options:
233
+
234
+ # Running:
235
+
236
+ S
237
+
238
+ Finished in 0.00
239
+
240
+ 1 runs, 0 assertions, 0 failures, 0 errors, 1 skips
241
+ EOM
242
+
243
+ assert_equal exp, normalize_output(io.string)
244
+ end
245
+ end