spec-unit 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,284 @@
1
+ $: << File.dirname(__FILE__)
2
+
3
+ require 'helper'
4
+ require 'spec-unit/context_helper'
5
+
6
+ class TestContextHelper < Test::Unit::TestCase
7
+ def setup
8
+ @test_class = Class.new(Test::Unit::TestCase)
9
+ end
10
+
11
+ def test_should_be_included_automatically
12
+ assert @test_class.respond_to?(:context)
13
+ end
14
+
15
+ def test_should_accept_tests
16
+ @test_class.module_eval do
17
+ def test_there_is_a_test
18
+ assert true
19
+ end
20
+ end
21
+
22
+ assert_test_passes :test_there_is_a_test
23
+ end
24
+
25
+ def test_should_have_contexts
26
+ @test_class.module_eval do
27
+ context 'a new context' do
28
+ def specify_has_tests
29
+ assert true
30
+ end
31
+ end
32
+ end
33
+
34
+ assert_test_passes :test_a_new_context_has_tests
35
+ end
36
+
37
+ def test_context_specification_should_be_removed
38
+ @test_class.module_eval do
39
+ context 'a new context' do
40
+ def specify_has_tests
41
+ assert true
42
+ end
43
+ end
44
+ end
45
+
46
+ @test_object = @test_class.new(:test_a_new_context_has_tests)
47
+ assert !@test_object.respond_to?(:specify_has_tests)
48
+ end
49
+
50
+ def test_should_have_nested_contexts
51
+ @test_class.module_eval do
52
+ context 'a new context' do
53
+ context 'with a subcontext' do
54
+ def specify_has_tests
55
+ assert true
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ assert_test_passes :test_a_new_context_with_a_subcontext_has_tests
62
+ end
63
+
64
+ def test_two_specifications_should_exist_in_peace
65
+ @test_class.module_eval do
66
+ context 'a new context' do
67
+ def specify_has_tests
68
+ assert true
69
+ end
70
+ end
71
+
72
+ context 'another context' do
73
+ def specify_has_tests
74
+ flunk
75
+ end
76
+ end
77
+ end
78
+
79
+ assert_test_passes :test_a_new_context_has_tests
80
+ assert_test_fails :test_another_context_has_tests
81
+ end
82
+
83
+ def test_two_nested_contexts_should_exist_in_peace
84
+ @test_class.module_eval do
85
+ context 'a new context' do
86
+ context 'with a sub context' do
87
+ def specify_has_tests
88
+ assert true
89
+ end
90
+ end
91
+ end
92
+
93
+ context 'another context' do
94
+ context 'with a sub context' do
95
+ def specify_has_tests
96
+ flunk
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+
103
+ assert_test_passes :test_a_new_context_with_a_sub_context_has_tests
104
+ assert_test_fails :test_another_context_with_a_sub_context_has_tests
105
+ end
106
+
107
+ def test_context_has_setup
108
+ @test_class.module_eval do
109
+ context 'a new context' do
110
+ def setup
111
+ @test_val = 1
112
+ end
113
+
114
+ def specify_has_tests
115
+ assert_equal 1, @test_val
116
+ end
117
+ end
118
+ end
119
+
120
+ assert_test_passes :test_a_new_context_has_tests
121
+ end
122
+
123
+ def test_nested_setups_work
124
+ @test_class.module_eval do
125
+ context 'a new context' do
126
+ def setup
127
+ @test_val = 2
128
+ end
129
+
130
+ context 'with a sub context' do
131
+ def setup
132
+ @test_val *= @test_val
133
+ end
134
+
135
+ def specify_has_tests
136
+ assert_equal 4, @test_val
137
+ end
138
+ end
139
+ end
140
+ end
141
+
142
+ assert_test_passes :test_a_new_context_with_a_sub_context_has_tests
143
+ end
144
+
145
+ def test_nested_setup_is_only_called_once
146
+ @test_class.module_eval do
147
+ context 'a new context' do
148
+ context 'with a sub context' do
149
+ def setup
150
+ @test_val ||= 0
151
+ @test_val += 1
152
+ end
153
+
154
+ def specify_has_tests
155
+ assert_equal 1, @test_val
156
+ end
157
+ end
158
+ end
159
+ end
160
+
161
+ assert_test_passes :test_a_new_context_with_a_sub_context_has_tests
162
+ end
163
+
164
+ def test_two_nested_setups_do_not_conflict
165
+ @test_class.module_eval do
166
+ context 'a new context' do
167
+ context 'with a sub context' do
168
+ def setup
169
+ @test_val ||= 1
170
+ end
171
+
172
+ def specify_has_tests
173
+ assert_equal 1, @test_val
174
+ end
175
+ end
176
+ end
177
+
178
+ context 'another context' do
179
+ context 'with a sub context' do
180
+ def setup
181
+ @test_val ||= 2
182
+ end
183
+
184
+ def specify_has_tests
185
+ assert_equal 2, @test_val
186
+ end
187
+ end
188
+ end
189
+ end
190
+
191
+ assert_test_passes :test_a_new_context_with_a_sub_context_has_tests
192
+ assert_test_passes :test_another_context_with_a_sub_context_has_tests
193
+ end
194
+
195
+
196
+ def test_context_has_teardown
197
+ @test_class.module_eval do
198
+ context 'a new context' do
199
+ def teardown
200
+ assert_equal 2, @test_val
201
+ end
202
+
203
+ def specify_has_tests
204
+ @test_val = 2
205
+ end
206
+ end
207
+ end
208
+
209
+ assert_test_passes :test_a_new_context_has_tests
210
+ end
211
+
212
+ def test_nested_teardowns_work
213
+ @test_class.module_eval do
214
+ context 'a new context' do
215
+ def teardown
216
+ assert_equal 4, @test_val
217
+ end
218
+
219
+ context 'with a sub context' do
220
+ def teardown
221
+ @test_val -= 1
222
+ end
223
+
224
+ def specify_has_tests
225
+ @test_val = 5
226
+ end
227
+ end
228
+ end
229
+ end
230
+
231
+ assert_test_passes :test_a_new_context_with_a_sub_context_has_tests
232
+ end
233
+
234
+ def test_nested_teardown_is_only_called_once
235
+ @test_class.module_eval do
236
+ context 'a new context' do
237
+ context 'with a sub context' do
238
+ def teardown
239
+ assert_equal 1, @test_val
240
+ @test_val -= 1
241
+ end
242
+
243
+ def specify_has_tests
244
+ @test_val = 1
245
+ end
246
+ end
247
+ end
248
+ end
249
+
250
+ assert_test_passes :test_a_new_context_with_a_sub_context_has_tests
251
+ end
252
+
253
+ def test_nested_teardowns_dont_clash_namespaces
254
+ @test_class.module_eval do
255
+ context 'a new context' do
256
+ context 'with a sub context' do
257
+ def teardown
258
+ assert_equal 1, @test_val
259
+ end
260
+
261
+ def specify_has_tests
262
+ @test_val = 1
263
+ end
264
+ end
265
+ end
266
+
267
+ context 'another context' do
268
+ context 'with a sub context' do
269
+ def teardown
270
+ assert_equal 2, @test_val
271
+ end
272
+
273
+ def specify_has_tests
274
+ @test_val = 2
275
+ end
276
+ end
277
+ end
278
+ end
279
+
280
+ assert_test_passes :test_a_new_context_with_a_sub_context_has_tests
281
+ assert_test_passes :test_another_context_with_a_sub_context_has_tests
282
+ end
283
+
284
+ end