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 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 an initialization block and parameterization to fine-tune the usage of the shared functionality.
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 up by using .when and an initialization block
36
- use_should("be available for checkout").when("rentable") { @book.rentable = true }
37
- use_should("be available for checkout").when("purchasable") { @book.purchasable = true }
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
- use_context("for a book available for checkout").when("rentable") { @book.rentable = true }
100
- use_context("for a book available for checkout").when("purchasable") { @book.purchasable = true }
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
- ### Initialization Blocks
122
+ ### Chaining
123
+
124
+ Shared Should provides powerful chaining capabilities.
115
125
 
116
- The shared invocation accepts an initialization block by chaining <tt>with</tt> (or its alias <tt>when</tt>) followed by a block. This block can be used to create or modify instance variables used by the shared functionality. It always executes before the shared functionality.
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
- setup { @book = Book.new(:quantity => 1, :price => 10_00) }
120
-
121
- share_should "be available for checkout" { assert @book.available_for_checkout? }
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
- # when share_should "be available for checkout" is executed, @book will have rentable equal to true
125
- use_should("be available for checkout").with("a rentable book") { @book.rentable = true }
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
- context "with a purchasable book" do
129
- use_should("be available for checkout").when("purchasable") { @book.purchasable = true }
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 setups and shoulds 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.
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.7.1
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
- shared_proxy = Shoulda::SharedProxy.new(self, :should, shared_name)
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
- shared_proxy = Shoulda::SharedProxy.new(self, :context, shared_name)
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
- shared_proxy = Shoulda::SharedProxy.new(self, :setup, shared_name)
109
- shared_setup_block = find_shared_block(:setup, shared_name)
110
- setup do
111
- shared_proxy.execute_and_set_shared_value(self)
112
- call_block_with_shared_value(shared_setup_block)
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 shared_context_name do
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 shared_should_name do
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 :shared_name, :share_type, :description, :block_configs, :context
251
+ attr_accessor :source_context, :setup_block_configs, :test_type, :test_block, :test_description, :current_action
260
252
 
261
- def initialize(context, share_type, shared_name)
262
- self.context = context
263
- self.share_type = share_type
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 with(description = nil, &initialization_block)
269
- add_proxy_block(:with, "with", description, &initialization_block)
258
+ def setup(description = nil, &initialization_block)
259
+ add_setup_block(:setup, description, &initialization_block)
270
260
  end
271
261
 
272
- def when(description = nil, &initialization_block)
273
- add_proxy_block(:with, "when", description, &initialization_block)
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
- # given needs to execute before its with_setup
278
- add_proxy_block(:given, "given", description, :disable_and => true, :insert_block_before_end => @current_action == :with_setup, &initialization_block)
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 with_setup(description)
282
- initialization_block = context.find_shared_block(:setup, description)
283
- add_proxy_block(:with_setup, "with setup", description, :disable_and => true, &initialization_block)
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 execute
287
- raise "execute cannot be called for share_setup" if share_type == :setup
288
- shared_proxy = self
289
- context.context description do
290
- setup do
291
- shared_proxy.execute_and_set_shared_value(self)
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 execute_and_set_shared_value(test_context)
298
- initialization_block.bind(test_context).call
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 initialization_block
302
- the_block_configs = block_configs
303
- return Proc.new do
304
- the_block_configs.collect do |block_config|
305
- block = block_config[:block]
306
- # really should only allow value set from :given blocks but done for compatability
307
- if [:given, :with, :when].include?(block_config[:action])
308
- setup_shared_value(block)
309
- else
310
- call_block_with_shared_value(block) if block
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
- end.last
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 add_proxy_block(action, conditional, description, options = {}, &block)
319
- @current_action = action
320
- if description
321
- and_text = options[:disable_and] ? ' ' : ' and '
322
- self.description = "#{self.description}#{self.description.nil? ? nil : and_text}#{conditional} #{description}"
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
- block_config = {:block => block, :action => action}
325
- if options[:insert_block_before_end]
326
- self.block_configs.insert(-2, block_config)
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
- self.block_configs << block_config
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
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{shared_should}
8
- s.version = "0.7.1"
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-03-21}
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 = [
@@ -58,7 +58,7 @@ class TestSharedShould < Test::Unit::TestCase
58
58
  end
59
59
 
60
60
  context "with value in initializer" do
61
- use_context("for a valid value").when("a true value") { @value = true }
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
- use_context("for a chained value").with("an initialization chain") { @chain = true }.given("true") { true }
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
- use_should("be a true value").when("value is true") { @value = true }
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
- use_should("be a true value").when("value is true") { @value = true }
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
- use_should("be a valid specified value").when("using initialization chain") { @chain = true }.given("true") { true }
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
- use_setup("for value").when("initialization value is true") { @initialization_value = true }
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").when("using initialization chain") { @chain = true }.given("true") { true }
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
- use_should("be valid shared should").
270
- with_setup("shared setup 1").given("setup value") { assert_equal 0, @count; @count += 1 }.
271
- with_setup("shared setup 2").given("setup value") { assert_equal 2, @count; @count += 1 }.
272
- with("initialization value") { assert_equal 4, @count; @count += 1 }.
273
- with("initialization value") { assert_equal 5, @count; @count += 1 }.
274
- given { assert_equal 6, @count; @count += 1 }
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 parameters" do
278
- share_setup "shared setup 1" do |value|
279
- assert_equal :one, value
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
- share_setup "shared setup 2" do |value|
283
- assert_equal :two, value
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
- share_should "be valid shared should with parameter" do |value|
287
- assert_equal :three, @value
288
- assert_equal :four, value
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
- use_should("be valid shared should with parameter").
292
- with_setup("shared setup 1").given { :one }.
293
- with_setup("shared setup 2").given { :two }.
294
- with("initialization value") { @value = :three }.
295
- given { :four }
296
-
297
- # TODO: this assertion enforces that only a trailing 'given' block is used for parameter argument
298
- # For legacy reasons, return value from with/when currently can also be used for parameter arguement
299
- #
300
- # share_should "be valid shared should without parameter" do
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
- 'test: should be a valid should test in class. ',
347
- 'test: .share_context without params with value in initializer when a true value for a valid value should call setup in shared context. ',
348
- 'test: .share_context without params with value in initializer when a true value for a valid value should have true value. ',
349
- 'test: .share_context without params with value in setup for a valid value should call setup in shared context. ',
350
- 'test: .share_context without params with value in setup for a valid value should have true value. ',
351
- 'test: .share_setup with param block with chaining should have used share with chain and params. ',
352
- 'test: .share_setup with param block with shared setup value should have a true value from shared setup. ',
353
- 'test: .share_setup without params with initialization block should have a true value from shared setup. ',
354
- 'test: .share_setup without params without initialization block should have a true value from shared setup. ',
355
- 'test: .share_should without params when value in initializer when value is true should be a true value. ',
356
- 'test: .share_should without params with value in initializer when value is true should be a true value. ',
357
- 'test: .share_should without params with value in setup should be a true value. ',
358
- 'test: SharedShould should execute setup instance method. ',
359
- 'test: context directly under test class for a valid context test should have a true value. ',
360
- 'test: context directly under test class should be a valid should test. ',
361
- 'test: for a valid context test in class should have a true value. ',
362
- 'test: shoulda macro should be a valid macro. ',
363
- 'test: expected methods should have expected methods in test. ',
364
- "test: .share_context with params given true for a valid specified value should call setup in shared context. ",
365
- "test: .share_context with params with chaining with an initialization chain given true for a chained value should chain initialization block and be with params. ",
366
- "test: .share_context with params given true for a valid specified value should setup @expected_value. ",
367
- "test: .share_should with params with chaining with an initialization chain given true should be a valid specified value. ",
368
- "test: .share_context with params given true for a valid specified value should have specified value. ",
369
- "test: .share_context with params with chaining when using initialization chain given true for a chained value should chain initialization block and be with params. ",
370
- "test: .share_should with params with chaining when using initialization chain given true should be a valid specified value. ",
371
- "test: .share_should with params given true should be a valid specified value. ",
372
- "test: chaining with parameters with setup shared setup 1 with setup shared setup 2 and with initialization value should be valid shared should with parameter. ",
373
- "test: chaining with ordering verification with setup shared setup 1 given setup value with setup shared setup 2 given setup value and with initialization value and with initialization value should be valid shared should. "
374
- ].inject({}) do |hash, expected_method_name|
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: 1
4
+ hash: 63
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 7
9
- - 1
10
- version: 0.7.1
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-03-21 00:00:00 -07:00
18
+ date: 2011-04-05 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency