mattly-minitest 1.4.2.1

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