~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to test/minitest/test_minitest_spec.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
######################################################################
 
6
 
 
7
require 'minitest/autorun'
 
8
require 'stringio'
 
9
 
 
10
class MiniSpecA < MiniTest::Spec; end
 
11
class MiniSpecB < MiniTest::Spec; end
 
12
class ExampleA; end
 
13
class ExampleB < ExampleA; end
 
14
 
 
15
describe MiniTest::Spec do
 
16
  before do
 
17
    @assertion_count = 4
 
18
  end
 
19
 
 
20
  after do
 
21
    self._assertions.must_equal @assertion_count
 
22
  end
 
23
 
 
24
  # TODO: figure out how the hell to write a test for this
 
25
  # it "will skip if there is no block"
 
26
 
 
27
  it "needs to have all methods named well" do
 
28
    @assertion_count = 2
 
29
 
 
30
    methods = Object.public_instance_methods.find_all { |n| n =~ /^must|^wont/ }
 
31
    methods.map! { |m| m.to_s } if Symbol === methods.first
 
32
 
 
33
    musts, wonts = methods.sort.partition { |m| m =~ /^must/ }
 
34
 
 
35
    expected_musts = %w(must_be
 
36
                        must_be_close_to
 
37
                        must_be_empty
 
38
                        must_be_instance_of
 
39
                        must_be_kind_of
 
40
                        must_be_nil
 
41
                        must_be_same_as
 
42
                        must_be_silent
 
43
                        must_be_within_delta
 
44
                        must_be_within_epsilon
 
45
                        must_equal
 
46
                        must_include
 
47
                        must_match
 
48
                        must_output
 
49
                        must_raise
 
50
                        must_respond_to
 
51
                        must_send
 
52
                        must_throw)
 
53
 
 
54
    bad = %w[not raise throw send output be_silent]
 
55
 
 
56
    expected_wonts = expected_musts.map { |m| m.sub(/^must/, 'wont') }
 
57
    expected_wonts.reject! { |m| m =~ /wont_#{Regexp.union(*bad)}/ }
 
58
 
 
59
    musts.must_equal expected_musts
 
60
    wonts.must_equal expected_wonts
 
61
  end
 
62
 
 
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
 
66
  end
 
67
 
 
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
 
71
  end
 
72
 
 
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
 
76
  end
 
77
 
 
78
  it "needs to verify kinds of objects" do
 
79
    @assertion_count = 6
 
80
 
 
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
 
84
  end
 
85
 
 
86
  it "needs to verify regexp matches" do
 
87
    @assertion_count = 6
 
88
 
 
89
    "blah".must_match(/\w+/).must_equal true
 
90
    proc { "blah".must_match(/\d+/) }.must_raise MiniTest::Assertion
 
91
  end
 
92
 
 
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
 
96
  end
 
97
 
 
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
 
101
  end
 
102
 
 
103
  it "needs to catch an expected exception" do
 
104
    @assertion_count = 2
 
105
 
 
106
    proc { raise "blah" }.must_raise RuntimeError
 
107
    proc { raise MiniTest::Assertion }.must_raise MiniTest::Assertion
 
108
  end
 
109
 
 
110
  it "needs to catch an unexpected exception" do
 
111
    @assertion_count = 2
 
112
 
 
113
    proc {
 
114
      proc { raise MiniTest::Assertion }.must_raise(RuntimeError)
 
115
    }.must_raise MiniTest::Assertion
 
116
  end
 
117
 
 
118
  it "needs raise if an expected exception is not raised" do
 
119
    @assertion_count = 2
 
120
 
 
121
    proc { proc { 42 }.must_raise(RuntimeError) }.must_raise MiniTest::Assertion
 
122
  end
 
123
 
 
124
  it "needs to be able to catch a MiniTest::Assertion exception" do
 
125
    @assertion_count = 2
 
126
 
 
127
    proc { 1.wont_equal 1 }.must_raise MiniTest::Assertion
 
128
  end
 
129
 
 
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
 
133
  end
 
134
 
 
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
 
138
  end
 
139
 
 
140
  it "needs to verify throw" do
 
141
    @assertion_count = 6
 
142
 
 
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
 
146
  end
 
147
 
 
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
 
151
  end
 
152
 
 
153
  it "needs to verify mismatch" do
 
154
    @assertion_count = 6
 
155
    "blah".wont_match(/\d+/).must_equal false
 
156
    proc { "blah".wont_match(/\w+/) }.must_raise MiniTest::Assertion
 
157
  end
 
158
 
 
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
 
162
  end
 
163
 
 
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
 
167
  end
 
168
 
 
169
  it "needs to verify output in stdout" do
 
170
    proc { print "blah" }.must_output("blah").must_equal true
 
171
 
 
172
    proc {
 
173
      proc { print "xxx" }.must_output("blah")
 
174
    }.must_raise MiniTest::Assertion
 
175
  end
 
176
 
 
177
  it "needs to verify output in stderr" do
 
178
    proc { $stderr.print "blah" }.must_output(nil, "blah").must_equal true
 
179
 
 
180
    proc {
 
181
      proc { $stderr.print "xxx" }.must_output(nil, "blah")
 
182
    }.must_raise MiniTest::Assertion
 
183
  end
 
184
 
 
185
  it "needs to ensure silence" do
 
186
    @assertion_count = 5
 
187
 
 
188
    proc {  }.must_be_silent.must_equal true
 
189
 
 
190
    proc {
 
191
      proc { print "xxx" }.must_be_silent
 
192
    }.must_raise MiniTest::Assertion
 
193
  end
 
194
 
 
195
  it "needs to be sensible about must_include order" do
 
196
    @assertion_count = 6
 
197
    [1, 2, 3].must_include(2).must_equal true
 
198
    proc { [1, 2, 3].must_include 5 }.must_raise MiniTest::Assertion
 
199
  end
 
200
 
 
201
  it "needs to be sensible about wont_include order" do
 
202
    @assertion_count = 6
 
203
    [1, 2, 3].wont_include(5).must_equal false
 
204
    proc { [1, 2, 3].wont_include 2 }.must_raise MiniTest::Assertion
 
205
  end
 
206
end
 
207
 
 
208
describe MiniTest::Spec, :let do
 
209
  i_suck_and_my_tests_are_order_dependent!
 
210
 
 
211
  def _count
 
212
    $let_count ||= 0
 
213
  end
 
214
 
 
215
  let :count do
 
216
    $let_count += 1
 
217
    $let_count
 
218
  end
 
219
 
 
220
  it "is evaluated once per example" do
 
221
    _count.must_equal 0
 
222
 
 
223
    count.must_equal 1
 
224
    count.must_equal 1
 
225
 
 
226
    _count.must_equal 1
 
227
  end
 
228
 
 
229
  it "is REALLY evaluated once per example" do
 
230
    _count.must_equal 1
 
231
 
 
232
    count.must_equal 2
 
233
    count.must_equal 2
 
234
 
 
235
    _count.must_equal 2
 
236
  end
 
237
end
 
238
 
 
239
describe MiniTest::Spec, :subject do
 
240
  attr_reader :subject_evaluation_count
 
241
 
 
242
  subject do
 
243
    @subject_evaluation_count ||= 0
 
244
    @subject_evaluation_count  += 1
 
245
    @subject_evaluation_count
 
246
  end
 
247
 
 
248
  it "is evaluated once per example" do
 
249
    subject.must_equal 1
 
250
    subject.must_equal 1
 
251
    subject_evaluation_count.must_equal 1
 
252
  end
 
253
end
 
254
 
 
255
class TestMeta < MiniTest::Unit::TestCase
 
256
  def test_setup
 
257
    srand 42
 
258
    MiniTest::Unit::TestCase.reset
 
259
  end
 
260
 
 
261
  def util_structure
 
262
    x = y = z = nil
 
263
    before_list = []
 
264
    after_list  = []
 
265
    x = describe "top-level thingy" do
 
266
      before { before_list << 1 }
 
267
      after  { after_list  << 1 }
 
268
 
 
269
      it "top-level-it" do end
 
270
 
 
271
      y = describe "inner thingy" do
 
272
        before { before_list << 2 }
 
273
        after  { after_list  << 2 }
 
274
        it "inner-it" do end
 
275
 
 
276
        z = describe "very inner thingy" do
 
277
          before { before_list << 3 }
 
278
          after  { after_list  << 3 }
 
279
          it "inner-it" do end
 
280
        end
 
281
      end
 
282
    end
 
283
 
 
284
    return x, y, z, before_list, after_list
 
285
  end
 
286
 
 
287
  def test_register_spec_type
 
288
    original_types = MiniTest::Spec::TYPES.dup
 
289
 
 
290
    assert_equal [[//, MiniTest::Spec]], MiniTest::Spec::TYPES
 
291
 
 
292
    MiniTest::Spec.register_spec_type(/woot/, TestMeta)
 
293
 
 
294
    p = lambda do |x| true end
 
295
    MiniTest::Spec.register_spec_type TestMeta, &p
 
296
 
 
297
    keys = MiniTest::Spec::TYPES.map(&:first)
 
298
 
 
299
    assert_includes keys, /woot/
 
300
    assert_includes keys, p
 
301
  ensure
 
302
    MiniTest::Spec::TYPES.replace original_types
 
303
  end
 
304
 
 
305
  def test_spec_type
 
306
    original_types = MiniTest::Spec::TYPES.dup
 
307
 
 
308
    MiniTest::Spec.register_spec_type(/A$/, MiniSpecA)
 
309
    MiniTest::Spec.register_spec_type MiniSpecB do |desc|
 
310
      desc.superclass == ExampleA
 
311
    end
 
312
 
 
313
    assert_equal MiniSpecA, MiniTest::Spec.spec_type(ExampleA)
 
314
    assert_equal MiniSpecB, MiniTest::Spec.spec_type(ExampleB)
 
315
  ensure
 
316
    MiniTest::Spec::TYPES.replace original_types
 
317
  end
 
318
 
 
319
  def test_structure
 
320
    x, y, z, * = util_structure
 
321
 
 
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
 
325
 
 
326
    assert_equal "top-level thingy", x.desc
 
327
    assert_equal "inner thingy", y.desc
 
328
    assert_equal "very inner thingy", z.desc
 
329
 
 
330
    top_methods = %w(test_0001_top_level_it)
 
331
    inner_methods = %w(test_0001_inner_it)
 
332
 
 
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 }
 
336
  end
 
337
 
 
338
  def test_setup_teardown_behavior
 
339
    _, _, z, before_list, after_list = util_structure
 
340
 
 
341
    tc = z.new(nil)
 
342
 
 
343
    tc.run_setup_hooks
 
344
    tc.run_teardown_hooks
 
345
 
 
346
    assert_equal [1, 2, 3], before_list
 
347
    assert_equal [3, 2, 1], after_list
 
348
  end
 
349
 
 
350
  def test_children
 
351
    MiniTest::Spec.children.clear
 
352
 
 
353
    x = y = z = nil
 
354
    x = describe "top-level thingy" do
 
355
      y = describe "first thingy" do end
 
356
 
 
357
      it "top-level-it" do end
 
358
 
 
359
      z = describe "second thingy" do end
 
360
    end
 
361
 
 
362
    assert_equal [x], MiniTest::Spec.children
 
363
    assert_equal [y, z], x.children
 
364
    assert_equal [], y.children
 
365
    assert_equal [], z.children
 
366
  end
 
367
 
 
368
  def test_describe_first_structure
 
369
    x = y = z = nil
 
370
    x = describe "top-level thingy" do
 
371
      y = describe "first thingy" do end
 
372
 
 
373
      it "top-level-it" do end
 
374
 
 
375
      z = describe "second thingy" do end
 
376
    end
 
377
 
 
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/)
 
382
  end
 
383
 
 
384
  def test_structure_subclasses
 
385
    z = nil
 
386
    x = Class.new MiniTest::Spec do
 
387
      def xyz; end
 
388
    end
 
389
    y = Class.new x do
 
390
      z = describe("inner") {}
 
391
    end
 
392
 
 
393
    assert_respond_to x.new(nil), "xyz"
 
394
    assert_respond_to y.new(nil), "xyz"
 
395
    assert_respond_to z.new(nil), "xyz"
 
396
  end
 
397
end