minitest 1.3.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.
@@ -0,0 +1,77 @@
1
+ require 'minitest/mock'
2
+ require 'minitest/unit'
3
+
4
+ MiniTest::Unit.autorun
5
+
6
+ class TestMiniMock < 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_should_create_stub_method
13
+ assert_nil @mock.foo
14
+ end
15
+
16
+ def test_should_allow_return_value_specification
17
+ assert_equal 42, @mock.meaning_of_life
18
+ end
19
+
20
+ def test_should_blow_up_if_not_called
21
+ @mock.foo
22
+
23
+ util_verify_bad
24
+ end
25
+
26
+ def test_should_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_should_allow_expectations_to_be_added_after_creation
34
+ @mock.expect(:bar, true)
35
+ assert @mock.bar
36
+ end
37
+
38
+ def test_should_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_should_not_verify_if_unexpected_method_is_called
47
+ assert_raises NoMethodError do
48
+ @mock.unexpected
49
+ end
50
+ end
51
+
52
+ def test_should_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_should_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 util_verify_bad
73
+ assert_raises MockExpectationError do
74
+ @mock.verify
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,149 @@
1
+ require 'minitest/spec'
2
+
3
+ MiniTest::Unit.autorun
4
+
5
+ describe MiniTest::Spec do
6
+ before do
7
+ @assertion_count = 5
8
+ end
9
+
10
+ after do
11
+ self._assertions.must_equal @assertion_count
12
+ end
13
+
14
+ it "needs to have all methods named well" do
15
+ @assertion_count = 2
16
+
17
+ methods = Object.public_instance_methods.find_all { |n| n =~ /^must|^wont/ }
18
+ methods.map! { |m| m.to_s } if Symbol === methods.first
19
+
20
+ musts, wonts = methods.sort.partition { |m| m =~ /^must/ }
21
+
22
+ expected_musts = %w(must_be
23
+ must_be_close_to
24
+ must_be_empty
25
+ must_be_instance_of
26
+ must_be_kind_of
27
+ must_be_nil
28
+ must_be_same_as
29
+ must_be_within_delta
30
+ must_be_within_epsilon
31
+ must_equal
32
+ must_include
33
+ must_match
34
+ must_raise
35
+ must_respond_to
36
+ must_send
37
+ must_throw)
38
+
39
+ expected_wonts = expected_musts.map { |m| m.sub(/^must/, 'wont') }
40
+ expected_wonts.reject! { |m| m =~ /wont_(not|raise|throw|send)/ }
41
+
42
+ musts.must_equal expected_musts
43
+ wonts.must_equal expected_wonts
44
+ end
45
+
46
+ it "needs to verify equality" do
47
+ (6 * 7).must_equal(42).must_equal true
48
+ proc { (6 * 9).must_equal(42) }.must_raise MiniTest::Assertion
49
+ end
50
+
51
+ it "needs to verify floats within a delta" do
52
+ (6.0 * 7).must_be_close_to(42.0).must_equal true
53
+ proc { 42.002.must_be_close_to 42.0 }.must_raise MiniTest::Assertion
54
+ end
55
+
56
+ it "needs to verify types of objects" do
57
+ (6 * 7).must_be_instance_of(Fixnum).must_equal true
58
+ proc { (6 * 7).must_be_instance_of String }.must_raise MiniTest::Assertion
59
+ end
60
+
61
+ it "needs to verify kinds of objects" do
62
+ @assertion_count = 7
63
+
64
+ (6 * 7).must_be_kind_of(Fixnum).must_equal true
65
+ (6 * 7).must_be_kind_of(Numeric).must_equal true
66
+ proc { (6 * 7).must_be_kind_of String }.must_raise MiniTest::Assertion
67
+ end
68
+
69
+ it "needs to verify regexp matches" do
70
+ @assertion_count = 7
71
+ "blah".must_match(/\w+/).must_equal true
72
+ proc { "blah".must_match(/\d+/) }.must_raise MiniTest::Assertion
73
+ end
74
+
75
+ it "needs to verify nil" do
76
+ nil.must_be_nil.must_equal true
77
+ proc { 42.must_be_nil }.must_raise MiniTest::Assertion
78
+ end
79
+
80
+ it "needs to verify using any operator" do
81
+ 41.must_be(:<, 42).must_equal true
82
+ proc { 42.must_be(:<, 41) }.must_raise MiniTest::Assertion
83
+ end
84
+
85
+ it "needs to catch an expected exception" do
86
+ @assertion_count = 4
87
+
88
+ proc { raise "blah" }.must_raise RuntimeError
89
+ proc { raise MiniTest::Assertion }.must_raise MiniTest::Assertion
90
+ end
91
+
92
+ it "needs to catch an unexpected exception" do
93
+ @assertion_count = 4
94
+
95
+ proc {
96
+ proc { raise MiniTest::Assertion }.must_raise(RuntimeError)
97
+ }.must_raise MiniTest::Assertion
98
+ end
99
+
100
+ it "needs raise if an expected exception is not raised" do
101
+ @assertion_count = 3
102
+
103
+ proc { proc { 42 }.must_raise(RuntimeError) }.must_raise MiniTest::Assertion
104
+ end
105
+
106
+ it "needs to be able to catch a MiniTest::Assertion exception" do
107
+ @assertion_count = 3
108
+
109
+ proc { 1.wont_equal 1 }.must_raise MiniTest::Assertion
110
+ end
111
+
112
+ it "needs to verify using respond_to" do
113
+ 42.must_respond_to(:+).must_equal true
114
+ proc { 42.must_respond_to(:clear) }.must_raise MiniTest::Assertion
115
+ end
116
+
117
+ it "needs to verify identity" do
118
+ 1.must_be_same_as(1).must_equal true
119
+ proc { 1.must_be_same_as 2 }.must_raise MiniTest::Assertion
120
+ end
121
+
122
+ it "needs to verify throw" do
123
+ @assertion_count = 8
124
+
125
+ proc { throw :blah }.must_throw(:blah).must_equal true
126
+ proc { proc { }.must_throw(:blah) }.must_raise MiniTest::Assertion
127
+ proc { proc { throw :xxx }.must_throw(:blah) }.must_raise MiniTest::Assertion
128
+ end
129
+
130
+ it "needs to verify inequality" do
131
+ 42.wont_equal(6 * 9).must_equal false
132
+ proc { 1.wont_equal 1 }.must_raise MiniTest::Assertion
133
+ end
134
+
135
+ it "needs to verify mismatch" do
136
+ "blah".wont_match(/\d+/).must_equal false
137
+ proc { "blah".wont_match(/\w+/) }.must_raise MiniTest::Assertion
138
+ end
139
+
140
+ it "needs to verify non-nil" do
141
+ 42.wont_be_nil.must_equal false
142
+ proc { nil.wont_be_nil }.must_raise MiniTest::Assertion
143
+ end
144
+
145
+ it "needs to verify non-identity" do
146
+ 1.wont_be_same_as(2).must_equal false
147
+ proc { 1.wont_be_same_as 1 }.must_raise MiniTest::Assertion
148
+ end
149
+ end
@@ -0,0 +1,829 @@
1
+ require 'stringio'
2
+ require 'minitest/unit'
3
+
4
+ MiniTest::Unit.autorun
5
+
6
+ class TestMiniTest < MiniTest::Unit::TestCase
7
+
8
+ def setup
9
+ srand 42
10
+ MiniTest::Unit::TestCase.reset
11
+ @tu = MiniTest::Unit.new
12
+ @output = StringIO.new("")
13
+ MiniTest::Unit.output = @output
14
+ assert_equal [0, 0], @tu.run_test_suites
15
+ end
16
+
17
+ def teardown
18
+ MiniTest::Unit.output = $stdout
19
+ Object.send :remove_const, :ATestCase if defined? ATestCase
20
+ end
21
+
22
+ BT_MIDDLE = ["./lib/mini/test.rb:165:in `run_test_suites'",
23
+ "./lib/mini/test.rb:161:in `each'",
24
+ "./lib/mini/test.rb:161:in `run_test_suites'",
25
+ "./lib/mini/test.rb:158:in `each'",
26
+ "./lib/mini/test.rb:158:in `run_test_suites'",
27
+ "./lib/mini/test.rb:139:in `run'",
28
+ "./lib/mini/test.rb:106:in `run'"]
29
+
30
+ def test_filter_backtrace
31
+ # this is a semi-lame mix of relative paths.
32
+ # I cheated by making the autotest parts not have ./
33
+ bt = (["lib/autotest.rb:571:in `add_exception'",
34
+ "test/test_autotest.rb:62:in `test_add_exception'",
35
+ "./lib/mini/test.rb:165:in `__send__'"] +
36
+ BT_MIDDLE +
37
+ ["./lib/mini/test.rb:29",
38
+ "test/test_autotest.rb:422"])
39
+ bt = util_expand_bt bt
40
+
41
+ ex = ["lib/autotest.rb:571:in `add_exception'",
42
+ "test/test_autotest.rb:62:in `test_add_exception'"]
43
+ ex = util_expand_bt ex
44
+
45
+ fu = MiniTest::filter_backtrace(bt)
46
+
47
+ assert_equal ex, fu
48
+ end
49
+
50
+ def util_expand_bt bt
51
+ if RUBY_VERSION =~ /^1\.9/ then
52
+ bt.map { |f| (f =~ /^\./) ? File.expand_path(f) : f }
53
+ else
54
+ bt
55
+ end
56
+ end
57
+
58
+ def test_filter_backtrace_all_unit
59
+ bt = (["./lib/mini/test.rb:165:in `__send__'"] +
60
+ BT_MIDDLE +
61
+ ["./lib/mini/test.rb:29"])
62
+ ex = bt.clone
63
+ fu = MiniTest::filter_backtrace(bt)
64
+ assert_equal ex, fu
65
+ end
66
+
67
+ def test_filter_backtrace_unit_starts
68
+ bt = (["./lib/mini/test.rb:165:in `__send__'"] +
69
+ BT_MIDDLE +
70
+ ["./lib/mini/test.rb:29",
71
+ "-e:1"])
72
+
73
+ bt = util_expand_bt bt
74
+
75
+ ex = ["-e:1"]
76
+ fu = MiniTest::filter_backtrace(bt)
77
+ assert_equal ex, fu
78
+ end
79
+
80
+ def test_class_puke_with_assertion_failed
81
+ exception = MiniTest::Assertion.new "Oh no!"
82
+ exception.set_backtrace ["unhappy"]
83
+ assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception)
84
+ assert_equal 1, @tu.failures
85
+ assert_match(/^Failure.*Oh no!/m, @tu.report.first)
86
+ end
87
+
88
+ def test_class_puke_with_failure_and_flunk_in_backtrace
89
+ exception = begin
90
+ MiniTest::Unit::TestCase.new('fake tc').flunk
91
+ rescue MiniTest::Assertion => failure
92
+ failure
93
+ end
94
+ assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception)
95
+ refute @tu.report.any?{|line| line =~ /in .flunk/}
96
+ end
97
+
98
+ def test_class_puke_with_non_failure_exception
99
+ exception = Exception.new("Oh no again!")
100
+ assert_equal 'E', @tu.puke('SomeClass', 'method_name', exception)
101
+ assert_equal 1, @tu.errors
102
+ assert_match(/^Exception.*Oh no again!/m, @tu.report.first)
103
+ end
104
+
105
+ def test_class_run_test_suites
106
+ tc = Class.new(MiniTest::Unit::TestCase) do
107
+ def test_something
108
+ assert true
109
+ end
110
+ end
111
+
112
+ Object.const_set(:ATestCase, tc)
113
+
114
+ assert_equal [1, 1], @tu.run_test_suites
115
+ end
116
+
117
+ def test_run_failing # TODO: add error test
118
+ tc = Class.new(MiniTest::Unit::TestCase) do
119
+ def test_something
120
+ assert true
121
+ end
122
+
123
+ def test_failure
124
+ assert false
125
+ end
126
+ end
127
+
128
+ Object.const_set(:ATestCase, tc)
129
+
130
+ @tu.run
131
+
132
+ expected = "Loaded suite blah
133
+ Started
134
+ F.
135
+ Finished in 0.00
136
+
137
+ 1) Failure:
138
+ test_failure(ATestCase) [FILE:LINE]:
139
+ Failed assertion, no message given.
140
+
141
+ 2 tests, 2 assertions, 1 failures, 0 errors, 0 skips
142
+ "
143
+ util_assert_report expected
144
+ end
145
+
146
+ def test_run_error
147
+ tc = Class.new(MiniTest::Unit::TestCase) do
148
+ def test_something
149
+ assert true
150
+ end
151
+
152
+ def test_error
153
+ raise "unhandled exception"
154
+ end
155
+ end
156
+
157
+ Object.const_set(:ATestCase, tc)
158
+
159
+ @tu.run
160
+
161
+ expected = "Loaded suite blah
162
+ Started
163
+ E.
164
+ Finished in 0.00
165
+
166
+ 1) Error:
167
+ test_error(ATestCase):
168
+ RuntimeError: unhandled exception
169
+ FILE:LINE:in `test_error'
170
+
171
+ 2 tests, 1 assertions, 0 failures, 1 errors, 0 skips
172
+ "
173
+ util_assert_report expected
174
+ end
175
+
176
+ def test_run_error_teardown
177
+ tc = Class.new(MiniTest::Unit::TestCase) do
178
+ def test_something
179
+ assert true
180
+ end
181
+
182
+ def teardown
183
+ raise "unhandled exception"
184
+ end
185
+ end
186
+
187
+ Object.const_set(:ATestCase, tc)
188
+
189
+ @tu.run
190
+
191
+ expected = "Loaded suite blah
192
+ Started
193
+ E
194
+ Finished in 0.00
195
+
196
+ 1) Error:
197
+ test_something(ATestCase):
198
+ RuntimeError: unhandled exception
199
+ FILE:LINE:in `teardown'
200
+
201
+ 1 tests, 1 assertions, 0 failures, 1 errors, 0 skips
202
+ "
203
+ util_assert_report expected
204
+ end
205
+
206
+ def test_run_skip
207
+ tc = Class.new(MiniTest::Unit::TestCase) do
208
+ def test_something
209
+ assert true
210
+ end
211
+
212
+ def test_skip
213
+ skip "not yet"
214
+ end
215
+ end
216
+
217
+ Object.const_set(:ATestCase, tc)
218
+
219
+ @tu.run
220
+
221
+ expected = "Loaded suite blah
222
+ Started
223
+ S.
224
+ Finished in 0.00
225
+
226
+ 1) Skipped:
227
+ test_skip(ATestCase) [FILE:LINE]:
228
+ not yet
229
+
230
+ 2 tests, 1 assertions, 0 failures, 0 errors, 1 skips
231
+ "
232
+ util_assert_report expected
233
+ end
234
+
235
+ def util_assert_report expected = nil
236
+ expected ||= "Loaded suite blah
237
+ Started
238
+ .
239
+ Finished in 0.00
240
+
241
+ 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
242
+ "
243
+ output = @output.string.sub(/Finished in .*/, "Finished in 0.00")
244
+ output.sub!(/Loaded suite .*/, 'Loaded suite blah')
245
+ output.sub!(/[\w\/\.]+:\d+/, 'FILE:LINE')
246
+ assert_equal(expected, output)
247
+ end
248
+
249
+ def test_run_failing_filtered
250
+ tc = Class.new(MiniTest::Unit::TestCase) do
251
+ def test_something
252
+ assert true
253
+ end
254
+
255
+ def test_failure
256
+ assert false
257
+ end
258
+ end
259
+
260
+ Object.const_set(:ATestCase, tc)
261
+
262
+ @tu.run(%w(-n /something/))
263
+
264
+ util_assert_report
265
+ end
266
+
267
+ def test_run_passing
268
+ tc = Class.new(MiniTest::Unit::TestCase) do
269
+ def test_something
270
+ assert true
271
+ end
272
+ end
273
+
274
+ Object.const_set(:ATestCase, tc)
275
+
276
+ @tu.run
277
+
278
+ util_assert_report
279
+ end
280
+ end
281
+
282
+ class TestMiniTestTestCase < MiniTest::Unit::TestCase
283
+ def setup
284
+ MiniTest::Unit::TestCase.reset
285
+
286
+ @tc = MiniTest::Unit::TestCase.new 'fake tc'
287
+ @zomg = "zomg ponies!"
288
+ @assertion_count = 1
289
+ end
290
+
291
+ def teardown
292
+ assert_equal(@assertion_count, @tc._assertions,
293
+ "expected #{@assertion_count} assertions to be fired during the test, not #{@tc._assertions}") if @tc._assertions
294
+ Object.send :remove_const, :ATestCase if defined? ATestCase
295
+ end
296
+
297
+ def test_class_inherited
298
+ @assertion_count = 0
299
+
300
+ Object.const_set(:ATestCase, Class.new(MiniTest::Unit::TestCase))
301
+
302
+ assert_equal [ATestCase], MiniTest::Unit::TestCase.test_suites
303
+ end
304
+
305
+ def test_class_test_suites
306
+ @assertion_count = 0
307
+
308
+ Object.const_set(:ATestCase, Class.new(MiniTest::Unit::TestCase))
309
+
310
+ assert_equal 1, MiniTest::Unit::TestCase.test_suites.size
311
+ assert_equal [ATestCase], MiniTest::Unit::TestCase.test_suites
312
+ end
313
+
314
+ def test_class_asserts_match_refutes
315
+ @assertion_count = 0
316
+
317
+ methods = MiniTest::Assertions.public_instance_methods
318
+ methods.map! { |m| m.to_s } if Symbol === methods.first
319
+
320
+ ignores = %w(assert_block assert_no_match assert_not_equal assert_not_nil
321
+ assert_not_same assert_nothing_thrown assert_raise
322
+ assert_nothing_raised assert_raises assert_throws assert_send)
323
+ asserts = methods.grep(/^assert/).sort - ignores
324
+ refutes = methods.grep(/^refute/).sort - ignores
325
+
326
+ assert_empty refutes.map { |n| n.sub(/^refute/, 'assert') } - asserts
327
+ assert_empty asserts.map { |n| n.sub(/^assert/, 'refute') } - refutes
328
+ end
329
+
330
+ def test_assert
331
+ @assertion_count = 2
332
+
333
+ @tc.assert_equal true, @tc.assert(true), "returns true on success"
334
+ end
335
+
336
+ def test_assert__triggered
337
+ util_assert_triggered "Failed assertion, no message given." do
338
+ @tc.assert false
339
+ end
340
+ end
341
+
342
+ def test_assert__triggered_message
343
+ util_assert_triggered @zomg do
344
+ @tc.assert false, @zomg
345
+ end
346
+ end
347
+
348
+ def test_assert_block
349
+ @tc.assert_block do
350
+ true
351
+ end
352
+ end
353
+
354
+ def test_assert_block_triggered
355
+ util_assert_triggered 'Expected block to return true value.' do
356
+ @tc.assert_block do
357
+ false
358
+ end
359
+ end
360
+ end
361
+
362
+ def test_assert_empty
363
+ @assertion_count = 2
364
+
365
+ @tc.assert_empty []
366
+ end
367
+
368
+ def test_assert_empty_triggered
369
+ @assertion_count = 2
370
+
371
+ util_assert_triggered "Expected [1] to be empty." do
372
+ @tc.assert_empty [1]
373
+ end
374
+ end
375
+
376
+ def test_assert_equal
377
+ @tc.assert_equal 1, 1
378
+ end
379
+
380
+ def test_assert_equal_different
381
+ util_assert_triggered "Expected 1, not 2." do
382
+ @tc.assert_equal 1, 2
383
+ end
384
+ end
385
+
386
+ def test_assert_in_delta
387
+ @tc.assert_in_delta 0.0, 1.0 / 1000, 0.1
388
+ end
389
+
390
+ def test_assert_in_delta_triggered
391
+ util_assert_triggered 'Expected 0.0 - 0.001 (0.001) to be < 1.0e-06.' do
392
+ @tc.assert_in_delta 0.0, 1.0 / 1000, 0.000001
393
+ end
394
+ end
395
+
396
+ def test_assert_in_epsilon
397
+ @assertion_count = 8
398
+
399
+ @tc.assert_in_epsilon 10000, 9991
400
+ @tc.assert_in_epsilon 9991, 10000
401
+ @tc.assert_in_epsilon 1.0, 1.001
402
+ @tc.assert_in_epsilon 1.001, 1.0
403
+
404
+ @tc.assert_in_epsilon 10000, 9999.1, 0.0001
405
+ @tc.assert_in_epsilon 9999.1, 10000, 0.0001
406
+ @tc.assert_in_epsilon 1.0, 1.0001, 0.0001
407
+ @tc.assert_in_epsilon 1.0001, 1.0, 0.0001
408
+ end
409
+
410
+ def test_assert_in_epsilon_triggered
411
+ util_assert_triggered 'Expected 10000 - 9990 (10) to be < 9.99.' do
412
+ @tc.assert_in_epsilon 10000, 9990
413
+ end
414
+ end
415
+
416
+ def test_assert_includes
417
+ @assertion_count = 2
418
+
419
+ @tc.assert_includes [true], true
420
+ end
421
+
422
+ def test_assert_includes_triggered
423
+ @assertion_count = 4
424
+
425
+ e = @tc.assert_raises MiniTest::Assertion do
426
+ @tc.assert_includes [true], false
427
+ end
428
+
429
+ expected = "Expected [true] to include false."
430
+ assert_equal expected, e.message
431
+ end
432
+
433
+ def test_assert_instance_of
434
+ @tc.assert_instance_of String, "blah"
435
+ end
436
+
437
+ def test_assert_instance_of_triggered
438
+ util_assert_triggered 'Expected "blah" to be an instance of Array, not String.' do
439
+ @tc.assert_instance_of Array, "blah"
440
+ end
441
+ end
442
+
443
+ def test_assert_kind_of
444
+ @tc.assert_kind_of String, "blah"
445
+ end
446
+
447
+ def test_assert_kind_of_triggered
448
+ util_assert_triggered 'Expected "blah" to be a kind of Array, not String.' do
449
+ @tc.assert_kind_of Array, "blah"
450
+ end
451
+ end
452
+
453
+ def test_assert_match
454
+ @assertion_count = 2
455
+ @tc.assert_match "blah blah blah", /\w+/
456
+ end
457
+
458
+ def test_assert_match_triggered
459
+ @assertion_count = 2
460
+ util_assert_triggered 'Expected /\d+/ to match "blah blah blah".' do
461
+ @tc.assert_match "blah blah blah", /\d+/
462
+ end
463
+ end
464
+
465
+ def test_assert_nil
466
+ @tc.assert_nil nil
467
+ end
468
+
469
+ def test_assert_nil_triggered
470
+ util_assert_triggered 'Expected 42 to be nil.' do
471
+ @tc.assert_nil 42
472
+ end
473
+ end
474
+
475
+ def test_assert_operator
476
+ @tc.assert_operator 2, :>, 1
477
+ end
478
+
479
+ def test_assert_operator_triggered
480
+ util_assert_triggered "Expected 2 to be < 1." do
481
+ @tc.assert_operator 2, :<, 1
482
+ end
483
+ end
484
+
485
+ def test_assert_raises
486
+ @assertion_count = 2
487
+
488
+ @tc.assert_raises RuntimeError do
489
+ raise "blah"
490
+ end
491
+ end
492
+
493
+ def test_assert_raises_triggered_different
494
+ @assertion_count = 2
495
+
496
+ e = assert_raises MiniTest::Assertion do
497
+ @tc.assert_raises RuntimeError do
498
+ raise SyntaxError, "icky"
499
+ end
500
+ end
501
+
502
+ expected = "<[RuntimeError]> exception expected, not
503
+ Class: <SyntaxError>
504
+ Message: <\"icky\">
505
+ ---Backtrace---
506
+ FILE:LINE:in `test_assert_raises_triggered_different'
507
+ ---------------.
508
+ Expected [RuntimeError] to include SyntaxError."
509
+
510
+ assert_equal expected, expected.gsub(/[\w\/\.]+:\d+/, 'FILE:LINE')
511
+ end
512
+
513
+ def test_assert_raises_triggered_none
514
+ e = assert_raises MiniTest::Assertion do
515
+ @tc.assert_raises MiniTest::Assertion do
516
+ # do nothing
517
+ end
518
+ end
519
+
520
+ expected = "MiniTest::Assertion expected but nothing was raised."
521
+
522
+ assert_equal expected, e.message
523
+ end
524
+
525
+ def test_assert_respond_to
526
+ @tc.assert_respond_to "blah", :empty?
527
+ end
528
+
529
+ def test_assert_respond_to_triggered
530
+ util_assert_triggered 'Expected "blah" (String) to respond to #rawr!.' do
531
+ @tc.assert_respond_to "blah", :rawr!
532
+ end
533
+ end
534
+
535
+ def test_assert_same
536
+ @assertion_count = 3
537
+
538
+ o = "blah"
539
+ @tc.assert_same 1, 1
540
+ @tc.assert_same :blah, :blah
541
+ @tc.assert_same o, o
542
+ end
543
+
544
+ def test_assert_same_triggered
545
+ @assertion_count = 2
546
+
547
+ util_assert_triggered 'Expected 2 (0xXXX) to be the same as 1 (0xXXX).' do
548
+ @tc.assert_same 1, 2
549
+ end
550
+
551
+ s1 = "blah"
552
+ s2 = "blah"
553
+
554
+ util_assert_triggered 'Expected "blah" (0xXXX) to be the same as "blah" (0xXXX).' do
555
+ @tc.assert_same s1, s2
556
+ end
557
+ end
558
+
559
+ def test_assert_send
560
+ @tc.assert_send [1, :<, 2]
561
+ end
562
+
563
+ def test_assert_send_bad
564
+ util_assert_triggered "Expected 1.>(*[2]) to return true." do
565
+ @tc.assert_send [1, :>, 2]
566
+ end
567
+ end
568
+
569
+ def test_assert_throws
570
+ @tc.assert_throws(:blah) do
571
+ throw :blah
572
+ end
573
+ end
574
+
575
+ def test_assert_throws_different
576
+ util_assert_triggered 'Expected :blah to have been thrown, not :not_blah.' do
577
+ @tc.assert_throws(:blah) do
578
+ throw :not_blah
579
+ end
580
+ end
581
+ end
582
+
583
+ def test_assert_throws_unthrown
584
+ util_assert_triggered 'Expected :blah to have been thrown.' do
585
+ @tc.assert_throws(:blah) do
586
+ # do nothing
587
+ end
588
+ end
589
+ end
590
+
591
+ def test_capture_io
592
+ @assertion_count = 0
593
+
594
+ out, err = capture_io do
595
+ puts 'hi'
596
+ warn 'bye!'
597
+ end
598
+
599
+ assert_equal "hi\n", out
600
+ assert_equal "bye!\n", err
601
+ end
602
+
603
+ def test_flunk
604
+ util_assert_triggered 'Epic Fail!' do
605
+ @tc.flunk
606
+ end
607
+ end
608
+
609
+ def test_flunk_message
610
+ util_assert_triggered @zomg do
611
+ @tc.flunk @zomg
612
+ end
613
+ end
614
+
615
+ def test_message
616
+ @assertion_count = 0
617
+
618
+ assert_equal "blah2.", @tc.message { "blah2" }.call
619
+ assert_equal "blah2.", @tc.message("") { "blah2" }.call
620
+ assert_equal "blah1.\nblah2.", @tc.message("blah1") { "blah2" }.call
621
+ end
622
+
623
+ def test_pass
624
+ @tc.pass
625
+ end
626
+
627
+ def test_test_methods_sorted
628
+ @assertion_count = 0
629
+
630
+ sample_test_case = Class.new(MiniTest::Unit::TestCase)
631
+
632
+ class << sample_test_case
633
+ def test_order; :sorted end
634
+ end
635
+
636
+ sample_test_case.instance_eval do
637
+ define_method :test_test3 do assert "does not matter" end
638
+ define_method :test_test2 do assert "does not matter" end
639
+ define_method :test_test1 do assert "does not matter" end
640
+ end
641
+
642
+ expected = %w(test_test1 test_test2 test_test3)
643
+ assert_equal expected, sample_test_case.test_methods
644
+ end
645
+
646
+ def test_test_methods_random
647
+ @assertion_count = 0
648
+
649
+ sample_test_case = Class.new(MiniTest::Unit::TestCase)
650
+
651
+ class << sample_test_case
652
+ def test_order; :random end
653
+ end
654
+
655
+ sample_test_case.instance_eval do
656
+ define_method :test_test1 do assert "does not matter" end
657
+ define_method :test_test2 do assert "does not matter" end
658
+ define_method :test_test3 do assert "does not matter" end
659
+ end
660
+
661
+ srand 42
662
+ expected = %w(test_test1 test_test2 test_test3)
663
+ max = expected.size
664
+ expected = expected.sort_by { rand(max) }
665
+
666
+ srand 42
667
+ result = sample_test_case.test_methods
668
+
669
+ assert_equal expected, result
670
+ end
671
+
672
+ def test_refute
673
+ @assertion_count = 2
674
+
675
+ @tc.assert_equal false, @tc.refute(false), "returns false on success"
676
+ end
677
+
678
+ def test_refute_empty
679
+ @assertion_count = 2
680
+
681
+ @tc.refute_empty [1]
682
+ end
683
+
684
+ def test_refute_empty_triggered
685
+ @assertion_count = 2
686
+
687
+ util_assert_triggered "Expected [] to not be empty." do
688
+ @tc.refute_empty []
689
+ end
690
+ end
691
+
692
+ def test_refute_equal
693
+ @tc.refute_equal "blah", "yay"
694
+ end
695
+
696
+ def test_refute_equal_triggered
697
+ util_assert_triggered 'Expected "blah" to not be equal to "blah".' do
698
+ @tc.refute_equal "blah", "blah"
699
+ end
700
+ end
701
+
702
+ def test_refute_in_delta
703
+ @tc.refute_in_delta 0.0, 1.0 / 1000, 0.000001
704
+ end
705
+
706
+ def test_refute_in_delta_triggered
707
+ util_assert_triggered 'Expected 0.0 - 0.001 (0.001) to not be < 0.1.' do
708
+ @tc.refute_in_delta 0.0, 1.0 / 1000, 0.1
709
+ end
710
+ end
711
+
712
+ def test_refute_in_epsilon
713
+ @tc.refute_in_epsilon 10000, 9990
714
+ end
715
+
716
+ def test_refute_in_epsilon_triggered
717
+ util_assert_triggered 'Expected 10000 - 9991 (9) to not be < 10.0.' do
718
+ @tc.refute_in_epsilon 10000, 9991
719
+ fail
720
+ end
721
+ end
722
+
723
+ def test_refute_includes
724
+ @assertion_count = 2
725
+
726
+ @tc.refute_includes [true], false
727
+ end
728
+
729
+ def test_refute_includes_triggered
730
+ @assertion_count = 4
731
+
732
+ e = @tc.assert_raises MiniTest::Assertion do
733
+ @tc.refute_includes [true], true
734
+ end
735
+
736
+ expected = "Expected [true] to not include true."
737
+ assert_equal expected, e.message
738
+ end
739
+
740
+ def test_refute_instance_of
741
+ @tc.refute_instance_of Array, "blah"
742
+ end
743
+
744
+ def test_refute_instance_of_triggered
745
+ util_assert_triggered 'Expected "blah" to not be an instance of String.' do
746
+ @tc.refute_instance_of String, "blah"
747
+ end
748
+ end
749
+
750
+ def test_refute_kind_of
751
+ @tc.refute_kind_of Array, "blah"
752
+ end
753
+
754
+ def test_refute_kind_of_triggered
755
+ util_assert_triggered 'Expected "blah" to not be a kind of String.' do
756
+ @tc.refute_kind_of String, "blah"
757
+ end
758
+ end
759
+
760
+ def test_refute_match
761
+ @tc.refute_match "blah blah blah", /\d+/
762
+ end
763
+
764
+ def test_refute_match_triggered
765
+ util_assert_triggered 'Expected /\w+/ to not match "blah blah blah".' do
766
+ @tc.refute_match "blah blah blah", /\w+/
767
+ end
768
+ end
769
+
770
+ def test_refute_nil
771
+ @tc.refute_nil 42
772
+ end
773
+
774
+ def test_refute_nil_triggered
775
+ util_assert_triggered 'Expected nil to not be nil.' do
776
+ @tc.refute_nil nil
777
+ end
778
+ end
779
+
780
+ def test_refute_operator
781
+ @tc.refute_operator 2, :<, 1
782
+ end
783
+
784
+ def test_refute_operator_triggered
785
+ util_assert_triggered "Expected 2 to not be > 1." do
786
+ @tc.refute_operator 2, :>, 1
787
+ end
788
+ end
789
+
790
+ def test_refute_respond_to
791
+ @tc.refute_respond_to "blah", :rawr!
792
+ end
793
+
794
+ def test_refute_respond_to_triggered
795
+ util_assert_triggered 'Expected "blah" to not respond to empty?.' do
796
+ @tc.refute_respond_to "blah", :empty?
797
+ end
798
+ end
799
+
800
+ def test_refute_same
801
+ @tc.refute_same 1, 2
802
+ end
803
+
804
+ # TODO: "with id <id>" crap from assertions.rb
805
+ def test_refute_same_triggered
806
+ util_assert_triggered 'Expected 1 to not be the same as 1.' do
807
+ @tc.refute_same 1, 1
808
+ end
809
+ end
810
+
811
+ def test_skip
812
+ @assertion_count = 0
813
+
814
+ util_assert_triggered "haha!", MiniTest::Skip do
815
+ @tc.skip "haha!"
816
+ end
817
+ end
818
+
819
+ def util_assert_triggered expected, klass = MiniTest::Assertion
820
+ e = assert_raises(klass) do
821
+ yield
822
+ end
823
+
824
+ msg = e.message.sub(/(---Backtrace---).*/m, '\1')
825
+ msg.gsub!(/\(0x[0-9a-f]+\)/, '(0xXXX)')
826
+
827
+ assert_equal expected, msg
828
+ end
829
+ end