rspec-given 1.5.1 → 1.6.0.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +225 -0
- data/README.md +7 -3
- data/README.old +264 -0
- data/Rakefile +15 -4
- data/examples/stack/stack.rb +4 -0
- data/examples/stack/stack_spec.rb +5 -0
- data/lib/rspec/given.rb +2 -0
- data/lib/rspec/given/configure.rb +2 -0
- data/lib/rspec/given/extensions.rb +9 -1
- data/lib/rspec/given/failure.rb +18 -0
- data/lib/rspec/given/have_failed.rb +24 -0
- data/lib/rspec/given/version.rb +4 -2
- metadata +15 -11
data/README
ADDED
@@ -0,0 +1,225 @@
|
|
1
|
+
= rspec-given
|
2
|
+
|
3
|
+
Covering rspec-given, version 1.6.0.beta.1.
|
4
|
+
|
5
|
+
rspec-given is an RSpec extension to allow Given/When/Then notation in
|
6
|
+
RSpec specifications. It is a natural extension of the experimental
|
7
|
+
work done on the Given framework. It turns out that 90% of the Given
|
8
|
+
framework can be trivially implemented on top of RSpec.
|
9
|
+
|
10
|
+
= Why Given/When/Then
|
11
|
+
|
12
|
+
RSpec has done a great job of making specifications more readable for
|
13
|
+
humans. However, I really like the given / when / then nature of
|
14
|
+
Cucumber stories and would like to follow the same structure in my
|
15
|
+
unit tests. rspec-given allows a simple given/when/then structure
|
16
|
+
RSpec specifications.
|
17
|
+
|
18
|
+
== Status
|
19
|
+
|
20
|
+
_rspec-given_ is ready for production use.
|
21
|
+
|
22
|
+
== Example
|
23
|
+
|
24
|
+
Here is a specification written in the rspec-given framework:
|
25
|
+
|
26
|
+
require 'rspec/given'
|
27
|
+
require 'spec_helper'
|
28
|
+
require 'stack'
|
29
|
+
|
30
|
+
describe Stack do
|
31
|
+
def stack_with(initial_contents)
|
32
|
+
stack = Stack.new
|
33
|
+
initial_contents.each do |item| stack.push(item) end
|
34
|
+
stack
|
35
|
+
end
|
36
|
+
|
37
|
+
Given(:stack) { stack_with(initial_contents) }
|
38
|
+
|
39
|
+
context "when empty" do
|
40
|
+
Given(:initial_contents) { [] }
|
41
|
+
Then { stack.depth.should == 0 }
|
42
|
+
|
43
|
+
context "when pushing" do
|
44
|
+
When { stack.push(:an_item) }
|
45
|
+
|
46
|
+
Then { stack.depth.should == 1 }
|
47
|
+
Then { stack.top.should == :an_item }
|
48
|
+
end
|
49
|
+
|
50
|
+
context "when popping" do
|
51
|
+
When(:result) { stack.pop }
|
52
|
+
Then { result.should have_failed(Stack::UnderflowError, /empty/) }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "with one item" do
|
57
|
+
Given(:initial_contents) { [:an_item] }
|
58
|
+
|
59
|
+
context "when popping" do
|
60
|
+
When(:pop_result) { stack.pop }
|
61
|
+
|
62
|
+
Then { pop_result.should == :an_item }
|
63
|
+
Then { stack.should be_empty }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context "with several items" do
|
68
|
+
Given(:initial_contents) { [:second_item, :top_item] }
|
69
|
+
Given!(:original_depth) { stack.depth }
|
70
|
+
|
71
|
+
context "when pushing" do
|
72
|
+
When { stack.push(:new_item) }
|
73
|
+
|
74
|
+
Then { stack.top.should == :new_item }
|
75
|
+
Then { stack.depth.should == original_depth + 1 }
|
76
|
+
end
|
77
|
+
|
78
|
+
context "when popping" do
|
79
|
+
When(:pop_result) { stack.pop }
|
80
|
+
|
81
|
+
Then { pop_result.should == :top_item }
|
82
|
+
Then { stack.top.should == :second_item }
|
83
|
+
Then { stack.depth.should == original_depth - 1 }
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
Let's talk about the individual statements used in the Given
|
89
|
+
framework.
|
90
|
+
|
91
|
+
=== Given
|
92
|
+
|
93
|
+
The _Given_ section specifies a starting point, a set of preconditions
|
94
|
+
that must be true before the code under test is allowed to be run. In
|
95
|
+
standard test frameworks the preconditions are established with a
|
96
|
+
combination of setup methods (or :before actions in RSpec) and code in
|
97
|
+
the test.
|
98
|
+
|
99
|
+
In the example code above the preconditions are started with _Given_
|
100
|
+
statements. A top level _Given_ (that applies to the entire describe
|
101
|
+
block) says that one of the preconditions is that there is a stack
|
102
|
+
with some initial contents.
|
103
|
+
|
104
|
+
Note that initial contents are not specified in the top level describe
|
105
|
+
block, but are given in each of the nested contexts. By pushing the
|
106
|
+
definition of "initial_contents" into the nested contexts, we can vary
|
107
|
+
them as needed for that particular context.
|
108
|
+
|
109
|
+
A precondition in the form "Given(:var) {...}" creates an accessor
|
110
|
+
method named "var". The accessor is lazily initialized by the code
|
111
|
+
block. If you want a non-lazy given, use "Given!(:var) {...}".
|
112
|
+
|
113
|
+
A precondition in the form "Given {...}" just executes the code block
|
114
|
+
for side effects. Since there is no accessor, the code block is
|
115
|
+
executed immediately (i.e. no lazy evaluation).
|
116
|
+
|
117
|
+
The preconditions are run in order of definition. Nested contexts
|
118
|
+
will inherit the preconditions from the enclosing context, with out
|
119
|
+
preconditions running before inner preconditions.
|
120
|
+
|
121
|
+
==== Given examples:
|
122
|
+
|
123
|
+
Given(:stack) { Stack.new }
|
124
|
+
|
125
|
+
The given block is lazily run if 'stack' is ever referenced in the
|
126
|
+
test and the value of the block is bound to 'stack'. The first
|
127
|
+
reference to 'stack' in the specification will cause the code block to
|
128
|
+
execute. Futher references to 'stack' will reuse the previously
|
129
|
+
generated value.
|
130
|
+
|
131
|
+
Given!(:original_size) { stack.size }
|
132
|
+
|
133
|
+
The code block is run unconditionally once before each test and the
|
134
|
+
value of the block is bound to 'original_size'. This form is useful
|
135
|
+
when you want to record the value of something that might be affected
|
136
|
+
by the When code.
|
137
|
+
|
138
|
+
Given { stack.clear }
|
139
|
+
|
140
|
+
The given block is run unconditionally once before each test. This
|
141
|
+
form of given is used for code that is executed for side effects.
|
142
|
+
|
143
|
+
=== When
|
144
|
+
|
145
|
+
The _When_ block specifies the code to be tested ... oops, excuse me
|
146
|
+
... specified. After the preconditions in the given section are met,
|
147
|
+
the when code block is run.
|
148
|
+
|
149
|
+
There should only be one _When_ block for a given context. However, a
|
150
|
+
_When_ in an outer context shoud be treated as a _Given_ in an inner
|
151
|
+
context. E.g.
|
152
|
+
|
153
|
+
context "outer context" do
|
154
|
+
When { code specified in the outer context }
|
155
|
+
Then { assert something about the outer context }
|
156
|
+
|
157
|
+
context "inner context" do
|
158
|
+
|
159
|
+
# At this point, the _When_ of the outer context
|
160
|
+
# should be treated as a _Given_ of the inner context
|
161
|
+
|
162
|
+
When { code specified in the inner context }
|
163
|
+
Then { assert something about the inner context }
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
==== When examples:
|
168
|
+
|
169
|
+
When { stack.push(:item) }
|
170
|
+
|
171
|
+
The code block is executed once per test. The effect of the _When{}_
|
172
|
+
block is very similar to _Given{}_. However, When is used to identify
|
173
|
+
the particular code that is being specified in the current context or
|
174
|
+
describe block.
|
175
|
+
|
176
|
+
When(:result) { stack.pop }
|
177
|
+
|
178
|
+
The code block is executed once per test and the value of the code
|
179
|
+
block is bound to 'result'. Use this form when the code under test
|
180
|
+
returns a value that you wish to interrogate in the _Then_ code.
|
181
|
+
|
182
|
+
=== Then
|
183
|
+
|
184
|
+
The _Then_ sections are the postconditions of the specification. These
|
185
|
+
then conditions must be true after the code under test (the _When_
|
186
|
+
block) is run.
|
187
|
+
|
188
|
+
The code in the _Then_ block should be a single _should_
|
189
|
+
assertion. Code in _Then_ blocks should not have any side effects.
|
190
|
+
|
191
|
+
==== Then examples:
|
192
|
+
|
193
|
+
Then { stack.should be_empty }
|
194
|
+
|
195
|
+
After the related _When_ block is run, the stack should be empty. If
|
196
|
+
it is not empty, the test will fail.
|
197
|
+
|
198
|
+
|
199
|
+
= Future Directions
|
200
|
+
|
201
|
+
I really like the way the Given framework is working out. I feel my
|
202
|
+
tests are much more like specifications when I use it. However, I'm
|
203
|
+
not entirely happy with it.
|
204
|
+
|
205
|
+
First, I would like to introduce invariants. An _Invariant_ block
|
206
|
+
would essentially be a post-conditions that should be true after
|
207
|
+
_Then_ block in the same (or nested) context as the invariant.
|
208
|
+
|
209
|
+
Second, I would like to remove the need for the ".should" in all the
|
210
|
+
_Then_ blocks. In other words, instead of saying:
|
211
|
+
|
212
|
+
Then { x.should == y }
|
213
|
+
|
214
|
+
we could say:
|
215
|
+
|
216
|
+
Then { x == y }
|
217
|
+
|
218
|
+
I think the [wrong assertion library](http://rubygems.org/gems/wrong)
|
219
|
+
has laid some groundwork in this area.
|
220
|
+
|
221
|
+
= Links
|
222
|
+
|
223
|
+
* Github: [https://github.com/jimweirich/rspec-given](https://github.com/jimweirich/rspec-given)
|
224
|
+
* Clone URL: git://github.com/jimweirich/rspec-given.git
|
225
|
+
* Bug/Issue Reporting: [https://github.com/jimweirich/rspec-given/issues](https://github.com/jimweirich/rspec-given/issues)
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# rspec-given
|
2
2
|
|
3
|
-
Covering rspec-given, version 1.
|
3
|
+
Covering rspec-given, version 1.6.0.beta.1.
|
4
4
|
|
5
5
|
rspec-given is an RSpec extension to allow Given/When/Then notation in
|
6
6
|
RSpec specifications. It is a natural extension of the experimental
|
@@ -47,6 +47,11 @@ describe Stack do
|
|
47
47
|
Then { stack.depth.should == 1 }
|
48
48
|
Then { stack.top.should == :an_item }
|
49
49
|
end
|
50
|
+
|
51
|
+
context "when popping" do
|
52
|
+
When(:result) { stack.pop }
|
53
|
+
Then { result.should have_failed(Stack::UnderflowError, /empty/) }
|
54
|
+
end
|
50
55
|
end
|
51
56
|
|
52
57
|
context "with one item" do
|
@@ -256,5 +261,4 @@ has laid some groundwork in this area.
|
|
256
261
|
|
257
262
|
* Github: [https://github.com/jimweirich/rspec-given](https://github.com/jimweirich/rspec-given)
|
258
263
|
* Clone URL: git://github.com/jimweirich/rspec-given.git
|
259
|
-
* Bug/Issue Reporting: [
|
260
|
-
|
264
|
+
* Bug/Issue Reporting: [https://github.com/jimweirich/rspec-given/issues](https://github.com/jimweirich/rspec-given/issues)
|
data/README.old
ADDED
@@ -0,0 +1,264 @@
|
|
1
|
+
# rspec-given
|
2
|
+
|
3
|
+
Covering rspec-given, version 1.5.1.
|
4
|
+
|
5
|
+
rspec-given is an RSpec extension to allow Given/When/Then notation in
|
6
|
+
RSpec specifications. It is a natural extension of the experimental
|
7
|
+
work done on the Given framework. It turns out that 90% of the Given
|
8
|
+
framework can be trivially implemented on top of RSpec.
|
9
|
+
|
10
|
+
# Why Given/When/Then
|
11
|
+
|
12
|
+
RSpec has done a great job of making specifications more readable for
|
13
|
+
humans. However, I really like the given / when / then nature of
|
14
|
+
Cucumber stories and would like to follow the same structure in my
|
15
|
+
unit tests. rspec-given allows a simple given/when/then structure
|
16
|
+
RSpec specifications.
|
17
|
+
|
18
|
+
## Status
|
19
|
+
|
20
|
+
_rspec-given_ is ready for production use.
|
21
|
+
|
22
|
+
## Example
|
23
|
+
|
24
|
+
Here is a specification written in the rspec-given framework:
|
25
|
+
|
26
|
+
<pre>
|
27
|
+
require 'rspec/given'
|
28
|
+
require 'spec_helper'
|
29
|
+
require 'stack'
|
30
|
+
|
31
|
+
describe Stack do
|
32
|
+
def stack_with(initial_contents)
|
33
|
+
stack = Stack.new
|
34
|
+
initial_contents.each do |item| stack.push(item) end
|
35
|
+
stack
|
36
|
+
end
|
37
|
+
|
38
|
+
Given(:stack) { stack_with(initial_contents) }
|
39
|
+
|
40
|
+
context "when empty" do
|
41
|
+
Given(:initial_contents) { [] }
|
42
|
+
Then { stack.depth.should == 0 }
|
43
|
+
|
44
|
+
context "when pushing" do
|
45
|
+
When { stack.push(:an_item) }
|
46
|
+
|
47
|
+
Then { stack.depth.should == 1 }
|
48
|
+
Then { stack.top.should == :an_item }
|
49
|
+
end
|
50
|
+
|
51
|
+
context "when popping" do
|
52
|
+
When(:result) { stack.pop }
|
53
|
+
Then { result.should have_failed(Stack::UnderflowError, /empty/) }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "with one item" do
|
58
|
+
Given(:initial_contents) { [:an_item] }
|
59
|
+
|
60
|
+
context "when popping" do
|
61
|
+
When(:pop_result) { stack.pop }
|
62
|
+
|
63
|
+
Then { pop_result.should == :an_item }
|
64
|
+
Then { stack.should be_empty }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "with several items" do
|
69
|
+
Given(:initial_contents) { [:second_item, :top_item] }
|
70
|
+
Given!(:original_depth) { stack.depth }
|
71
|
+
|
72
|
+
context "when pushing" do
|
73
|
+
When { stack.push(:new_item) }
|
74
|
+
|
75
|
+
Then { stack.top.should == :new_item }
|
76
|
+
Then { stack.depth.should == original_depth + 1 }
|
77
|
+
end
|
78
|
+
|
79
|
+
context "when popping" do
|
80
|
+
When(:pop_result) { stack.pop }
|
81
|
+
|
82
|
+
Then { pop_result.should == :top_item }
|
83
|
+
Then { stack.top.should == :second_item }
|
84
|
+
Then { stack.depth.should == original_depth - 1 }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
</pre>
|
89
|
+
|
90
|
+
Let's talk about the individual statements used in the Given
|
91
|
+
framework.
|
92
|
+
|
93
|
+
### Given
|
94
|
+
|
95
|
+
The _Given_ section specifies a starting point, a set of preconditions
|
96
|
+
that must be true before the code under test is allowed to be run. In
|
97
|
+
standard test frameworks the preconditions are established with a
|
98
|
+
combination of setup methods (or :before actions in RSpec) and code in
|
99
|
+
the test.
|
100
|
+
|
101
|
+
In the example code above the preconditions are started with _Given_
|
102
|
+
statements. A top level _Given_ (that applies to the entire describe
|
103
|
+
block) says that one of the preconditions is that there is a stack
|
104
|
+
with some initial contents.
|
105
|
+
|
106
|
+
Note that initial contents are not specified in the top level describe
|
107
|
+
block, but are given in each of the nested contexts. By pushing the
|
108
|
+
definition of "initial_contents" into the nested contexts, we can vary
|
109
|
+
them as needed for that particular context.
|
110
|
+
|
111
|
+
A precondition in the form "Given(:var) {...}" creates an accessor
|
112
|
+
method named "var". The accessor is lazily initialized by the code
|
113
|
+
block. If you want a non-lazy given, use "Given!(:var) {...}".
|
114
|
+
|
115
|
+
A precondition in the form "Given {...}" just executes the code block
|
116
|
+
for side effects. Since there is no accessor, the code block is
|
117
|
+
executed immediately (i.e. no lazy evaluation).
|
118
|
+
|
119
|
+
The preconditions are run in order of definition. Nested contexts
|
120
|
+
will inherit the preconditions from the enclosing context, with out
|
121
|
+
preconditions running before inner preconditions.
|
122
|
+
|
123
|
+
#### Given examples:
|
124
|
+
|
125
|
+
<pre>
|
126
|
+
Given(:stack) { Stack.new }
|
127
|
+
</pre>
|
128
|
+
|
129
|
+
The given block is lazily run if 'stack' is ever referenced in the
|
130
|
+
test and the value of the block is bound to 'stack'. The first
|
131
|
+
reference to 'stack' in the specification will cause the code block to
|
132
|
+
execute. Futher references to 'stack' will reuse the previously
|
133
|
+
generated value.
|
134
|
+
|
135
|
+
<pre>
|
136
|
+
Given!(:original_size) { stack.size }
|
137
|
+
</pre>
|
138
|
+
|
139
|
+
The code block is run unconditionally once before each test and the
|
140
|
+
value of the block is bound to 'original_size'. This form is useful
|
141
|
+
when you want to record the value of something that might be affected
|
142
|
+
by the When code.
|
143
|
+
|
144
|
+
<pre>
|
145
|
+
Given { stack.clear }
|
146
|
+
</pre>
|
147
|
+
|
148
|
+
The given block is run unconditionally once before each test. This
|
149
|
+
form of given is used for code that is executed for side effects.
|
150
|
+
|
151
|
+
### When
|
152
|
+
|
153
|
+
The _When_ block specifies the code to be tested ... oops, excuse me
|
154
|
+
... specified. After the preconditions in the given section are met,
|
155
|
+
the when code block is run.
|
156
|
+
|
157
|
+
There should only be one _When_ block for a given context. However, a
|
158
|
+
_When_ in an outer context shoud be treated as a _Given_ in an inner
|
159
|
+
context. E.g.
|
160
|
+
|
161
|
+
<pre>
|
162
|
+
context "outer context" do
|
163
|
+
When { code specified in the outer context }
|
164
|
+
Then { assert something about the outer context }
|
165
|
+
|
166
|
+
context "inner context" do
|
167
|
+
|
168
|
+
# At this point, the _When_ of the outer context
|
169
|
+
# should be treated as a _Given_ of the inner context
|
170
|
+
|
171
|
+
When { code specified in the inner context }
|
172
|
+
Then { assert something about the inner context }
|
173
|
+
end
|
174
|
+
end
|
175
|
+
</pre>
|
176
|
+
|
177
|
+
#### When examples:
|
178
|
+
|
179
|
+
<pre>
|
180
|
+
When { stack.push(:item) }
|
181
|
+
</pre>
|
182
|
+
|
183
|
+
The code block is executed once per test. The effect of the _When{}_
|
184
|
+
block is very similar to _Given{}_. However, When is used to identify
|
185
|
+
the particular code that is being specified in the current context or
|
186
|
+
describe block.
|
187
|
+
|
188
|
+
<pre>
|
189
|
+
When(:result) { stack.pop }
|
190
|
+
</pre>
|
191
|
+
|
192
|
+
The code block is executed once per test and the value of the code
|
193
|
+
block is bound to 'result'. Use this form when the code under test
|
194
|
+
returns a value that you wish to interrogate in the _Then_ code.
|
195
|
+
|
196
|
+
### Then
|
197
|
+
|
198
|
+
The _Then_ sections are the postconditions of the specification. These
|
199
|
+
then conditions must be true after the code under test (the _When_
|
200
|
+
block) is run.
|
201
|
+
|
202
|
+
The code in the _Then_ block should be a single _should_
|
203
|
+
assertion. Code in _Then_ blocks should not have any side effects.
|
204
|
+
|
205
|
+
#### Then examples:
|
206
|
+
|
207
|
+
<pre>
|
208
|
+
Then { stack.should be_empty }
|
209
|
+
</pre>
|
210
|
+
|
211
|
+
After the related _When_ block is run, the stack should be empty. If
|
212
|
+
it is not empty, the test will fail.
|
213
|
+
|
214
|
+
<!--
|
215
|
+
### Invariant
|
216
|
+
|
217
|
+
The _Invariant_ block is a new idea that doesn't have an analog in
|
218
|
+
RSpec or Test::Unit. The invariant allows you specify things that
|
219
|
+
must always be true. In the stack example, <tt>empty?</tt> is defined
|
220
|
+
in term of <tt>size</tt>. Whenever <tt>size</tt> is 0,
|
221
|
+
<tt>empty?</tt> should be true. Whenever <tt>size</tt> is non-zero,
|
222
|
+
<tt>empty?</tt> should be false.
|
223
|
+
|
224
|
+
You can conceptually think of an _Invariant_ block as a _Then_ block
|
225
|
+
that automatically gets added to every _When_ within its scope.
|
226
|
+
|
227
|
+
Invariants nested within a context only apply to the _When_ blocks in
|
228
|
+
that context.
|
229
|
+
|
230
|
+
Invariants that reference a _Given_ precondition accessor must only be
|
231
|
+
used in contexts that define that accessor.
|
232
|
+
|
233
|
+
NOTE: Invariants are not yet implemented in the current version of
|
234
|
+
rspec-given.
|
235
|
+
|
236
|
+
-->
|
237
|
+
|
238
|
+
# Future Directions
|
239
|
+
|
240
|
+
I really like the way the Given framework is working out. I feel my
|
241
|
+
tests are much more like specifications when I use it. However, I'm
|
242
|
+
not entirely happy with it.
|
243
|
+
|
244
|
+
First, I would like to introduce invariants. An _Invariant_ block
|
245
|
+
would essentially be a post-conditions that should be true after
|
246
|
+
_Then_ block in the same (or nested) context as the invariant.
|
247
|
+
|
248
|
+
Second, I would like to remove the need for the ".should" in all the
|
249
|
+
_Then_ blocks. In other words, instead of saying:
|
250
|
+
|
251
|
+
Then { x.should == y }
|
252
|
+
|
253
|
+
we could say:
|
254
|
+
|
255
|
+
Then { x == y }
|
256
|
+
|
257
|
+
I think the [wrong assertion library](http://rubygems.org/gems/wrong)
|
258
|
+
has laid some groundwork in this area.
|
259
|
+
|
260
|
+
# Links
|
261
|
+
|
262
|
+
* Github: [https://github.com/jimweirich/rspec-given](https://github.com/jimweirich/rspec-given)
|
263
|
+
* Clone URL: git://github.com/jimweirich/rspec-given.git
|
264
|
+
* Bug/Issue Reporting: [https://github.com/jimweirich/rspec-given/issues](https://github.com/jimweirich/rspec-given/issues)
|
data/Rakefile
CHANGED
@@ -13,32 +13,43 @@ rescue LoadError => ex
|
|
13
13
|
puts "WARNING: BlueCloth not available"
|
14
14
|
end
|
15
15
|
|
16
|
-
task :default => :
|
16
|
+
task :default => :ex2
|
17
17
|
|
18
18
|
# Running examples ---------------------------------------------------
|
19
19
|
|
20
20
|
desc "Run all the examples"
|
21
|
-
task :examples => [:examples1, :examples2]
|
21
|
+
task :examples => [:specs, :examples1, :examples2]
|
22
|
+
|
23
|
+
desc "Run the RSpec 2 specs and examples"
|
24
|
+
task :ex2 => [:specs, :examples2]
|
25
|
+
|
26
|
+
desc "Run the specs"
|
27
|
+
task :specs do
|
28
|
+
puts "Running specs"
|
29
|
+
sh "rspec spec"
|
30
|
+
end
|
22
31
|
|
23
32
|
desc "Run the examples in RSpec 1"
|
24
33
|
task :examples1 => [:verify_rspec1] do
|
34
|
+
puts "Running examples (with RSpec2)"
|
25
35
|
sh "spec examples/stack/stack_spec1.rb"
|
26
36
|
end
|
27
37
|
|
28
38
|
desc "Run the examples in RSpec 2"
|
29
39
|
task :examples2 => [:verify_rspec2] do
|
40
|
+
puts "Running examples (with RSpec2)"
|
30
41
|
sh "rspec examples"
|
31
42
|
end
|
32
43
|
|
33
44
|
task :verify_rspec1 do
|
34
45
|
sh "type spec >/dev/null 2>&1", verbose: false do |status|
|
35
|
-
fail "You need to install RSpec 1 in order to test
|
46
|
+
fail "You need to install RSpec 1 in order to test against it." unless status
|
36
47
|
end
|
37
48
|
end
|
38
49
|
|
39
50
|
task :verify_rspec2 do
|
40
51
|
sh "type rspec >/dev/null 2>&1", verbose: false do |status|
|
41
|
-
fail "You need to install RSpec 2 in order to test
|
52
|
+
fail "You need to install RSpec 2 in order to test against it." unless status
|
42
53
|
end
|
43
54
|
end
|
44
55
|
|
data/examples/stack/stack.rb
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
class Stack
|
2
|
+
class StackError < StandardError; end
|
3
|
+
class UnderflowError < StackError; end
|
4
|
+
|
2
5
|
def initialize
|
3
6
|
@items = []
|
4
7
|
end
|
@@ -20,6 +23,7 @@ class Stack
|
|
20
23
|
end
|
21
24
|
|
22
25
|
def pop
|
26
|
+
fail UnderflowError, "Cannot pop an empty stack" if empty?
|
23
27
|
@items.pop
|
24
28
|
end
|
25
29
|
end
|
@@ -21,6 +21,11 @@ describe Stack do
|
|
21
21
|
Then { stack.depth.should == 1 }
|
22
22
|
Then { stack.top.should == :an_item }
|
23
23
|
end
|
24
|
+
|
25
|
+
context "when popping" do
|
26
|
+
When(:result) { stack.pop }
|
27
|
+
Then { result.should have_failed(Stack::UnderflowError, /empty/) }
|
28
|
+
end
|
24
29
|
end
|
25
30
|
|
26
31
|
context "with one item" do
|
data/lib/rspec/given.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'rspec/given/failure'
|
2
|
+
|
1
3
|
module RSpec
|
2
4
|
module Given
|
3
5
|
module Extensions
|
@@ -58,7 +60,13 @@ module RSpec
|
|
58
60
|
#
|
59
61
|
def When(*args, &block)
|
60
62
|
if args.first.is_a?(Symbol)
|
61
|
-
let!(args.first
|
63
|
+
let!(args.first) do
|
64
|
+
begin
|
65
|
+
instance_eval(&block)
|
66
|
+
rescue Exception => ex
|
67
|
+
Failure.new(ex)
|
68
|
+
end
|
69
|
+
end
|
62
70
|
else
|
63
71
|
before(&block)
|
64
72
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Given
|
3
|
+
|
4
|
+
# Failure objects will raise the given exception whenever you try
|
5
|
+
# to send it *any* message.
|
6
|
+
class Failure < BasicObject
|
7
|
+
undef_method :==, :!=, :!
|
8
|
+
|
9
|
+
def initialize(exception)
|
10
|
+
@exception = exception
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing(sym, *args, &block)
|
14
|
+
::Kernel.raise @exception
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Given
|
3
|
+
module HaveFailed
|
4
|
+
|
5
|
+
# Alias for raise_error(...), but reads a bit better when using
|
6
|
+
# a failure result from a when clause.
|
7
|
+
#
|
8
|
+
# NOTE: This is new for 1.6.0.beta.1. A name change for this
|
9
|
+
# method is possible.
|
10
|
+
#
|
11
|
+
# Typical Usage:
|
12
|
+
#
|
13
|
+
# When(:result) { fail "OUCH" }
|
14
|
+
# Then { result.should have_failed(StandardError, /OUCH/) }
|
15
|
+
#
|
16
|
+
# :call-seq:
|
17
|
+
# have_failed([exception_class [, message_pattern]])
|
18
|
+
#
|
19
|
+
def have_failed(*args, &block)
|
20
|
+
raise_error(*args, &block)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/rspec/given/version.rb
CHANGED
metadata
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-given
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.6.0.beta.1
|
5
|
+
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jim Weirich
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70359307141520 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>'
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 1.2.8
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70359307141520
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bluecloth
|
27
|
-
requirement: &
|
27
|
+
requirement: &70359307140800 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70359307140800
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rdoc
|
38
|
-
requirement: &
|
38
|
+
requirement: &70359307140080 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>'
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 2.4.2
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70359307140080
|
47
47
|
description: ! 'Given is an RSpec extension that allows explicit definition of the
|
48
48
|
|
49
49
|
pre and post-conditions for code under test.
|
@@ -56,10 +56,14 @@ extra_rdoc_files: []
|
|
56
56
|
files:
|
57
57
|
- MIT-LICENSE
|
58
58
|
- Rakefile
|
59
|
+
- README
|
59
60
|
- README.md
|
61
|
+
- README.old
|
60
62
|
- lib/rspec-given.rb
|
61
63
|
- lib/rspec/given/configure.rb
|
62
64
|
- lib/rspec/given/extensions.rb
|
65
|
+
- lib/rspec/given/failure.rb
|
66
|
+
- lib/rspec/given/have_failed.rb
|
63
67
|
- lib/rspec/given/rspec1_given.rb
|
64
68
|
- lib/rspec/given/version.rb
|
65
69
|
- lib/rspec/given.rb
|
@@ -88,9 +92,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
92
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
93
|
none: false
|
90
94
|
requirements:
|
91
|
-
- - ! '
|
95
|
+
- - ! '>'
|
92
96
|
- !ruby/object:Gem::Version
|
93
|
-
version:
|
97
|
+
version: 1.3.1
|
94
98
|
requirements: []
|
95
99
|
rubyforge_project: given
|
96
100
|
rubygems_version: 1.8.15
|