shared_should 0.6.5 → 0.6.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.5
1
+ 0.6.6
data/lib/shared_should.rb CHANGED
@@ -17,7 +17,6 @@ end
17
17
 
18
18
  class Test::Unit::TestCase
19
19
  attr_accessor :shared_value
20
- attr_accessor :shared_name
21
20
  @@shared_proxies_executed = {}
22
21
  @@setup_blocks = {}
23
22
 
@@ -30,7 +29,7 @@ class Test::Unit::TestCase
30
29
  # assuming 'suite' is called before executing any tests - may be a poor assumption. Find something better?
31
30
  unless @@shared_proxies_executed[self]
32
31
  shared_proxies.each do |shared_proxy|
33
- shared_proxy.execute(self)
32
+ shared_proxy.execute
34
33
  end
35
34
  @@shared_proxies_executed[self] = true
36
35
  end
@@ -56,9 +55,8 @@ class Test::Unit::TestCase
56
55
  @@setup_blocks[self] << setup_block
57
56
  end
58
57
 
59
- def setup_shared_values(name, initialization_block)
58
+ def setup_shared_value(initialization_block)
60
59
  self.shared_value = initialization_block.nil? ? nil : initialization_block.bind(self).call
61
- self.shared_name = name
62
60
  end
63
61
 
64
62
  def call_block_with_shared_value(test_block)
@@ -110,24 +108,22 @@ module Shoulda::SharedContext
110
108
  end
111
109
 
112
110
  def use_should(shared_name)
113
- shared_proxy = Shoulda::SharedProxy.new(shared_name)
111
+ shared_proxy = Shoulda::SharedProxy.new(self, shared_name)
114
112
  shared_proxies << shared_proxy
115
113
  return shared_proxy
116
114
  end
117
115
 
118
116
  def use_context(shared_name)
119
- shared_proxy = Shoulda::SharedProxy.new(shared_name)
117
+ shared_proxy = Shoulda::SharedProxy.new(self, shared_name)
120
118
  shared_proxies << shared_proxy
121
119
  return shared_proxy
122
120
  end
123
121
 
124
122
  def use_setup(shared_name)
125
- shared_proxy = Shoulda::SharedProxy.new(shared_name)
123
+ shared_proxy = Shoulda::SharedProxy.new(self, shared_name)
126
124
  shared_setup_block = find_shared_setup_block(shared_name)
127
125
  setup do
128
- if initialization_block = shared_proxy.initialization_block
129
- setup_shared_values(shared_proxy.description, shared_proxy.initialization_block)
130
- end
126
+ shared_proxy.execute_and_set_shared_value(self)
131
127
  call_block_with_shared_value(shared_setup_block)
132
128
  end
133
129
  return shared_proxy
@@ -139,7 +135,7 @@ module Shoulda::SharedContext
139
135
  block.bind(self).call
140
136
 
141
137
  shared_proxies.each do |shared_proxy|
142
- shared_proxy.execute(self)
138
+ shared_proxy.execute
143
139
  end
144
140
  end
145
141
  shared_proxies_executing_block.bind(eval("self", block.binding))
@@ -238,7 +234,7 @@ module Shoulda::SharedContext
238
234
 
239
235
  caller_context.context name do
240
236
  setup do
241
- setup_shared_values(name, initialization_block)
237
+ setup_shared_value(initialization_block)
242
238
  end
243
239
 
244
240
  merge_block(&shared_context_block)
@@ -260,13 +256,13 @@ module Shoulda::SharedContext
260
256
  caller_context.send(:alias_method, without_method, :setup)
261
257
  caller_context.send(:define_method, with_method) do
262
258
  send(without_method)
263
- setup_shared_values(name, setup_block)
259
+ setup_shared_value(setup_block)
264
260
  shared_setup_block.bind(self).call
265
261
  end
266
262
  caller_context.send(:alias_method, :setup, with_method)
267
263
  else
268
264
  caller_context.setup do
269
- setup_shared_values(name, setup_block)
265
+ setup_shared_value(setup_block)
270
266
  shared_setup_block.bind(self).call
271
267
  end
272
268
  end
@@ -316,45 +312,71 @@ end
316
312
 
317
313
 
318
314
  class Shoulda::SharedProxy
319
- attr_accessor :shared_name, :description, :initialization_blocks
315
+ attr_accessor :shared_name, :description, :block_configs, :context
320
316
 
321
- def initialize(shared_name)
317
+ def initialize(context, shared_name)
318
+ self.context = context
322
319
  self.shared_name = shared_name
323
- self.initialization_blocks = []
320
+ self.block_configs = []
324
321
  end
325
322
 
326
323
  def with(description = nil, &initialization_block)
327
- with_helper("with", description, &initialization_block)
324
+ add_proxy_block(:with, "with", description, &initialization_block)
328
325
  end
329
326
 
330
327
  def when(description = nil, &initialization_block)
331
- with_helper("when", description, &initialization_block)
328
+ add_proxy_block(:with, "when", description, &initialization_block)
332
329
  end
333
330
 
334
331
  def given(description = nil, &initialization_block)
335
- with_helper("given", description, :disable_and => true, &initialization_block)
332
+ # given needs to execute before its with_setup
333
+ add_proxy_block(:given, "given", description, :disable_and => true, :insert_block_before_end => @current_action == :with_setup, &initialization_block)
334
+ end
335
+
336
+ def with_setup(description)
337
+ initialization_block = context.send(:find_shared_setup_block, description)
338
+ add_proxy_block(:with_setup, "with setup", description, :disable_and => true, &initialization_block)
336
339
  end
337
340
 
338
- def execute(context)
341
+ def execute
339
342
  method_name = context.send(:shared_method_name, :should, shared_name)
340
343
  context.send(method_name, description, &initialization_block)
341
344
  end
342
345
 
346
+ def execute_and_set_shared_value(test_context)
347
+ initialization_block.bind(test_context).call
348
+ end
349
+
343
350
  def initialization_block
344
- blocks = initialization_blocks
351
+ the_block_configs = block_configs
345
352
  return Proc.new do
346
- blocks.collect {|block| block.bind(self).call if block}.last
353
+ the_block_configs.collect do |block_config|
354
+ block = block_config[:block]
355
+ if block_config[:action] == :given
356
+ setup_shared_value(block)
357
+ else
358
+ call_block_with_shared_value(block) if block
359
+ end
360
+ end.last
361
+ # # TODO: needs shared_value
362
+ # blocks.collect {|block| block.bind(self).call if block}.last
347
363
  end
348
364
  end
349
365
 
350
- private
366
+ private
351
367
 
352
- def with_helper(conditional, description, options = {}, &initialization_block)
368
+ def add_proxy_block(action, conditional, description, options = {}, &block)
369
+ @current_action = action
353
370
  if description
354
371
  and_text = options[:disable_and] ? ' ' : ' and '
355
372
  self.description = "#{self.description}#{self.description.nil? ? nil : and_text}#{conditional} #{description}"
356
373
  end
357
- self.initialization_blocks << initialization_block
374
+ block_config = {:block => block, :action => action}
375
+ if options[:insert_block_before_end]
376
+ self.block_configs.insert(-2, block_config)
377
+ else
378
+ self.block_configs << block_config
379
+ end
358
380
  return self
359
381
  end
360
382
  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.6.5"
8
+ s.version = "0.6.6"
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-18}
12
+ s.date = %q{2011-03-19}
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 = [
@@ -403,7 +403,69 @@ class TestSharedShould < Test::Unit::TestCase
403
403
  use_context "for a valid context test"
404
404
  end
405
405
 
406
-
406
+ context "chaining" do
407
+ context "with ordering verification" do
408
+ setup do
409
+ @count = 0
410
+ end
411
+
412
+ share_setup "shared setup 1" do
413
+ assert_equal 1, @count
414
+ @count += 1
415
+ end
416
+
417
+ share_setup "shared setup 2" do
418
+ assert_equal 3, @count
419
+ @count += 1
420
+ end
421
+
422
+ share_should "be valid shared should" do
423
+ assert_equal 7, @count
424
+ end
425
+
426
+ use_should("be valid shared should").
427
+ with_setup("shared setup 1").given("setup value") { assert_equal 0, @count; @count += 1 }.
428
+ with_setup("shared setup 2").given("setup value") { assert_equal 2, @count; @count += 1 }.
429
+ with("initialization value") { assert_equal 4, @count; @count += 1 }.
430
+ with("initialization value") { assert_equal 5, @count; @count += 1 }.
431
+ given { assert_equal 6, @count; @count += 1 }
432
+ end
433
+
434
+ context "with parameters" do
435
+ share_setup "shared setup 1" do |value|
436
+ assert_equal :one, value
437
+ end
438
+
439
+ share_setup "shared setup 2" do |value|
440
+ assert_equal :two, value
441
+ end
442
+
443
+ share_should "be valid shared should with parameter" do |value|
444
+ assert_equal :three, @value
445
+ assert_equal :four, value
446
+ end
447
+
448
+ use_should("be valid shared should with parameter").
449
+ with_setup("shared setup 1").given { :one }.
450
+ with_setup("shared setup 2").given { :two }.
451
+ with("initialization value") { @value = :three }.
452
+ given { :four }
453
+
454
+ # TODO: this assertion enforces that only a trailing 'given' block is used for parameter argument
455
+ # For legacy reasons, return value from with/when currently can also be used for parameter arguement
456
+ #
457
+ # share_should "be valid shared should without parameter" do
458
+ # assert_equal :three, @value
459
+ # assert_nil shared_value
460
+ # end
461
+ #
462
+ # use_should("be valid shared should without parameter").
463
+ # with_setup("shared setup 1").given { :one }.
464
+ # with_setup("shared setup 2").given { :two }.
465
+ # with("initialization value") { @value = :three }
466
+ end
467
+ end
468
+
407
469
  # ensure should macros work
408
470
  def self.should_be_a_valid_macro
409
471
  should "be a valid macro" do
@@ -476,7 +538,9 @@ class TestSharedShould < Test::Unit::TestCase
476
538
  "test: .share_context with params given true for a valid specified value should have specified value. ",
477
539
  "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. ",
478
540
  "test: .share_should with params with chaining when using initialization chain given true should be a valid specified value. ",
479
- "test: .share_should with params given true should be a valid specified value. "
541
+ "test: .share_should with params given true should be a valid specified value. ",
542
+ "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. ",
543
+ "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. "
480
544
  ].inject({}) do |hash, expected_method_name|
481
545
  hash[expected_method_name] = true
482
546
  hash
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: 13
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 5
10
- version: 0.6.5
9
+ - 6
10
+ version: 0.6.6
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-18 00:00:00 -07:00
18
+ date: 2011-03-19 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency