rspec 0.5.11 → 0.5.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/CHANGES +12 -0
  2. data/README +4 -2
  3. data/Rakefile +14 -4
  4. data/bin/spec +7 -7
  5. data/bin/test2spec +13 -8
  6. data/lib/spec/api/helper/diff.rb +53 -0
  7. data/lib/spec/api/helper/instance_helper.rb +11 -11
  8. data/lib/spec/api/helper/instance_negator.rb +11 -11
  9. data/lib/spec/api/helper/kind_helper.rb +11 -11
  10. data/lib/spec/api/helper/kind_negator.rb +11 -11
  11. data/lib/spec/api/helper/respond_helper.rb +11 -11
  12. data/lib/spec/api/helper/respond_negator.rb +11 -11
  13. data/lib/spec/api/helper/should_base.rb +6 -6
  14. data/lib/spec/api/helper/should_helper.rb +35 -35
  15. data/lib/spec/api/helper/should_negator.rb +20 -20
  16. data/lib/spec/rake/spectask.rb +15 -6
  17. data/lib/spec/runner.rb +1 -0
  18. data/lib/spec/runner/backtrace_tweaker.rb +2 -0
  19. data/lib/spec/runner/base_text_formatter.rb +3 -1
  20. data/lib/spec/runner/html_formatter.rb +153 -0
  21. data/lib/spec/runner/option_parser.rb +9 -2
  22. data/lib/spec/runner/progress_bar_formatter.rb +4 -0
  23. data/lib/spec/runner/rdoc_formatter.rb +3 -0
  24. data/lib/spec/runner/reporter.rb +5 -5
  25. data/lib/spec/runner/specdoc_formatter.rb +3 -0
  26. data/lib/spec/test_to_spec/sexp_transformer.rb +1 -0
  27. data/lib/spec/test_to_spec/translation_test_runner.rb +10 -2
  28. data/lib/spec/version.rb +1 -1
  29. data/test/spec/api/helper/arbitrary_predicate_test.rb +49 -73
  30. data/test/spec/api/helper/diff_test.rb +60 -0
  31. data/test/spec/api/helper/equality_test.rb +14 -0
  32. data/test/spec/api/helper/should_have_test.rb +0 -29
  33. data/test/spec/api/helper/typing_test.rb +87 -87
  34. data/test/spec/api/sugar_test.rb +0 -6
  35. data/test/spec/runner/backtrace_tweaker_test.rb +8 -2
  36. data/test/spec/runner/html_formatter_test.rb +47 -0
  37. data/test/spec/runner/option_parser_test.rb +0 -5
  38. data/test/test_classes.rb +73 -0
  39. data/test/test_helper.rb +3 -0
  40. metadata +8 -49
  41. data/doc/README +0 -3
  42. data/doc/config.yaml +0 -2
  43. data/doc/plugin/syntax.rb +0 -60
  44. data/doc/plugin/version.rb +0 -19
  45. data/doc/src/core_team.page +0 -31
  46. data/doc/src/default.css +0 -199
  47. data/doc/src/default.template +0 -31
  48. data/doc/src/documentation/index.page +0 -188
  49. data/doc/src/documentation/meta.info +0 -22
  50. data/doc/src/documentation/mocks.page +0 -287
  51. data/doc/src/documentation/underscores.page +0 -21
  52. data/doc/src/examples.page +0 -9
  53. data/doc/src/images/David_and_Aslak.jpg +0 -0
  54. data/doc/src/images/Whats_That_Dude.jpg +0 -0
  55. data/doc/src/images/ducks1.png +0 -0
  56. data/doc/src/images/ul.gif +0 -0
  57. data/doc/src/index.page +0 -74
  58. data/doc/src/meta.info +0 -33
  59. data/doc/src/tools/index.page +0 -49
  60. data/doc/src/tools/meta.info +0 -18
  61. data/doc/src/tools/rails.page +0 -132
  62. data/doc/src/tools/rake.page +0 -21
  63. data/doc/src/tools/rcov.page +0 -28
  64. data/doc/src/tools/spec.page +0 -129
  65. data/doc/src/tools/test2spec.page +0 -103
  66. data/doc/src/tutorials/index.page +0 -52
  67. data/doc/src/tutorials/meta.info +0 -36
  68. data/doc/src/tutorials/notes.txt +0 -263
  69. data/doc/src/tutorials/stack.rb +0 -11
  70. data/doc/src/tutorials/stack_01.page +0 -226
  71. data/doc/src/tutorials/stack_02.page +0 -182
  72. data/doc/src/tutorials/stack_03.page +0 -283
  73. data/doc/src/tutorials/stack_04.page +0 -164
  74. data/doc/src/tutorials/stack_04.page.orig +0 -123
  75. data/doc/src/tutorials/stack_05.page +0 -90
  76. data/doc/src/tutorials/stack_05.page.orig +0 -124
  77. data/doc/src/tutorials/stack_06.page +0 -359
  78. data/doc/src/tutorials/stack_06.page.orig +0 -359
  79. data/doc/src/tutorials/stack_spec.rb +0 -25
  80. data/doc/src/ul.gif +0 -0
@@ -1,359 +0,0 @@
1
- h2. A Simple Stack - Pop - IN PROGRESS - DISREGARD THIS PAGE
2
-
3
- So what next? We could specify a new context, or we could specify new messages in the existing contexts. How about 'pop'? We'll start with the empty stack:
4
-
5
- <ruby>
6
- context "An empty stack" do
7
-
8
- setup do
9
- @stack = Stack.new
10
- end
11
-
12
- specify "should keep its mouth shut when you send it 'push'" do
13
- lambda { @stack.push Object.new }.should_not.raise Exception
14
- end
15
-
16
- specify "should raise a StackUnderflowError when you send it 'top'" do
17
- lambda { @stack.top }.should_raise StackUnderflowError
18
- end
19
-
20
- specify "should raise a StackUnderflowError when you send it 'pop'" do
21
- lambda { @stack.pop }.should_raise StackUnderflowError
22
- end
23
-
24
- end
25
- </ruby>
26
-
27
- ... run the specs ...
28
-
29
- <pre>
30
- $ spec stack_spec.rb -v
31
-
32
- An empty stack
33
- - should keep its mouth shut when you send it 'push'
34
- - should raise a StackUnderflowError when you send it 'top'
35
- - should raise a StackUnderflowError when you send it 'pop' (FAILED - 1)
36
-
37
- A stack with one item
38
- - should keep its mouth shut when you send it 'push'
39
- - should return top when you send it 'top'
40
-
41
-
42
- 1)
43
- ExpectationNotMetError in 'An empty stack should raise a StackUnderflowError when you send it 'pop''
44
- <Proc> should raise <StackUnderflowError> but raised #<NoMethodError: undefined method `pop' for #<Stack:0x36f980>>
45
- ./stack_spec.rb:18:in `should raise a StackUnderflowError when you send it 'pop''
46
-
47
- Finished in 0.000967 seconds
48
-
49
- 2 contexts, 5 specifications, 1 failure
50
- </pre>
51
-
52
- The messages here are slightly different than what we've seen so far. Back when we specified sending 'top' to an empty stack, we had already specified sending 'top' to a stack with one element, so we didn't get the <code>NoMethodError</code> that we get here. And since we were expecting a <code>StackUnderflowError</code>, the message tells us that we were expecting one sort of error but got another. This is excellent feedback, as it not only tells us that our expectation was not met, it also tells us exactly what went wrong and indirectly what to do about it: add the pop method:
53
-
54
- <ruby>
55
- class Stack
56
- ...
57
- def pop
58
- end
59
- end
60
- </ruby>
61
-
62
- run the specs...
63
-
64
- <pre>
65
- $ spec stack_spec.rb -v
66
-
67
- An empty stack
68
- - should keep its mouth shut when you send it 'push'
69
- - should raise a StackUnderflowError when you send it 'top'
70
- - should raise a StackUnderflowError when you send it 'pop' (FAILED - 1)
71
-
72
- A stack with one item
73
- - should keep its mouth shut when you send it 'push'
74
- - should return top when you send it 'top'
75
-
76
- 1)
77
- ExpectationNotMetError in 'An empty stack should raise a StackUnderflowError when you send it 'pop''
78
- <Proc> should raise <StackUnderflowError> but raised nothing
79
- ./stack_spec.rb:18:in `should raise a StackUnderflowError when you send it 'pop''
80
-
81
- Finished in 0.000902 seconds
82
-
83
- 2 contexts, 5 specifications, 1 failure
84
- </pre>
85
-
86
- ...and now the message tells us that nothing was raised. So back to stack.rb...
87
-
88
- <ruby>
89
- def pop
90
- raise StackUnderflowError
91
- end
92
- </ruby>
93
-
94
- ...run the specs...
95
-
96
- <pre>
97
- $ spec stack_spec.rb -v
98
-
99
- An empty stack
100
- - should keep its mouth shut when you send it 'push'
101
- - should raise a StackUnderflowError when you send it 'top'
102
- - should raise a StackUnderflowError when you send it 'pop'
103
-
104
- A stack with one item
105
- - should keep its mouth shut when you send it 'push'
106
- - should return top when you send it 'top'
107
-
108
- Finished in 0.000812 seconds
109
-
110
- 2 contexts, 5 specifications, 0 failures
111
- </pre>
112
-
113
- ...and they pass. Notice that we did not include any conditional logic in the pop method. As things stand right now, any client that calls pop on any stack (empty or otherwise) will get a StackUnderflowError. Obviously, this is not what we want, but again, rather than going back to the code we're going to specify the behaviour that we're looking for. We do have one other context in place already, so let's specify calling 'pop' on a context with one element.
114
-
115
- <ruby>
116
- context "A stack with one item" do
117
-
118
- setup do
119
- @stack = Stack.new
120
- @stack.push "one item"
121
- end
122
-
123
- specify "should keep its mouth shut when you send it 'push'" do
124
- lambda { @stack.push Object.new }.should_not.raise Exception
125
- end
126
-
127
- specify "should return top when you send it 'top'" do
128
- @stack.top.should_equal "one item"
129
- end
130
-
131
- specify "should return top when you send it 'pop'" do
132
- @stack.pop.should_equal "one item"
133
- end
134
-
135
- end
136
- </ruby>
137
-
138
- Run the specs...
139
-
140
- <pre>
141
- $ spec stack_spec.rb -v
142
-
143
- An empty stack
144
- - should keep its mouth shut when you send it 'push'
145
- - should raise a StackUnderflowError when you send it 'top'
146
- - should raise a StackUnderflowError when you send it 'pop'
147
-
148
- A stack with one item
149
- - should keep its mouth shut when you send it 'push'
150
- - should return top when you send it 'top'
151
- - should return top when you send it 'pop' (FAILED - 1)
152
-
153
- 1)
154
- StackUnderflowError in 'A stack with one item should return top when you send it 'pop''
155
- StackUnderflowError
156
- ./stack.rb:16:in `pop'
157
- ./stack_spec.rb:39:in `should return top when you send it 'pop''
158
-
159
- Finished in 0.00102000000000002 seconds
160
-
161
- 2 contexts, 6 specifications, 1 failure
162
- </pre>
163
-
164
- ...and we can see that we're getting the StackUnderflowError in a case where we don't want it. So NOW we can add the conditional logic to handle it...
165
-
166
- <ruby>
167
- class Stack
168
-
169
- def push item
170
- @item = item
171
- end
172
-
173
- def top
174
- raise StackUnderflowError if @item.nil?
175
- @item
176
- end
177
-
178
- def pop
179
- raise StackUnderflowError if @item.nil?
180
- @item
181
- end
182
-
183
- end
184
- </ruby>
185
-
186
- ...run the specs...
187
-
188
- <pre>
189
- $ spec stack_spec.rb -v
190
-
191
- An empty stack
192
- - should keep its mouth shut when you send it 'push'
193
- - should raise a StackUnderflowError when you send it 'top'
194
- - should raise a StackUnderflowError when you send it 'pop'
195
-
196
- A stack with one item
197
- - should keep its mouth shut when you send it 'push'
198
- - should return top when you send it 'top'
199
- - should return top when you send it 'pop'
200
-
201
- Finished in 0.000936 seconds
202
-
203
- 2 contexts, 6 specifications, 0 failures
204
- </pre>
205
-
206
- ...and everything passes. Everything seems fine, but there's one thing missing. When you send 'pop' to a non-empty stack, it's also supposed to remove that element from the stack. Here's a typical solution to this problem using TDD:
207
-
208
- <ruby>
209
- #typical state-based example
210
- def test_should_remove_top_item_when_sent_pop
211
- assert_equal(stack.size, 1)
212
- stack.pop
213
- assert_equal(stack.size, 0)
214
- end
215
- </ruby>
216
-
217
- That is VERY tempting because it seems so simple. It tells the story we want to tell. But it does so at the expense of exposing internal state. "So what?" you may ask. "We're talking about something trivial here." you may say. "Exposing size is perfectly logical because the stack is a collection" you may add. While all those things are reasonable, this thinking almost always takes us down the slippery slope of exposing more and more state just because it makes the test easier to write.
218
-
219
- Now don't get me wrong. Tests should be easy to write. Testable code is a primary goal of what we're doing here. Whether you're doing TDD or BDD, that's absolutely key. But all to often we make bad design decisions in the name of testability, when there are perfectly reasonable alternatives right at our fingertips.
220
-
221
- So even if you buy these arguments, TDD would probably lead you to this instead:
222
-
223
- <ruby>
224
- #typical state-based example
225
- def test_should_remove_top_item_when_sent_pop
226
- assert(!stack.empty?)
227
- stack.pop
228
- assert(stack.empty?)
229
- end
230
- </ruby>
231
-
232
- That's a little better. We're exposing state, but it's not something as specific as size. But it's still state. We can do better.
233
-
234
- What we're looking for here is observable behaviour. How does a one-element stack behave after we send it 'pop'. We've stated that the stack should be empty at that point, right? So perhaps the observable behaviour is that it acts like an empty stack:
235
-
236
- <ruby>
237
- specify "should raise a StackUnderflowError the second time you sent it 'pop'" do
238
- @stack.pop
239
- lambda { @stack.pop }.should_raise StackUnderflowError
240
- end
241
- </ruby>
242
-
243
- Run the specs (this time without the -v flag)...
244
-
245
- <pre>
246
- $ spec stack_spec.rb
247
-
248
- ......F
249
-
250
- 1)
251
- ExpectationNotMetError in 'A stack with one item should raise a StackUnderflowError the second time you sent it 'pop''
252
- <Proc> should raise <StackUnderflowError> but raised nothing
253
- ./stack_spec.rb:44:in `should raise a StackUnderflowError the second time you sent it 'pop''
254
-
255
- Finished in 0.000948 seconds
256
-
257
- 2 contexts, 7 specifications, 1 failure
258
- </pre>
259
-
260
- ...implement just enough to meet this specification...
261
-
262
- <ruby>
263
- class Stack
264
-
265
- ...
266
-
267
- def pop
268
- raise StackUnderflowError if @item.nil?
269
- item = @item
270
- @item = nil
271
- item
272
- end
273
-
274
- end
275
- </ruby>
276
-
277
- <pre>
278
- $ spec stack_spec.rb
279
-
280
- .......
281
-
282
- Finished in 0.000859 seconds
283
-
284
- 2 contexts, 7 specifications, 0 failures
285
- </pre>
286
-
287
- ...and all specifications are met. Let's look back at our one-item stack spec so far:
288
-
289
- <ruby>
290
- context "A stack with one item" do
291
-
292
- setup do
293
- @stack = Stack.new
294
- @stack.push "one item"
295
- end
296
-
297
- specify "should keep its mouth shut when you send it 'push'" do
298
- lambda { @stack.push Object.new }.should.not.raise Exception
299
- end
300
-
301
- specify "should return top when you send it 'top'" do
302
- @stack.top.should.equal "one item"
303
- end
304
-
305
- specify "should return top when you send it 'pop'" do
306
- @stack.pop.should.equal "one item"
307
- end
308
-
309
- specify "should raise a StackUnderflowError the second time you sent it 'pop'" do
310
- @stack.pop
311
- lambda { @stack.pop }.should.raise StackUnderflowError
312
- end
313
-
314
- end
315
- </ruby>
316
-
317
- See some imbalance? We've specified what happens when you send 'pop' repeatedly, but not what happens when you send 'top' repeatedly. 'top' should keep returning the same value, right?
318
-
319
- <ruby>
320
- context "A stack with one item" do
321
-
322
- ...
323
-
324
- specify "should return top repeatedly when you send it 'top'" do
325
- @stack.top.should.equal "one item"
326
- @stack.top.should.equal "one item"
327
- @stack.top.should.equal "one item"
328
- end
329
-
330
- ...
331
-
332
- end
333
- </ruby>
334
-
335
- Run the specs (with the -v flag)...
336
-
337
- <pre>
338
- $ spec stack_spec.rb -v
339
-
340
- An empty stack
341
- - should keep its mouth shut when you send it 'push'
342
- - should raise a StackUnderflowError when you send it 'top'
343
- - should raise a StackUnderflowError when you send it 'pop'
344
-
345
- A stack with one item
346
- - should keep its mouth shut when you send it 'push'
347
- - should return top when you send it 'top'
348
- - should return top repeatedly when you send it 'top'
349
- - should return top when you send it 'pop'
350
- - should raise a StackUnderflowError the second time you sent it 'pop'
351
-
352
- Finished in 0.001746 seconds
353
-
354
- 2 contexts, 8 specifications, 0 failures
355
- </pre>
356
-
357
- So in this case we did not need any additional implementation, but look at how well rounded the specification becomes.
358
-
359
- <a href="stack_04.html">Previous</a>
@@ -1,359 +0,0 @@
1
- h2. A Simple Stack - Pop - IN PROGRESS - DISREGARD THIS PAGE
2
-
3
- So what next? We could specify a new context, or we could specify new messages in the existing contexts. How about 'pop'? We'll start with the empty stack:
4
-
5
- <ruby>
6
- context "An empty stack" do
7
-
8
- setup do
9
- @stack = Stack.new
10
- end
11
-
12
- specify "should keep its mouth shut when you send it 'push'" do
13
- lambda { @stack.push Object.new }.should.not.raise Exception
14
- end
15
-
16
- specify "should raise a StackUnderflowError when you send it 'top'" do
17
- lambda { @stack.top }.should.raise StackUnderflowError
18
- end
19
-
20
- specify "should raise a StackUnderflowError when you send it 'pop'" do
21
- lambda { @stack.pop }.should.raise StackUnderflowError
22
- end
23
-
24
- end
25
- </ruby>
26
-
27
- ... run the specs ...
28
-
29
- <pre>
30
- $ spec stack_spec.rb -v
31
-
32
- An empty stack
33
- - should keep its mouth shut when you send it 'push'
34
- - should raise a StackUnderflowError when you send it 'top'
35
- - should raise a StackUnderflowError when you send it 'pop' (FAILED - 1)
36
-
37
- A stack with one item
38
- - should keep its mouth shut when you send it 'push'
39
- - should return top when you send it 'top'
40
-
41
-
42
- 1)
43
- ExpectationNotMetError in 'An empty stack should raise a StackUnderflowError when you send it 'pop''
44
- <Proc> should raise <StackUnderflowError> but raised #<NoMethodError: undefined method `pop' for #<Stack:0x36f980>>
45
- ./stack_spec.rb:18:in `should raise a StackUnderflowError when you send it 'pop''
46
-
47
- Finished in 0.000967 seconds
48
-
49
- 2 contexts, 5 specifications, 1 failure
50
- </pre>
51
-
52
- The messages here are slightly different than what we've seen so far. Back when we specified sending 'top' to an empty stack, we had already specified sending 'top' to a stack with one element, so we didn't get the <code>NoMethodError</code> that we get here. And since we were expecting a <code>StackUnderflowError</code>, the message tells us that we were expecting one sort of error but got another. This is excellent feedback, as it not only tells us that our expectation was not met, it also tells us exactly what went wrong and indirectly what to do about it: add the pop method:
53
-
54
- <ruby>
55
- class Stack
56
- ...
57
- def pop
58
- end
59
- end
60
- </ruby>
61
-
62
- run the specs...
63
-
64
- <pre>
65
- $ spec stack_spec.rb -v
66
-
67
- An empty stack
68
- - should keep its mouth shut when you send it 'push'
69
- - should raise a StackUnderflowError when you send it 'top'
70
- - should raise a StackUnderflowError when you send it 'pop' (FAILED - 1)
71
-
72
- A stack with one item
73
- - should keep its mouth shut when you send it 'push'
74
- - should return top when you send it 'top'
75
-
76
- 1)
77
- ExpectationNotMetError in 'An empty stack should raise a StackUnderflowError when you send it 'pop''
78
- <Proc> should raise <StackUnderflowError> but raised nothing
79
- ./stack_spec.rb:18:in `should raise a StackUnderflowError when you send it 'pop''
80
-
81
- Finished in 0.000902 seconds
82
-
83
- 2 contexts, 5 specifications, 1 failure
84
- </pre>
85
-
86
- ...and now the message tells us that nothing was raised. So back to stack.rb...
87
-
88
- <ruby>
89
- def pop
90
- raise StackUnderflowError
91
- end
92
- </ruby>
93
-
94
- ...run the specs...
95
-
96
- <pre>
97
- $ spec stack_spec.rb -v
98
-
99
- An empty stack
100
- - should keep its mouth shut when you send it 'push'
101
- - should raise a StackUnderflowError when you send it 'top'
102
- - should raise a StackUnderflowError when you send it 'pop'
103
-
104
- A stack with one item
105
- - should keep its mouth shut when you send it 'push'
106
- - should return top when you send it 'top'
107
-
108
- Finished in 0.000812 seconds
109
-
110
- 2 contexts, 5 specifications, 0 failures
111
- </pre>
112
-
113
- ...and they pass. Notice that we did not include any conditional logic in the pop method. As things stand right now, any client that calls pop on any stack (empty or otherwise) will get a StackUnderflowError. Obviously, this is not what we want, but again, rather than going back to the code we're going to specify the behaviour that we're looking for. We do have one other context in place already, so let's specify calling 'pop' on a context with one element.
114
-
115
- <ruby>
116
- context "A stack with one item" do
117
-
118
- setup do
119
- @stack = Stack.new
120
- @stack.push "one item"
121
- end
122
-
123
- specify "should keep its mouth shut when you send it 'push'" do
124
- lambda { @stack.push Object.new }.should.not.raise Exception
125
- end
126
-
127
- specify "should return top when you send it 'top'" do
128
- @stack.top.should.equal "one item"
129
- end
130
-
131
- specify "should return top when you send it 'pop'" do
132
- @stack.pop.should.equal "one item"
133
- end
134
-
135
- end
136
- </ruby>
137
-
138
- Run the specs...
139
-
140
- <pre>
141
- $ spec stack_spec.rb -v
142
-
143
- An empty stack
144
- - should keep its mouth shut when you send it 'push'
145
- - should raise a StackUnderflowError when you send it 'top'
146
- - should raise a StackUnderflowError when you send it 'pop'
147
-
148
- A stack with one item
149
- - should keep its mouth shut when you send it 'push'
150
- - should return top when you send it 'top'
151
- - should return top when you send it 'pop' (FAILED - 1)
152
-
153
- 1)
154
- StackUnderflowError in 'A stack with one item should return top when you send it 'pop''
155
- StackUnderflowError
156
- ./stack.rb:16:in `pop'
157
- ./stack_spec.rb:39:in `should return top when you send it 'pop''
158
-
159
- Finished in 0.00102000000000002 seconds
160
-
161
- 2 contexts, 6 specifications, 1 failure
162
- </pre>
163
-
164
- ...and we can see that we're getting the StackUnderflowError in a case where we don't want it. So NOW we can add the conditional logic to handle it...
165
-
166
- <ruby>
167
- class Stack
168
-
169
- def push item
170
- @item = item
171
- end
172
-
173
- def top
174
- raise StackUnderflowError if @item.nil?
175
- @item
176
- end
177
-
178
- def pop
179
- raise StackUnderflowError if @item.nil?
180
- @item
181
- end
182
-
183
- end
184
- </ruby>
185
-
186
- ...run the specs...
187
-
188
- <pre>
189
- $ spec stack_spec.rb -v
190
-
191
- An empty stack
192
- - should keep its mouth shut when you send it 'push'
193
- - should raise a StackUnderflowError when you send it 'top'
194
- - should raise a StackUnderflowError when you send it 'pop'
195
-
196
- A stack with one item
197
- - should keep its mouth shut when you send it 'push'
198
- - should return top when you send it 'top'
199
- - should return top when you send it 'pop'
200
-
201
- Finished in 0.000936 seconds
202
-
203
- 2 contexts, 6 specifications, 0 failures
204
- </pre>
205
-
206
- ...and everything passes. Everything seems fine, but there's one thing missing. When you send 'pop' to a non-empty stack, it's also supposed to remove that element from the stack. Here's a typical solution to this problem using TDD:
207
-
208
- <ruby>
209
- #typical state-based example
210
- def test_should_remove_top_item_when_sent_pop
211
- assert_equal(stack.size, 1)
212
- stack.pop
213
- assert_equal(stack.size, 0)
214
- end
215
- </ruby>
216
-
217
- That is VERY tempting because it seems so simple. It tells the story we want to tell. But it does so at the expense of exposing internal state. "So what?" you may ask. "We're talking about something trivial here." you may say. "Exposing size is perfectly logical because the stack is a collection" you may add. While all those things are reasonable, this thinking almost always takes us down the slippery slope of exposing more and more state just because it makes the test easier to write.
218
-
219
- Now don't get me wrong. Tests should be easy to write. Testable code is a primary goal of what we're doing here. Whether you're doing TDD or BDD, that's absolutely key. But all to often we make bad design decisions in the name of testability, when there are perfectly reasonable alternatives right at our fingertips.
220
-
221
- So even if you buy these arguments, TDD would probably lead you to this instead:
222
-
223
- <ruby>
224
- #typical state-based example
225
- def test_should_remove_top_item_when_sent_pop
226
- assert(!stack.empty?)
227
- stack.pop
228
- assert(stack.empty?)
229
- end
230
- </ruby>
231
-
232
- That's a little better. We're exposing state, but it's not something as specific as size. But it's still state. We can do better.
233
-
234
- What we're looking for here is observable behaviour. How does a one-element stack behave after we send it 'pop'. We've stated that the stack should be empty at that point, right? So perhaps the observable behaviour is that it acts like an empty stack:
235
-
236
- <ruby>
237
- specify "should raise a StackUnderflowError the second time you sent it 'pop'" do
238
- @stack.pop
239
- lambda { @stack.pop }.should.raise StackUnderflowError
240
- end
241
- </ruby>
242
-
243
- Run the specs (this time without the -v flag)...
244
-
245
- <pre>
246
- $ spec stack_spec.rb
247
-
248
- ......F
249
-
250
- 1)
251
- ExpectationNotMetError in 'A stack with one item should raise a StackUnderflowError the second time you sent it 'pop''
252
- <Proc> should raise <StackUnderflowError> but raised nothing
253
- ./stack_spec.rb:44:in `should raise a StackUnderflowError the second time you sent it 'pop''
254
-
255
- Finished in 0.000948 seconds
256
-
257
- 2 contexts, 7 specifications, 1 failure
258
- </pre>
259
-
260
- ...implement just enough to meet this specification...
261
-
262
- <ruby>
263
- class Stack
264
-
265
- ...
266
-
267
- def pop
268
- raise StackUnderflowError if @item.nil?
269
- item = @item
270
- @item = nil
271
- item
272
- end
273
-
274
- end
275
- </ruby>
276
-
277
- <pre>
278
- $ spec stack_spec.rb
279
-
280
- .......
281
-
282
- Finished in 0.000859 seconds
283
-
284
- 2 contexts, 7 specifications, 0 failures
285
- </pre>
286
-
287
- ...and all specifications are met. Let's look back at our one-item stack spec so far:
288
-
289
- <ruby>
290
- context "A stack with one item" do
291
-
292
- setup do
293
- @stack = Stack.new
294
- @stack.push "one item"
295
- end
296
-
297
- specify "should keep its mouth shut when you send it 'push'" do
298
- lambda { @stack.push Object.new }.should_not_raise Exception
299
- end
300
-
301
- specify "should return top when you send it 'top'" do
302
- @stack.top.should_equal "one item"
303
- end
304
-
305
- specify "should return top when you send it 'pop'" do
306
- @stack.pop.should_equal "one item"
307
- end
308
-
309
- specify "should raise a StackUnderflowError the second time you sent it 'pop'" do
310
- @stack.pop
311
- lambda { @stack.pop }.should_raise StackUnderflowError
312
- end
313
-
314
- end
315
- </ruby>
316
-
317
- See some imbalance? We've specified what happens when you send 'pop' repeatedly, but not what happens when you send 'top' repeatedly. 'top' should keep returning the same value, right?
318
-
319
- <ruby>
320
- context "A stack with one item" do
321
-
322
- ...
323
-
324
- specify "should return top repeatedly when you send it 'top'" do
325
- @stack.top.should_equal "one item"
326
- @stack.top.should_equal "one item"
327
- @stack.top.should_equal "one item"
328
- end
329
-
330
- ...
331
-
332
- end
333
- </ruby>
334
-
335
- Run the specs (with the -v flag)...
336
-
337
- <pre>
338
- $ spec stack_spec.rb -v
339
-
340
- An empty stack
341
- - should keep its mouth shut when you send it 'push'
342
- - should raise a StackUnderflowError when you send it 'top'
343
- - should raise a StackUnderflowError when you send it 'pop'
344
-
345
- A stack with one item
346
- - should keep its mouth shut when you send it 'push'
347
- - should return top when you send it 'top'
348
- - should return top repeatedly when you send it 'top'
349
- - should return top when you send it 'pop'
350
- - should raise a StackUnderflowError the second time you sent it 'pop'
351
-
352
- Finished in 0.001746 seconds
353
-
354
- 2 contexts, 8 specifications, 0 failures
355
- </pre>
356
-
357
- So in this case we did not need any additional implementation, but look at how well rounded the specification becomes.
358
-
359
- <a href="stack_04.html">Previous</a>