shared_should 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,19 +1,235 @@
1
- = shared_should
1
+ = Shared Should - Share and reuse shoulds, contexts, and setups with Shoulda - easy, schmeasy.
2
2
 
3
- Description goes here.
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
5
 
5
- == Contributing to shared_should
6
-
7
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
- * Fork the project
10
- * Start a feature/bugfix branch
11
- * Commit and push until you are happy with your contribution
12
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
6
+ == Quick-Start Examples
14
7
 
15
- == Copyright
8
+ Some quick examples to get you started using Shared Should. The domain is customers renting and purchasing textbooks - like we do at Bookrenter.com.
16
9
 
17
- Copyright (c) 2011 Michael Pearce. See LICENSE.txt for
18
- further details.
10
+ === Shared Should
11
+
12
+ Sharing shoulds is easy.
13
+
14
+ context "Book" do
15
+ context "with an in-stock book" do
16
+ setup { @book = Book.new(:quantity => 1, :price => 10_00 }
17
+
18
+ ### Define a shared should
19
+ shared_should_be "available for checkout" { assert @book.available_for_checkout? }
20
+
21
+ context "with a rentable book" do
22
+ setup { @book.rentable = true }
23
+
24
+ ### Use the "available for checkout" shared_should
25
+ should_be "available for checkout"
26
+ end
27
+
28
+ context "with a purchasable book" do
29
+ setup { @book.purchasable = true }
30
+
31
+ ### Use the "available for checkout" shared_should in this context too
32
+ should_be "available for checkout"
33
+ end
34
+
35
+ ### ...or DRY it up by using .with or .when and an initialization block
36
+ should_be("available for checkout").when("rentable") { @book.rentable = true }
37
+ should_be("available for checkout").when("purchasable") { @book.purchasable = true }
38
+ end
39
+ end
40
+
41
+ === Shared Setup
42
+
43
+ Sharing setups is easy, too.
44
+
45
+ context "Book" do
46
+ ### Define a shared setup
47
+ shared_setup_for "an in-stock book" { @book = Book.new(:quantity => 1, :price => 10_00) }
48
+
49
+ context "with an in-stock rentable book" do
50
+ ### Use the shared setup here
51
+ setup_for "an in-stock book"
52
+
53
+ ### Do some additional setup after the shared setup
54
+ setup { @book.rentable = true }
55
+
56
+ should "be available for checkout" { assert @book.available_for_checkout? }
57
+ end
58
+
59
+ context "with an in-stock purchasable book" do
60
+ ### Use the shared setup again
61
+ setup_for "an in-stock book"
62
+
63
+ setup { @book.purchasable = true }
64
+
65
+ should "be available for checkout" { assert @book.available_for_checkout? }
66
+ end
67
+ end
68
+
69
+ === Shared Context
70
+
71
+ Sharing whole contexts? Schmeasy!
72
+
73
+ context "Book" do
74
+ context "with an in-stock book" do
75
+ setup { @book = Book.new(:quantity => 1, :price => 10_00) }
76
+
77
+ ### Define a shared context
78
+ shared_context_for "a book available for checkout" do
79
+ should "be in stock" { assert @book.quantity > 0 }
80
+ should "have a non-negative price" { assert @book.price > 0 }
81
+ should "be rentable or purchasable" { assert @book.rentable || @book.purchasable }
82
+ end
83
+
84
+ context "with a rentable book" do
85
+ setup { @book.rentable = true }
86
+
87
+ ### Run the shoulds inside the shared context with a rentable book
88
+ should_be "a book available for checkout"
89
+ end
90
+
91
+ context "with a purchasable book" do
92
+ setup { @book.purchasable = true }
93
+
94
+ ### Run the shoulds inside the shared context again with a purchasable book
95
+ should_be "a book available for checkout"
96
+ end
97
+
98
+ ### ...or DRY it up by using .with or .when and an initialization block
99
+ should_be("a book available for checkout").when("rentable") { @book.rentable = true }
100
+ should_be("a book available for checkout").when("purchasable") { @book.purchasable = true }
101
+ end
102
+ end
103
+
104
+ == More Information on Syntax and Usage
105
+
106
+ === Finding Your Share
107
+
108
+ Some rules:
109
+ * When <tt>should_be</tt> or <tt>setup_for</tt> is invoked, it searches up the context hierarchy to find a matching shared definition.
110
+ * You can redefine your shares by using the same name. These shares will only be available in in the current and descendant contexts.
111
+ * Shares defined at the root (on your TestCase) are available in all contexts.
112
+ * If you define a shared setup at the root level, you will need to call <tt>super</tt> if you have a setup instance method for your test.
113
+
114
+ === Initialization Block
115
+
116
+ The shared invocation accepts an initialization block by chaining <tt>when</tt> or <tt>with</tt>. This block can be used to create or modify instance variables used by the shared functionality. It always executes before the shared functionality.
117
+
118
+ context "Book" do
119
+ setup { @book = Book.new(:quantity => 1, :price => 10_00) }
120
+
121
+ shared_should_be "available for checkout" { assert @book.available_for_checkout? }
122
+
123
+ context "with a rentable book" do
124
+ # when shared_should_be "available for checkout" is executed, @book will have rentable equal to true
125
+ should_be "available for checkout".when("rentable") { @book.rentable = true }
126
+ end
127
+
128
+ context "with a purchasable book" do
129
+ should_be "available for checkout".when("purchasable") { @book.purchasable = true }
130
+ end
131
+ end
132
+
133
+ === Parameterizing Shares
134
+
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 declared shared function is the return value of the initialization block. The below example parameterizes a shared setup.
136
+
137
+ context "Book" do
138
+ shared_setup_for "an in-stock book" do |rentable|
139
+ @book = Book.new(:quantity => 1, :price => 10_00, :rentable => rentable, :purchasable => false)
140
+ end
141
+
142
+ context "with rentable book" do
143
+ # the return value of the block is "true" which will be passed as the block parameter "rentable"
144
+ setup_for("an in-stock book").with("a rentable book") { true }
145
+
146
+ should "be available for checkout" { assert @book.available_for_checkout? }
147
+ end
148
+ end
149
+
150
+ Here is a parameterized shared should.
151
+
152
+ context "Book" do
153
+ context "with in-stock book" do
154
+ setup { @book = Book.new(:quantity => 1) }
155
+
156
+ shared_should_be "unavailable for checkout for price" do |price|
157
+ @book.price = price
158
+ assert_false @book.available_for_checkout?
159
+ end
160
+
161
+ should_be("unavailable for checkout for price").when("zero") { 0 }
162
+ should_be("unavailable for checkout for price").when("negative") { -1 }
163
+ end
164
+ end
165
+
166
+ And a parameterized shared context.
167
+
168
+ context "Book" do
169
+ context "with in-stock book" do
170
+ setup { @book = Book.new(:quantity => 1) }
171
+
172
+ shared_context_for "a book available for checkout at price" do
173
+ # parameters are on the setup and shoulds, not on the context
174
+ setup { |price| @book.price = price }
175
+
176
+ # we could also access price in the should blocks, but we don't need it again
177
+ should "be in stock" { assert @book.quantity > 0 }
178
+ should "have a non-negative price" { assert @book.price > 0 }
179
+ should "be rentable or purchasable" { assert @book.rentable || @book.purchasable }
180
+ end
181
+
182
+ should_be("a book available for checkout at price").when("positive") { 10_00 }
183
+ end
184
+ end
185
+
186
+ The shared functions also accept multiple parameters when the initialization block returns an array.
187
+
188
+ context "Book" do
189
+ context "with rentable book" do
190
+ setup { @book = Book.new(:rentable => true) }
191
+
192
+ shared_should_be "unavailable for checkout for quantity and price" do |quantity, price|
193
+ @book.quantity = quantity
194
+ @book.price = price
195
+ assert_false @book.available_for_checkout?
196
+ end
197
+
198
+ should_be("unavailable for checkout for quantity and price").when("zero quantity") { [0, 10_00] }
199
+ should_be("unavailable for checkout for quantity and price").when("zero price") { [1, 0] }
200
+ end
201
+ end
202
+
203
+ === Creating a Library of Shared Functionality
204
+
205
+ The shared functions can also be re-usable across multiple test cases.
206
+
207
+ In your test helper file:
208
+
209
+ class Test::Unit::TestCase
210
+ shared_setup_for "an in-stock book" do |rentable, purchasable|
211
+ @book = Book.new(:quantity => 1, :price => 10_00, :rentable => rentable, :purchasable => purchasable)
212
+ end
213
+ end
214
+
215
+ In your test file:
216
+
217
+ class BookTest < Test::Unit::TestCase
218
+ context "with an in-stock book" do
219
+ shared_setup_for "an in-stock book".with { [true, true] }
220
+
221
+ should "be in stock" { assert @book.quantity > 0 }
222
+ end
223
+ end
224
+
225
+
226
+ = Credits
227
+
228
+ Shared Shoulda is maintained by Michael Pearce (michael.pearce__at__bookrenter__com) and is funded by Bookrenter.com[http://www.bookrenter.com]. Many of the ideas that have inspired Shared Should come
229
+ from practical usage by the Bookrenter software development team and conversations with Bookrenter developers Andrew Wheeler and Philippe Huibonhoa.
230
+
231
+
232
+ = Copyright
233
+
234
+ Copyright (c) 2011 Michael Pearce, Bookrenter.com. See LICENSE.txt for further details.
19
235
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.5.2
data/lib/shared_should.rb CHANGED
@@ -180,10 +180,10 @@ module Shoulda::SharedContext
180
180
  end
181
181
 
182
182
  def shared_should(shared_should_name, &shared_should_block)
183
- shared_should_for(shared_should_name, &shared_should_block)
183
+ shared_should_be(shared_should_name, &shared_should_block)
184
184
  end
185
185
 
186
- def shared_should_for(shared_should_name, &shared_should_block)
186
+ def shared_should_be(shared_should_name, &shared_should_block)
187
187
  shared_context_block = Proc.new do
188
188
  should "be #{shared_should_name}" do
189
189
  call_block_with_shared_value(shared_should_block)
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{shared_should}
8
- s.version = "0.5.1"
8
+ s.version = "0.5.2"
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-02-03}
12
+ s.date = %q{2011-02-04}
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 = [
@@ -118,9 +118,9 @@ class TestSharedShould < Test::Unit::TestCase
118
118
  end
119
119
  end
120
120
 
121
- context ".shared_should_for" do
121
+ context ".shared_should_be" do
122
122
  context "without params" do
123
- shared_should_for "a true value" do
123
+ shared_should_be "a true value" do
124
124
  assert @value
125
125
  end
126
126
 
@@ -146,7 +146,7 @@ class TestSharedShould < Test::Unit::TestCase
146
146
  @value = true
147
147
  end
148
148
 
149
- shared_should_for "a valid specified value" do |value|
149
+ shared_should_be "a valid specified value" do |value|
150
150
  assert_equal value, @value
151
151
  end
152
152
 
@@ -322,7 +322,7 @@ class TestSharedShould < Test::Unit::TestCase
322
322
  @value = true
323
323
  end
324
324
 
325
- shared_should_for("a valid should test") do
325
+ shared_should_be("a valid should test") do
326
326
  assert @value
327
327
  end
328
328
 
@@ -344,7 +344,7 @@ class TestSharedShould < Test::Unit::TestCase
344
344
  @class_value = true
345
345
  end
346
346
 
347
- shared_should_for("a valid should test in class") do
347
+ shared_should_be("a valid should test in class") do
348
348
  assert @class_value
349
349
  end
350
350
 
@@ -387,10 +387,10 @@ class TestSharedShould < Test::Unit::TestCase
387
387
  "test: .shared_setup_for without params without initialization block should have a true value from shared setup. ",
388
388
  "test: .shared_should with params should be have specified value. ",
389
389
  "test: .shared_should without params should be have true value. ",
390
- "test: .shared_should_for with params when true should be a valid specified value. ",
391
- "test: .shared_should_for without params when value in initializer when value is true should be a true value. ",
392
- "test: .shared_should_for without params with value in initializer with true value should be a true value. ",
393
- "test: .shared_should_for without params with value in setup should be a true value. ",
390
+ "test: .shared_should_be with params when true should be a valid specified value. ",
391
+ "test: .shared_should_be without params when value in initializer when value is true should be a true value. ",
392
+ "test: .shared_should_be without params with value in initializer with true value should be a true value. ",
393
+ "test: .shared_should_be without params with value in setup should be a true value. ",
394
394
  "test: context directly under test class for a valid context test should have a true value. ",
395
395
  "test: context directly under test class should be a valid should test. ",
396
396
  "test: parameterized block with an array for be valid with shared context should do something with shared_value. ",
@@ -6,7 +6,7 @@ class SubclassTestCase < Test::Unit::TestCase
6
6
  assert @value
7
7
  end
8
8
 
9
- shared_should_for "a true value for shared should helper" do
9
+ shared_should_be "a true value for shared should helper" do
10
10
  assert @value
11
11
  end
12
12
 
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: 9
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 1
10
- version: 0.5.1
9
+ - 2
10
+ version: 0.5.2
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-02-03 00:00:00 -08:00
18
+ date: 2011-02-04 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency