1
######################################################################
2
# This file is imported from the minitest project.
3
# DO NOT make modifications in this repo. They _will_ be reverted!
4
# File a patch instead and assign it to Ryan Davis.
5
######################################################################
7
require 'minitest/autorun'
10
class MiniSpecA < MiniTest::Spec; end
11
class MiniSpecB < MiniTest::Spec; end
13
class ExampleB < ExampleA; end
15
describe MiniTest::Spec do
21
self._assertions.must_equal @assertion_count
24
# TODO: figure out how the hell to write a test for this
25
# it "will skip if there is no block"
27
it "needs to have all methods named well" do
30
methods = Object.public_instance_methods.find_all { |n| n =~ /^must|^wont/ }
31
methods.map! { |m| m.to_s } if Symbol === methods.first
33
musts, wonts = methods.sort.partition { |m| m =~ /^must/ }
35
expected_musts = %w(must_be
44
must_be_within_epsilon
54
bad = %w[not raise throw send output be_silent]
56
expected_wonts = expected_musts.map { |m| m.sub(/^must/, 'wont') }
57
expected_wonts.reject! { |m| m =~ /wont_#{Regexp.union(*bad)}/ }
59
musts.must_equal expected_musts
60
wonts.must_equal expected_wonts
63
it "needs to verify equality" do
64
(6 * 7).must_equal(42).must_equal true
65
proc { (6 * 9).must_equal(42) }.must_raise MiniTest::Assertion
68
it "needs to verify floats within a delta" do
69
(6.0 * 7).must_be_close_to(42.0).must_equal true
70
proc { 42.002.must_be_close_to 42.0 }.must_raise MiniTest::Assertion
73
it "needs to verify types of objects" do
74
(6 * 7).must_be_instance_of(Fixnum).must_equal true
75
proc { (6 * 7).must_be_instance_of String }.must_raise MiniTest::Assertion
78
it "needs to verify kinds of objects" do
81
(6 * 7).must_be_kind_of(Fixnum).must_equal true
82
(6 * 7).must_be_kind_of(Numeric).must_equal true
83
proc { (6 * 7).must_be_kind_of String }.must_raise MiniTest::Assertion
86
it "needs to verify regexp matches" do
89
"blah".must_match(/\w+/).must_equal true
90
proc { "blah".must_match(/\d+/) }.must_raise MiniTest::Assertion
93
it "needs to verify nil" do
94
nil.must_be_nil.must_equal true
95
proc { 42.must_be_nil }.must_raise MiniTest::Assertion
98
it "needs to verify using any operator" do
99
41.must_be(:<, 42).must_equal true
100
proc { 42.must_be(:<, 41) }.must_raise MiniTest::Assertion
103
it "needs to catch an expected exception" do
106
proc { raise "blah" }.must_raise RuntimeError
107
proc { raise MiniTest::Assertion }.must_raise MiniTest::Assertion
110
it "needs to catch an unexpected exception" do
114
proc { raise MiniTest::Assertion }.must_raise(RuntimeError)
115
}.must_raise MiniTest::Assertion
118
it "needs raise if an expected exception is not raised" do
121
proc { proc { 42 }.must_raise(RuntimeError) }.must_raise MiniTest::Assertion
124
it "needs to be able to catch a MiniTest::Assertion exception" do
127
proc { 1.wont_equal 1 }.must_raise MiniTest::Assertion
130
it "needs to verify using respond_to" do
131
42.must_respond_to(:+).must_equal true
132
proc { 42.must_respond_to(:clear) }.must_raise MiniTest::Assertion
135
it "needs to verify identity" do
136
1.must_be_same_as(1).must_equal true
137
proc { 1.must_be_same_as 2 }.must_raise MiniTest::Assertion
140
it "needs to verify throw" do
143
proc { throw :blah }.must_throw(:blah).must_equal true
144
proc { proc { }.must_throw(:blah) }.must_raise MiniTest::Assertion
145
proc { proc { throw :xxx }.must_throw(:blah) }.must_raise MiniTest::Assertion
148
it "needs to verify inequality" do
149
42.wont_equal(6 * 9).must_equal false
150
proc { 1.wont_equal 1 }.must_raise MiniTest::Assertion
153
it "needs to verify mismatch" do
155
"blah".wont_match(/\d+/).must_equal false
156
proc { "blah".wont_match(/\w+/) }.must_raise MiniTest::Assertion
159
it "needs to verify non-nil" do
160
42.wont_be_nil.must_equal false
161
proc { nil.wont_be_nil }.must_raise MiniTest::Assertion
164
it "needs to verify non-identity" do
165
1.wont_be_same_as(2).must_equal false
166
proc { 1.wont_be_same_as 1 }.must_raise MiniTest::Assertion
169
it "needs to verify output in stdout" do
170
proc { print "blah" }.must_output("blah").must_equal true
173
proc { print "xxx" }.must_output("blah")
174
}.must_raise MiniTest::Assertion
177
it "needs to verify output in stderr" do
178
proc { $stderr.print "blah" }.must_output(nil, "blah").must_equal true
181
proc { $stderr.print "xxx" }.must_output(nil, "blah")
182
}.must_raise MiniTest::Assertion
185
it "needs to ensure silence" do
188
proc { }.must_be_silent.must_equal true
191
proc { print "xxx" }.must_be_silent
192
}.must_raise MiniTest::Assertion
195
it "needs to be sensible about must_include order" do
197
[1, 2, 3].must_include(2).must_equal true
198
proc { [1, 2, 3].must_include 5 }.must_raise MiniTest::Assertion
201
it "needs to be sensible about wont_include order" do
203
[1, 2, 3].wont_include(5).must_equal false
204
proc { [1, 2, 3].wont_include 2 }.must_raise MiniTest::Assertion
208
describe MiniTest::Spec, :let do
209
i_suck_and_my_tests_are_order_dependent!
220
it "is evaluated once per example" do
229
it "is REALLY evaluated once per example" do
239
describe MiniTest::Spec, :subject do
240
attr_reader :subject_evaluation_count
243
@subject_evaluation_count ||= 0
244
@subject_evaluation_count += 1
245
@subject_evaluation_count
248
it "is evaluated once per example" do
251
subject_evaluation_count.must_equal 1
255
class TestMeta < MiniTest::Unit::TestCase
258
MiniTest::Unit::TestCase.reset
265
x = describe "top-level thingy" do
266
before { before_list << 1 }
267
after { after_list << 1 }
269
it "top-level-it" do end
271
y = describe "inner thingy" do
272
before { before_list << 2 }
273
after { after_list << 2 }
276
z = describe "very inner thingy" do
277
before { before_list << 3 }
278
after { after_list << 3 }
284
return x, y, z, before_list, after_list
287
def test_register_spec_type
288
original_types = MiniTest::Spec::TYPES.dup
290
assert_equal [[//, MiniTest::Spec]], MiniTest::Spec::TYPES
292
MiniTest::Spec.register_spec_type(/woot/, TestMeta)
294
p = lambda do |x| true end
295
MiniTest::Spec.register_spec_type TestMeta, &p
297
keys = MiniTest::Spec::TYPES.map(&:first)
299
assert_includes keys, /woot/
300
assert_includes keys, p
302
MiniTest::Spec::TYPES.replace original_types
306
original_types = MiniTest::Spec::TYPES.dup
308
MiniTest::Spec.register_spec_type(/A$/, MiniSpecA)
309
MiniTest::Spec.register_spec_type MiniSpecB do |desc|
310
desc.superclass == ExampleA
313
assert_equal MiniSpecA, MiniTest::Spec.spec_type(ExampleA)
314
assert_equal MiniSpecB, MiniTest::Spec.spec_type(ExampleB)
316
MiniTest::Spec::TYPES.replace original_types
320
x, y, z, * = util_structure
322
assert_equal "top-level thingy", x.to_s
323
assert_equal "top-level thingy::inner thingy", y.to_s
324
assert_equal "top-level thingy::inner thingy::very inner thingy", z.to_s
326
assert_equal "top-level thingy", x.desc
327
assert_equal "inner thingy", y.desc
328
assert_equal "very inner thingy", z.desc
330
top_methods = %w(test_0001_top_level_it)
331
inner_methods = %w(test_0001_inner_it)
333
assert_equal top_methods, x.instance_methods(false).sort.map {|o| o.to_s }
334
assert_equal inner_methods, y.instance_methods(false).sort.map {|o| o.to_s }
335
assert_equal inner_methods, z.instance_methods(false).sort.map {|o| o.to_s }
338
def test_setup_teardown_behavior
339
_, _, z, before_list, after_list = util_structure
344
tc.run_teardown_hooks
346
assert_equal [1, 2, 3], before_list
347
assert_equal [3, 2, 1], after_list
351
MiniTest::Spec.children.clear
354
x = describe "top-level thingy" do
355
y = describe "first thingy" do end
357
it "top-level-it" do end
359
z = describe "second thingy" do end
362
assert_equal [x], MiniTest::Spec.children
363
assert_equal [y, z], x.children
364
assert_equal [], y.children
365
assert_equal [], z.children
368
def test_describe_first_structure
370
x = describe "top-level thingy" do
371
y = describe "first thingy" do end
373
it "top-level-it" do end
375
z = describe "second thingy" do end
378
assert_equal ['test_0001_top_level_it'],
379
x.instance_methods.grep(/^test/).map {|o| o.to_s}
380
assert_equal [], y.instance_methods.grep(/^test/)
381
assert_equal [], z.instance_methods.grep(/^test/)
384
def test_structure_subclasses
386
x = Class.new MiniTest::Spec do
390
z = describe("inner") {}
393
assert_respond_to x.new(nil), "xyz"
394
assert_respond_to y.new(nil), "xyz"
395
assert_respond_to z.new(nil), "xyz"