nullstyle-test-spec 0.4.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,118 @@
1
+ # Adapted from mocha (http://mocha.rubyforge.org/).
2
+ #
3
+ # Copyright (C) 2006 Revieworld Ltd.
4
+ #
5
+ # You may use, copy and redistribute this library under the same terms
6
+ # as Ruby itself (see www.ruby-lang.org/en/LICENSE.txt) or under the
7
+ # MIT license (see MIT-LICENSE file).
8
+
9
+ require 'test/spec'
10
+
11
+ $: << "/home/chris/src/mocha-0.3.2/lib"
12
+
13
+ begin
14
+ require 'mocha'
15
+ rescue LoadError
16
+ context "mocha" do
17
+ specify "can not be found. BAIL OUT!" do
18
+ end
19
+ end
20
+ else
21
+
22
+ context "mocha" do
23
+ specify "works with test/spec" do
24
+ object = mock()
25
+ object.expects(:expected_method).with(:p1, :p2).returns(:result)
26
+ object.expected_method(:p1, :p2).should.equal :result
27
+ end
28
+ end
29
+
30
+ class Enterprise
31
+ def initialize(dilithium)
32
+ @dilithium = dilithium
33
+ end
34
+
35
+ def go(warp_factor)
36
+ warp_factor.times { @dilithium.nuke(:anti_matter) }
37
+ end
38
+ end
39
+
40
+ context "mocha" do
41
+ specify "works with test/spec and Enterprise example" do
42
+ dilithium = mock()
43
+ dilithium.expects(:nuke).with(:anti_matter).at_least_once # auto-verified at end of test
44
+ enterprise = Enterprise.new(dilithium)
45
+ enterprise.go(2)
46
+ end
47
+ end
48
+
49
+ end # if not rescue LoadError
50
+
51
+
52
+ begin
53
+ require 'stubba'
54
+ rescue LoadError
55
+ context "stubba" do
56
+ specify "can not be found. BAIL OUT!" do
57
+ end
58
+ end
59
+ else
60
+
61
+ class Order
62
+ attr_accessor :shipped_on
63
+
64
+ def total_cost
65
+ line_items.inject(0) { |total, line_item| total + line_item.price } + shipping_cost
66
+ end
67
+
68
+ def total_weight
69
+ line_items.inject(0) { |total, line_item| total + line_item.weight }
70
+ end
71
+
72
+ def shipping_cost
73
+ total_weight * 5 + 10
74
+ end
75
+
76
+ class << self
77
+
78
+ def find_all
79
+ # Database.connection.execute('select * from orders...
80
+ end
81
+
82
+ def number_shipped_since(date)
83
+ find_all.select { |order| order.shipped_on > date }.size
84
+ end
85
+
86
+ def unshipped_value
87
+ find_all.inject(0) { |total, order| order.shipped_on ? total : total + order.total_cost }
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+
94
+ context "stubba" do
95
+ specify "works with test/spec and instance method stubbing" do
96
+ order = Order.new
97
+ order.stubs(:total_weight).returns(10)
98
+ order.shipping_cost.should.equal 60
99
+ end
100
+
101
+ specify "works with test/spec and class method stubbing" do
102
+ now = Time.now; week_in_secs = 7 * 24 * 60 * 60
103
+ order_1 = Order.new; order_1.shipped_on = now - 1 * week_in_secs
104
+ order_2 = Order.new; order_2.shipped_on = now - 3 * week_in_secs
105
+ Order.stubs(:find_all).returns([order_1, order_2])
106
+ Order.number_shipped_since(now - 2 * week_in_secs).should.equal 1
107
+ end
108
+
109
+ specify "works with test/spec and global instance method stubbing" do
110
+ Order.stubs(:find_all).returns([Order.new, Order.new, Order.new])
111
+ Order.any_instance.stubs(:shipped_on).returns(nil)
112
+ Order.any_instance.stubs(:total_cost).returns(10)
113
+ Order.unshipped_value.should.equal 30
114
+ end
115
+ end
116
+
117
+ end # if not rescue LoadError
118
+
@@ -0,0 +1,26 @@
1
+ require 'test/spec'
2
+
3
+ context "Empty context" do
4
+ # should.not.raise
5
+ end
6
+
7
+ context "Outer context" do
8
+ context "Inner context" do
9
+ specify "is nested" do
10
+ end
11
+ specify "has multiple empty specifications" do
12
+ end
13
+ end
14
+ context "Second Inner context" do
15
+ context "Inmost context" do
16
+ specify "works too!" do
17
+ end
18
+ specify "whoo!" do
19
+ end
20
+ end
21
+ specify "is indented properly" do
22
+ end
23
+ specify "still runs in order of definition" do
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,80 @@
1
+ describe_shared "A new-style shared description" do
2
+ it "should work as well with shared descriptions" do
3
+ true.should.be true
4
+ end
5
+ end
6
+
7
+ describe "A new-style description" do
8
+ before do
9
+ @before = true
10
+ @a = 2
11
+ end
12
+
13
+ before(:each) do
14
+ @before_each = true
15
+ end
16
+
17
+ before(:all) do
18
+ $before_all = true
19
+ end
20
+
21
+ it "should run before-clauses" do
22
+ $before_all.should.be true
23
+ @before.should.be true
24
+ @before_each.should.be true
25
+ end
26
+
27
+ it "should behave like context/specify" do
28
+ (1+1).should.equal 2
29
+ end
30
+
31
+ xit "this is disabled" do
32
+ bla
33
+ end
34
+
35
+ after do
36
+ @a.should.equal 2
37
+ @a = 3
38
+ end
39
+
40
+ after(:each) do
41
+ @a.should.equal 3
42
+ end
43
+
44
+ after(:all) do
45
+ @b = 1
46
+ end
47
+
48
+ after(:all) do
49
+ @b.should.equal 1
50
+ end
51
+
52
+ $describescope = self
53
+ it "should raise on unimplement{ed,able} before/after" do
54
+ lambda {
55
+ $describescope.before(:foo) {}
56
+ }.should.raise(ArgumentError)
57
+ lambda {
58
+ $describescope.after(:foo) {}
59
+ }.should.raise(ArgumentError)
60
+
61
+ lambda {
62
+ context "foo" do
63
+ end
64
+ }.should.raise(Test::Spec::DefinitionError)
65
+ end
66
+
67
+ describe "when nested" do
68
+ it "should work" do
69
+ end
70
+ end
71
+
72
+ behaves_like "A new-style shared description"
73
+ end
74
+
75
+ describe "An empty description" do
76
+ end
77
+
78
+ xdescribe "An disabled description" do
79
+ it "should not be run"
80
+ end
@@ -0,0 +1,26 @@
1
+ $: << File.dirname(__FILE__) + '/../lib/'
2
+ require 'test/spec'
3
+ require 'test/spec/should-output'
4
+
5
+ context "should.output" do
6
+ specify "works for print" do
7
+ lambda { print "foo" }.should.output "foo"
8
+ lambda { print "foo" }.should.output(/oo/)
9
+ end
10
+
11
+ specify "works for puts" do
12
+ lambda { puts "foo" }.should.output "foo\n"
13
+ lambda { puts "foo" }.should.output(/foo/)
14
+ end
15
+
16
+ specify "works with readline" do
17
+ lambda { require 'readline' }.should.not.raise(LoadError)
18
+ lambda { puts "foo" }.should.output "foo\n"
19
+ lambda { puts "foo" }.should.output(/foo/)
20
+
21
+ File.should.not.exist(File.join(Dir.tmpdir, "should_output_#{$$}"))
22
+ end
23
+ end
24
+
25
+
26
+
@@ -0,0 +1,700 @@
1
+ $: << File.dirname(__FILE__) + '/../lib/'
2
+ require 'test/spec'
3
+
4
+ $WARNING = ""
5
+ class Object
6
+ def warn(msg)
7
+ $WARNING << msg.to_s
8
+ super msg
9
+ end
10
+ end
11
+
12
+ class Test::Spec::Should
13
+ def _warn
14
+ _wrap_assertion {
15
+ begin
16
+ old, $-w = $-w, nil
17
+ $WARNING = ""
18
+ self.not.raise
19
+ $WARNING.should.blaming("no warning printed").not.be.empty
20
+ ensure
21
+ $-w = old
22
+ end
23
+ }
24
+ end
25
+ end
26
+
27
+ # Hooray for meta-testing.
28
+ module MetaTests
29
+ class ShouldFail < Test::Spec::CustomShould
30
+ def initialize
31
+ end
32
+
33
+ def assumptions(block)
34
+ block.should.raise(Test::Unit::AssertionFailedError)
35
+ end
36
+
37
+ def failure_message
38
+ "Block did not fail."
39
+ end
40
+ end
41
+
42
+ class ShouldSucceed < Test::Spec::CustomShould
43
+ def initialize
44
+ end
45
+
46
+ def assumptions(block)
47
+ block.should.not.raise(Test::Unit::AssertionFailedError)
48
+ end
49
+
50
+ def failure_message
51
+ "Block raised Test::Unit::AssertionFailedError."
52
+ end
53
+ end
54
+
55
+ class ShouldBeDeprecated < Test::Spec::CustomShould
56
+ def initialize
57
+ end
58
+
59
+ def assumptions(block)
60
+ block.should._warn
61
+ $WARNING.should =~ /deprecated/
62
+ end
63
+
64
+ def failure_message
65
+ "warning was not a deprecation"
66
+ end
67
+ end
68
+
69
+
70
+ def fail
71
+ ShouldFail.new
72
+ end
73
+
74
+ def succeed
75
+ ShouldSucceed.new
76
+ end
77
+
78
+ def deprecated
79
+ ShouldBeDeprecated.new
80
+ end
81
+ end
82
+
83
+ module TestShoulds
84
+ class EmptyShould < Test::Spec::CustomShould
85
+ end
86
+
87
+ class EqualString < Test::Spec::CustomShould
88
+ def matches?(other)
89
+ object == other.to_s
90
+ end
91
+ end
92
+
93
+ class EqualString2 < Test::Spec::CustomShould
94
+ def matches?(other)
95
+ object == other.to_s
96
+ end
97
+
98
+ def failure_message
99
+ "yada yada yada"
100
+ end
101
+ end
102
+
103
+ def empty_should(obj)
104
+ EmptyShould.new(obj)
105
+ end
106
+
107
+ def equal_string(str)
108
+ EqualString.new(str)
109
+ end
110
+
111
+ def equal_string2(str)
112
+ EqualString2.new(str)
113
+ end
114
+ end
115
+
116
+ context "test/spec" do
117
+ include MetaTests
118
+
119
+ specify "has should.satisfy" do
120
+ lambda { should.satisfy { 1 == 1 } }.should succeed
121
+ lambda { should.satisfy { 1 } }.should succeed
122
+
123
+ lambda { should.satisfy { 1 == 2 } }.should fail
124
+ lambda { should.satisfy { false } }.should fail
125
+ lambda { should.satisfy { false } }.should fail
126
+
127
+ lambda { 1.should.satisfy { |n| n % 2 == 0 } }.should fail
128
+ lambda { 2.should.satisfy { |n| n % 2 == 0 } }.should succeed
129
+ end
130
+
131
+ specify "has should.equal" do
132
+ lambda { "string1".should.equal "string1" }.should succeed
133
+ lambda { "string1".should.equal "string2" }.should fail
134
+ lambda { "1".should.equal 1 }.should fail
135
+
136
+ lambda { "string1".should == "string1" }.should succeed
137
+ lambda { "string1".should == "string2" }.should fail
138
+ lambda { "1".should == 1 }.should fail
139
+ end
140
+
141
+ specify "has should.raise" do
142
+ lambda { lambda { raise "Error" }.should.raise }.should succeed
143
+ lambda { lambda { raise "Error" }.should.raise RuntimeError }.should succeed
144
+ lambda { lambda { raise "Error" }.should.not.raise }.should fail
145
+ lambda { lambda { raise "Error" }.should.not.raise(RuntimeError) }.should fail
146
+
147
+ lambda { lambda { 1 + 1 }.should.raise }.should fail
148
+ lambda { lambda { raise "Error" }.should.raise(Interrupt) }.should fail
149
+ end
150
+
151
+ specify "has should.raise with a block" do
152
+ lambda { should.raise { raise "Error" } }.should succeed
153
+ lambda { should.raise(RuntimeError) { raise "Error" } }.should succeed
154
+ lambda { should.not.raise { raise "Error" } }.should fail
155
+ lambda { should.not.raise(RuntimeError) { raise "Error" } }.should fail
156
+
157
+ lambda { should.raise { 1 + 1 } }.should fail
158
+ lambda { should.raise(Interrupt) { raise "Error" } }.should fail
159
+ end
160
+
161
+ specify "should.raise should return the exception" do
162
+ ex = lambda { raise "foo!" }.should.raise
163
+ ex.should.be.kind_of RuntimeError
164
+ ex.message.should.match(/foo/)
165
+ end
166
+
167
+ specify "has should.be.an.instance_of" do
168
+ lambda {
169
+ lambda { "string".should.be_an_instance_of String }.should succeed
170
+ }.should.be deprecated
171
+ lambda {
172
+ lambda { "string".should.be_an_instance_of Hash }.should fail
173
+ }.should.be deprecated
174
+
175
+ lambda { "string".should.be.instance_of String }.should succeed
176
+ lambda { "string".should.be.instance_of Hash }.should fail
177
+
178
+ lambda { "string".should.be.an.instance_of String }.should succeed
179
+ lambda { "string".should.be.an.instance_of Hash }.should fail
180
+ end
181
+
182
+ specify "has should.be.nil" do
183
+ lambda { nil.should.be.nil }.should succeed
184
+ lambda { nil.should.be nil }.should succeed
185
+ lambda { nil.should.be_nil }.should.be deprecated
186
+
187
+ lambda { nil.should.not.be.nil }.should fail
188
+ lambda { nil.should.not.be nil }.should fail
189
+ lambda { lambda { nil.should.not.be_nil }.should fail }.should.be deprecated
190
+
191
+ lambda { "foo".should.be.nil }.should fail
192
+ lambda { "bar".should.be nil }.should fail
193
+
194
+ lambda { "foo".should.not.be.nil }.should succeed
195
+ lambda { "bar".should.not.be nil }.should succeed
196
+ end
197
+
198
+ specify "has should.include" do
199
+ lambda { [1,2,3].should.include 2 }.should succeed
200
+ lambda { [1,2,3].should.include 4 }.should fail
201
+
202
+ lambda { {1=>2, 3=>4}.should.include 1 }.should succeed
203
+ lambda { {1=>2, 3=>4}.should.include 2 }.should fail
204
+ end
205
+
206
+ specify "has should.be.a.kind_of" do
207
+ lambda { Array.should.be.kind_of Module }.should succeed
208
+ lambda { "string".should.be.kind_of Object }.should succeed
209
+ lambda { 1.should.be.kind_of Comparable }.should succeed
210
+
211
+ lambda { Array.should.be.a.kind_of Module }.should succeed
212
+
213
+ lambda { "string".should.be.a.kind_of Class }.should fail
214
+ lambda {
215
+ lambda { "string".should.be_a_kind_of Class }.should fail
216
+ }.should.be deprecated
217
+
218
+ lambda { Array.should.be_a_kind_of Module }.should.be deprecated
219
+ lambda { "string".should.be_a_kind_of Object }.should.be deprecated
220
+ lambda { 1.should.be_a_kind_of Comparable }.should.be deprecated
221
+ end
222
+
223
+ specify "has should.match" do
224
+ lambda { "string".should.match(/strin./) }.should succeed
225
+ lambda { "string".should.match("strin") }.should succeed
226
+ lambda { "string".should =~ /strin./ }.should succeed
227
+ lambda { "string".should =~ "strin" }.should succeed
228
+
229
+ lambda { "string".should.match(/slin./) }.should fail
230
+ lambda { "string".should.match("slin") }.should fail
231
+ lambda { "string".should =~ /slin./ }.should fail
232
+ lambda { "string".should =~ "slin" }.should fail
233
+ end
234
+
235
+ specify "has should.be" do
236
+ thing = "thing"
237
+ lambda { thing.should.be thing }.should succeed
238
+ lambda { thing.should.be "thing" }.should fail
239
+
240
+ lambda { 1.should.be(2, 3) }.should.raise(ArgumentError)
241
+ end
242
+
243
+ specify "has should.not.raise" do
244
+ lambda { lambda { 1 + 1 }.should.not.raise }.should succeed
245
+ lambda { lambda { 1 + 1 }.should.not.raise(Interrupt) }.should succeed
246
+
247
+ lambda {
248
+ begin
249
+ lambda {
250
+ raise ZeroDivisionError.new("ArgumentError")
251
+ }.should.not.raise(RuntimeError, StandardError, Comparable)
252
+ rescue ZeroDivisionError
253
+ end
254
+ }.should succeed
255
+
256
+ lambda { lambda { raise "Error" }.should.not.raise }.should fail
257
+ end
258
+
259
+ specify "has should.not.satisfy" do
260
+ lambda { should.not.satisfy { 1 == 2 } }.should succeed
261
+ lambda { should.not.satisfy { 1 == 1 } }.should fail
262
+ end
263
+
264
+ specify "has should.not.be" do
265
+ thing = "thing"
266
+ lambda { thing.should.not.be "thing" }.should succeed
267
+ lambda { thing.should.not.be thing }.should fail
268
+
269
+ lambda { thing.should.not.be thing, thing }.should.raise(ArgumentError)
270
+ end
271
+
272
+ specify "has should.not.equal" do
273
+ lambda { "string1".should.not.equal "string2" }.should succeed
274
+ lambda { "string1".should.not.equal "string1" }.should fail
275
+ end
276
+
277
+ specify "has should.not.match" do
278
+ lambda { "string".should.not.match(/sling/) }.should succeed
279
+ lambda { "string".should.not.match(/string/) }.should fail
280
+ lambda { "string".should.not.match("strin") }.should fail
281
+
282
+ lambda { "string".should.not =~ /sling/ }.should succeed
283
+ lambda { "string".should.not =~ /string/ }.should fail
284
+ lambda { "string".should.not =~ "strin" }.should fail
285
+ end
286
+
287
+ specify "has should.throw" do
288
+ lambda { lambda { throw :thing }.should.throw(:thing) }.should succeed
289
+
290
+ lambda { lambda { throw :thing2 }.should.throw(:thing) }.should fail
291
+ lambda { lambda { 1 + 1 }.should.throw(:thing) }.should fail
292
+ end
293
+
294
+ specify "has should.not.throw" do
295
+ lambda { lambda { 1 + 1 }.should.not.throw }.should succeed
296
+ lambda { lambda { throw :thing }.should.not.throw }.should fail
297
+ end
298
+
299
+ specify "has should.respond_to" do
300
+ lambda { "foo".should.respond_to :to_s }.should succeed
301
+ lambda { 5.should.respond_to :to_str }.should fail
302
+ lambda { :foo.should.respond_to :nx }.should fail
303
+ end
304
+
305
+ specify "has should.be_close" do
306
+ lambda { 1.4.should.be.close 1.4, 0 }.should succeed
307
+ lambda { 0.4.should.be.close 0.5, 0.1 }.should succeed
308
+
309
+ lambda {
310
+ lambda { 1.4.should.be_close 1.4, 0 }.should succeed
311
+ }.should.be deprecated
312
+ lambda {
313
+ lambda { 0.4.should.be_close 0.5, 0.1 }.should succeed
314
+ }.should.be deprecated
315
+
316
+ lambda {
317
+ float_thing = Object.new
318
+ def float_thing.to_f
319
+ 0.2
320
+ end
321
+ float_thing.should.be.close 0.1, 0.1
322
+ }.should succeed
323
+
324
+ lambda { 0.4.should.be.close 0.5, 0.05 }.should fail
325
+ lambda { 0.4.should.be.close Object.new, 0.1 }.should fail
326
+ lambda { 0.4.should.be.close 0.5, -0.1 }.should fail
327
+ end
328
+
329
+ specify "multiple negation works" do
330
+ lambda { 1.should.equal 1 }.should succeed
331
+ lambda { 1.should.not.equal 1 }.should fail
332
+ lambda { 1.should.not.not.equal 1 }.should succeed
333
+ lambda { 1.should.not.not.not.equal 1 }.should fail
334
+
335
+ lambda { 1.should.equal 2 }.should fail
336
+ lambda { 1.should.not.equal 2 }.should succeed
337
+ lambda { 1.should.not.not.equal 2 }.should fail
338
+ lambda { 1.should.not.not.not.equal 2 }.should succeed
339
+ end
340
+
341
+ specify "has should.<predicate>" do
342
+ lambda { [].should.be.empty }.should succeed
343
+ lambda { [1,2,3].should.not.be.empty }.should succeed
344
+
345
+ lambda { [].should.not.be.empty }.should fail
346
+ lambda { [1,2,3].should.be.empty }.should fail
347
+
348
+ lambda { {1=>2, 3=>4}.should.has_key 1 }.should succeed
349
+ lambda { {1=>2, 3=>4}.should.not.has_key 2 }.should succeed
350
+
351
+ lambda { nil.should.bla }.should.raise(NoMethodError)
352
+ lambda { nil.should.not.bla }.should.raise(NoMethodError)
353
+ end
354
+
355
+ specify "has should.<predicate>?" do
356
+ lambda { [].should.be.empty? }.should succeed
357
+ lambda { [1,2,3].should.not.be.empty? }.should succeed
358
+
359
+ lambda { [].should.not.be.empty? }.should fail
360
+ lambda { [1,2,3].should.be.empty? }.should fail
361
+
362
+ lambda { {1=>2, 3=>4}.should.has_key? 1 }.should succeed
363
+ lambda { {1=>2, 3=>4}.should.not.has_key? 2 }.should succeed
364
+
365
+ lambda { nil.should.bla? }.should.raise(NoMethodError)
366
+ lambda { nil.should.not.bla? }.should.raise(NoMethodError)
367
+ end
368
+
369
+ specify "has should <operator> (>, >=, <, <=, ===)" do
370
+ lambda { 2.should.be > 1 }.should succeed
371
+ lambda { 1.should.be > 2 }.should fail
372
+
373
+ lambda { 1.should.be < 2 }.should succeed
374
+ lambda { 2.should.be < 1 }.should fail
375
+
376
+ lambda { 2.should.be >= 1 }.should succeed
377
+ lambda { 2.should.be >= 2 }.should succeed
378
+ lambda { 2.should.be >= 2.1 }.should fail
379
+
380
+ lambda { 2.should.be <= 1 }.should fail
381
+ lambda { 2.should.be <= 2 }.should succeed
382
+ lambda { 2.should.be <= 2.1 }.should succeed
383
+
384
+ lambda { Array.should === [1,2,3] }.should succeed
385
+ lambda { Integer.should === [1,2,3] }.should fail
386
+
387
+ lambda { /foo/.should === "foobar" }.should succeed
388
+ lambda { "foobar".should === /foo/ }.should fail
389
+ end
390
+
391
+ $contextscope = self
392
+ specify "is robust against careless users" do
393
+ lambda {
394
+ $contextscope.specify
395
+ }.should.raise(ArgumentError)
396
+ lambda {
397
+ $contextscope.xspecify
398
+ }.should.raise(ArgumentError)
399
+ lambda {
400
+ $contextscope.xspecify "foo"
401
+ }.should.not.raise(ArgumentError) # allow empty xspecifys
402
+ lambda {
403
+ Kernel.send(:context, "foo")
404
+ }.should.raise(ArgumentError)
405
+ lambda {
406
+ context "foo" do
407
+ end
408
+ }.should.raise(Test::Spec::DefinitionError)
409
+ end
410
+
411
+ specify "should detect warnings" do
412
+ lambda { lambda { 0 }.should._warn }.should fail
413
+ lambda { lambda { warn "just a test" }.should._warn }.should succeed
414
+ end
415
+
416
+ specify "should message/blame faults" do
417
+ begin
418
+ 2.should.blaming("Two is not two anymore!").equal 3
419
+ rescue Test::Unit::AssertionFailedError => e
420
+ e.message.should =~ /Two/
421
+ end
422
+
423
+ begin
424
+ 2.should.messaging("Two is not two anymore!").equal 3
425
+ rescue Test::Unit::AssertionFailedError => e
426
+ e.message.should =~ /Two/
427
+ end
428
+
429
+ begin
430
+ 2.should.blaming("I thought two was three").not.equal 2
431
+ rescue Test::Unit::AssertionFailedError => e
432
+ e.message.should =~ /three/
433
+ end
434
+
435
+ begin
436
+ 2.should.blaming("I thought two was three").not.not.not.equal 3
437
+ rescue Test::Unit::AssertionFailedError => e
438
+ e.message.should =~ /three/
439
+ end
440
+
441
+ lambda {
442
+ lambda { raise "Error" }.should.messaging("Duh.").not.raise
443
+ }.should.raise(Test::Unit::AssertionFailedError).message.should =~ /Duh/
444
+ end
445
+
446
+ include TestShoulds
447
+ specify "should allow for custom shoulds" do
448
+ lambda { (1+1).should equal_string("2") }.should succeed
449
+ lambda { (1+2).should equal_string("2") }.should fail
450
+
451
+ lambda { (1+1).should.pass equal_string("2") }.should succeed
452
+ lambda { (1+2).should.pass equal_string("2") }.should fail
453
+
454
+ lambda { (1+1).should.be equal_string("2") }.should succeed
455
+ lambda { (1+2).should.be equal_string("2") }.should fail
456
+
457
+ lambda { (1+1).should.not equal_string("2") }.should fail
458
+ lambda { (1+2).should.not equal_string("2") }.should succeed
459
+ lambda { (1+2).should.not.not equal_string("2") }.should fail
460
+
461
+ lambda { (1+1).should.not.pass equal_string("2") }.should fail
462
+ lambda { (1+2).should.not.pass equal_string("2") }.should succeed
463
+
464
+ lambda { (1+1).should.not.be equal_string("2") }.should fail
465
+ lambda { (1+2).should.not.be equal_string("2") }.should succeed
466
+
467
+ lambda {
468
+ (1+2).should equal_string("2")
469
+ }.should.raise(Test::Unit::AssertionFailedError).message.should =~ /EqualString/
470
+
471
+ lambda { (1+1).should equal_string2("2") }.should succeed
472
+ lambda { (1+2).should equal_string2("2") }.should fail
473
+
474
+ lambda {
475
+ (1+2).should equal_string2("2")
476
+ }.should.raise(Test::Unit::AssertionFailedError).message.should =~ /yada/
477
+
478
+ lambda {
479
+ (1+2).should.blaming("foo").pass equal_string2("2")
480
+ }.should.raise(Test::Unit::AssertionFailedError).message.should =~ /foo/
481
+
482
+ lambda {
483
+ nil.should empty_should(nil)
484
+ }.should.raise(NotImplementedError)
485
+
486
+ lambda { nil.should :break, :now }.should.raise(ArgumentError)
487
+ lambda { nil.should.not :pass, :now }.should.raise(ArgumentError)
488
+ lambda { nil.should.not.not :break, :now }.should.raise(ArgumentError)
489
+ end
490
+
491
+ xspecify "disabled specification" do
492
+ # just trying
493
+ end
494
+
495
+ xspecify "empty specification"
496
+ # specify "another empty specification"
497
+
498
+ context "more disabled" do
499
+ xspecify "this is intentional" do
500
+ # ...
501
+ end
502
+
503
+ specify "an empty specification" do
504
+ # ...
505
+ end
506
+
507
+ xcontext "even more disabled" do
508
+ specify "we can cut out" do
509
+ # ...
510
+ end
511
+
512
+ specify "entire contexts, now" do
513
+ # ...
514
+ end
515
+ end
516
+ end
517
+ end
518
+
519
+ context "setup/teardown" do
520
+ setup do
521
+ @a = 1
522
+ @b = 2
523
+ end
524
+
525
+ setup do
526
+ @a = 2
527
+ end
528
+
529
+ teardown do
530
+ @a.should.equal 2
531
+ @a = 3
532
+ end
533
+
534
+ teardown do
535
+ @a.should.equal 3
536
+ end
537
+
538
+ specify "run in the right order" do
539
+ @a.should.equal 2
540
+ @b.should.equal 2
541
+ end
542
+ end
543
+
544
+ context "before all" do
545
+ before(:all) { @a = 1 }
546
+
547
+ specify "runs parent before all" do
548
+ @a.should == 1
549
+ end
550
+ end
551
+
552
+ context "nested teardown" do
553
+ context "nested" do
554
+ specify "should call local teardown then parent teardown" do
555
+ @a = 3
556
+ end
557
+
558
+ teardown do
559
+ p "local td"
560
+ @a = 2
561
+ end
562
+ end
563
+
564
+ teardown do
565
+ p "global td"
566
+ @a.should.equal 2
567
+ @a = 1
568
+ end
569
+
570
+ after(:all) do
571
+ p "global aa"
572
+ @a.should.equal 1
573
+ end
574
+ end
575
+
576
+ context "before all" do
577
+ context "nested" do
578
+ before(:all) do
579
+ @a = 2
580
+ end
581
+
582
+ specify "should call parent then local" do
583
+ @a.should.equal 2
584
+ @b.should.equal 2
585
+ end
586
+ end
587
+
588
+ before(:all) do
589
+ @a = 1
590
+ @b = 2
591
+ end
592
+ end
593
+
594
+ context "after all" do
595
+ context "after nested" do
596
+ after(:all) do
597
+ @a = 2
598
+ end
599
+
600
+ specify "should call local then parent" do
601
+ self.after_all
602
+ @a.should.equal 1
603
+ @b.should.equal 2
604
+ end
605
+ end
606
+
607
+ after(:all) do
608
+ @b = @a
609
+ @a = 1
610
+ end
611
+ end
612
+
613
+
614
+ module ContextHelper
615
+ def foo
616
+ 42
617
+ end
618
+ end
619
+
620
+ context "contexts" do
621
+ include ContextHelper
622
+
623
+ FOO = 42
624
+ $class = self.class
625
+
626
+ specify "are defined in class scope" do
627
+ lambda { FOO }.should.not.raise(NameError)
628
+ FOO.should.equal 42
629
+ $class.should.equal Class
630
+ end
631
+
632
+ specify "can include modules" do
633
+ lambda { foo }.should.not.raise(NameError)
634
+ foo.should.equal 42
635
+ end
636
+ end
637
+
638
+ class CustomTestUnitSubclass < Test::Unit::TestCase
639
+ def test_truth
640
+ assert true
641
+ end
642
+ end
643
+
644
+ context "contexts with subclasses", CustomTestUnitSubclass do
645
+ specify "use the supplied class as the superclass" do
646
+ self.should.be.a.kind_of CustomTestUnitSubclass
647
+ end
648
+ end
649
+
650
+ xcontext "xcontexts with subclasses", CustomTestUnitSubclass do
651
+ specify "work great!" do
652
+ self.should.be.a.kind_of CustomTestUnitSubclass
653
+ end
654
+ end
655
+
656
+ shared_context "a shared context" do
657
+ specify "can be included several times" do
658
+ true.should.be true
659
+ end
660
+
661
+ behaves_like "yet another shared context"
662
+ end
663
+
664
+ shared_context "another shared context" do
665
+ specify "can access data" do
666
+ @answer.should.be 42
667
+ end
668
+ end
669
+
670
+ shared_context "yet another shared context" do
671
+ specify "can include other shared contexts" do
672
+ true.should.be true
673
+ end
674
+ end
675
+
676
+ context "Shared contexts" do
677
+ shared_context "a nested context" do
678
+ specify "can be nested" do
679
+ true.should.be true
680
+ end
681
+ end
682
+
683
+ setup do
684
+ @answer = 42
685
+ end
686
+
687
+ behaves_like "a shared context"
688
+ it_should_behave_like "a shared context"
689
+
690
+ behaves_like "a nested context"
691
+
692
+ behaves_like "another shared context"
693
+
694
+ ctx = self
695
+ specify "should raise when the context cannot be found" do
696
+ should.raise(NameError) {
697
+ ctx.behaves_like "no such context"
698
+ }
699
+ end
700
+ end