1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
$-w,w = nil, $-w
require File.join(File.dirname(__FILE__), '../lib/bacon')
$-w = w
# Hooray for meta-testing.
module MetaTests
def succeed
lambda { |block|
block.should.not.raise Bacon::Error
true
}
end
def fail
lambda { |block|
block.should.raise Bacon::Error
true
}
end
def equal_string(x)
lambda { |s|
x == s.to_s
}
end
end
describe "Bacon" do
extend MetaTests
it "should have should.satisfy" do
lambda { should.satisfy { 1 == 1 } }.should succeed
lambda { should.satisfy { 1 } }.should succeed
lambda { should.satisfy { 1 != 1 } }.should fail
lambda { should.satisfy { false } }.should fail
lambda { should.satisfy { false } }.should fail
lambda { 1.should.satisfy { |n| n % 2 == 0 } }.should fail
lambda { 2.should.satisfy { |n| n % 2 == 0 } }.should succeed
end
it "should have should.equal" do
lambda { "string1".should == "string1" }.should succeed
lambda { "string1".should == "string2" }.should fail
lambda { "1".should == 1 }.should fail
lambda { "string1".should.equal "string1" }.should succeed
lambda { "string1".should.equal "string2" }.should fail
lambda { "1".should.equal 1 }.should fail
end
it "should have should.raise" do
lambda { lambda { raise "Error" }.should.raise }.should succeed
lambda { lambda { raise "Error" }.should.raise RuntimeError }.should succeed
lambda { lambda { raise "Error" }.should.not.raise }.should fail
lambda { lambda { raise "Error" }.should.not.raise(RuntimeError) }.should fail
lambda { lambda { 1 + 1 }.should.raise }.should fail
lambda {
lambda { raise "Error" }.should.raise(Interrupt)
}.should.raise
end
it "should have should.raise with a block" do
lambda { should.raise { raise "Error" } }.should succeed
lambda { should.raise(RuntimeError) { raise "Error" } }.should succeed
lambda { should.not.raise { raise "Error" } }.should fail
lambda { should.not.raise(RuntimeError) { raise "Error" } }.should fail
lambda { should.raise { 1 + 1 } }.should fail
lambda {
should.raise(Interrupt) { raise "Error" }
}.should.raise
end
it "should have a should.raise should return the exception" do
ex = lambda { raise "foo!" }.should.raise
ex.should.be.kind_of RuntimeError
ex.message.should =~ /foo/
end
it "should have should.be.an.instance_of" do
lambda { "string".should.be.instance_of String }.should succeed
lambda { "string".should.be.instance_of Hash }.should fail
lambda { "string".should.be.an.instance_of String }.should succeed
lambda { "string".should.be.an.instance_of Hash }.should fail
end
it "should have should.be.nil" do
lambda { nil.should.be.nil }.should succeed
lambda { nil.should.not.be.nil }.should fail
lambda { "foo".should.be.nil }.should fail
lambda { "foo".should.not.be.nil }.should succeed
end
it "should have should.include" do
lambda { [1,2,3].should.include 2 }.should succeed
lambda { [1,2,3].should.include 4 }.should fail
lambda { {1=>2, 3=>4}.should.include 1 }.should succeed
lambda { {1=>2, 3=>4}.should.include 2 }.should fail
end
it "should have should.be.a.kind_of" do
lambda { Array.should.be.kind_of Module }.should succeed
lambda { "string".should.be.kind_of Object }.should succeed
lambda { 1.should.be.kind_of Comparable }.should succeed
lambda { Array.should.be.a.kind_of Module }.should succeed
lambda { "string".should.be.a.kind_of Class }.should fail
end
it "should have should.match" do
lambda { "string".should.match(/strin./) }.should succeed
lambda { "string".should =~ /strin./ }.should succeed
lambda { "string".should.match(/slin./) }.should fail
lambda { "string".should =~ /slin./ }.should fail
end
it "should have should.not.raise" do
lambda { lambda { 1 + 1 }.should.not.raise }.should succeed
lambda { lambda { 1 + 1 }.should.not.raise(Interrupt) }.should succeed
lambda {
lambda {
lambda {
Kernel.raise ZeroDivisionError.new("ArgumentError")
}.should.not.raise(RuntimeError, Comparable)
}.should.raise ZeroDivisionError
}.should succeed
lambda { lambda { raise "Error" }.should.not.raise }.should fail
end
it "should have should.throw" do
lambda { lambda { throw :foo }.should.throw(:foo) }.should succeed
lambda { lambda { :foo }.should.throw(:foo) }.should fail
should.throw(:foo) { throw :foo }
end
it "should have should.not.satisfy" do
lambda { should.not.satisfy { 1 == 2 } }.should succeed
lambda { should.not.satisfy { 1 == 1 } }.should fail
end
it "should have should.not.equal" do
lambda { "string1".should.not == "string2" }.should succeed
lambda { "string1".should.not == "string1" }.should fail
end
it "should have should.not.match" do
lambda { "string".should.not.match(/sling/) }.should succeed
lambda { "string".should.not.match(/string/) }.should fail
# lambda { "string".should.not.match("strin") }.should fail
lambda { "string".should.not =~ /sling/ }.should succeed
lambda { "string".should.not =~ /string/ }.should fail
# lambda { "string".should.not =~ "strin" }.should fail
end
it "should have should.be.identical_to/same_as" do
lambda { s = "string"; s.should.be.identical_to s }.should succeed
lambda { "string".should.be.identical_to "string" }.should fail
lambda { s = "string"; s.should.be.same_as s }.should succeed
lambda { "string".should.be.same_as "string" }.should fail
end
it "should have should.respond_to" do
lambda { "foo".should.respond_to :to_s }.should succeed
lambda { 5.should.respond_to :to_str }.should fail
lambda { :foo.should.respond_to :nx }.should fail
end
it "should have should.be.close" do
lambda { 1.4.should.be.close 1.4, 0 }.should succeed
lambda { 0.4.should.be.close 0.5, 0.1 }.should succeed
lambda { 0.4.should.be.close 0.5, 0.05 }.should fail
lambda { 0.4.should.be.close Object.new, 0.1 }.should fail
lambda { 0.4.should.be.close 0.5, -0.1 }.should fail
end
it "should support multiple negation" do
lambda { 1.should.equal 1 }.should succeed
lambda { 1.should.not.equal 1 }.should fail
lambda { 1.should.not.not.equal 1 }.should succeed
lambda { 1.should.not.not.not.equal 1 }.should fail
lambda { 1.should.equal 2 }.should fail
lambda { 1.should.not.equal 2 }.should succeed
lambda { 1.should.not.not.equal 2 }.should fail
lambda { 1.should.not.not.not.equal 2 }.should succeed
end
it "should have should.<predicate>" do
lambda { [].should.be.empty }.should succeed
lambda { [1,2,3].should.not.be.empty }.should succeed
lambda { [].should.not.be.empty }.should fail
lambda { [1,2,3].should.be.empty }.should fail
lambda { {1=>2, 3=>4}.should.has_key 1 }.should succeed
lambda { {1=>2, 3=>4}.should.not.has_key 2 }.should succeed
lambda { nil.should.bla }.should.raise(NoMethodError)
lambda { nil.should.not.bla }.should.raise(NoMethodError)
end
it "should have should <operator> (>, >=, <, <=, ===)" do
lambda { 2.should.be > 1 }.should succeed
lambda { 1.should.be > 2 }.should fail
lambda { 1.should.be < 2 }.should succeed
lambda { 2.should.be < 1 }.should fail
lambda { 2.should.be >= 1 }.should succeed
lambda { 2.should.be >= 2 }.should succeed
lambda { 2.should.be >= 2.1 }.should fail
lambda { 2.should.be <= 1 }.should fail
lambda { 2.should.be <= 2 }.should succeed
lambda { 2.should.be <= 2.1 }.should succeed
lambda { Array.should === [1,2,3] }.should succeed
lambda { Integer.should === [1,2,3] }.should fail
lambda { /foo/.should === "foobar" }.should succeed
lambda { "foobar".should === /foo/ }.should fail
end
it "should allow for custom shoulds" do
lambda { (1+1).should equal_string("2") }.should succeed
lambda { (1+2).should equal_string("2") }.should fail
lambda { (1+1).should.be equal_string("2") }.should succeed
lambda { (1+2).should.be equal_string("2") }.should fail
lambda { (1+1).should.not equal_string("2") }.should fail
lambda { (1+2).should.not equal_string("2") }.should succeed
lambda { (1+2).should.not.not equal_string("2") }.should fail
lambda { (1+1).should.not.be equal_string("2") }.should fail
lambda { (1+2).should.not.be equal_string("2") }.should succeed
end
it "should have should.flunk" do
lambda { should.flunk }.should fail
lambda { should.flunk "yikes" }.should fail
end
end
describe "before/after" do
before do
@a = 1
@b = 2
end
before do
@a = 2
end
after do
@a.should.equal 2
@a = 3
end
after do
@a.should.equal 3
end
it "should run in the right order" do
@a.should.equal 2
@b.should.equal 2
end
describe "when nested" do
before do
@c = 5
end
it "should run from higher level" do
@a.should.equal 2
@b.should.equal 2
end
it "should run at the nested level" do
@c.should.equal 5
end
before do
@a = 5
end
it "should run in the right order" do
@a.should.equal 5
@a = 2
end
end
it "should not run from lower level" do
@c.should.be.nil
end
describe "when nested at a sibling level" do
it "should not run from sibling level" do
@c.should.be.nil
end
end
end
shared "a shared context" do
it "gets called where it is included" do
true.should.be.true
end
end
shared "another shared context" do
it "can access data" do
@magic.should.be.equal 42
end
end
describe "shared/behaves_like" do
behaves_like "a shared context"
ctx = self
it "raises NameError when the context is not found" do
lambda {
ctx.behaves_like "whoops"
}.should.raise NameError
end
behaves_like "a shared context"
before {
@magic = 42
}
behaves_like "another shared context"
end
describe 'describe arguments' do
def check(ctx,name)
ctx.should.be.an.instance_of Bacon::Context
ctx.instance_variable_get('@name').should == name
end
it 'should work with string' do
check(describe('string') {},'string')
end
it 'should work with symbols' do
check(describe(:behaviour) {},'behaviour')
end
it 'should work with modules' do
check(describe(Bacon) {},'Bacon')
end
it 'should work with namespaced modules' do
check(describe(Bacon::Context) {},'Bacon::Context')
end
it 'should work with multiple arguments' do
check(describe(Bacon::Context, :empty) {},'Bacon::Context empty')
end
end
|