shared_should 0.7.1 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +38 -52
- data/VERSION +1 -1
- data/lib/shared_should.rb +104 -69
- data/shared_should.gemspec +2 -2
- data/test/test_shared_should.rb +164 -78
- data/test/test_shared_should_helper.rb +5 -4
- metadata +5 -5
data/README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# Shared Should - Share and reuse shoulds, contexts, and setups with Shoulda - easy, schmeasy.
|
2
2
|
|
3
3
|
Shared Should allows you to easily create reusable shoulds, contexts and setups with familiar looking Shoulda syntax. Inspired by Rspec's shared example groups for context reuse, Shared Should allows sharing of contexts, shoulds,
|
4
|
-
and setup blocks. Shared Should goes even further by allowing
|
4
|
+
and setup blocks. Shared Should goes even further by allowing chaining and parameterization to fine-tune the usage of the shared functionality.
|
5
5
|
|
6
6
|
## Quick-Start Examples
|
7
7
|
|
8
|
-
Some quick examples to get you started using Shared Should. The domain of the examples is customers renting and purchasing textbooks - like we do at [BookRenter.com](http://www.bookrenter.com "BookRenter.com").
|
8
|
+
Some quick examples to get you started using Shared Should. The domain of the examples is customers renting and purchasing textbooks - something like we do at [BookRenter.com](http://www.bookrenter.com "BookRenter.com").
|
9
9
|
|
10
10
|
### Shared Should
|
11
11
|
|
@@ -32,9 +32,9 @@ Sharing shoulds is easy.
|
|
32
32
|
use_should "be available for checkout"
|
33
33
|
end
|
34
34
|
|
35
|
-
### ...or DRY it
|
36
|
-
|
37
|
-
|
35
|
+
### ...or DRY it with chaining
|
36
|
+
setup("with a rentable book") { @book.rentable = true }.use_should("be available for checkout")
|
37
|
+
setup("with a purchasable book") { @book.purchasable = true }.use_should("be available for checkout")
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
@@ -64,6 +64,14 @@ Sharing setups is easy, too.
|
|
64
64
|
|
65
65
|
should "be available for checkout" { assert @book.available_for_checkout? }
|
66
66
|
end
|
67
|
+
|
68
|
+
### ...or DRY it with chaining
|
69
|
+
use_setup("for an in-stock book").
|
70
|
+
setup("with a rentable book") { @book.rentable = true }.
|
71
|
+
should "be available for checkout" { assert @book.available_for_checkout? }
|
72
|
+
use_setup("for an in-stock book").
|
73
|
+
setup("with a purchasable book") { @book.purchasable = true }.
|
74
|
+
should "be available for checkout" { assert @book.available_for_checkout? }
|
67
75
|
end
|
68
76
|
|
69
77
|
### Shared Context
|
@@ -96,8 +104,8 @@ Sharing whole contexts? Schmeasy!
|
|
96
104
|
end
|
97
105
|
|
98
106
|
### ...or DRY it up by using .when and an initialization block
|
99
|
-
|
100
|
-
|
107
|
+
setup("with a rentable book") { @book.rentable = true }.use_context("for a book available for checkout")
|
108
|
+
setup("with a purchasable book") { @book.purchasable = true }.use_context("for a book available for checkout")
|
101
109
|
end
|
102
110
|
end
|
103
111
|
|
@@ -111,28 +119,39 @@ Some rules:
|
|
111
119
|
* You can redefine your shares by using the same name. These shares will only be available in in the current and descendant contexts.
|
112
120
|
* Shares defined at the root (on your TestCase) are available in all contexts.
|
113
121
|
|
114
|
-
###
|
122
|
+
### Chaining
|
123
|
+
|
124
|
+
Shared Should provides powerful chaining capabilities.
|
115
125
|
|
116
|
-
|
126
|
+
Chained <tt>setup</tt>s and <tt>use\_setup</tt>s are applied to their parent context:
|
117
127
|
|
118
128
|
context "Book" do
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
129
|
+
share_setup "for an in-stock book" do
|
130
|
+
@book = Book.new(:quantity => 1, :price => 10_00
|
131
|
+
end
|
132
|
+
|
123
133
|
context "with a rentable book" do
|
124
|
-
|
125
|
-
|
134
|
+
use_setup("for an in-stock book").setup { @book.rentable = true }
|
135
|
+
|
136
|
+
should "be available for checkout" { assert @book.available_for_checkout? }
|
126
137
|
end
|
127
|
-
|
128
|
-
|
129
|
-
|
138
|
+
end
|
139
|
+
|
140
|
+
Or chained <tt>setup</tt>s and <tt>use\_setup</tt>s can be chained to a <tt>should</tt>, <tt>use\_should</tt>, <tt>context</tt>, or <tt>use\_context</tt>:
|
141
|
+
|
142
|
+
context "Book" do
|
143
|
+
share_setup "for an in-stock book" do
|
144
|
+
@book = Book.new(:quantity => 1, :price => 10_00
|
130
145
|
end
|
146
|
+
|
147
|
+
use_setup("for an in-stock book").
|
148
|
+
setup("for a rentable book") { @book.rentable = true }.
|
149
|
+
should("be available for checkout") { assert @book.available_for_checkout? }
|
131
150
|
end
|
132
151
|
|
133
152
|
### Parameterizing Shares
|
134
153
|
|
135
|
-
Shared functions can also be parameterized using block parameters. This can be done for shared setups, shoulds, and the
|
154
|
+
Shared functions can also be parameterized using block parameters. This can be done for shared setups, shared shoulds, and the <tt>setup</tt>s and <tt>should</tt>s contained within a shared context. The value passed to the shared function is the return value of the <tt>given</tt> parameterization block. The below example parameterizes a shared setup.
|
136
155
|
|
137
156
|
context "Book" do
|
138
157
|
share_setup "for an in-stock book" do |rentable|
|
@@ -200,39 +219,6 @@ The shared functions also accept multiple parameters when the parameterization b
|
|
200
219
|
end
|
201
220
|
end
|
202
221
|
|
203
|
-
### Chaining shared setups with <tt>with_setup</tt>
|
204
|
-
|
205
|
-
Shared setup can be chained for use with shared shoulds and shared contexts.
|
206
|
-
|
207
|
-
context "Book" do
|
208
|
-
share_setup "for a rentable book" do
|
209
|
-
@book = Book.new(:quantity => 1, :price => 10_00, :rentable => true, :purchasable => false)
|
210
|
-
end
|
211
|
-
|
212
|
-
share_should "be available for checkout" { assert @book.available_for_checkout? }
|
213
|
-
|
214
|
-
use_should("be available for checkout").with_setup("for a rentable book")
|
215
|
-
end
|
216
|
-
|
217
|
-
And can be parameterized using a <tt>given</tt> clause.
|
218
|
-
|
219
|
-
context "Book" do
|
220
|
-
share_setup "for an in-stock book" do |rentable|
|
221
|
-
@book = Book.new(:quantity => 1, :price => 10_00, :rentable => rentable, :purchasable => false)
|
222
|
-
end
|
223
|
-
|
224
|
-
share_should "be available for checkout" { assert @book.available_for_checkout? }
|
225
|
-
|
226
|
-
use_should("be available for checkout").with_setup("for an in-stock book").given("the book is rentable") { true }
|
227
|
-
end
|
228
|
-
|
229
|
-
Multiple shared setups can then be combined with an initialization block.
|
230
|
-
|
231
|
-
use_should("successfully checkout shopping cart").
|
232
|
-
with_setup("for an empty shopping cart").
|
233
|
-
with_setup("for a rentable book").
|
234
|
-
with("a book in shopping cart") { @shopping_cart.add_book(@book) }
|
235
|
-
|
236
222
|
### Creating a Library of Shared Functionality
|
237
223
|
|
238
224
|
The shared functions can also be re-usable across multiple test cases.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.8.0
|
data/lib/shared_should.rb
CHANGED
@@ -55,6 +55,10 @@ class Test::Unit::TestCase
|
|
55
55
|
test_block.bind(self).call()
|
56
56
|
end
|
57
57
|
end
|
58
|
+
|
59
|
+
def call_block_shared_value(test_block)
|
60
|
+
self.shared_value = call_block_with_shared_value(test_block)
|
61
|
+
end
|
58
62
|
end
|
59
63
|
|
60
64
|
module Shoulda::SharedContext
|
@@ -93,25 +97,19 @@ module Shoulda::SharedContext
|
|
93
97
|
end
|
94
98
|
|
95
99
|
def use_should(shared_name)
|
96
|
-
|
97
|
-
shared_proxies << shared_proxy
|
98
|
-
return shared_proxy
|
100
|
+
return add_shared_proxy.use_should(shared_name)
|
99
101
|
end
|
100
102
|
|
101
103
|
def use_context(shared_name)
|
102
|
-
|
103
|
-
shared_proxies << shared_proxy
|
104
|
-
return shared_proxy
|
104
|
+
return add_shared_proxy.use_context(shared_name)
|
105
105
|
end
|
106
106
|
|
107
107
|
def use_setup(shared_name)
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
end
|
114
|
-
return shared_proxy
|
108
|
+
return add_shared_proxy.use_setup(shared_name)
|
109
|
+
end
|
110
|
+
|
111
|
+
def setup(name = nil, &block)
|
112
|
+
return add_shared_proxy.setup(name, &block)
|
115
113
|
end
|
116
114
|
|
117
115
|
def context(name = nil, &block)
|
@@ -138,19 +136,9 @@ module Shoulda::SharedContext
|
|
138
136
|
end
|
139
137
|
end
|
140
138
|
|
141
|
-
def setup(&block)
|
142
|
-
if block.nil?
|
143
|
-
setup_without_param_support()
|
144
|
-
else
|
145
|
-
setup_without_param_support() do
|
146
|
-
call_block_with_shared_value(block)
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
139
|
def share_context(shared_context_name, &shared_context_block)
|
152
140
|
wrapping_shared_context_block = Proc.new do
|
153
|
-
context
|
141
|
+
context share_description do
|
154
142
|
merge_block(&shared_context_block)
|
155
143
|
end
|
156
144
|
end
|
@@ -160,7 +148,7 @@ module Shoulda::SharedContext
|
|
160
148
|
|
161
149
|
def share_should(shared_should_name, &shared_should_block)
|
162
150
|
shared_context_block = Proc.new do
|
163
|
-
should
|
151
|
+
should share_description do
|
164
152
|
call_block_with_shared_value(shared_should_block)
|
165
153
|
end
|
166
154
|
end
|
@@ -199,6 +187,10 @@ module Shoulda::SharedContext
|
|
199
187
|
end
|
200
188
|
|
201
189
|
private
|
190
|
+
|
191
|
+
def add_shared_proxy
|
192
|
+
(shared_proxies << Shoulda::SharedProxy.new(self)).last
|
193
|
+
end
|
202
194
|
|
203
195
|
def shared_context_for_block(shared_block)
|
204
196
|
eval("self", shared_block.binding)
|
@@ -256,77 +248,120 @@ end
|
|
256
248
|
|
257
249
|
|
258
250
|
class Shoulda::SharedProxy
|
259
|
-
attr_accessor :
|
251
|
+
attr_accessor :source_context, :setup_block_configs, :test_type, :test_block, :test_description, :current_action
|
260
252
|
|
261
|
-
def initialize(
|
262
|
-
self.
|
263
|
-
self.
|
264
|
-
self.shared_name = shared_name
|
265
|
-
self.block_configs = []
|
253
|
+
def initialize(source_context)
|
254
|
+
self.setup_block_configs = []
|
255
|
+
self.source_context = source_context
|
266
256
|
end
|
267
257
|
|
268
|
-
def
|
269
|
-
|
258
|
+
def setup(description = nil, &initialization_block)
|
259
|
+
add_setup_block(:setup, description, &initialization_block)
|
270
260
|
end
|
271
261
|
|
272
|
-
def
|
273
|
-
|
262
|
+
def use_setup(share_name)
|
263
|
+
add_setup_block(:use_setup, share_name, &source_context.find_shared_block(:setup, share_name))
|
274
264
|
end
|
275
265
|
|
276
266
|
def given(description = nil, &initialization_block)
|
277
|
-
|
278
|
-
|
267
|
+
valid_share_types = [:use_setup, :use_should, :use_context]
|
268
|
+
@failed = true and raise ArgumentError, "'given' can only appear after #{valid_share_types.join(', ')}" unless valid_share_types.include?(current_action)
|
269
|
+
|
270
|
+
add_setup_block(:given, description ? "given #{description}" : nil, &initialization_block)
|
279
271
|
end
|
280
272
|
|
281
|
-
def
|
282
|
-
|
283
|
-
|
273
|
+
def should(description = nil, options = {}, &should_block)
|
274
|
+
shared_context_block = Proc.new do
|
275
|
+
should share_description do
|
276
|
+
call_block_with_shared_value(should_block)
|
277
|
+
end
|
278
|
+
end
|
279
|
+
add_test_block(:should, description, &shared_context_block)
|
284
280
|
end
|
285
281
|
|
286
|
-
def
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
282
|
+
def use_should(share_name)
|
283
|
+
add_test_block(:use_should, share_name, &source_context.find_shared_block(:should, share_name))
|
284
|
+
end
|
285
|
+
|
286
|
+
def context(description = nil, &context_block)
|
287
|
+
shared_context_block = Proc.new do
|
288
|
+
context share_description do
|
289
|
+
merge_block(&context_block)
|
292
290
|
end
|
293
|
-
shared_proxy.context.find_shared_block(shared_proxy.share_type, shared_proxy.shared_name).bind(self).call
|
294
291
|
end
|
292
|
+
add_test_block(:context, description, &shared_context_block)
|
295
293
|
end
|
296
294
|
|
297
|
-
def
|
298
|
-
|
295
|
+
def use_context(share_name)
|
296
|
+
add_test_block(:use_context, share_name, &source_context.find_shared_block(:context, share_name))
|
299
297
|
end
|
300
298
|
|
301
|
-
def
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
299
|
+
def execute
|
300
|
+
return if @failed
|
301
|
+
|
302
|
+
shared_proxy = self
|
303
|
+
if test_type == :should || test_type == :context || test_type == :use_should || test_type == :use_context
|
304
|
+
# create a new context for setups and should/context
|
305
|
+
source_context.context setup_block_configs_description do
|
306
|
+
setup_without_param_support do
|
307
|
+
shared_proxy.setup_block_configs.each do |config|
|
308
|
+
call_block_shared_value(config[:block])
|
309
|
+
end
|
311
310
|
end
|
312
|
-
|
311
|
+
|
312
|
+
# share_description called when creating test names
|
313
|
+
eval("def share_description; #{shared_proxy.send(:escaped_test_description)}; end")
|
314
|
+
merge_block(&shared_proxy.test_block)
|
315
|
+
end
|
316
|
+
else
|
317
|
+
# call setups directly in this context
|
318
|
+
source_context.setup_without_param_support do
|
319
|
+
shared_proxy.setup_block_configs.each do |config|
|
320
|
+
call_block_shared_value(config[:block])
|
321
|
+
end
|
322
|
+
end
|
313
323
|
end
|
314
324
|
end
|
315
325
|
|
316
326
|
private
|
327
|
+
|
328
|
+
def escaped_test_description
|
329
|
+
test_description.nil? ? 'nil' : "'#{test_description.gsub('\\', '\\\\\\').gsub("'", "\\\\'")}'"
|
330
|
+
end
|
317
331
|
|
318
|
-
def
|
319
|
-
@
|
320
|
-
|
321
|
-
|
322
|
-
|
332
|
+
def setup_block_configs_description
|
333
|
+
@setup_block_configs_description
|
334
|
+
end
|
335
|
+
|
336
|
+
def add_test_block(test_type, description, &test_block)
|
337
|
+
@failed = true and raise ArgumentError, 'Only a single should or context can be chained' if self.test_type
|
338
|
+
|
339
|
+
self.test_type = test_type
|
340
|
+
self.current_action = test_type
|
341
|
+
self.test_description = description
|
342
|
+
self.test_block = test_block
|
343
|
+
return self
|
344
|
+
end
|
345
|
+
|
346
|
+
def add_setup_block(action, description, &block)
|
347
|
+
if test_type
|
348
|
+
@failed = true and raise ArgumentError, "'#{action}' may not be applied" unless action == :given
|
349
|
+
# add final given description to test description
|
350
|
+
self.test_description = "#{test_description} #{description}" if description
|
351
|
+
description = nil
|
323
352
|
end
|
324
|
-
|
325
|
-
|
326
|
-
|
353
|
+
|
354
|
+
setup_block_config = {:block => block, :action => action, :description => description}
|
355
|
+
if action == :given and (current_action == :setup || current_action == :use_setup)
|
356
|
+
setup_block_configs.insert(-2, setup_block_config)
|
327
357
|
else
|
328
|
-
|
358
|
+
setup_block_configs << setup_block_config
|
329
359
|
end
|
360
|
+
if description
|
361
|
+
@setup_block_configs_description = "#{@setup_block_configs_description}#{' ' if @setup_block_configs_description}#{description}"
|
362
|
+
end
|
363
|
+
|
364
|
+
self.current_action = action
|
330
365
|
return self
|
331
366
|
end
|
332
367
|
end
|
data/shared_should.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{shared_should}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.8.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Michael Pearce"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-04-05}
|
13
13
|
s.description = %q{Share and reuse shoulds, contexts, and setup in Shoulda.}
|
14
14
|
s.email = %q{michael.pearce@bookrenter.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/test/test_shared_should.rb
CHANGED
@@ -58,7 +58,7 @@ class TestSharedShould < Test::Unit::TestCase
|
|
58
58
|
end
|
59
59
|
|
60
60
|
context "with value in initializer" do
|
61
|
-
|
61
|
+
setup("with a true value") { @value = true }.use_context("for a valid value")
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
@@ -96,8 +96,7 @@ class TestSharedShould < Test::Unit::TestCase
|
|
96
96
|
end
|
97
97
|
end
|
98
98
|
|
99
|
-
|
100
|
-
use_context("for a chained value").when("using initialization chain") { @chain = true }.given("true") { true }
|
99
|
+
setup("for an initialization chain") { @chain = true }.use_context("for a chained value").given("true") { true }
|
101
100
|
end
|
102
101
|
end
|
103
102
|
end
|
@@ -117,11 +116,11 @@ class TestSharedShould < Test::Unit::TestCase
|
|
117
116
|
end
|
118
117
|
|
119
118
|
context "when value in initializer" do
|
120
|
-
|
119
|
+
setup("with true value") { @value = true }.use_should("be a true value")
|
121
120
|
end
|
122
121
|
|
123
122
|
context "with value in initializer" do
|
124
|
-
|
123
|
+
setup("with true value") { @value = true }.use_should("be a true value")
|
125
124
|
end
|
126
125
|
end
|
127
126
|
|
@@ -142,8 +141,7 @@ class TestSharedShould < Test::Unit::TestCase
|
|
142
141
|
assert value
|
143
142
|
end
|
144
143
|
|
145
|
-
|
146
|
-
use_should("be a valid specified value").with("an initialization chain") { @chain = true }.given("true") { true }
|
144
|
+
setup("with initialization chain") { @chain = true }.use_should("be a valid specified value").given("true") { true }
|
147
145
|
end
|
148
146
|
end
|
149
147
|
end
|
@@ -177,7 +175,7 @@ class TestSharedShould < Test::Unit::TestCase
|
|
177
175
|
@value = @initialization_value
|
178
176
|
end
|
179
177
|
|
180
|
-
|
178
|
+
setup("with initialization true value") { @initialization_value = true }.use_setup("for value")
|
181
179
|
|
182
180
|
should "have a true value from shared setup" do
|
183
181
|
assert @value
|
@@ -213,8 +211,7 @@ class TestSharedShould < Test::Unit::TestCase
|
|
213
211
|
@value = value
|
214
212
|
end
|
215
213
|
|
216
|
-
use_setup("for value").
|
217
|
-
use_setup("for value").with("an initialization chain") { @chain = true }.given("true") { true }
|
214
|
+
use_setup("for value").given("true") { true }.setup("with chain true") { @chain = true }
|
218
215
|
|
219
216
|
should "have used share with chain and params" do
|
220
217
|
assert @chain
|
@@ -251,62 +248,148 @@ class TestSharedShould < Test::Unit::TestCase
|
|
251
248
|
setup do
|
252
249
|
@count = 0
|
253
250
|
end
|
254
|
-
|
255
|
-
share_setup "shared setup 1" do
|
251
|
+
|
252
|
+
share_setup "for shared setup 1" do |count|
|
256
253
|
assert_equal 1, @count
|
254
|
+
assert_equal count, @count
|
257
255
|
@count += 1
|
258
256
|
end
|
259
|
-
|
260
|
-
share_setup "shared setup 2" do
|
257
|
+
|
258
|
+
share_setup "for shared setup 2" do |count|
|
261
259
|
assert_equal 3, @count
|
260
|
+
assert_equal count, @count
|
262
261
|
@count += 1
|
263
262
|
end
|
264
|
-
|
265
|
-
share_should "be valid shared should" do
|
263
|
+
|
264
|
+
share_should "be valid shared should" do |count|
|
266
265
|
assert_equal 7, @count
|
266
|
+
assert_equal count, @count
|
267
267
|
end
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
268
|
+
|
269
|
+
use_setup("for shared setup 1").given("increment 1") { assert_equal 0, @count; @count += 1 }.
|
270
|
+
use_setup("for shared setup 2").given("increment 2") { assert_equal 2, @count; @count += 1 }.
|
271
|
+
setup("with setup value 3") { assert_equal 4, @count; @count += 1 }.
|
272
|
+
setup("with setup value 4") { assert_equal 5, @count; @count += 1 }.
|
273
|
+
use_should("be valid shared should").given("increment 3") { assert_equal 6, @count; @count += 1 }
|
274
|
+
end
|
275
|
+
|
276
|
+
context "with invalid given" do
|
277
|
+
begin
|
278
|
+
setup {}.setup { assert false }.given {}
|
279
|
+
raise "Should not allow given after setup"
|
280
|
+
rescue ArgumentError
|
281
|
+
# correct
|
282
|
+
end
|
283
|
+
|
284
|
+
begin
|
285
|
+
setup {}.should("fail") { assert false }.given {}
|
286
|
+
raise "Should not allow given after should"
|
287
|
+
rescue ArgumentError
|
288
|
+
# correct
|
289
|
+
end
|
290
|
+
|
291
|
+
begin
|
292
|
+
setup {}.context do
|
293
|
+
should("fail") { assert false }
|
294
|
+
end.given {}
|
295
|
+
raise "Should not allow given after should"
|
296
|
+
rescue ArgumentError
|
297
|
+
# correct
|
298
|
+
end
|
299
|
+
|
300
|
+
begin
|
301
|
+
share_setup("for setup") { assert false }
|
302
|
+
use_setup("for setup").given {}.given {}
|
303
|
+
raise "Should not allow given after another given"
|
304
|
+
rescue ArgumentError
|
305
|
+
# correct
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
context "with invalid setup chain to should" do
|
310
|
+
begin
|
311
|
+
setup {}.should("1") { assert false }.setup { assert false }
|
312
|
+
raise "Should not allow setup chained after should"
|
313
|
+
rescue ArgumentError
|
314
|
+
# correct
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
context "with invalid chain to should" do
|
319
|
+
begin
|
320
|
+
setup {}.should("1") { assert false }.should("2") { assert false }
|
321
|
+
raise "Should not allow chained shoulds"
|
322
|
+
rescue ArgumentError
|
323
|
+
# correct
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
context "with invalid chained contexts" do
|
328
|
+
begin
|
329
|
+
setup {}.context("1") { should("1") { assert false } }.should("2") { assert false }
|
330
|
+
raise "Should not allow chain to context"
|
331
|
+
rescue ArgumentError
|
332
|
+
# correct
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
context "with should" do
|
337
|
+
setup("for true value") { @value = true }.should("be true value") do
|
338
|
+
assert @value
|
339
|
+
end
|
340
|
+
end
|
341
|
+
|
342
|
+
context "with shared should" do
|
343
|
+
share_should "be true value" do |value|
|
344
|
+
assert @value
|
345
|
+
assert value
|
346
|
+
end
|
347
|
+
|
348
|
+
setup("for true value") { @value = true }.use_should("be true value").given("true") { true }
|
275
349
|
end
|
276
350
|
|
277
|
-
context "with
|
278
|
-
|
279
|
-
|
351
|
+
context "with context" do
|
352
|
+
setup("for true value") { @value = true }.context do
|
353
|
+
should "be true value" do
|
354
|
+
assert @value
|
355
|
+
end
|
280
356
|
end
|
357
|
+
end
|
281
358
|
|
282
|
-
|
283
|
-
|
359
|
+
context "with shared context" do
|
360
|
+
share_context "for a shared context" do
|
361
|
+
should "have a true value" do |value|
|
362
|
+
assert @value
|
363
|
+
assert value
|
364
|
+
end
|
284
365
|
end
|
285
366
|
|
286
|
-
|
287
|
-
|
288
|
-
|
367
|
+
setup("for true value") { @value = true }.use_context("for a shared context").given("true") { true }
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
371
|
+
context "with unusual characters in name" do
|
372
|
+
name = "-- \\ ' \" --"
|
373
|
+
|
374
|
+
share_context name do
|
375
|
+
should "be valid" do
|
376
|
+
assert true
|
289
377
|
end
|
378
|
+
end
|
290
379
|
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
# assert_equal :three, @value
|
302
|
-
# assert_nil shared_value
|
303
|
-
# end
|
304
|
-
#
|
305
|
-
# use_should("be valid shared should without parameter").
|
306
|
-
# with_setup("shared setup 1").given { :one }.
|
307
|
-
# with_setup("shared setup 2").given { :two }.
|
308
|
-
# with("initialization value") { @value = :three }
|
380
|
+
use_context name
|
381
|
+
|
382
|
+
share_should name do
|
383
|
+
assert true
|
384
|
+
end
|
385
|
+
|
386
|
+
use_should name
|
387
|
+
|
388
|
+
share_setup name do
|
389
|
+
assert true
|
309
390
|
end
|
391
|
+
|
392
|
+
use_setup name
|
310
393
|
end
|
311
394
|
|
312
395
|
# ensure should macros work
|
@@ -343,35 +426,38 @@ class TestSharedShould < Test::Unit::TestCase
|
|
343
426
|
should "have expected methods in test" do
|
344
427
|
# ensure test methods are created
|
345
428
|
expected_method_names = [
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
"test: .share_context with params
|
365
|
-
"test: .share_context with params
|
366
|
-
"test:
|
367
|
-
"test:
|
368
|
-
"test:
|
369
|
-
"test:
|
370
|
-
"test:
|
371
|
-
"test:
|
372
|
-
"test:
|
373
|
-
"test:
|
374
|
-
|
429
|
+
"test: .share_should without params with value in setup should be a true value. ",
|
430
|
+
"test: .share_context without params with value in setup for a valid value should call setup in shared context. ",
|
431
|
+
"test: .share_context without params with value in initializer with a true value for a valid value should have true value. ",
|
432
|
+
"test: for a valid context test in class should have a true value. ",
|
433
|
+
"test: .share_should with params should be a valid specified value given true. ",
|
434
|
+
"test: .share_should without params with value in initializer with true value should be a true value. ",
|
435
|
+
"test: .share_should without params when value in initializer with true value should be a true value. ",
|
436
|
+
"test: .share_should with params with chaining with initialization chain should be a valid specified value given true. ",
|
437
|
+
"test: .share_setup with param block with shared setup value should have a true value from shared setup. ",
|
438
|
+
"test: .share_context without params with value in initializer with a true value for a valid value should call setup in shared context. ",
|
439
|
+
"test: .share_context with params with chaining for an initialization chain for a chained value given true should chain initialization block and be with params. ",
|
440
|
+
"test: should be a valid should test in class. ",
|
441
|
+
"test: .share_setup without params with initialization block should have a true value from shared setup. ",
|
442
|
+
"test: .share_context without params with value in setup for a valid value should have true value. ",
|
443
|
+
"test: shoulda macro should be a valid macro. ",
|
444
|
+
"test: .share_setup with param block with chaining should have used share with chain and params. ",
|
445
|
+
"test: .share_context with params for a valid specified value given true should call setup in shared context. ",
|
446
|
+
"test: chaining with ordering verification for shared setup 1 given increment 1 for shared setup 2 given increment 2 with setup value 3 with setup value 4 should be valid shared should given increment 3. ",
|
447
|
+
"test: .share_context with params for a valid specified value given true should setup @expected_value. ",
|
448
|
+
"test: .share_context with params for a valid specified value given true should have specified value. ",
|
449
|
+
"test: chaining with shared should for true value should be true value given true. ",
|
450
|
+
"test: chaining with should for true value should be true value. ",
|
451
|
+
"test: chaining with context for true value should be true value. ",
|
452
|
+
"test: expected methods should have expected methods in test. ",
|
453
|
+
"test: chaining with shared context for true value for a shared context given true should have a true value. ",
|
454
|
+
"test: context directly under test class should be a valid should test. ",
|
455
|
+
"test: context directly under test class for a valid context test should have a true value. ",
|
456
|
+
"test: .share_setup without params without initialization block should have a true value from shared setup. ",
|
457
|
+
"test: SharedShould should execute setup instance method. ",
|
458
|
+
"test: with unusual characters in name -- \\ ' \" -- should be valid. ",
|
459
|
+
"test: with unusual characters in name should -- \\ ' \" --. "
|
460
|
+
].inject({}) do |hash, expected_method_name|
|
375
461
|
hash[expected_method_name] = true
|
376
462
|
hash
|
377
463
|
end
|
@@ -2,10 +2,6 @@ require 'helper'
|
|
2
2
|
|
3
3
|
# Create a subclass for some shares
|
4
4
|
class SubclassTestCase < Test::Unit::TestCase
|
5
|
-
share_should "be a true value for shared should helper" do
|
6
|
-
assert @value
|
7
|
-
end
|
8
|
-
|
9
5
|
should "have a test" do
|
10
6
|
assert true
|
11
7
|
end
|
@@ -13,6 +9,10 @@ end
|
|
13
9
|
|
14
10
|
# re-open Test::Unit::TestCase class for some shares
|
15
11
|
class Test::Unit::TestCase
|
12
|
+
share_should "be a true value for shared should helper" do
|
13
|
+
assert @value
|
14
|
+
end
|
15
|
+
|
16
16
|
share_context "for a true value for shared context helper" do
|
17
17
|
should "be true value" do
|
18
18
|
assert @value
|
@@ -22,6 +22,7 @@ class Test::Unit::TestCase
|
|
22
22
|
share_setup "for a true value for shared setup helper" do
|
23
23
|
@value = true
|
24
24
|
end
|
25
|
+
|
25
26
|
end
|
26
27
|
|
27
28
|
class TestSharedShouldHelper < SubclassTestCase
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shared_should
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 63
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 8
|
9
|
+
- 0
|
10
|
+
version: 0.8.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Michael Pearce
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-04-05 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|