rspec 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,271 @@
1
+ require 'test/unit'
2
+
3
+ require 'spec'
4
+
5
+
6
+ class ErrorReportingContext < Spec::Context
7
+
8
+ # should
9
+
10
+ def use_the_standard_message_for_should
11
+ Object.should { false == true }
12
+ end
13
+
14
+ def use_the_provided_message_for_should
15
+ Object.should("provided message") { false == true }
16
+ end
17
+
18
+ # should_equal
19
+
20
+ def use_the_standard_message_for_should_equal
21
+ Object.should_equal Class
22
+ end
23
+
24
+ def use_the_provided_message_for_should_equal
25
+ Object.should_equal(Class, "provided for should_equal")
26
+ end
27
+
28
+ # should_not_equal
29
+
30
+ def use_the_standard_message_for_should_not_equal
31
+ Object.should_not_equal Object
32
+ end
33
+
34
+ def use_the_provided_message_for_should_not_equal
35
+ Object.should_not_equal(Object, "provided for should_not_equal")
36
+ end
37
+
38
+ # should_be_nil
39
+
40
+ def use_the_standard_message_for_should_be_nil
41
+ Object.should_be_nil
42
+ end
43
+
44
+ def use_the_provided_message_for_should_be_nil
45
+ Object.should_be_nil("provided for should_be_nil")
46
+ end
47
+
48
+ # should_not_be_nil
49
+
50
+ def use_the_standard_message_for_should_not_be_nil
51
+ nil.should_not_be_nil
52
+ end
53
+
54
+ def use_the_provided_message_for_should_not_be_nil
55
+ nil.should_not_be_nil("provided for should_not_be_nil")
56
+ end
57
+
58
+ # should_be_empty
59
+
60
+ def use_the_standard_message_for_should_be_empty
61
+ ['foo', 'bar'].should_be_empty
62
+ end
63
+
64
+ def use_the_provided_message_for_should_be_empty
65
+ ['foo', 'bar'].should_be_empty("provided for should_be_empty")
66
+ end
67
+
68
+ # should_not_be_empty
69
+
70
+ def use_the_standard_message_for_should_not_be_empty
71
+ [].should_not_be_empty
72
+ end
73
+
74
+ def use_the_provided_message_for_should_not_be_empty
75
+ [].should_not_be_empty("provided for should_not_be_empty")
76
+ end
77
+
78
+ # should_include
79
+
80
+ def use_the_standard_message_for_should_include
81
+ ['foo'].should_include('bar')
82
+ end
83
+
84
+ def use_the_provided_message_for_should_include
85
+ ['foo'].should_include('bar', "provided for should_include")
86
+ end
87
+
88
+ # should_not_include
89
+
90
+ def use_the_standard_message_for_should_not_include
91
+ ['foo'].should_not_include('foo')
92
+ end
93
+
94
+ def use_the_provided_message_for_should_not_include
95
+ ['foo'].should_not_include('foo', "provided for should_not_include")
96
+ end
97
+
98
+ # should_be_true
99
+
100
+ def use_the_standard_message_for_should_be_true
101
+ false.should_be_true
102
+ end
103
+
104
+ def use_the_provided_message_for_should_be_true
105
+ false.should_be_true("provided for should_be_true")
106
+ end
107
+
108
+ # should_be_false
109
+
110
+ def use_the_standard_message_for_should_be_false
111
+ true.should_be_false
112
+ end
113
+
114
+ def use_the_provided_message_for_should_be_false
115
+ true.should_be_false("provided for should_be_false")
116
+ end
117
+
118
+ end
119
+
120
+
121
+ class ErrorReportingRunner
122
+
123
+ def initialize
124
+ @failures = []
125
+ end
126
+
127
+ def pass(spec)
128
+ end
129
+
130
+ def failure(spec, exception)
131
+ @failures << exception.message
132
+ end
133
+
134
+ def error(spec)
135
+ end
136
+
137
+ def spec(spec)
138
+ end
139
+
140
+ def run(context)
141
+ context.specifications.each do |example|
142
+ the_context = context.new(example)
143
+ the_context.run(self)
144
+ end
145
+ end
146
+
147
+ def dump_failures
148
+ @failures
149
+ end
150
+
151
+ end
152
+
153
+
154
+ class ErrorReportingTest < Test::Unit::TestCase
155
+
156
+ def setup
157
+ @runner = ErrorReportingRunner.new
158
+ @runner.run(ErrorReportingContext)
159
+ end
160
+
161
+ # should
162
+
163
+ def test_should_report_standard_message_for_should
164
+ assert @runner.dump_failures.include?("Expectation not met.")
165
+ end
166
+
167
+ def test_should_report_provided_message_for_should
168
+ assert @runner.dump_failures.include?("provided message")
169
+ end
170
+
171
+ # should_equal
172
+
173
+ def test_should_report_standard_message_for_should_equal
174
+ assert @runner.dump_failures.include?("<Object>\nshould be equal to:\n<Class>")
175
+ end
176
+
177
+ def test_should_report_provided_message_for_should_equal
178
+ assert @runner.dump_failures.include?("provided for should_equal")
179
+ end
180
+
181
+ # should_not_equal
182
+
183
+ def test_should_report_standard_message_for_should_not_equal
184
+ assert @runner.dump_failures.include?("<Object>\nshould not be equal to:\n<Object>")
185
+ end
186
+
187
+ def test_should_report_provided_message_for_should_not_equal
188
+ assert @runner.dump_failures.include?("provided for should_not_equal")
189
+ end
190
+
191
+ # should_be_nil
192
+
193
+ def test_should_report_standard_message_for_should_be_nil
194
+ assert @runner.dump_failures.include?("<Object> should be nil")
195
+ end
196
+
197
+ def test_should_report_provided_message_fro_should_be_nil
198
+ assert @runner.dump_failures.include?("provided for should_be_nil")
199
+ end
200
+
201
+ # should_not_be_nil
202
+
203
+ def test_should_report_standard_message_for_should_not_be_nil
204
+ assert @runner.dump_failures.include?("<> should not be nil")
205
+ end
206
+
207
+ def test_should_report_provided_message_for_should_not_be_nil
208
+ assert @runner.dump_failures.include?("provided for should_not_be_nil")
209
+ end
210
+
211
+ # should_be_empty
212
+
213
+ def test_should_report_standard_message_for_should_be_empty
214
+ assert @runner.dump_failures.include?('<["foo", "bar"]> should be empty')
215
+ end
216
+
217
+ def test_should_report_provided_message_for_should_be_empty
218
+ assert @runner.dump_failures.include?("provided for should_be_empty")
219
+ end
220
+
221
+ # should_not_be_empty
222
+
223
+ def test_should_report_standard_message_for_should_not_be_empty
224
+ assert @runner.dump_failures.include?("<[]> should not be empty")
225
+ end
226
+
227
+ def test_should_report_provided_message_for_should_not_be_empty
228
+ assert @runner.dump_failures.include?("provided for should_not_be_empty")
229
+ end
230
+
231
+ # should_include
232
+
233
+ def test_should_report_standard_message_for_should_include
234
+ assert @runner.dump_failures.include?('<["foo"]> should include <"bar">')
235
+ end
236
+
237
+ def test_should_report_provided_message_for_should_include
238
+ assert @runner.dump_failures.include?("provided for should_include")
239
+ end
240
+
241
+ # should_not_include
242
+
243
+ def test_should_report_standard_message_for_should_not_include
244
+ assert @runner.dump_failures.include?('<["foo"]> should not include <"foo">')
245
+ end
246
+
247
+ def test_should_report_provided_message_for_should_not_include
248
+ assert @runner.dump_failures.include?("provided for should_not_include")
249
+ end
250
+
251
+ # should_be_true
252
+
253
+ def test_should_report_standard_message_for_should_be_true
254
+ assert @runner.dump_failures.include?("<false> should be true")
255
+ end
256
+
257
+ def test_should_report_provided_message_for_should_be_true
258
+ assert @runner.dump_failures.include?("provided for should_be_true")
259
+ end
260
+
261
+ # should_be_false
262
+
263
+ def test_should_report_standard_message_for_should_be_false
264
+ assert @runner.dump_failures.include?("<true> should be false")
265
+ end
266
+
267
+ def test_should_report_provided_message_for_should_be_false
268
+ assert @runner.dump_failures.include?("provided for should_be_false")
269
+ end
270
+
271
+ end
@@ -0,0 +1,399 @@
1
+ require 'test/unit'
2
+
3
+ require 'spec'
4
+
5
+
6
+ class DummyObject
7
+
8
+ def initialize(foo)
9
+ @foo = foo
10
+ end
11
+
12
+ end
13
+
14
+
15
+ class ExpectationsTest < Test::Unit::TestCase
16
+
17
+ def setup
18
+ @dummy = 'dummy'
19
+ @equal_dummy = 'dummy'
20
+ @another_dummy = 'another_dummy'
21
+ @nil_var = nil
22
+ end
23
+
24
+ # should
25
+
26
+ def test_should_raise_exception_when_block_yields_false
27
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
28
+ should do
29
+ false
30
+ end
31
+ end
32
+ end
33
+
34
+ def test_should_not_raise_exception_when_block_yields_true
35
+ assert_nothing_raised do
36
+ should do
37
+ true
38
+ end
39
+ end
40
+ end
41
+
42
+ # should_equal
43
+
44
+ def test_should_equal_should_not_raise_when_objects_are_equal
45
+ assert_nothing_raised do
46
+ @dummy.should_equal(@equal_dummy)
47
+ end
48
+ end
49
+
50
+ def test_should_equal_should_raise_when_objects_are_not_equal
51
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
52
+ @dummy.should_equal(@another_dummy)
53
+ end
54
+ end
55
+
56
+ # should_not_equal
57
+
58
+ def test_should_not_equal_should_not_raise_when_objects_are_not_equal
59
+ assert_nothing_raised do
60
+ @dummy.should_not_equal(@another_dummy)
61
+ end
62
+ end
63
+
64
+ def test_should_not_equal_should_raise_when_objects_are_not_equal
65
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
66
+ @dummy.should_not_equal(@equal_dummy)
67
+ end
68
+ end
69
+
70
+ # should_be_same_as
71
+
72
+ def test_should_be_same_as_should_not_raise_when_objects_are_same
73
+ assert_nothing_raised do
74
+ @dummy.should_be_same_as(@dummy)
75
+ end
76
+ end
77
+
78
+ def test_should_be_same_as_should_raise_when_objects_are_not_same
79
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
80
+ @dummy.should_be_same_as(@equal_dummy)
81
+ end
82
+ end
83
+
84
+ # should_not_be_same_as
85
+
86
+ def test_should_not_be_same_as_should_not_raise_when_objects_are_not_same
87
+ assert_nothing_raised do
88
+ @dummy.should_not_be_same_as(@equal_dummy)
89
+ end
90
+ end
91
+
92
+ def test_should_not_be_same_as_should_raise_when_objects_are_not_same
93
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
94
+ @dummy.should_not_be_same_as(@dummy)
95
+ end
96
+ end
97
+
98
+ # should_be_nil
99
+
100
+ def test_should_be_nil_should_not_raise_when_object_is_nil
101
+ assert_nothing_raised do
102
+ @nil_var.should_be_nil
103
+ end
104
+ end
105
+
106
+ def test_should_be_nil_should_raise_when_object_is_not_nil
107
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
108
+ @dummy.should_be_nil
109
+ end
110
+ end
111
+
112
+ # should_not_be_nil
113
+
114
+ def test_should_not_be_nil_should_not_raise_when_object_is_not_nil
115
+ assert_nothing_raised do
116
+ @dummy.should_not_be_nil
117
+ end
118
+ end
119
+
120
+ def test_should_not_be_nil_should_raise_when_object_is_nil
121
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
122
+ @nil_var.should_not_be_nil
123
+ end
124
+ end
125
+
126
+ # should_be_empty
127
+
128
+ def test_should_be_empty_should_raise_when_object_is_not_a_container
129
+ assert_raise(NoMethodError) do
130
+ 5.should_be_empty
131
+ end
132
+ end
133
+
134
+ def test_should_be_empty_should_raise_when_object_is_a_non_empty_array
135
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
136
+ [1, 2, 3].should_be_empty
137
+ end
138
+ end
139
+
140
+ def test_should_be_empty_shouldnt_raise_when_object_is_an_empty_array
141
+ assert_nothing_raised do
142
+ [].should_be_empty
143
+ end
144
+ end
145
+
146
+ def test_should_be_empty_should_raise_when_object_is_a_non_empty_hash
147
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
148
+ {"a" => 1}.should_be_empty
149
+ end
150
+ end
151
+
152
+ def test_should_be_empty_shouldnt_raise_when_object_is_an_empty_hash
153
+ assert_nothing_raised do
154
+ {}.should_be_empty
155
+ end
156
+ end
157
+
158
+ # should_not_be_empty
159
+
160
+ def test_should_be_empty_should_raise_when_object_is_a_non_empty_string
161
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
162
+ @dummy.should_be_empty
163
+ end
164
+ end
165
+
166
+ def test_should_be_empty_shouldnt_raise_when_object_is_an_empty_string
167
+ assert_nothing_raised do
168
+ "".should_be_empty
169
+ end
170
+ end
171
+
172
+ def test_should_not_be_empty_should_raise_when_object_is_not_a_container
173
+ assert_raise(NoMethodError) do
174
+ 5.should_not_be_empty
175
+ end
176
+ end
177
+
178
+ def test_should_not_be_empty_should_raise_when_object_is_an_empty_array
179
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
180
+ [].should_not_be_empty
181
+ end
182
+ end
183
+
184
+ def test_should_not_be_empty_shouldnt_raise_when_object_is_a_non_empty_array
185
+ assert_nothing_raised do
186
+ [1, 2, 3].should_not_be_empty
187
+ end
188
+ end
189
+
190
+ def test_should_not_be_empty_should_raise_when_object_is_an_empty_hash
191
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
192
+ {}.should_not_be_empty
193
+ end
194
+ end
195
+
196
+ def test_should_not_be_empty_shouldnt_raise_when_object_is_a_non_empty_hash
197
+ assert_nothing_raised do
198
+ {"a"=>1}.should_not_be_empty
199
+ end
200
+ end
201
+
202
+ def test_should_not_be_empty_should_raise_when_object_is_an_empty_string
203
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
204
+ "".should_not_be_empty
205
+ end
206
+ end
207
+
208
+ def test_should_not_be_empty_shouldnt_raise_when_object_is_a_non_empty_string
209
+ assert_nothing_raised do
210
+ @dummy.should_not_be_empty
211
+ end
212
+ end
213
+
214
+ # should_include
215
+
216
+ def test_should_include_shouldnt_raise_when_string_inclusion_is_present
217
+ assert_nothing_raised do
218
+ @dummy.should_include("mm")
219
+ end
220
+ end
221
+
222
+ def test_should_include_should_raise_when_string_inclusion_is_missing
223
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
224
+ @dummy.should_include("abc")
225
+ end
226
+ end
227
+
228
+ def test_should_include_shouldnt_raise_when_array_inclusion_is_present
229
+ assert_nothing_raised do
230
+ [1, 2, 3].should_include(2)
231
+ end
232
+ end
233
+
234
+ def test_should_include_should_raise_when_array_inclusion_is_missing
235
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
236
+ [1, 2, 3].should_include(5)
237
+ end
238
+ end
239
+
240
+ def test_should_include_shouldnt_raise_when_hash_inclusion_is_present
241
+ assert_nothing_raised do
242
+ {"a"=>1}.should_include("a")
243
+ end
244
+ end
245
+
246
+ def test_should_include_should_raise_when_hash_inclusion_is_missing
247
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
248
+ {"a"=>1}.should_include("b")
249
+ end
250
+ end
251
+
252
+ def test_should_include_shouldnt_raise_when_enumerable_inclusion_is_present
253
+ assert_nothing_raised do
254
+ IO.constants.should_include("SEEK_SET")
255
+ end
256
+ end
257
+
258
+ def test_should_include_should_raise_when_enumerable_inclusion_is_missing
259
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
260
+ IO.constants.should_include("BLAH")
261
+ end
262
+ end
263
+
264
+ # should_not_include
265
+
266
+ def test_should_not_include_shouldnt_raise_when_string_inclusion_is_missing
267
+ assert_nothing_raised do
268
+ @dummy.should_not_include("abc")
269
+ end
270
+ end
271
+
272
+ def test_should_not_include_should_raise_when_string_inclusion_is_present
273
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
274
+ @dummy.should_not_include("mm")
275
+ end
276
+ end
277
+
278
+ def test_should_not_include_shouldnt_raise_when_array_inclusion_is_missing
279
+ assert_nothing_raised do
280
+ [1, 2, 3].should_not_include(5)
281
+ end
282
+ end
283
+
284
+ def test_should_not_include_should_raise_when_array_inclusion_is_present
285
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
286
+ [1, 2, 3].should_not_include(2)
287
+ end
288
+ end
289
+
290
+ def test_should_not_include_shouldnt_raise_when_hash_inclusion_is_missing
291
+ assert_nothing_raised do
292
+ {"a"=>1}.should_not_include("b")
293
+ end
294
+ end
295
+
296
+ def test_should_not_include_should_raise_when_hash_inclusion_is_present
297
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
298
+ {"a"=>1}.should_not_include("a")
299
+ end
300
+ end
301
+
302
+ def test_should_not_include_shouldnt_raise_when_enumerable_inclusion_is_present
303
+ assert_nothing_raised do
304
+ IO.constants.should_not_include("BLAH")
305
+ end
306
+ end
307
+
308
+ def test_should_not_include_should_raise_when_enumerable_inclusion_is_missing
309
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
310
+ IO.constants.should_not_include("SEEK_SET")
311
+ end
312
+ end
313
+
314
+ # violated
315
+
316
+ def test_violated_should_raise
317
+ assert_raise (Spec::Exceptions::ExpectationNotMetError) do
318
+ c = Spec::Context.new
319
+ c.violated
320
+ end
321
+ end
322
+
323
+ # should_be_true
324
+
325
+ def test_should_be_true_should_raise_when_object_is_nil
326
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
327
+ nil.should_be_true
328
+ end
329
+ end
330
+
331
+ def test_should_be_true_should_raise_when_object_is_false
332
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
333
+ false.should_be_true
334
+ end
335
+ end
336
+
337
+ def test_should_be_true_shouldnt_raise_when_object_is_true
338
+ assert_nothing_raised do
339
+ true.should_be_true
340
+ end
341
+ end
342
+
343
+ def test_should_be_true_shouldnt_raise_when_object_is_a_number
344
+ assert_nothing_raised do
345
+ 5.should_be_true
346
+ end
347
+ end
348
+
349
+ def test_should_be_true_shouldnt_raise_when_object_is_a_string
350
+ assert_nothing_raised do
351
+ "hello".should_be_true
352
+ end
353
+ end
354
+
355
+ def test_should_be_true_shouldnt_raise_when_object_is_a_some_random_object
356
+ assert_nothing_raised do
357
+ self.should_be_true
358
+ end
359
+ end
360
+
361
+ # should_be_false
362
+
363
+ def test_should_be_true_shouldnt_raise_when_object_is_nil
364
+ assert_nothing_raised do
365
+ nil.should_be_false
366
+ end
367
+ end
368
+
369
+ def test_should_be_true_shouldnt_raise_when_object_is_false
370
+ assert_nothing_raised do
371
+ false.should_be_false
372
+ end
373
+ end
374
+
375
+ def test_should_be_true_should_raise_when_object_is_true
376
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
377
+ true.should_be_false
378
+ end
379
+ end
380
+
381
+ def test_should_be_true_shouldnt_raise_when_object_is_a_number
382
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
383
+ 5.should_be_false
384
+ end
385
+ end
386
+
387
+ def test_should_be_true_shouldnt_raise_when_object_is_a_string
388
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
389
+ "hello".should_be_false
390
+ end
391
+ end
392
+
393
+ def test_should_be_true_shouldnt_raise_when_object_is_a_some_random_object
394
+ assert_raise(Spec::Exceptions::ExpectationNotMetError) do
395
+ self.should_be_false
396
+ end
397
+ end
398
+
399
+ end