test-spec 0.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,522 @@
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 "should.raise should return the exception" do
152
+ ex = lambda { raise "foo!" }.should.raise
153
+ ex.should.be.kind_of RuntimeError
154
+ ex.message.should.match(/foo/)
155
+ end
156
+
157
+ specify "has should.be.an.instance_of" do
158
+ lambda {
159
+ lambda { "string".should.be_an_instance_of String }.should succeed
160
+ }.should.be deprecated
161
+ lambda {
162
+ lambda { "string".should.be_an_instance_of Hash }.should fail
163
+ }.should.be deprecated
164
+
165
+ lambda { "string".should.be.instance_of String }.should succeed
166
+ lambda { "string".should.be.instance_of Hash }.should fail
167
+
168
+ lambda { "string".should.be.an.instance_of String }.should succeed
169
+ lambda { "string".should.be.an.instance_of Hash }.should fail
170
+ end
171
+
172
+ specify "has should.be.nil" do
173
+ lambda { nil.should.be.nil }.should succeed
174
+ lambda { nil.should.be nil }.should succeed
175
+ lambda { nil.should.be_nil }.should.be deprecated
176
+
177
+ lambda { nil.should.not.be.nil }.should fail
178
+ lambda { nil.should.not.be nil }.should fail
179
+ lambda { lambda { nil.should.not.be_nil }.should fail }.should.be deprecated
180
+
181
+ lambda { "foo".should.be.nil }.should fail
182
+ lambda { "bar".should.be nil }.should fail
183
+
184
+ lambda { "foo".should.not.be.nil }.should succeed
185
+ lambda { "bar".should.not.be nil }.should succeed
186
+ end
187
+
188
+ specify "has should.include" do
189
+ lambda { [1,2,3].should.include 2 }.should succeed
190
+ lambda { [1,2,3].should.include 4 }.should fail
191
+
192
+ lambda { {1=>2, 3=>4}.should.include 1 }.should succeed
193
+ lambda { {1=>2, 3=>4}.should.include 2 }.should fail
194
+ end
195
+
196
+ specify "has should.be.a.kind_of" do
197
+ lambda { Array.should.be.kind_of Module }.should succeed
198
+ lambda { "string".should.be.kind_of Object }.should succeed
199
+ lambda { 1.should.be.kind_of Comparable }.should succeed
200
+
201
+ lambda { Array.should.be.a.kind_of Module }.should succeed
202
+
203
+ lambda { "string".should.be.a.kind_of Class }.should fail
204
+ lambda {
205
+ lambda { "string".should.be_a_kind_of Class }.should fail
206
+ }.should.be deprecated
207
+
208
+ lambda { Array.should.be_a_kind_of Module }.should.be deprecated
209
+ lambda { "string".should.be_a_kind_of Object }.should.be deprecated
210
+ lambda { 1.should.be_a_kind_of Comparable }.should.be deprecated
211
+ end
212
+
213
+ specify "has should.match" do
214
+ lambda { "string".should.match(/strin./) }.should succeed
215
+ lambda { "string".should.match("strin") }.should succeed
216
+ lambda { "string".should =~ /strin./ }.should succeed
217
+ lambda { "string".should =~ "strin" }.should succeed
218
+
219
+ lambda { "string".should.match(/slin./) }.should fail
220
+ lambda { "string".should.match("slin") }.should fail
221
+ lambda { "string".should =~ /slin./ }.should fail
222
+ lambda { "string".should =~ "slin" }.should fail
223
+ end
224
+
225
+ specify "has should.be" do
226
+ thing = "thing"
227
+ lambda { thing.should.be thing }.should succeed
228
+ lambda { thing.should.be "thing" }.should fail
229
+
230
+ lambda { 1.should.be(2, 3) }.should.raise(ArgumentError)
231
+ end
232
+
233
+ specify "has should.not.raise" do
234
+ lambda { lambda { 1 + 1 }.should.not.raise }.should succeed
235
+ lambda { lambda { 1 + 1 }.should.not.raise(Interrupt) }.should succeed
236
+
237
+ lambda {
238
+ begin
239
+ lambda {
240
+ raise ZeroDivisionError.new("ArgumentError")
241
+ }.should.not.raise(RuntimeError, StandardError, Comparable)
242
+ rescue ZeroDivisionError
243
+ end
244
+ }.should succeed
245
+
246
+ lambda { lambda { raise "Error" }.should.not.raise }.should fail
247
+ end
248
+
249
+ specify "has should.not.satisfy" do
250
+ lambda { should.not.satisfy { 1 == 2 } }.should succeed
251
+ lambda { should.not.satisfy { 1 == 1 } }.should fail
252
+ end
253
+
254
+ specify "has should.not.be" do
255
+ thing = "thing"
256
+ lambda { thing.should.not.be "thing" }.should succeed
257
+ lambda { thing.should.not.be thing }.should fail
258
+
259
+ lambda { thing.should.not.be thing, thing }.should.raise(ArgumentError)
260
+ end
261
+
262
+ specify "has should.not.equal" do
263
+ lambda { "string1".should.not.equal "string2" }.should succeed
264
+ lambda { "string1".should.not.equal "string1" }.should fail
265
+ end
266
+
267
+ specify "has should.not.match" do
268
+ lambda { "string".should.not.match(/sling/) }.should succeed
269
+ lambda { "string".should.not.match(/string/) }.should fail
270
+ lambda { "string".should.not.match("strin") }.should fail
271
+
272
+ lambda { "string".should.not =~ /sling/ }.should succeed
273
+ lambda { "string".should.not =~ /string/ }.should fail
274
+ lambda { "string".should.not =~ "strin" }.should fail
275
+ end
276
+
277
+ specify "has should.throw" do
278
+ lambda { lambda { throw :thing }.should.throw(:thing) }.should succeed
279
+
280
+ lambda { lambda { throw :thing2 }.should.throw(:thing) }.should fail
281
+ lambda { lambda { 1 + 1 }.should.throw(:thing) }.should fail
282
+ end
283
+
284
+ specify "has should.not.throw" do
285
+ lambda { lambda { 1 + 1 }.should.not.throw }.should succeed
286
+ lambda { lambda { throw :thing }.should.not.throw }.should fail
287
+ end
288
+
289
+ specify "has should.respond_to" do
290
+ lambda { "foo".should.respond_to :to_s }.should succeed
291
+ lambda { 5.should.respond_to :to_str }.should fail
292
+ lambda { :foo.should.respond_to :nx }.should fail
293
+ end
294
+
295
+ specify "has should.be_close" do
296
+ lambda { 1.4.should.be.close 1.4, 0 }.should succeed
297
+ lambda { 0.4.should.be.close 0.5, 0.1 }.should succeed
298
+
299
+ lambda {
300
+ lambda { 1.4.should.be_close 1.4, 0 }.should succeed
301
+ }.should.be deprecated
302
+ lambda {
303
+ lambda { 0.4.should.be_close 0.5, 0.1 }.should succeed
304
+ }.should.be deprecated
305
+
306
+ lambda {
307
+ float_thing = Object.new
308
+ def float_thing.to_f
309
+ 0.2
310
+ end
311
+ float_thing.should.be.close 0.1, 0.1
312
+ }.should succeed
313
+
314
+ lambda { 0.4.should.be.close 0.5, 0.05 }.should fail
315
+ lambda { 0.4.should.be.close Object.new, 0.1 }.should fail
316
+ lambda { 0.4.should.be.close 0.5, -0.1 }.should fail
317
+ end
318
+
319
+ specify "multiple negation works" do
320
+ lambda { 1.should.equal 1 }.should succeed
321
+ lambda { 1.should.not.equal 1 }.should fail
322
+ lambda { 1.should.not.not.equal 1 }.should succeed
323
+ lambda { 1.should.not.not.not.equal 1 }.should fail
324
+
325
+ lambda { 1.should.equal 2 }.should fail
326
+ lambda { 1.should.not.equal 2 }.should succeed
327
+ lambda { 1.should.not.not.equal 2 }.should fail
328
+ lambda { 1.should.not.not.not.equal 2 }.should succeed
329
+ end
330
+
331
+ specify "has should.<predicate>" do
332
+ lambda { [].should.be.empty }.should succeed
333
+ lambda { [1,2,3].should.not.be.empty }.should succeed
334
+
335
+ lambda { [].should.not.be.empty }.should fail
336
+ lambda { [1,2,3].should.be.empty }.should fail
337
+
338
+ lambda { {1=>2, 3=>4}.should.has_key 1 }.should succeed
339
+ lambda { {1=>2, 3=>4}.should.not.has_key 2 }.should succeed
340
+
341
+ lambda { nil.should.bla }.should.raise(NoMethodError)
342
+ lambda { nil.should.not.bla }.should.raise(NoMethodError)
343
+ end
344
+
345
+ specify "has should <operator> (>, >=, <, <=, ===)" do
346
+ lambda { 2.should.be > 1 }.should succeed
347
+ lambda { 1.should.be > 2 }.should fail
348
+
349
+ lambda { 1.should.be < 2 }.should succeed
350
+ lambda { 2.should.be < 1 }.should fail
351
+
352
+ lambda { 2.should.be >= 1 }.should succeed
353
+ lambda { 2.should.be >= 2 }.should succeed
354
+ lambda { 2.should.be >= 2.1 }.should fail
355
+
356
+ lambda { 2.should.be <= 1 }.should fail
357
+ lambda { 2.should.be <= 2 }.should succeed
358
+ lambda { 2.should.be <= 2.1 }.should succeed
359
+
360
+ lambda { Array.should === [1,2,3] }.should succeed
361
+ lambda { Integer.should === [1,2,3] }.should fail
362
+
363
+ lambda { /foo/.should === "foobar" }.should succeed
364
+ lambda { "foobar".should === /foo/ }.should fail
365
+ end
366
+
367
+ $contextscope = self
368
+ specify "is robust against careless users" do
369
+ lambda {
370
+ $contextscope.specify
371
+ }.should.raise(ArgumentError)
372
+ lambda {
373
+ $contextscope.specify "foo"
374
+ }.should.raise(ArgumentError)
375
+ lambda {
376
+ Kernel.send(:context, "foo")
377
+ }.should.raise(ArgumentError)
378
+ lambda {
379
+ context "foo" do
380
+ end
381
+ }.should.raise(Test::Spec::DefinitionError)
382
+ end
383
+
384
+ specify "should detect warnings" do
385
+ lambda { lambda { 0 }.should._warn }.should fail
386
+ lambda { lambda { warn "just a test" }.should._warn }.should succeed
387
+ end
388
+
389
+ specify "should message/blame faults" do
390
+ begin
391
+ 2.should.blaming("Two is not two anymore!").equal 3
392
+ rescue Test::Unit::AssertionFailedError => e
393
+ e.message.should =~ /Two/
394
+ end
395
+
396
+ begin
397
+ 2.should.messaging("Two is not two anymore!").equal 3
398
+ rescue Test::Unit::AssertionFailedError => e
399
+ e.message.should =~ /Two/
400
+ end
401
+
402
+ begin
403
+ 2.should.blaming("I thought two was three").not.equal 2
404
+ rescue Test::Unit::AssertionFailedError => e
405
+ e.message.should =~ /three/
406
+ end
407
+
408
+ begin
409
+ 2.should.blaming("I thought two was three").not.not.not.equal 3
410
+ rescue Test::Unit::AssertionFailedError => e
411
+ e.message.should =~ /three/
412
+ end
413
+
414
+ lambda {
415
+ lambda { raise "Error" }.should.messaging("Duh.").not.raise
416
+ }.should.raise(Test::Unit::AssertionFailedError).message.should =~ /Duh/
417
+ end
418
+
419
+ include TestShoulds
420
+ specify "should allow for custom shoulds" do
421
+ lambda { (1+1).should equal_string("2") }.should succeed
422
+ lambda { (1+2).should equal_string("2") }.should fail
423
+
424
+ lambda { (1+1).should.pass equal_string("2") }.should succeed
425
+ lambda { (1+2).should.pass equal_string("2") }.should fail
426
+
427
+ lambda { (1+1).should.be equal_string("2") }.should succeed
428
+ lambda { (1+2).should.be equal_string("2") }.should fail
429
+
430
+ lambda { (1+1).should.not equal_string("2") }.should fail
431
+ lambda { (1+2).should.not equal_string("2") }.should succeed
432
+ lambda { (1+2).should.not.not equal_string("2") }.should fail
433
+
434
+ lambda { (1+1).should.not.pass equal_string("2") }.should fail
435
+ lambda { (1+2).should.not.pass equal_string("2") }.should succeed
436
+
437
+ lambda { (1+1).should.not.be equal_string("2") }.should fail
438
+ lambda { (1+2).should.not.be equal_string("2") }.should succeed
439
+
440
+ lambda {
441
+ (1+2).should equal_string("2")
442
+ }.should.raise(Test::Unit::AssertionFailedError).message.should =~ /EqualString/
443
+
444
+ lambda { (1+1).should equal_string2("2") }.should succeed
445
+ lambda { (1+2).should equal_string2("2") }.should fail
446
+
447
+ lambda {
448
+ (1+2).should equal_string2("2")
449
+ }.should.raise(Test::Unit::AssertionFailedError).message.should =~ /yada/
450
+
451
+ lambda {
452
+ (1+2).should.blaming("foo").pass equal_string2("2")
453
+ }.should.raise(Test::Unit::AssertionFailedError).message.should =~ /foo/
454
+
455
+ lambda {
456
+ nil.should empty_should(nil)
457
+ }.should.raise(NotImplementedError)
458
+
459
+ lambda { nil.should :break, :now }.should.raise(ArgumentError)
460
+ lambda { nil.should.not :pass, :now }.should.raise(ArgumentError)
461
+ lambda { nil.should.not.not :break, :now }.should.raise(ArgumentError)
462
+ end
463
+
464
+ xspecify "disabled specification" do
465
+ # just trying
466
+ end
467
+
468
+ context "more disabled" do
469
+ xspecify "this is intentional" do
470
+ # ...
471
+ end
472
+ end
473
+ end
474
+
475
+ context "setup/teardown" do
476
+ setup do
477
+ @a = 1
478
+ @b = 2
479
+ end
480
+
481
+ setup do
482
+ @a = 2
483
+ end
484
+
485
+ teardown do
486
+ @a.should.equal 2
487
+ @a = 3
488
+ end
489
+
490
+ teardown do
491
+ @a.should.equal 3
492
+ end
493
+
494
+ specify "run in the right order" do
495
+ @a.should.equal 2
496
+ @b.should.equal 2
497
+ end
498
+ end
499
+
500
+ module ContextHelper
501
+ def foo
502
+ 42
503
+ end
504
+ end
505
+
506
+ context "contexts" do
507
+ include ContextHelper
508
+
509
+ FOO = 42
510
+ $class = self.class
511
+
512
+ specify "are defined in class scope" do
513
+ lambda { FOO }.should.not.raise(NameError)
514
+ FOO.should.equal 42
515
+ $class.should.equal Class
516
+ end
517
+
518
+ specify "can include modules" do
519
+ lambda { foo }.should.not.raise(NameError)
520
+ foo.should.equal 42
521
+ end
522
+ end