spec-unit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/spec-unit.rb +61 -0
  2. data/test/test_spec_unit.rb +283 -0
  3. metadata +47 -0
data/lib/spec-unit.rb ADDED
@@ -0,0 +1,61 @@
1
+ module SpecUnit
2
+ def self.included(base)
3
+ base.extend(ClassMethods)
4
+ end
5
+
6
+ module ClassMethods
7
+ def context(name, &block)
8
+ self.send :include, new_context(name, &block)
9
+ end
10
+
11
+ def new_context(name, &block)
12
+ context_module = new_context_module
13
+
14
+ context_module.module_eval(&block)
15
+ context_module.convert_specifications_to_tests(name.gsub(' ', '_'))
16
+ context_module
17
+ end
18
+
19
+ def new_context_module
20
+ Module.new do
21
+ include SpecUnit
22
+
23
+ def self.convert_specifications_to_tests(name)
24
+ teardown, setup = unbound_teardown, unbound_setup
25
+ specifications = self.instance_methods.select {|m| m.to_s =~ /^specify_/ || m.to_s =~ /^test_/}
26
+
27
+ specifications.each do |spec|
28
+ spec_method = unbound_method(spec)
29
+ spec_name = spec.to_s.split(/^\w+?_/)[1]
30
+
31
+ define_method(:"test_#{name}_#{spec_name}") do
32
+ setup.bind(self).call if setup
33
+ spec_method.bind(self).call
34
+ teardown.bind(self).call if teardown
35
+ end
36
+ end
37
+ end
38
+
39
+ def self.unbound_setup
40
+ unbound_method(:setup)
41
+ end
42
+
43
+ def self.unbound_teardown
44
+ unbound_method(:teardown)
45
+ end
46
+
47
+ def self.unbound_method(meth)
48
+ if self.instance_methods.include? meth.to_s
49
+ unbound_meth = instance_method(meth)
50
+ remove_method(meth) rescue module_where_defined(meth).module_eval { remove_method meth }
51
+ end
52
+ unbound_meth
53
+ end
54
+
55
+ def self.module_where_defined(meth)
56
+ included_modules.detect { |mod| mod.method_defined?(meth) }
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,283 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/spec-unit'
3
+
4
+ class TestSpecUnit < Test::Unit::TestCase
5
+ def setup
6
+ @test_class = Class.new do
7
+ include SpecUnit
8
+ attr_reader :test_val
9
+ end
10
+ end
11
+
12
+ def test_should_accept_tests
13
+ @test_object = build_test_object(@test_class) do
14
+ def test_there_is_a_test
15
+ @test_val = 1
16
+ end
17
+ end
18
+
19
+ assert_method_sets_test_val(@test_object, :test_there_is_a_test, 1)
20
+ end
21
+
22
+ def test_should_have_contexts
23
+ @test_object = build_test_object(@test_class) do
24
+ context 'a new context' do
25
+ def specify_has_tests
26
+ @test_val = 1
27
+ end
28
+ end
29
+ end
30
+
31
+ assert_method_sets_test_val(@test_object, :test_a_new_context_has_tests, 1)
32
+ end
33
+
34
+ def test_context_specification_should_be_removed
35
+ @test_object = build_test_object(@test_class) do
36
+ context 'a new context' do
37
+ def specify_has_tests
38
+ @test_val = 1
39
+ end
40
+ end
41
+ end
42
+
43
+ assert !@test_object.respond_to?(:specify_has_tests)
44
+ end
45
+
46
+ def test_should_have_nested_contexts
47
+ @test_object = build_test_object(@test_class) do
48
+ context 'a new context' do
49
+ context 'with a subcontext' do
50
+ def specify_has_tests
51
+ @test_val = 1
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ assert_method_sets_test_val(@test_object, :test_a_new_context_with_a_subcontext_has_tests, 1)
58
+ end
59
+
60
+ def test_two_specifications_should_exist_in_peace
61
+ @test_object = build_test_object(@test_class) do
62
+ context 'a new context' do
63
+ def specify_has_tests
64
+ @test_val = 1
65
+ end
66
+ end
67
+
68
+ context 'another context' do
69
+ def specify_has_tests
70
+ @test_val = 2
71
+ end
72
+ end
73
+ end
74
+
75
+ assert_method_sets_test_val(@test_object, :test_a_new_context_has_tests, 1)
76
+ assert_method_sets_test_val(@test_object, :test_another_context_has_tests, 2)
77
+ end
78
+
79
+ def test_two_nested_contexts_should_exist_in_peace
80
+ @test_object = build_test_object(@test_class) do
81
+ context 'a new context' do
82
+ context 'with a sub context' do
83
+ def specify_has_tests
84
+ @test_val = 1
85
+ end
86
+ end
87
+ end
88
+
89
+ context 'another context' do
90
+ context 'with a sub context' do
91
+ def specify_has_tests
92
+ @test_val = 2
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ assert_method_sets_test_val(@test_object, :test_a_new_context_with_a_sub_context_has_tests, 1)
99
+ assert_method_sets_test_val(@test_object, :test_another_context_with_a_sub_context_has_tests, 2)
100
+ end
101
+
102
+ def test_context_has_setup
103
+ @test_object = build_test_object(@test_class) do
104
+ context 'a new context' do
105
+ def setup
106
+ @test_val = 1
107
+ end
108
+
109
+ def specify_has_tests
110
+ end
111
+ end
112
+ end
113
+
114
+ assert_method_sets_test_val(@test_object, :test_a_new_context_has_tests, 1)
115
+ end
116
+
117
+ def test_nested_setups_work
118
+ @test_object = build_test_object(@test_class) do
119
+ context 'a new context' do
120
+ def setup
121
+ @test_val = 1
122
+ end
123
+
124
+ context 'with a sub context' do
125
+ def setup
126
+ @test_val += 1
127
+ end
128
+
129
+ def specify_has_tests
130
+ end
131
+ end
132
+ end
133
+ end
134
+
135
+ assert_method_sets_test_val(@test_object, :test_a_new_context_with_a_sub_context_has_tests, 2)
136
+ end
137
+
138
+ def test_nested_setup_is_only_called_once
139
+ @test_object = build_test_object(@test_class) do
140
+ context 'a new context' do
141
+ context 'with a sub context' do
142
+ def setup
143
+ @test_val ||= 0
144
+ @test_val += 1
145
+ end
146
+
147
+ def specify_has_tests
148
+ end
149
+ end
150
+ end
151
+ end
152
+
153
+ assert_method_sets_test_val(@test_object, :test_a_new_context_with_a_sub_context_has_tests, 1)
154
+ end
155
+
156
+ def test_two_nested_setups_do_not_conflict
157
+ @test_object = build_test_object(@test_class) do
158
+ context 'a new context' do
159
+ context 'with a sub context' do
160
+ def setup
161
+ @test_val = 1
162
+ end
163
+
164
+ def specify_has_tests
165
+ end
166
+ end
167
+ end
168
+
169
+ context 'another context' do
170
+ context 'with a sub context' do
171
+ def setup
172
+ @test_val = 2
173
+ end
174
+
175
+ def specify_has_tests
176
+ end
177
+ end
178
+ end
179
+ end
180
+
181
+ assert_method_sets_test_val(@test_object, :test_a_new_context_with_a_sub_context_has_tests, 1)
182
+ assert_method_sets_test_val(@test_object, :test_another_context_with_a_sub_context_has_tests, 2)
183
+ end
184
+
185
+
186
+ def test_context_has_teardown
187
+ @test_object = build_test_object(@test_class) do
188
+ context 'a new context' do
189
+ def teardown
190
+ @test_val -= 1
191
+ end
192
+
193
+ def specify_has_tests
194
+ @test_val = 2
195
+ end
196
+ end
197
+ end
198
+
199
+ assert_method_sets_test_val(@test_object, :test_a_new_context_has_tests, 1)
200
+ end
201
+
202
+ def test_nested_teardowns_work
203
+ @test_object = build_test_object(@test_class) do
204
+ context 'a new context' do
205
+ def teardown
206
+ @test_val -= 1
207
+ end
208
+
209
+ context 'with a sub context' do
210
+ def teardown
211
+ @test_val -= 1
212
+ end
213
+
214
+ def specify_has_tests
215
+ @test_val = 5
216
+ end
217
+ end
218
+ end
219
+ end
220
+
221
+ assert_method_sets_test_val(@test_object, :test_a_new_context_with_a_sub_context_has_tests, 3)
222
+ end
223
+
224
+ def test_nested_teardown_is_only_called_once
225
+ @test_object = build_test_object(@test_class) do
226
+ context 'a new context' do
227
+ context 'with a sub context' do
228
+ def teardown
229
+ @test_val -= 1
230
+ end
231
+
232
+ def specify_has_tests
233
+ @test_val = 1
234
+ end
235
+ end
236
+ end
237
+ end
238
+
239
+ assert_method_sets_test_val(@test_object, :test_a_new_context_with_a_sub_context_has_tests, 0)
240
+ end
241
+
242
+ def test_nested_teardowns_dont_clash_namespaces
243
+ @test_object = build_test_object(@test_class) do
244
+ context 'a new context' do
245
+ context 'with a sub context' do
246
+ def teardown
247
+ @test_val -= 1
248
+ end
249
+
250
+ def specify_has_tests
251
+ @test_val = 1
252
+ end
253
+ end
254
+ end
255
+
256
+ context 'another context' do
257
+ context 'with a sub context' do
258
+ def teardown
259
+ @test_val -= 2
260
+ end
261
+
262
+ def specify_has_tests
263
+ @test_val = 1
264
+ end
265
+ end
266
+ end
267
+ end
268
+
269
+ assert_method_sets_test_val(@test_object, :test_a_new_context_with_a_sub_context_has_tests, 0)
270
+ assert_method_sets_test_val(@test_object, :test_another_context_with_a_sub_context_has_tests, -1)
271
+ end
272
+
273
+
274
+ def build_test_object(klass, &block)
275
+ klass.module_eval(&block)
276
+ klass.new
277
+ end
278
+
279
+ def assert_method_sets_test_val(object, method, return_value)
280
+ object.send(method)
281
+ assert_equal return_value, object.test_val
282
+ end
283
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: spec-unit
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2006-10-06 00:00:00 -04:00
8
+ summary: A library for separating Test::Unit tests into contexts.
9
+ require_paths:
10
+ - lib
11
+ email: trotter@eastmedia.com
12
+ homepage:
13
+ rubyforge_project:
14
+ description: This library allows the use of contexts within Test::Unit. It is meant to bridge the gap between rSpec and Test::Unit, allowing developers to practice BDD in Test::Unit.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Trotter Cashion
31
+ files:
32
+ - lib/spec-unit.rb
33
+ - test/test_spec_unit.rb
34
+ test_files:
35
+ - test/test_spec_unit.rb
36
+ rdoc_options: []
37
+
38
+ extra_rdoc_files: []
39
+
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ requirements: []
45
+
46
+ dependencies: []
47
+