minitest 1.7.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,98 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/benchmark'
3
+
4
+ ##
5
+ # Used to verify data:
6
+ # http://www.wolframalpha.com/examples/RegressionAnalysis.html
7
+
8
+ class TestMiniTestBenchmark < MiniTest::Unit::TestCase
9
+ def test_cls_bench_exp
10
+ assert_equal [2, 4, 8, 16, 32], self.class.bench_exp(2, 32, 2)
11
+ end
12
+
13
+ def test_cls_bench_linear
14
+ assert_equal [2, 4, 6, 8, 10], self.class.bench_linear(2, 10, 2)
15
+ end
16
+
17
+ def test_cls_benchmark_methods
18
+ assert_equal [], self.class.benchmark_methods
19
+
20
+ c = Class.new(MiniTest::Unit::TestCase) do
21
+ def bench_blah
22
+ end
23
+ end
24
+
25
+ assert_equal ["bench_blah"], c.benchmark_methods
26
+ end
27
+
28
+ def test_cls_bench_range
29
+ assert_equal [1, 10, 100, 1_000, 10_000], self.class.bench_range
30
+ end
31
+
32
+ def test_fit_exponential_clean
33
+ x = [1.0, 2.0, 3.0, 4.0, 5.0]
34
+ y = x.map { |n| 1.1 * Math.exp(2.1 * n) }
35
+
36
+ assert_fit :exponential, x, y, 1.0, 1.1, 2.1
37
+ end
38
+
39
+ def test_fit_exponential_noisy
40
+ x = [1.0, 1.9, 2.6, 3.4, 5.0]
41
+ y = [12, 10, 8.2, 6.9, 5.9]
42
+
43
+ # verified with Numbers and R
44
+ assert_fit :exponential, x, y, 0.95, 13.81148, -0.1820
45
+ end
46
+
47
+ def test_fit_linear_clean
48
+ # y = m * x + b where m = 2.2, b = 3.1
49
+ x = (1..5).to_a
50
+ y = x.map { |n| 2.2 * n + 3.1 }
51
+
52
+ assert_fit :linear, x, y, 1.0, 3.1, 2.2
53
+ end
54
+
55
+ def test_fit_linear_noisy
56
+ x = [ 60, 61, 62, 63, 65]
57
+ y = [3.1, 3.6, 3.8, 4.0, 4.1]
58
+
59
+ # verified in numbers and R
60
+ assert_fit :linear, x, y, 0.8315, -7.9635, 0.1878
61
+ end
62
+
63
+ def test_fit_power_clean
64
+ # y = A x ** B, where B = b and A = e ** a
65
+ # if, A = 1, B = 2, then
66
+
67
+ x = [1.0, 2.0, 3.0, 4.0, 5.0]
68
+ y = [1.0, 4.0, 9.0, 16.0, 25.0]
69
+
70
+ assert_fit :power, x, y, 1.0, 1.0, 2.0
71
+ end
72
+
73
+ def test_fit_power_noisy
74
+ # from www.engr.uidaho.edu/thompson/courses/ME330/lecture/least_squares.html
75
+ x = [10, 12, 15, 17, 20, 22, 25, 27, 30, 32, 35]
76
+ y = [95, 105, 125, 141, 173, 200, 253, 298, 385, 459, 602]
77
+
78
+ # verified in numbers
79
+ assert_fit :power, x, y, 0.90, 2.6217, 1.4556
80
+
81
+ # income to % of households below income amount
82
+ # http://library.wolfram.com/infocenter/Conferences/6461/PowerLaws.nb
83
+ x = [15000, 25000, 35000, 50000, 75000, 100000]
84
+ y = [0.154, 0.283, 0.402, 0.55, 0.733, 0.843]
85
+
86
+ # verified in numbers
87
+ assert_fit :power, x, y, 0.96, 3.119e-5, 0.8959
88
+ end
89
+
90
+ def assert_fit msg, x, y, fit, exp_a, exp_b
91
+ a, b, rr = send "fit_#{msg}", x, y
92
+
93
+ assert_operator rr, :>=, fit
94
+ assert_in_delta exp_a, a
95
+ assert_in_delta exp_b, b
96
+ end
97
+ end
98
+
@@ -0,0 +1,106 @@
1
+ require 'minitest/mock'
2
+ require 'minitest/unit'
3
+
4
+ MiniTest::Unit.autorun
5
+
6
+ class TestMiniTestMock < MiniTest::Unit::TestCase
7
+ def setup
8
+ @mock = MiniTest::Mock.new.expect(:foo, nil)
9
+ @mock.expect(:meaning_of_life, 42)
10
+ end
11
+
12
+ def test_create_stub_method
13
+ assert_nil @mock.foo
14
+ end
15
+
16
+ def test_allow_return_value_specification
17
+ assert_equal 42, @mock.meaning_of_life
18
+ end
19
+
20
+ def test_blow_up_if_not_called
21
+ @mock.foo
22
+
23
+ util_verify_bad
24
+ end
25
+
26
+ def test_not_blow_up_if_everything_called
27
+ @mock.foo
28
+ @mock.meaning_of_life
29
+
30
+ assert @mock.verify
31
+ end
32
+
33
+ def test_allow_expectations_to_be_added_after_creation
34
+ @mock.expect(:bar, true)
35
+ assert @mock.bar
36
+ end
37
+
38
+ def test_not_verify_if_new_expected_method_is_not_called
39
+ @mock.foo
40
+ @mock.meaning_of_life
41
+ @mock.expect(:bar, true)
42
+
43
+ util_verify_bad
44
+ end
45
+
46
+ def test_not_verify_if_unexpected_method_is_called
47
+ assert_raises NoMethodError do
48
+ @mock.unexpected
49
+ end
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
+ assert_raises ArgumentError do
58
+ @mock.sum
59
+ end
60
+ end
61
+
62
+ def test_blow_up_on_wrong_arguments
63
+ @mock.foo
64
+ @mock.meaning_of_life
65
+ @mock.expect(:sum, 3, [1, 2])
66
+
67
+ @mock.sum(2, 4)
68
+
69
+ util_verify_bad
70
+ end
71
+
72
+ def test_respond_appropriately
73
+ assert @mock.respond_to?(:foo)
74
+ assert !@mock.respond_to?(:bar)
75
+ end
76
+
77
+ def test_no_method_error_on_unexpected_methods
78
+ assert_raises NoMethodError do
79
+ @mock.bar
80
+ end
81
+ end
82
+
83
+ def test_assign_per_mock_return_values
84
+ a = MiniTest::Mock.new
85
+ b = MiniTest::Mock.new
86
+
87
+ a.expect(:foo, :a)
88
+ b.expect(:foo, :b)
89
+
90
+ assert_equal :a, a.foo
91
+ assert_equal :b, b.foo
92
+ end
93
+
94
+ def test_do_not_create_stub_method_on_new_mocks
95
+ a = MiniTest::Mock.new
96
+ a.expect(:foo, :a)
97
+
98
+ assert !MiniTest::Mock.new.respond_to?(:foo)
99
+ end
100
+
101
+ def util_verify_bad
102
+ assert_raises MockExpectationError do
103
+ @mock.verify
104
+ end
105
+ end
106
+ end
@@ -197,7 +197,7 @@ end
197
197
 
198
198
  class TestMeta < MiniTest::Unit::TestCase
199
199
  def test_structure
200
- x = y = nil
200
+ x = y = z = nil
201
201
  x = describe "top-level thingy" do
202
202
  before {}
203
203
  after {}
@@ -207,13 +207,23 @@ class TestMeta < MiniTest::Unit::TestCase
207
207
  y = describe "inner thingy" do
208
208
  before {}
209
209
  it "inner-it" do end
210
+
211
+ z = describe "very inner thingy" do
212
+ before {}
213
+ it "inner-it" do end
214
+ end
210
215
  end
211
216
  end
212
217
 
218
+ assert_equal "top-level thingy", x.to_s
219
+ assert_equal "top-level thingy::inner thingy", y.to_s
220
+ assert_equal "top-level thingy::inner thingy::very inner thingy", z.to_s
221
+
213
222
  top_methods = %w(setup teardown test_0001_top_level_it)
214
223
  inner_methods = %w(setup test_0001_inner_it)
215
224
 
216
- assert_equal top_methods, x.instance_methods(false).sort.map {|o| o.to_s }
225
+ assert_equal top_methods, x.instance_methods(false).sort.map {|o| o.to_s }
217
226
  assert_equal inner_methods, y.instance_methods(false).sort.map {|o| o.to_s }
227
+ assert_equal inner_methods, z.instance_methods(false).sort.map {|o| o.to_s }
218
228
  end
219
229
  end
@@ -4,35 +4,31 @@ require 'minitest/unit'
4
4
 
5
5
  MiniTest::Unit.autorun
6
6
 
7
- module M; end
8
- class E < StandardError; include M; end
7
+ module MyModule; end
8
+ class AnError < StandardError; include MyModule; end
9
9
 
10
- class TestMiniTest < MiniTest::Unit::TestCase
10
+ class TestMiniTestUnit < MiniTest::Unit::TestCase
11
11
  pwd = Pathname.new(File.expand_path(Dir.pwd))
12
12
  basedir = Pathname.new(File.expand_path(MiniTest::MINI_DIR)) + 'mini'
13
13
  basedir = basedir.relative_path_from(pwd).to_s
14
14
  MINITEST_BASE_DIR = basedir[/\A\./] ? basedir : "./#{basedir}"
15
- BT_MIDDLE = ["#{MINITEST_BASE_DIR}/test.rb:165:in `run_test_suites'",
16
- "#{MINITEST_BASE_DIR}/test.rb:161:in `each'",
17
- "#{MINITEST_BASE_DIR}/test.rb:161:in `run_test_suites'",
15
+ BT_MIDDLE = ["#{MINITEST_BASE_DIR}/test.rb:161:in `each'",
18
16
  "#{MINITEST_BASE_DIR}/test.rb:158:in `each'",
19
- "#{MINITEST_BASE_DIR}/test.rb:158:in `run_test_suites'",
20
17
  "#{MINITEST_BASE_DIR}/test.rb:139:in `run'",
21
18
  "#{MINITEST_BASE_DIR}/test.rb:106:in `run'"]
22
19
 
23
20
  def assert_report expected = nil
24
- expected ||= "Test run options: --seed 42
21
+ expected ||= "Run options: --seed 42
22
+
23
+ # Running tests:
25
24
 
26
- Loaded suite blah
27
- Started
28
25
  .
29
- Finished in 0.00
30
26
 
31
- 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
27
+ Finished tests in 0.00
32
28
 
33
- Test run options: --seed 42
29
+ 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
34
30
  "
35
- output = @output.string.sub(/Finished in .*/, "Finished in 0.00")
31
+ output = @output.string.sub(/Finished tests in .*/, "Finished tests in 0.00")
36
32
  output.sub!(/Loaded suite .*/, 'Loaded suite blah')
37
33
  output.sub!(/^(\s+)(?:#{Regexp.union(__FILE__, File.expand_path(__FILE__))}):\d+:/o, '\1FILE:LINE:')
38
34
  output.sub!(/\[(?:#{Regexp.union(__FILE__, File.expand_path(__FILE__))}):\d+\]/o, '[FILE:LINE]')
@@ -45,7 +41,6 @@ Test run options: --seed 42
45
41
  @tu = MiniTest::Unit.new
46
42
  @output = StringIO.new("")
47
43
  MiniTest::Unit.output = @output
48
- assert_equal [0, 0], @tu.run_test_suites
49
44
  end
50
45
 
51
46
  def teardown
@@ -145,18 +140,6 @@ Test run options: --seed 42
145
140
  assert_match(/^Exception.*Oh no again!/m, @tu.report.first)
146
141
  end
147
142
 
148
- def test_class_run_test_suites
149
- tc = Class.new(MiniTest::Unit::TestCase) do
150
- def test_something
151
- assert true
152
- end
153
- end
154
-
155
- Object.const_set(:ATestCase, tc)
156
-
157
- assert_equal [1, 1], @tu.run_test_suites
158
- end
159
-
160
143
  def test_filter_backtrace
161
144
  # this is a semi-lame mix of relative paths.
162
145
  # I cheated by making the autotest parts not have ./
@@ -212,14 +195,15 @@ Test run options: --seed 42
212
195
 
213
196
  Object.const_set(:ATestCase, tc)
214
197
 
215
- @tu.run %w[-s 42]
198
+ @tu.run %w[--seed 42]
199
+
200
+ expected = "Run options: --seed 42
216
201
 
217
- expected = "Test run options: --seed 42
202
+ # Running tests:
218
203
 
219
- Loaded suite blah
220
- Started
221
204
  E.
222
- Finished in 0.00
205
+
206
+ Finished tests in 0.00
223
207
 
224
208
  1) Error:
225
209
  test_error(ATestCase):
@@ -227,8 +211,6 @@ RuntimeError: unhandled exception
227
211
  FILE:LINE:in `test_error'
228
212
 
229
213
  2 tests, 1 assertions, 0 failures, 1 errors, 0 skips
230
-
231
- Test run options: --seed 42
232
214
  "
233
215
  assert_report expected
234
216
  end
@@ -246,14 +228,15 @@ Test run options: --seed 42
246
228
 
247
229
  Object.const_set(:ATestCase, tc)
248
230
 
249
- @tu.run %w[-s 42]
231
+ @tu.run %w[--seed 42]
250
232
 
251
- expected = "Test run options: --seed 42
233
+ expected = "Run options: --seed 42
234
+
235
+ # Running tests:
252
236
 
253
- Loaded suite blah
254
- Started
255
237
  E
256
- Finished in 0.00
238
+
239
+ Finished tests in 0.00
257
240
 
258
241
  1) Error:
259
242
  test_something(ATestCase):
@@ -261,8 +244,6 @@ RuntimeError: unhandled exception
261
244
  FILE:LINE:in `teardown'
262
245
 
263
246
  1 tests, 1 assertions, 0 failures, 1 errors, 0 skips
264
-
265
- Test run options: --seed 42
266
247
  "
267
248
  assert_report expected
268
249
  end
@@ -280,22 +261,21 @@ Test run options: --seed 42
280
261
 
281
262
  Object.const_set(:ATestCase, tc)
282
263
 
283
- @tu.run %w[-s 42]
264
+ @tu.run %w[--seed 42]
265
+
266
+ expected = "Run options: --seed 42
284
267
 
285
- expected = "Test run options: --seed 42
268
+ # Running tests:
286
269
 
287
- Loaded suite blah
288
- Started
289
270
  F.
290
- Finished in 0.00
271
+
272
+ Finished tests in 0.00
291
273
 
292
274
  1) Failure:
293
275
  test_failure(ATestCase) [FILE:LINE]:
294
276
  Failed assertion, no message given.
295
277
 
296
278
  2 tests, 2 assertions, 1 failures, 0 errors, 0 skips
297
-
298
- Test run options: --seed 42
299
279
  "
300
280
  assert_report expected
301
281
  end
@@ -313,18 +293,17 @@ Test run options: --seed 42
313
293
 
314
294
  Object.const_set(:ATestCase, tc)
315
295
 
316
- @tu.run %w[-n /something/ -s 42]
296
+ @tu.run %w[--name /some|thing/ --seed 42]
297
+
298
+ expected = "Run options: --name \"/some|thing/\" --seed 42
317
299
 
318
- expected = "Test run options: --seed 42 --name \"/something/\"
300
+ # Running tests:
319
301
 
320
- Loaded suite blah
321
- Started
322
302
  .
323
- Finished in 0.00
324
303
 
325
- 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
304
+ Finished tests in 0.00
326
305
 
327
- Test run options: --seed 42 --name \"/something/\"
306
+ 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
328
307
  "
329
308
  assert_report expected
330
309
  end
@@ -338,7 +317,7 @@ Test run options: --seed 42 --name \"/something/\"
338
317
 
339
318
  Object.const_set(:ATestCase, tc)
340
319
 
341
- @tu.run %w[-s 42]
320
+ @tu.run %w[--seed 42]
342
321
 
343
322
  assert_report
344
323
  end
@@ -356,22 +335,21 @@ Test run options: --seed 42 --name \"/something/\"
356
335
 
357
336
  Object.const_set(:ATestCase, tc)
358
337
 
359
- @tu.run %w[-s 42]
338
+ @tu.run %w[--seed 42]
360
339
 
361
- expected = "Test run options: --seed 42
340
+ expected = "Run options: --seed 42
341
+
342
+ # Running tests:
362
343
 
363
- Loaded suite blah
364
- Started
365
344
  S.
366
- Finished in 0.00
345
+
346
+ Finished tests in 0.00
367
347
 
368
348
  1) Skipped:
369
349
  test_skip(ATestCase) [FILE:LINE]:
370
350
  not yet
371
351
 
372
352
  2 tests, 1 assertions, 0 failures, 0 errors, 1 skips
373
-
374
- Test run options: --seed 42
375
353
  "
376
354
  assert_report expected
377
355
  end
@@ -385,7 +363,7 @@ Test run options: --seed 42
385
363
  end
386
364
  end
387
365
 
388
- class TestMiniTestTestCase < MiniTest::Unit::TestCase
366
+ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
389
367
  def setup
390
368
  MiniTest::Unit::TestCase.reset
391
369
 
@@ -542,9 +520,9 @@ class TestMiniTestTestCase < MiniTest::Unit::TestCase
542
520
 
543
521
  pattern = Object.new
544
522
  def pattern.=~(other) false end
545
- def pattern.inspect; "<<Object>>" end
523
+ def pattern.inspect; "[Object]" end
546
524
 
547
- util_assert_triggered 'Expected <<Object>> to match 5.' do
525
+ util_assert_triggered 'Expected [Object] to match 5.' do
548
526
  @tc.assert_match pattern, 5
549
527
  end
550
528
  end
@@ -657,8 +635,8 @@ class TestMiniTestTestCase < MiniTest::Unit::TestCase
657
635
  end
658
636
 
659
637
  def test_assert_raises_module
660
- @tc.assert_raises M do
661
- raise E
638
+ @tc.assert_raises MyModule do
639
+ raise AnError
662
640
  end
663
641
  end
664
642
 
@@ -730,13 +708,13 @@ FILE:LINE:in `test_assert_raises_triggered_different_msg'
730
708
  def test_assert_raises_triggered_subclass
731
709
  e = assert_raises MiniTest::Assertion do
732
710
  @tc.assert_raises StandardError do
733
- raise E
711
+ raise AnError
734
712
  end
735
713
  end
736
714
 
737
715
  expected = "[StandardError] exception expected, not
738
- Class: <E>
739
- Message: <\"E\">
716
+ Class: <AnError>
717
+ Message: <\"AnError\">
740
718
  ---Backtrace---
741
719
  FILE:LINE:in `test_assert_raises_triggered_subclass'
742
720
  ---------------"
@@ -1017,9 +995,9 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
1017
995
 
1018
996
  pattern = Object.new
1019
997
  def pattern.=~(other) true end
1020
- def pattern.inspect; "<<Object>>" end
998
+ def pattern.inspect; "[Object]" end
1021
999
 
1022
- util_assert_triggered 'Expected <<Object>> to not match 5.' do
1000
+ util_assert_triggered 'Expected [Object] to not match 5.' do
1023
1001
  @tc.refute_match pattern, 5
1024
1002
  end
1025
1003
  end