given_core 3.0.0.beta.4 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +0 -1
- data/Gemfile.lock +2 -4
- data/README.md +46 -8
- data/README.old +829 -0
- data/lib/given/binary_operation.rb +33 -0
- data/lib/given/evaluator.rb +4 -0
- data/lib/given/extensions.rb +31 -12
- data/lib/given/failure.rb +23 -6
- data/lib/given/minitest/all.rb +1 -0
- data/lib/given/minitest/failure_must_raise.rb +13 -0
- data/lib/given/module_methods.rb +20 -4
- data/lib/given/natural_assertion.rb +19 -18
- data/lib/given/rspec/all.rb +1 -1
- data/lib/given/rspec/monkey.rb +1 -0
- data/lib/given/version.rb +0 -2
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d0202f63aad80f12a4a00c7c783f12e34ee8370
|
4
|
+
data.tar.gz: d63f151d4f5c704956b42f6f6892398649b191c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29034571bea4ef8f6526079d15b6c091c78e059c210ffe4e2ea9a4b537b8068562536236eb87a417c5773975e507ca82479e6baf21810c5e945d9c20f67cee6d
|
7
|
+
data.tar.gz: 374f4799fe58e262ea1c2aaed3cec7f37753248cb44ecd5ca4c120dc3386243fb2ebd9264b69993dd7ce99bf511c122254938eabc23f7d32b151f543494572c1
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -2,14 +2,13 @@ GEM
|
|
2
2
|
remote: https://rubygems.org/
|
3
3
|
specs:
|
4
4
|
diff-lcs (1.2.4)
|
5
|
-
flexmock (1.3.2)
|
6
5
|
minitest (5.0.6)
|
7
6
|
rake (10.0.4)
|
8
|
-
rspec (2.14.
|
7
|
+
rspec (2.14.1)
|
9
8
|
rspec-core (~> 2.14.0)
|
10
9
|
rspec-expectations (~> 2.14.0)
|
11
10
|
rspec-mocks (~> 2.14.0)
|
12
|
-
rspec-core (2.14.
|
11
|
+
rspec-core (2.14.1)
|
13
12
|
rspec-expectations (2.14.0)
|
14
13
|
diff-lcs (>= 1.1.3, < 2.0)
|
15
14
|
rspec-mocks (2.14.1)
|
@@ -20,7 +19,6 @@ PLATFORMS
|
|
20
19
|
ruby
|
21
20
|
|
22
21
|
DEPENDENCIES
|
23
|
-
flexmock
|
24
22
|
minitest (>= 4.3)
|
25
23
|
rake
|
26
24
|
rspec (>= 2.12)
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
| :----: |
|
5
5
|
| [![Master Build Status](https://secure.travis-ci.org/jimweirich/rspec-given.png?branch=master)](https://travis-ci.org/jimweirich/rspec-given) |
|
6
6
|
|
7
|
-
Covering rspec-given, minitest-given, and given-core, version 3.0.0.
|
7
|
+
Covering rspec-given, minitest-given, and given-core, version 3.0.0.
|
8
8
|
|
9
9
|
rspec-given and minitest-given are extensions to your favorite testing
|
10
10
|
framework to allow Given/When/Then notation when writing specs.
|
@@ -77,6 +77,44 @@ See
|
|
77
77
|
and
|
78
78
|
[example_helper.rb](https://github.com/jimweirich/rspec-given/blob/minispec/examples/example_helper.rb)
|
79
79
|
|
80
|
+
## Installation
|
81
|
+
|
82
|
+
### If you are using bundler
|
83
|
+
|
84
|
+
Add `rspec-given` (or `minitest-given`) to the `:test` group in the `Gemfile`:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
group :test do
|
88
|
+
gem 'rspec-given'
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
group :test do
|
94
|
+
gem 'minitest-given'
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
Download and install:
|
99
|
+
|
100
|
+
`$ bundle`
|
101
|
+
|
102
|
+
Then just require `rspec/given` (or `minitest/given`) in the
|
103
|
+
`spec_helper` of your project and it is ready to go.
|
104
|
+
|
105
|
+
### If you are not using bundler
|
106
|
+
|
107
|
+
Install the gem:
|
108
|
+
|
109
|
+
`$ gem install rspec-given`
|
110
|
+
|
111
|
+
or
|
112
|
+
|
113
|
+
`$ gem install minitest-given`
|
114
|
+
|
115
|
+
Then just require `rspec/given` (or `minitest/given`) in the
|
116
|
+
`spec_helper` of your project and it is ready to go.
|
117
|
+
|
80
118
|
## Example
|
81
119
|
|
82
120
|
Here is a specification written in the rspec-given framework:
|
@@ -575,7 +613,7 @@ like this is good:
|
|
575
613
|
```
|
576
614
|
|
577
615
|
It is good to note that non-idempotent assertions will also cause
|
578
|
-
problems with And clauses.
|
616
|
+
problems with And and Invariant clauses.
|
579
617
|
|
580
618
|
### Mixing Natural Assertions and RSpec Assertions
|
581
619
|
|
@@ -665,7 +703,7 @@ fuzzy numbers.
|
|
665
703
|
|
666
704
|
The RSpec matcher used for detecting exceptions will work with natural
|
667
705
|
assertions out of the box. Just check for equality against the
|
668
|
-
<code>
|
706
|
+
<code>Failure()</code> method return value.
|
669
707
|
|
670
708
|
For example, the following two Then clauses are equivalent:
|
671
709
|
|
@@ -715,9 +753,6 @@ Conover](http://rubygems.org/profiles/sconoversf).
|
|
715
753
|
|
716
754
|
## Configuration
|
717
755
|
|
718
|
-
Just require 'rspec/given' in the spec helper of your project and it
|
719
|
-
is ready to go.
|
720
|
-
|
721
756
|
If the RSpec format option document, html or textmate is chosen,
|
722
757
|
RSpec/Given will automatically add additional source code information to
|
723
758
|
the examples to produce better looking output. If you don't care about
|
@@ -751,8 +786,11 @@ License. See the MIT-LICENSE file in the source distribution.
|
|
751
786
|
|
752
787
|
* Support for minitest added.
|
753
788
|
|
754
|
-
*
|
755
|
-
|
789
|
+
* Introduced gem given_core to contain the common logic between the
|
790
|
+
RSpec and Minitest versions. Both the rspec-given gem and the
|
791
|
+
minitest-given gem have a dependency on given_core.
|
792
|
+
|
793
|
+
* Natural assertions are now enabled by default.
|
756
794
|
|
757
795
|
* Version 2.4.4
|
758
796
|
|
data/README.old
ADDED
@@ -0,0 +1,829 @@
|
|
1
|
+
# Given/When/Then for RSpec and Minitest
|
2
|
+
|
3
|
+
| Master | Minispec |
|
4
|
+
| :----: | :----: |
|
5
|
+
| [![Master Build Status](https://secure.travis-ci.org/jimweirich/rspec-given.png?branch=master)](https://travis-ci.org/jimweirich/rspec-given) | [![Minispec Build Status](https://secure.travis-ci.org/jimweirich/rspec-given.png?branch=minispec)](https://travis-ci.org/jimweirich/rspec-given) |
|
6
|
+
|
7
|
+
Covering rspec-given, minitest-given, and given-core, version 3.0.0.beta.1.
|
8
|
+
|
9
|
+
rspec-given and minitest-given are extensions to your favorite testing
|
10
|
+
framework to allow Given/When/Then notation when writing specs.
|
11
|
+
|
12
|
+
# Why Given/When/Then
|
13
|
+
|
14
|
+
RSpec has done a great job of making specifications more readable for
|
15
|
+
humans. However, I really like the given/when/then nature of Cucumber
|
16
|
+
stories and would like to follow the same structure in my unit tests.
|
17
|
+
rspec-given (and now minitest-given) allows a simple given/when/then
|
18
|
+
structure RSpec specifications.
|
19
|
+
|
20
|
+
## Status
|
21
|
+
|
22
|
+
* rspec-given is ready for production use.
|
23
|
+
* minitest-given is experimental.
|
24
|
+
|
25
|
+
### RSpec/Given
|
26
|
+
|
27
|
+
The rspec-given gem is the original given/when/then extension for
|
28
|
+
RSpec. It now depends on a given_core gem for the basic functionality
|
29
|
+
and then adds the RSpec specific code.
|
30
|
+
|
31
|
+
* rspec-given now requires RSpec version 2.12 or better.
|
32
|
+
|
33
|
+
### Minitest/Given
|
34
|
+
|
35
|
+
A new minitest-given gem allows Given/When/Then notation directly in
|
36
|
+
Minitest::Spec specifications.
|
37
|
+
|
38
|
+
To use minitest-given, just place the following require at the top of
|
39
|
+
the file (or in a convenient spec_helper).
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require 'minitest/given'
|
43
|
+
```
|
44
|
+
|
45
|
+
All the features of rspec-given are available in minitest-given.
|
46
|
+
|
47
|
+
When switching from RSpec/Given to Minitest/Given, here are some
|
48
|
+
things to watch out for:
|
49
|
+
|
50
|
+
* You need to use Minitest version 4.3 or better (yes, Minitest 5.x
|
51
|
+
should work as well).
|
52
|
+
|
53
|
+
* Minitest/Given adds the missing "context" block to Minitest::Spec.
|
54
|
+
|
55
|
+
* Only one before block is allowed in any given Minitest::Spec
|
56
|
+
describe block. This doesn't effect the number of Givens you are
|
57
|
+
allowed to use, but it may surprise if you are use to RSpec.
|
58
|
+
|
59
|
+
### Auto Selecting
|
60
|
+
|
61
|
+
If you use natural assertions exclusively in your specs, it's quite
|
62
|
+
possible to write specs that run under both RSpec and Minitest::Spec.
|
63
|
+
|
64
|
+
Use this at the start of your spec file:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
if defined?(RSpec)
|
68
|
+
require 'rspec/given'
|
69
|
+
else
|
70
|
+
require 'minitest/autorun'
|
71
|
+
require 'minitest/given'
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
See
|
76
|
+
[stack_spec.rb](https://github.com/jimweirich/rspec-given/blob/minispec/examples/stack/stack_spec.rb)
|
77
|
+
and
|
78
|
+
[example_helper.rb](https://github.com/jimweirich/rspec-given/blob/minispec/examples/example_helper.rb)
|
79
|
+
|
80
|
+
## Installation
|
81
|
+
|
82
|
+
### If you are using bundler
|
83
|
+
|
84
|
+
Add `rspec-given` (or `minitest-given`) to the `:test` group in the `Gemfile`:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
group :test do
|
88
|
+
gem 'rspec-given'
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
group :test do
|
94
|
+
gem 'minitest-given'
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
Download and install:
|
99
|
+
|
100
|
+
`$ bundle`
|
101
|
+
|
102
|
+
Then just require `rspec/given` (or `minitest/given`) in the
|
103
|
+
`spec_helper` of your project and it is ready to go.
|
104
|
+
|
105
|
+
### If you are not using bundler
|
106
|
+
|
107
|
+
Install the gem:
|
108
|
+
|
109
|
+
`$ gem install rspec-given`
|
110
|
+
|
111
|
+
or
|
112
|
+
|
113
|
+
`$ gem install minitest-given`
|
114
|
+
|
115
|
+
Then just require `rspec/given` (or `minitest/given`) in the
|
116
|
+
`spec_helper` of your project and it is ready to go.
|
117
|
+
|
118
|
+
## Example
|
119
|
+
|
120
|
+
Here is a specification written in the rspec-given framework:
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
require 'rspec/given'
|
124
|
+
require 'spec_helper'
|
125
|
+
require 'stack'
|
126
|
+
|
127
|
+
describe Stack do
|
128
|
+
def stack_with(initial_contents)
|
129
|
+
stack = Stack.new
|
130
|
+
initial_contents.each do |item| stack.push(item) end
|
131
|
+
stack
|
132
|
+
end
|
133
|
+
|
134
|
+
Given(:stack) { stack_with(initial_contents) }
|
135
|
+
Invariant { stack.empty? == (stack.depth == 0) }
|
136
|
+
|
137
|
+
context "with no items" do
|
138
|
+
Given(:initial_contents) { [] }
|
139
|
+
Then { stack.depth == 0 }
|
140
|
+
|
141
|
+
context "when pushing" do
|
142
|
+
When { stack.push(:an_item) }
|
143
|
+
|
144
|
+
Then { stack.depth == 1 }
|
145
|
+
Then { stack.top == :an_item }
|
146
|
+
end
|
147
|
+
|
148
|
+
context "when popping" do
|
149
|
+
When(:result) { stack.pop }
|
150
|
+
Then { result == Failure(Stack::UnderflowError, /empty/) }
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
context "with one item" do
|
155
|
+
Given(:initial_contents) { [:an_item] }
|
156
|
+
|
157
|
+
context "when popping" do
|
158
|
+
When(:pop_result) { stack.pop }
|
159
|
+
|
160
|
+
Then { pop_result == :an_item }
|
161
|
+
Then { stack.depth == 0 }
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
context "with several items" do
|
166
|
+
Given(:initial_contents) { [:second_item, :top_item] }
|
167
|
+
Given!(:original_depth) { stack.depth }
|
168
|
+
|
169
|
+
context "when pushing" do
|
170
|
+
When { stack.push(:new_item) }
|
171
|
+
|
172
|
+
Then { stack.top == :new_item }
|
173
|
+
Then { stack.depth == original_depth + 1 }
|
174
|
+
end
|
175
|
+
|
176
|
+
context "when popping" do
|
177
|
+
When(:pop_result) { stack.pop }
|
178
|
+
|
179
|
+
Then { pop_result == :top_item }
|
180
|
+
Then { stack.top == :second_item }
|
181
|
+
Then { stack.depth == original_depth - 1 }
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
```
|
186
|
+
|
187
|
+
Let's talk about the individual statements used in the Given
|
188
|
+
framework.
|
189
|
+
|
190
|
+
### Given
|
191
|
+
|
192
|
+
The _Given_ section specifies a starting point, a set of preconditions
|
193
|
+
that must be true before the code under test is allowed to be run. In
|
194
|
+
standard test frameworks the preconditions are established with a
|
195
|
+
combination of setup methods (or :before actions in RSpec) and code in
|
196
|
+
the test.
|
197
|
+
|
198
|
+
In the example code above the preconditions are started with _Given_
|
199
|
+
statements. A top level _Given_ (that applies to the entire describe
|
200
|
+
block) says that one of the preconditions is that there is a stack
|
201
|
+
with some initial contents.
|
202
|
+
|
203
|
+
Note that initial contents are not specified in the top level describe
|
204
|
+
block, but are given in each of the nested contexts. By pushing the
|
205
|
+
definition of "initial_contents" into the nested contexts, we can vary
|
206
|
+
them as needed for that particular context.
|
207
|
+
|
208
|
+
A precondition in the form "Given(:var) {...}" creates an accessor
|
209
|
+
method named "var". The accessor is lazily initialized by the code
|
210
|
+
block. If you want a non-lazy given, use "Given!(:var) {...}".
|
211
|
+
|
212
|
+
A precondition in the form "Given {...}" just executes the code block
|
213
|
+
for side effects. Since there is no accessor, the code block is
|
214
|
+
executed immediately (i.e. no lazy evaluation).
|
215
|
+
|
216
|
+
The preconditions are run in order of definition. Nested contexts
|
217
|
+
will inherit the preconditions from the enclosing context, with outer
|
218
|
+
preconditions running before inner preconditions.
|
219
|
+
|
220
|
+
#### Given examples:
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
Given(:stack) { Stack.new }
|
224
|
+
```
|
225
|
+
|
226
|
+
The block for the given clause is lazily run and its value bound to
|
227
|
+
'stack' if 'stack' is ever referenced in the test.
|
228
|
+
The first reference to 'stack' in the specification will cause the
|
229
|
+
code block to execute. Futher references to 'stack' will reuse the
|
230
|
+
previously generated value.
|
231
|
+
|
232
|
+
```ruby
|
233
|
+
Given!(:original_size) { stack.size }
|
234
|
+
```
|
235
|
+
|
236
|
+
The code block is run unconditionally once before each test and the
|
237
|
+
value of the block is bound to 'original_size'. This form is useful
|
238
|
+
when you want to record the value of something that might be affected
|
239
|
+
by the When code.
|
240
|
+
|
241
|
+
```ruby
|
242
|
+
Given { stack.clear }
|
243
|
+
```
|
244
|
+
|
245
|
+
The block for the given clause is run unconditionally once before each
|
246
|
+
test. This form of given is used for code that is executed for side
|
247
|
+
effects.
|
248
|
+
|
249
|
+
### When
|
250
|
+
|
251
|
+
The _When_ clause specifies the code to be tested ... oops, excuse me
|
252
|
+
... specified. After the preconditions in the given section are met,
|
253
|
+
the when code block is run.
|
254
|
+
|
255
|
+
In general there should not be more than one _When_ clause for a given
|
256
|
+
direct context. However, a _When_ in an outer context will be run
|
257
|
+
after all the _Givens_ but before the inner _When_. You can think of
|
258
|
+
an outer _When_ as setting up additional given state for the inner
|
259
|
+
_When_.
|
260
|
+
|
261
|
+
E.g.
|
262
|
+
|
263
|
+
```ruby
|
264
|
+
context "outer context" do
|
265
|
+
When { code specified in the outer context }
|
266
|
+
Then { assert something about the outer context }
|
267
|
+
|
268
|
+
context "inner context" do
|
269
|
+
|
270
|
+
# At this point, the _When_ of the outer context
|
271
|
+
# should be treated as a _Given_ of the inner context
|
272
|
+
|
273
|
+
When { code specified in the inner context }
|
274
|
+
Then { assert something about the inner context }
|
275
|
+
end
|
276
|
+
end
|
277
|
+
```
|
278
|
+
|
279
|
+
#### When examples:
|
280
|
+
|
281
|
+
```ruby
|
282
|
+
When { stack.push(:item) }
|
283
|
+
```
|
284
|
+
|
285
|
+
The code block is executed once per test. The effect of the _When{}_
|
286
|
+
block is very similar to _Given{}_. However, When is used to identify
|
287
|
+
the particular code that is being specified in the current context or
|
288
|
+
describe block.
|
289
|
+
|
290
|
+
```ruby
|
291
|
+
When(:result) { stack.pop }
|
292
|
+
```
|
293
|
+
|
294
|
+
The code block is executed once per test and the value of the code
|
295
|
+
block is bound to 'result'. Use this form when the code under test
|
296
|
+
returns a value that you wish to interrogate in the _Then_ code.
|
297
|
+
|
298
|
+
If an exception occurs during the execution of the block for the When
|
299
|
+
clause, the exception is caught and a failure object is bound to
|
300
|
+
'result'. The failure can be checked in a then block with the
|
301
|
+
'have_failed' matcher.
|
302
|
+
|
303
|
+
The failure object will rethrow the captured exception if anything
|
304
|
+
other than have_failed matcher is used on the failure object.
|
305
|
+
|
306
|
+
For example, if the stack is empty when it is popped, then it is
|
307
|
+
reasonable for pop to raise an UnderflowError. This is how you might
|
308
|
+
specify that behavior:
|
309
|
+
|
310
|
+
```ruby
|
311
|
+
When(:result) { stack.pop }
|
312
|
+
Then { result.should have_failed(UnderflowError, /empty/) }
|
313
|
+
```
|
314
|
+
|
315
|
+
Note that the arguments to the 'have_failed' matcher are the same as
|
316
|
+
those given to the standard RSpec matcher 'raise_error'.
|
317
|
+
|
318
|
+
### Then
|
319
|
+
|
320
|
+
The _Then_ clauses are the postconditions of the specification. These
|
321
|
+
then conditions must be true after the code under test (the _When_
|
322
|
+
clause) is run.
|
323
|
+
|
324
|
+
The code in the block of a _Then_ clause should be a single _should_
|
325
|
+
assertion. Code in _Then_ clauses should not have any side effects.
|
326
|
+
|
327
|
+
Let me repeat that: <b>_Then_ clauses should not have any side
|
328
|
+
effects!</b> _Then_ clauses with side effects are erroneous. _Then_
|
329
|
+
clauses need to be idempotent, so that running them once, twice, a
|
330
|
+
hundred times, or never does not change the state of the program. (The
|
331
|
+
same is true of _And_ and _Invariant_ clauses).
|
332
|
+
|
333
|
+
In RSpec terms, a _Then_ clause forms a RSpec Example that runs in the
|
334
|
+
context of an Example Group (defined by a describe or context clause).
|
335
|
+
|
336
|
+
Each Example Group must have at least one _Then_ clause, otherwise
|
337
|
+
there will be no examples to be run for that group. If all the
|
338
|
+
assertions in an example group are done via Invariants, then the group
|
339
|
+
should use an empty _Then_ clause, like this:
|
340
|
+
|
341
|
+
```ruby
|
342
|
+
Then { }
|
343
|
+
```
|
344
|
+
|
345
|
+
#### Then examples:
|
346
|
+
|
347
|
+
```ruby
|
348
|
+
Then { stack.empty? }
|
349
|
+
```
|
350
|
+
|
351
|
+
After the related block for the _When_ clause is run, the stack should
|
352
|
+
be empty. If it is not empty, the test will fail.
|
353
|
+
|
354
|
+
### And
|
355
|
+
|
356
|
+
The _And_ clause is similar to _Then_, but does not form its own RSpec
|
357
|
+
example. This means that _And_ clauses reuse the setup from a sibling
|
358
|
+
_Then_ clause. Using a single _Then_ and multiple _And_ clauses in an
|
359
|
+
example group means the setup for that group is run only once (for the
|
360
|
+
_Then_ clause) and reused for all the _And_ clauses. This can be a
|
361
|
+
significant speed savings where the setup for an example group is
|
362
|
+
expensive.
|
363
|
+
|
364
|
+
Some things to keep in mind about _And_ clauses:
|
365
|
+
|
366
|
+
* There must be at least one _Then_ in the example group and it must
|
367
|
+
be declared before the _And_ clauses. Forgetting the _Then_ clause
|
368
|
+
is an error.
|
369
|
+
|
370
|
+
* The code in the _And_ clause is run immediately after the first
|
371
|
+
(executed) _Then_ of an example group.
|
372
|
+
|
373
|
+
* An assertion failure in a _Then_ clause or an _And_ clause will
|
374
|
+
cause all the subsequent _And_ clauses to be skipped.
|
375
|
+
|
376
|
+
* Since _And_ clauses do not form their own RSpec examples, they are
|
377
|
+
not represented in the formatted output of RSpec. That means _And_
|
378
|
+
clauses do not produce dots in the Progress format, nor do they
|
379
|
+
appear in the documentation, html or textmate formats (options
|
380
|
+
-fhtml, -fdoc, or -ftextmate).
|
381
|
+
|
382
|
+
* Like _Then_ clauses, _And_ clauses must be idempotent. That means
|
383
|
+
they should not execute any code that changes global program state.
|
384
|
+
(See the section on the _Then_ clause).
|
385
|
+
|
386
|
+
The choice to use an _And_ clause is primarily a speed consideration.
|
387
|
+
If an example group has expensive setup and there are a lot of _Then_
|
388
|
+
clauses, then choosing to make some of the _Then_ clauses into _And_
|
389
|
+
clauses will speed up the spec. Otherwise it is probably better to
|
390
|
+
stick with _Then_ clauses.
|
391
|
+
|
392
|
+
#### Then/And examples:
|
393
|
+
|
394
|
+
```ruby
|
395
|
+
Then { pop_result == :top_item } # Required
|
396
|
+
And { stack.top == :second_item } # No Setup rerun
|
397
|
+
And { stack.depth == original_depth - 1 } # ... for these
|
398
|
+
```
|
399
|
+
|
400
|
+
### Invariant
|
401
|
+
|
402
|
+
The _Invariant_ clause is a new idea that doesn't have an analog in
|
403
|
+
RSpec or Test::Unit. The invariant allows you specify things that must
|
404
|
+
always be true in the scope of the invariant. In the stack example, the method
|
405
|
+
<tt>empty?</tt> is defined in term of <tt>size</tt>.
|
406
|
+
|
407
|
+
```ruby
|
408
|
+
Invariant { stack.empty? == (stack.depth == 0) }
|
409
|
+
```
|
410
|
+
|
411
|
+
This invariant states that <code>empty?</code> is true if and only if
|
412
|
+
the stack depth is zero, and that assertion is checked at every _Then_
|
413
|
+
clause that is in the same scope.
|
414
|
+
|
415
|
+
You can conceptually think of an _Invariant_ clause as a _Then_ block
|
416
|
+
that automatically gets added to every _Then_ within its scope.
|
417
|
+
Invariants nested within a context only apply to the _Then_ clauses
|
418
|
+
that are in the scope of that context.
|
419
|
+
|
420
|
+
Invariants that reference a _Given_ precondition accessor must only be
|
421
|
+
used in contexts that define that accessor.
|
422
|
+
|
423
|
+
Notes:
|
424
|
+
|
425
|
+
* Since Invariants do not form their own RSpec example, they are not
|
426
|
+
represented in the RSpec formatted output (e.g. the '--format html'
|
427
|
+
option).
|
428
|
+
|
429
|
+
## Execution Ordering
|
430
|
+
|
431
|
+
When running the test for a specific _Then_ clause, the following will
|
432
|
+
be true:
|
433
|
+
|
434
|
+
* The non-lazy _Given_ clauses will be run in the order that they are
|
435
|
+
specified, from the outermost scope to the innermost scope
|
436
|
+
containing the _Then_. (The lazy _Given_ clauses will be run upon
|
437
|
+
demand).
|
438
|
+
|
439
|
+
* All of the _Given_ clauses in all of the relevant scopes will run
|
440
|
+
before the first (outermost) _When_ clause in those same scopes.
|
441
|
+
That means that the _When_ code can assume that the givens have been
|
442
|
+
established, even if the givens are in a more nested scope than the
|
443
|
+
When.
|
444
|
+
|
445
|
+
* _When_ clauses and RSpec _before_ blocks will be executed in the
|
446
|
+
order that they are specified, from the outermost block to the
|
447
|
+
innermost block. This makes _before_ blocks an excellent choice when
|
448
|
+
writing narrative tests to specify actions that happen between the
|
449
|
+
"whens" of a narrative-style test.
|
450
|
+
|
451
|
+
Note that the ordering between _Given_ clauses and _before_ blocks are
|
452
|
+
not strongly specified. Hoisting a _When_ clause out of an inner scope
|
453
|
+
to an outer scope may change the order of execution between related
|
454
|
+
_Given_ clauses and any _before_ blocks (hoisting the _When_ clause
|
455
|
+
might cause the related _Given_ clauses to possibly run earlier).
|
456
|
+
Because of this, do not split order dependent code between _Given_
|
457
|
+
clauses and _before_ blocks.
|
458
|
+
|
459
|
+
## Natural Assertions
|
460
|
+
|
461
|
+
RSpec/Given now supports the use of "natural assertions" in _Then_,
|
462
|
+
_And_, and _Invariant_ blocks. Natural assertions are just Ruby
|
463
|
+
conditionals, without the _should_ or _expect_ methods that RSpec
|
464
|
+
provides. Here are the Then/And examples showing natural assertions:
|
465
|
+
|
466
|
+
### Using Natural Assertions
|
467
|
+
|
468
|
+
```ruby
|
469
|
+
Then { stack.top == :second_item }
|
470
|
+
Then { stack.depth == original_depth - 1 }
|
471
|
+
Then { result == Failure(Stack::UnderflowError, /empty/) }
|
472
|
+
```
|
473
|
+
|
474
|
+
### Using RSpec expect().to
|
475
|
+
|
476
|
+
```ruby
|
477
|
+
Then { expect(stack.top).to eq(:second_item) }
|
478
|
+
Then { expect(stack.depth).to eq(original_depth - 1) }
|
479
|
+
Then { expect(result).to have_failed(Stack::UnderflowError, /empty/) }
|
480
|
+
```
|
481
|
+
|
482
|
+
### Using Minitest asserts
|
483
|
+
|
484
|
+
```ruby
|
485
|
+
Then { assert_equal :second_item, stack.top }
|
486
|
+
Then { assert_equal original_depth - 1, stack.depth }
|
487
|
+
Then {
|
488
|
+
assert_raises(Stack::UnderflowError, /empty/) do
|
489
|
+
result.call()
|
490
|
+
end
|
491
|
+
}
|
492
|
+
```
|
493
|
+
|
494
|
+
### Using Minitest expectations
|
495
|
+
|
496
|
+
```ruby
|
497
|
+
Then { stack.top.must_equal :second_item }
|
498
|
+
Then { stack.depth.must_equal original_depth - 1}
|
499
|
+
Then { result.must_raise(Stack::UnderflowError, /empty/) }
|
500
|
+
```
|
501
|
+
|
502
|
+
### Disabling Natural Assertions
|
503
|
+
|
504
|
+
Natural assertions may be disabled, either globally or on a per
|
505
|
+
context basis. See the **configuration** section below to see how to
|
506
|
+
disable natural assertions project wide.
|
507
|
+
|
508
|
+
Here's a heads up: If you use natural assertions, but configure Given
|
509
|
+
to disable them, then all your specs will mysteriously pass. This is
|
510
|
+
why the **red** part of _Red/Green/Refactor_ is so important.
|
511
|
+
|
512
|
+
### Failure Messages with Natural Assertions
|
513
|
+
|
514
|
+
Since natural assertions do not depend upon matchers, you don't get
|
515
|
+
customized error messages from them. What you _do_ get is a complete
|
516
|
+
analsysis of the expression that failed.
|
517
|
+
|
518
|
+
For example, given the following failing specification:
|
519
|
+
|
520
|
+
```ruby
|
521
|
+
Given.use_natural_assertions
|
522
|
+
|
523
|
+
describe "Natural Assertions" do
|
524
|
+
Given(:foo) { 1 }
|
525
|
+
Given(:bar) { 2 }
|
526
|
+
Then { foo + bar == 2 }
|
527
|
+
end
|
528
|
+
```
|
529
|
+
|
530
|
+
You would get:
|
531
|
+
|
532
|
+
```
|
533
|
+
1) Natural Assertions
|
534
|
+
Failure/Error: Then { foo + bar == 2 }
|
535
|
+
Then expression failed at /Users/jim/working/git/rspec-given/examples/failing/sample_spec.rb:6
|
536
|
+
expected: 3
|
537
|
+
to equal: 2
|
538
|
+
false <- foo + bar == 2
|
539
|
+
3 <- foo + bar
|
540
|
+
1 <- foo
|
541
|
+
2 <- bar
|
542
|
+
# ./examples/failing/sample_spec.rb:6:in `block in Then'
|
543
|
+
```
|
544
|
+
|
545
|
+
Notice how the failing expression "<code>foo+bar == 2</code>" was
|
546
|
+
broken down into subexpressions and values for each subexpression.
|
547
|
+
This gives you all the information you need to figure out exactly what
|
548
|
+
part of the expression is causing the failure.
|
549
|
+
|
550
|
+
Natural assertions will give additional information (e.g. "expected:
|
551
|
+
3 to equal: 2") for top level expressions involving any of the
|
552
|
+
comparison operators (==, !=, <, <=, >, >=) or matching operators (=~,
|
553
|
+
!~).
|
554
|
+
|
555
|
+
### Checking for exceptions with Natural Assertions
|
556
|
+
|
557
|
+
If you wish to see if the result of a _When_ clause is an exception,
|
558
|
+
you can use the following:
|
559
|
+
|
560
|
+
```ruby
|
561
|
+
When(:result) { stack.pop }
|
562
|
+
Then { result == Failure(UnderflowError, /empty/) }
|
563
|
+
```
|
564
|
+
|
565
|
+
The <code>Failure()</code> method accepts the same arguments as
|
566
|
+
<code>have_failed</code> and <code>raise_error</code>.
|
567
|
+
|
568
|
+
### Caveats on Natural Assertions
|
569
|
+
|
570
|
+
Keep the following in mind when using natural assertions.
|
571
|
+
|
572
|
+
* Only a single expression/assertion per _Then_. The single expression
|
573
|
+
of the _Then_ block will be considered when determining pass/fail
|
574
|
+
for the assertion. If you _want_ to express a complex condition for
|
575
|
+
the _Then_, you need to use ||, && or some other logical operation
|
576
|
+
to join the conditions into a single expression (and the failure
|
577
|
+
message will break down the values for each part).
|
578
|
+
|
579
|
+
* Then clauses need be **idempotent**. This is true in general, but it
|
580
|
+
is particularly important for natural assertions to obey this
|
581
|
+
restriction. This means that assertions in a Then clause should not
|
582
|
+
change anything. Since the Natural Assertion error message contains
|
583
|
+
the values of all the subexpressions, the expression and its
|
584
|
+
subexpressions will be evaluated multiple times. If the Then clause
|
585
|
+
is not idempotent, you will get changing answers as the
|
586
|
+
subexpressions are evaluated.
|
587
|
+
|
588
|
+
That last point is important. If you write code like this:
|
589
|
+
|
590
|
+
```ruby
|
591
|
+
# DO NOT WRITE CODE LIKE THIS
|
592
|
+
context "Incorrect non-idempotent conditions" do
|
593
|
+
Given(:ary) { [1, 2, 3] }
|
594
|
+
Then { ary.delete(1) == nil }
|
595
|
+
end
|
596
|
+
```
|
597
|
+
|
598
|
+
Then the assertion will fail (because <code>ary.delete(1)</code> will
|
599
|
+
initially return 1). But when the error message is formated, the
|
600
|
+
system reports that <code>ary.delete(1)</code> returns nil. You will
|
601
|
+
scratch your head over that for a good while.
|
602
|
+
|
603
|
+
Instead, move the state changing code into a _When(:result)_ block, then
|
604
|
+
assert what you need to about :result. Something
|
605
|
+
like this is good:
|
606
|
+
|
607
|
+
```ruby
|
608
|
+
context "Correct idempotent conditions" do
|
609
|
+
Given(:ary) { [1, 2, 3] }
|
610
|
+
When(:result) { ary.delete(1) }
|
611
|
+
Then { result == nil }
|
612
|
+
end
|
613
|
+
```
|
614
|
+
|
615
|
+
It is good to note that non-idempotent assertions will also cause
|
616
|
+
problems with And and Invariant clauses.
|
617
|
+
|
618
|
+
### Mixing Natural Assertions and RSpec Assertions
|
619
|
+
|
620
|
+
Natural assertions, RSpec should assertions and Minitest assertions
|
621
|
+
can be intermixed in a single test suite, even within a single
|
622
|
+
context.
|
623
|
+
|
624
|
+
```ruby
|
625
|
+
context "Outer" do
|
626
|
+
context "Inner" do
|
627
|
+
Then { a == b } # Natural Assertions
|
628
|
+
Then { a.should == b } # RSpec style
|
629
|
+
Then { expect(a).to eq(b) } # RSpec style
|
630
|
+
Then { assert_equal b, a } # Minitest style
|
631
|
+
Then { a.must_equal b } # Minitest style
|
632
|
+
end
|
633
|
+
|
634
|
+
context "Disabled" do
|
635
|
+
use_natural_assertions false
|
636
|
+
end
|
637
|
+
end
|
638
|
+
```
|
639
|
+
|
640
|
+
Both the _Outer_ and _Inner_ contexts will use natural assertions. The
|
641
|
+
_Disabled_ context overrides the setting inherited from _Outer_ and
|
642
|
+
will not process natural assertions.
|
643
|
+
|
644
|
+
See the **configuration** section below to see how to disable natural
|
645
|
+
assertions project wide.
|
646
|
+
|
647
|
+
### Matchers and Natural Assertions
|
648
|
+
|
649
|
+
In RSpec, matchers are used to provide nice, readable error messages
|
650
|
+
when an assertion is not met. Natural assertions provide
|
651
|
+
self-explanatory failure messages for most things without requiring
|
652
|
+
any special matchers from the programmer.
|
653
|
+
|
654
|
+
In the rare case that some extra information would be helpful, it is
|
655
|
+
useful to create special objects that respond to the == operator.
|
656
|
+
|
657
|
+
#### Asserting Nearly Equal with Fuzzy Numbers
|
658
|
+
|
659
|
+
Operations on floating point numbers rarely create numbers that are
|
660
|
+
exactly equal, therefore it is useful to assert that two floating
|
661
|
+
point numbers are nearly equal. We do that by creating a fuzzy number
|
662
|
+
that has a looser interpretation of what it means to be equal.
|
663
|
+
|
664
|
+
For example, the following asserts that the square root of 10 is about
|
665
|
+
3.1523 with an accuracy of 1 percent.
|
666
|
+
|
667
|
+
```ruby
|
668
|
+
Then { Math.sqrt(10) == about(3.1623).percent(1) }
|
669
|
+
```
|
670
|
+
|
671
|
+
As long as the real value of <code>Math.sqrt(10)</code> is within plus
|
672
|
+
or minus 1% of 3.1623 (i.e. 3.1623 +/- 0.031623), then the assertion
|
673
|
+
will pass.
|
674
|
+
|
675
|
+
There are several ways of creating fuzzy numbers:
|
676
|
+
|
677
|
+
* <code>about(n).delta(d)</code> -- A fuzzy number matching the range
|
678
|
+
(n-d)..(n+d)
|
679
|
+
|
680
|
+
* <code>about(n).percent(p)</code> -- A fuzzy number matching the
|
681
|
+
range (n-(n*p/100)) .. (n+(n*p/100))
|
682
|
+
|
683
|
+
* <code>about(n).epsilon(neps)</code> -- A fuzzy number matching the
|
684
|
+
range (n-(neps*e)) .. (n+(neps*e)), where e is the difference
|
685
|
+
between 1.0 and the next smallest floating point number.
|
686
|
+
|
687
|
+
* <code>about(n)</code> -- Same as <code>about(n).epsilon(10)</code>.
|
688
|
+
|
689
|
+
When the file <code>given/fuzzy_shortcuts</code> is required,
|
690
|
+
the following unicode shortcut methods are added to Numeric to create
|
691
|
+
fuzzy numbers.
|
692
|
+
|
693
|
+
* <code>n.±(del)</code> is the same as <code>about(n).delta(del)</code>
|
694
|
+
|
695
|
+
* <code>n.‰(percentage)</code> is the same as <code>about(n).percent(percentage)</code>
|
696
|
+
|
697
|
+
* <code>n.€(neps)</code> is the same as <code>about(n).epsilon(neps)</code>
|
698
|
+
|
699
|
+
* <code>n.±</code>, <code>n.‰</code>, and <code>n.€</code> are all
|
700
|
+
the same as <code>about(n)</code>
|
701
|
+
|
702
|
+
#### Detecting Exceptions
|
703
|
+
|
704
|
+
The RSpec matcher used for detecting exceptions will work with natural
|
705
|
+
assertions out of the box. Just check for equality against the
|
706
|
+
<code>Failure()</code> method return value.
|
707
|
+
|
708
|
+
For example, the following two Then clauses are equivalent:
|
709
|
+
|
710
|
+
```ruby
|
711
|
+
# Using an RSpec matcher
|
712
|
+
Then { result.should have_failed(StandardError, /message/) }
|
713
|
+
|
714
|
+
# Using natural assertions
|
715
|
+
Then { result == Failure(StandardError, /message/) }
|
716
|
+
```
|
717
|
+
|
718
|
+
### Processing Natural Assertions
|
719
|
+
|
720
|
+
When natural assertions are enabled, they are only used if all of the
|
721
|
+
following are true:
|
722
|
+
|
723
|
+
1. The block does not throw an RSpec assertion failure (or any other
|
724
|
+
exception for that matter).
|
725
|
+
|
726
|
+
1. The block returns false (blocks that return true pass the
|
727
|
+
assertion and don't need a failure message).
|
728
|
+
|
729
|
+
1. The block does not use the native frameworks assertions or
|
730
|
+
expectations (e.g. RSpec's _should_ or _expect_ methods, or
|
731
|
+
Minitest's _assert\_xxx_ or _must\_xxx_ methods).
|
732
|
+
|
733
|
+
Detecting that last point (the use of _should_ and _expect_) is done
|
734
|
+
by modifying the RSpec runtime to report uses of _should_ and
|
735
|
+
_expect_.
|
736
|
+
|
737
|
+
### Platform Support
|
738
|
+
|
739
|
+
Natural assertions use the Ripper library to parse the failing
|
740
|
+
condition and find all the sub-expression values upon a failure.
|
741
|
+
Currently Ripper is not fully supported on JRuby 1.7.4. Charles Nutter
|
742
|
+
has said that Ripper support is coming soon and may arrive soon. Until
|
743
|
+
then, natural assertions are disabled when running under JRuby. Never
|
744
|
+
fear, JRuby supports all the other features of rspec-given and will
|
745
|
+
work just fine.
|
746
|
+
|
747
|
+
### Further Reading
|
748
|
+
|
749
|
+
Natural assertions were inspired by the [wrong assertion
|
750
|
+
library](http://rubygems.org/gems/wrong) by [Alex
|
751
|
+
Chaffee](http://rubygems.org/profiles/alexch) and [Steve
|
752
|
+
Conover](http://rubygems.org/profiles/sconoversf).
|
753
|
+
|
754
|
+
## Configuration
|
755
|
+
|
756
|
+
If the RSpec format option document, html or textmate is chosen,
|
757
|
+
RSpec/Given will automatically add additional source code information to
|
758
|
+
the examples to produce better looking output. If you don't care about
|
759
|
+
the pretty output and wish to disable source code caching
|
760
|
+
unconditionally, then add the following line to your spec helper file:
|
761
|
+
|
762
|
+
```ruby
|
763
|
+
Given.source_caching_disabled = true
|
764
|
+
```
|
765
|
+
|
766
|
+
Natural assertions are disabled by default. To globally configure
|
767
|
+
natural assertions, add one of the following lines to your spec_helper
|
768
|
+
file:
|
769
|
+
|
770
|
+
```ruby
|
771
|
+
Given.use_natural_assertions # Enable natural assertions
|
772
|
+
Given.use_natural_assertions true # Same as above
|
773
|
+
Given.use_natural_assertions false # Disable natural assertions
|
774
|
+
Given.use_natural_assertions :always # Always process natural assertions
|
775
|
+
# ... even when should/expect are detected
|
776
|
+
```
|
777
|
+
|
778
|
+
# License
|
779
|
+
|
780
|
+
rspec-given, minitest-given and given_core are available under the MIT
|
781
|
+
License. See the MIT-LICENSE file in the source distribution.
|
782
|
+
|
783
|
+
# History
|
784
|
+
|
785
|
+
* Version 3.0.0
|
786
|
+
|
787
|
+
* Support for minitest added.
|
788
|
+
|
789
|
+
* Introduced gem given_core to contain the common logic between the
|
790
|
+
RSpec and Minitest versions. Both the rspec-given gem and the
|
791
|
+
minitest-given gem have a dependency on given_core.
|
792
|
+
|
793
|
+
* Natural assertions are now enabled by default.
|
794
|
+
|
795
|
+
* Version 2.4.4
|
796
|
+
|
797
|
+
* Support for RSpec 2.13 added.
|
798
|
+
|
799
|
+
* Version 2.4.3
|
800
|
+
|
801
|
+
* Better natural assertion messages when dealing with multi-line
|
802
|
+
output.
|
803
|
+
|
804
|
+
* Version 2.4.2
|
805
|
+
|
806
|
+
* Minor adjustment to natural assertion error messages to better
|
807
|
+
handle multi-line values.
|
808
|
+
|
809
|
+
* Remove flog, flay and other development tools from the bundle and
|
810
|
+
gemspec. The Rakefile was updated to suggest installing them if
|
811
|
+
they are not there.
|
812
|
+
|
813
|
+
* Version 2.4.1
|
814
|
+
|
815
|
+
* Fix bug where constants from nested modules were not properly
|
816
|
+
accessed.
|
817
|
+
|
818
|
+
* Version 2.4.0
|
819
|
+
|
820
|
+
* Add fuzzy number helper methods (with unicode method shortcuts).
|
821
|
+
|
822
|
+
* Fix bug caused by blank lines in Thens.
|
823
|
+
|
824
|
+
# Links
|
825
|
+
|
826
|
+
* Github: [https://github.com/jimweirich/rspec-given](https://github.com/jimweirich/rspec-given)
|
827
|
+
* Clone URL: git://github.com/jimweirich/rspec-given.git
|
828
|
+
* Bug/Issue Reporting: [https://github.com/jimweirich/rspec-given/issues](https://github.com/jimweirich/rspec-given/issues)
|
829
|
+
* Continuous Integration: [http://travis-ci.org/#!/jimweirich/rspec-given](http://travis-ci.org/#!/jimweirich/rspec-given)
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Given
|
2
|
+
|
3
|
+
class BinaryOperation
|
4
|
+
BINARY_EXPLAINATIONS = {
|
5
|
+
:== => "to equal",
|
6
|
+
:!= => "to not equal",
|
7
|
+
:< => "to be less than",
|
8
|
+
:<= => "to be less or equal to",
|
9
|
+
:> => "to be greater than",
|
10
|
+
:>= => "to be greater or equal to",
|
11
|
+
:=~ => "to match",
|
12
|
+
:!~ => "to not match",
|
13
|
+
}
|
14
|
+
|
15
|
+
attr_reader :left, :operator, :right
|
16
|
+
|
17
|
+
def initialize(left, operator, right)
|
18
|
+
@left = left
|
19
|
+
@operator = operator
|
20
|
+
@right = right
|
21
|
+
end
|
22
|
+
|
23
|
+
def explain
|
24
|
+
BINARY_EXPLAINATIONS[operator]
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.parse(sexp)
|
28
|
+
return nil unless sexp.first == :binary
|
29
|
+
new(sexp[1], sexp[2], sexp[3])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
data/lib/given/evaluator.rb
CHANGED
data/lib/given/extensions.rb
CHANGED
@@ -4,9 +4,18 @@ require 'given/failure'
|
|
4
4
|
|
5
5
|
module Given
|
6
6
|
|
7
|
-
# Provide run-time methods to support Given infrastructure.
|
8
|
-
# methods in this module are considered private and
|
9
|
-
# implementation-specific
|
7
|
+
# Provide run-time instance methods to support Given infrastructure.
|
8
|
+
# All the methods in this module are considered private and
|
9
|
+
# implementation-specific, and should not be directly called by the
|
10
|
+
# application developer.
|
11
|
+
#
|
12
|
+
# By convention, these private instance specific methods are
|
13
|
+
# prefixed with _gvn_ to avoid name collisions with application
|
14
|
+
# methods defined in a spec.
|
15
|
+
#
|
16
|
+
# (Note that private class methods are prefixed with _Gvn_ and
|
17
|
+
# private instance methods are prefixed with _gvn_).
|
18
|
+
#
|
10
19
|
module InstanceExtensions # :nodoc:
|
11
20
|
|
12
21
|
# List of containing contexts in order from innermost to
|
@@ -96,15 +105,15 @@ module Given
|
|
96
105
|
|
97
106
|
# Evaluate a Then, And, or Invariant assertion.
|
98
107
|
def _gvn_evaluate(clause_type, block) # :nodoc:
|
99
|
-
Given.
|
108
|
+
Given.start_evaluation
|
100
109
|
passed = instance_eval(&block)
|
101
|
-
if ! Given.
|
110
|
+
if ! Given.explicit_assertions? && _gvn_na_configured?
|
102
111
|
_gvn_naturally_assert(clause_type, block, passed)
|
103
112
|
end
|
104
113
|
end
|
105
114
|
|
106
115
|
def _gvn_naturally_assert(clause_type, block, passed)
|
107
|
-
Given.
|
116
|
+
Given.count_assertion
|
108
117
|
unless passed
|
109
118
|
nassert = NaturalAssertion.new(clause_type, block, self, self.class._Gvn_lines)
|
110
119
|
Given.fail_with nassert.message if _gvn_need_na_message?(nassert)
|
@@ -112,6 +121,15 @@ module Given
|
|
112
121
|
end
|
113
122
|
end
|
114
123
|
|
124
|
+
# Provide run-time class methods to support Given infrastructure.
|
125
|
+
# Methods that begin with _Gvn_ are considered private and
|
126
|
+
# implementation specific, and should not be directly called by
|
127
|
+
# appliation code. Other methods without the _Gvn_ prefix are public
|
128
|
+
# and intended for use by the application developer.
|
129
|
+
#
|
130
|
+
# (Note that private class methods are prefixed with _Gvn_ and
|
131
|
+
# private instance methods are prefixed with _gvn_).
|
132
|
+
#
|
115
133
|
module ClassExtensions
|
116
134
|
|
117
135
|
# List of all givens directly in the current describe/context
|
@@ -204,13 +222,9 @@ module Given
|
|
204
222
|
# in a Failure object.
|
205
223
|
def _Gvn_when_actions_with_capture(name, block) # :nodoc:
|
206
224
|
let(name) do
|
207
|
-
|
225
|
+
Failure.capture(Given.pending_error) do
|
208
226
|
_gvn_establish_givens
|
209
227
|
instance_eval(&block)
|
210
|
-
rescue Given.pending_error => ex
|
211
|
-
raise
|
212
|
-
rescue Exception => ex
|
213
|
-
Failure.new(ex)
|
214
228
|
end
|
215
229
|
end
|
216
230
|
_Gvn_before do __send__(name) end
|
@@ -226,13 +240,18 @@ module Given
|
|
226
240
|
# Then { ... assertion ... }
|
227
241
|
#
|
228
242
|
def Then(&block)
|
229
|
-
file, line =
|
243
|
+
file, line = _Gvn_location_of(block)
|
230
244
|
description = _Gvn_lines.line(file, line) unless Given.source_caching_disabled
|
231
245
|
cmd = description ? "it(description)" : "specify"
|
232
246
|
eval %{#{cmd} do _gvn_then(&block) end}, binding, file, line
|
233
247
|
_Gvn_context_info[:then_defined] = true
|
234
248
|
end
|
235
249
|
|
250
|
+
# Return file and line number where the block is defined.
|
251
|
+
def _Gvn_location_of(block)
|
252
|
+
eval "[__FILE__, __LINE__]", block.binding
|
253
|
+
end
|
254
|
+
|
236
255
|
# Establish an invariant that must be true for all Then blocks
|
237
256
|
# in the current (and nested) scopes.
|
238
257
|
def Invariant(&block)
|
data/lib/given/failure.rb
CHANGED
@@ -6,20 +6,32 @@ module Given
|
|
6
6
|
class Failure < BasicObject
|
7
7
|
undef_method :==, :!=, :!
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
# Evaluate a block. If an exception is raised, capture it in a
|
10
|
+
# Failure object. Explicitly listed exceptions are passed thru
|
11
|
+
# without capture.
|
12
|
+
def self.capture(*exceptions)
|
13
|
+
begin
|
14
|
+
yield
|
15
|
+
rescue *exceptions => ex
|
16
|
+
raise
|
17
|
+
rescue ::Exception => ex
|
18
|
+
new(ex)
|
12
19
|
end
|
13
20
|
end
|
14
21
|
|
22
|
+
# Create a failure object that will rethrow the given exception
|
23
|
+
# whenever an undefined method is called.
|
15
24
|
def initialize(exception)
|
16
25
|
@exception = exception
|
17
26
|
end
|
18
27
|
|
28
|
+
# Failure objects will respond to #is_a?.
|
19
29
|
def is_a?(klass)
|
20
30
|
klass == Failure
|
21
31
|
end
|
22
32
|
|
33
|
+
# Failure objects may be compared for equality. If the comparison
|
34
|
+
# object is not a matcher, then the exception is re-raised.
|
23
35
|
def ==(other)
|
24
36
|
if failure_matcher?(other)
|
25
37
|
other.matches?(self)
|
@@ -28,6 +40,8 @@ module Given
|
|
28
40
|
end
|
29
41
|
end
|
30
42
|
|
43
|
+
# Failure objects may be compared for in-equality. If the comparison
|
44
|
+
# object is not a matcher, then the exception is re-raised.
|
31
45
|
def !=(other)
|
32
46
|
if failure_matcher?(other)
|
33
47
|
other.does_not_match?(self)
|
@@ -36,10 +50,12 @@ module Given
|
|
36
50
|
end
|
37
51
|
end
|
38
52
|
|
53
|
+
# Most methods will just re-raise the captured exception.
|
39
54
|
def method_missing(sym, *args, &block)
|
40
55
|
die
|
41
56
|
end
|
42
57
|
|
58
|
+
# Report that we respond to a limited number of methods.
|
43
59
|
def respond_to?(method_symbol)
|
44
60
|
method_symbol == :call ||
|
45
61
|
method_symbol == :== ||
|
@@ -49,14 +65,15 @@ module Given
|
|
49
65
|
|
50
66
|
private
|
51
67
|
|
68
|
+
# Re-raise the captured exception.
|
52
69
|
def die
|
53
70
|
::Kernel.raise @exception
|
54
71
|
end
|
55
72
|
|
73
|
+
# Is the comparison object a failure matcher?
|
56
74
|
def failure_matcher?(other)
|
57
|
-
other.
|
58
|
-
(defined?(::RSpec) && other.is_a?(::RSpec::Given::HaveFailed::HaveFailedMatcher))
|
75
|
+
other.respond_to?(:matches?) && other.respond_to?(:does_not_match?)
|
59
76
|
end
|
60
|
-
|
61
77
|
end
|
78
|
+
|
62
79
|
end
|
data/lib/given/minitest/all.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
module Given
|
2
|
+
|
3
|
+
class Failure
|
4
|
+
# Minitest expectation method. Since Failure inherits from
|
5
|
+
# BasicObject, we need to add this method explicitly.
|
6
|
+
def must_raise(*args)
|
7
|
+
::Minitest::Spec.current.assert_raises(*args) do
|
8
|
+
die
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
data/lib/given/module_methods.rb
CHANGED
@@ -54,15 +54,31 @@ module Given
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
+
# Methods forwarded to the framework object.
|
58
|
+
|
57
59
|
# Fail an example with the given messages.
|
58
|
-
#
|
59
|
-
# This should be the only place we reference the RSpec function.
|
60
|
-
# Everywhere else in rspec-given should be calling this function.
|
61
60
|
def self.fail_with(*args)
|
62
61
|
Given.framework.fail_with(*args)
|
63
62
|
end
|
64
63
|
|
65
|
-
#
|
64
|
+
# Mark the start of a Then assertion evaluation.
|
65
|
+
def self.start_evaluation(*args)
|
66
|
+
Given.framework.start_evaluation(*args)
|
67
|
+
end
|
68
|
+
|
69
|
+
# Were there any explicit framework assertions made during the
|
70
|
+
# execution of the Then block?
|
71
|
+
def self.explicit_assertions?(*args)
|
72
|
+
Given.framework.explicit_assertions?(*args)
|
73
|
+
end
|
74
|
+
|
75
|
+
# Increment the number of assertions made in the framework.
|
76
|
+
def self.count_assertion(*args)
|
77
|
+
Given.framework.count_assertion(*args)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Error object used by the current framework to indicate a pending
|
81
|
+
# example.
|
66
82
|
def self.pending_error
|
67
83
|
Given.framework.pending_error
|
68
84
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'given/module_methods'
|
2
2
|
require 'given/evaluator'
|
3
|
+
require 'given/binary_operation'
|
3
4
|
|
4
5
|
if Given::NATURAL_ASSERTIONS_SUPPORTED
|
5
6
|
require 'ripper'
|
@@ -36,20 +37,10 @@ module Given
|
|
36
37
|
|
37
38
|
private
|
38
39
|
|
39
|
-
BINARY_EXPLAINATIONS = {
|
40
|
-
:== => "to equal",
|
41
|
-
:!= => "to not equal",
|
42
|
-
:< => "to be less than",
|
43
|
-
:<= => "to be less or equal to",
|
44
|
-
:> => "to be greater than",
|
45
|
-
:>= => "to be greater or equal to",
|
46
|
-
:=~ => "to match",
|
47
|
-
:!~ => "to not match",
|
48
|
-
}
|
49
|
-
|
50
40
|
def explain_failure
|
51
|
-
|
52
|
-
|
41
|
+
binary = BinaryOperation.parse(assertion_sexp)
|
42
|
+
if binary && binary.explain
|
43
|
+
@output << explain_expected("expected", binary.left, binary.explain, binary.right)
|
53
44
|
end
|
54
45
|
end
|
55
46
|
|
@@ -79,7 +70,7 @@ module Given
|
|
79
70
|
end
|
80
71
|
|
81
72
|
def set_file_and_line(block)
|
82
|
-
@code_file, @code_line =
|
73
|
+
@code_file, @code_line = @evaluator.location
|
83
74
|
@code_line = @code_line.to_i
|
84
75
|
end
|
85
76
|
|
@@ -97,10 +88,20 @@ module Given
|
|
97
88
|
end
|
98
89
|
|
99
90
|
def then_block?(sexp)
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
91
|
+
program_sexp?(sexp) && method_with_block?(sexp) && has_block_sexp?(sexp)
|
92
|
+
end
|
93
|
+
|
94
|
+
def program_sexp?(sexp)
|
95
|
+
delve(sexp,0) == :program
|
96
|
+
end
|
97
|
+
|
98
|
+
def method_with_block?(sexp)
|
99
|
+
delve(sexp,1,0) == :stmts_add &&
|
100
|
+
delve(sexp,1,2,0) == :method_add_block
|
101
|
+
end
|
102
|
+
|
103
|
+
def has_block_sexp?(sexp)
|
104
|
+
delve(sexp,1,2,2,0) == :brace_block || delve(sexp,1,2,2,0) == :do_block
|
104
105
|
end
|
105
106
|
|
106
107
|
def extract_first_statement(block_sexp)
|
data/lib/given/rspec/all.rb
CHANGED
@@ -8,7 +8,7 @@ end
|
|
8
8
|
|
9
9
|
if Given::NATURAL_ASSERTIONS_SUPPORTED
|
10
10
|
require 'given/rspec/monkey'
|
11
|
-
raise "Unsupported version of RSpec (unable to detect assertions
|
11
|
+
raise "Unsupported version of RSpec (#{RSpec::Version::STRING}), unable to detect assertions" unless RSpec::Given::MONKEY
|
12
12
|
end
|
13
13
|
|
14
14
|
require 'given/rspec/have_failed'
|
data/lib/given/rspec/monkey.rb
CHANGED
data/lib/given/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: given_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.0
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jim Weirich
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sorcerer
|
@@ -37,9 +37,11 @@ files:
|
|
37
37
|
- Gemfile.lock
|
38
38
|
- MIT-LICENSE
|
39
39
|
- README.md
|
40
|
+
- README.old
|
40
41
|
- Rakefile
|
41
42
|
- TODO
|
42
43
|
- lib/given.rb
|
44
|
+
- lib/given/binary_operation.rb
|
43
45
|
- lib/given/core.rb
|
44
46
|
- lib/given/evaluator.rb
|
45
47
|
- lib/given/ext/numeric.rb
|
@@ -54,6 +56,7 @@ files:
|
|
54
56
|
- lib/given/minitest/before_extension.rb
|
55
57
|
- lib/given/minitest/configure.rb
|
56
58
|
- lib/given/minitest/context_extension.rb
|
59
|
+
- lib/given/minitest/failure_must_raise.rb
|
57
60
|
- lib/given/minitest/framework.rb
|
58
61
|
- lib/given/minitest/new_assertions.rb
|
59
62
|
- lib/given/module_methods.rb
|
@@ -96,9 +99,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
96
99
|
version: 1.9.2
|
97
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
101
|
requirements:
|
99
|
-
- - '
|
102
|
+
- - '>='
|
100
103
|
- !ruby/object:Gem::Version
|
101
|
-
version:
|
104
|
+
version: '0'
|
102
105
|
requirements: []
|
103
106
|
rubyforge_project: given
|
104
107
|
rubygems_version: 2.0.3
|