assay 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.yardopts +7 -0
- data/HISTORY.md +128 -0
- data/LICENSE.txt +22 -0
- data/README.md +287 -0
- data/demo/ae/01_overview.md +98 -0
- data/demo/ae/03_assert.md +398 -0
- data/demo/ae/04_subjunctive.md +100 -0
- data/demo/ae/05_expect.md +136 -0
- data/demo/ae/06_counts.md +33 -0
- data/demo/ae/07_matchers.md +34 -0
- data/demo/ae/08_check.md +61 -0
- data/demo/ae/09_assay.md +124 -0
- data/demo/ae/applique/setup.rb +11 -0
- data/{test → demo/applique}/helper.rb +8 -2
- data/demo/applique/setup.rb +1 -0
- data/demo/bang/01_bang.md +220 -0
- data/demo/core/00_introduction.md +2 -0
- data/demo/core/01_assay_classes/00_introduction.md +2 -0
- data/demo/core/01_assay_classes/01_nil_assay.md +35 -0
- data/demo/core/01_assay_classes/02_boolean_assay.md +35 -0
- data/demo/core/01_assay_classes/03_false_assay.md +19 -0
- data/demo/core/01_assay_classes/03_true_assay.md +20 -0
- data/demo/core/01_assay_classes/04_like_assay.md +19 -0
- data/demo/core/01_assay_classes/05_equal_assay.md +19 -0
- data/demo/core/01_assay_classes/06_unequal_assay.md +35 -0
- data/demo/core/01_assay_classes/07_equality_assay.md +19 -0
- data/demo/core/01_assay_classes/08_identity_assay.md +24 -0
- data/demo/core/01_assay_classes/09_case_assay.md +22 -0
- data/demo/core/01_assay_classes/10_match_assay.md +15 -0
- data/demo/core/01_assay_classes/11_nomatch_assay.md +16 -0
- data/demo/core/01_assay_classes/12_compare_assay.md +24 -0
- data/demo/core/01_assay_classes/13_less_assay.md +29 -0
- data/demo/core/01_assay_classes/14_more_assay.md +29 -0
- data/demo/core/01_assay_classes/15_less_equal_assay.md +30 -0
- data/demo/core/01_assay_classes/16_more_equal_assay.md +29 -0
- data/demo/core/01_assay_classes/17_close_assay.md +42 -0
- data/demo/core/01_assay_classes/17_within_assay.md +42 -0
- data/demo/core/01_assay_classes/18_kind_assay.md +17 -0
- data/demo/core/01_assay_classes/19_instance_assay.md +16 -0
- data/demo/core/01_assay_classes/20_include_assay.md +17 -0
- data/demo/core/01_assay_classes/21_empty_assay.md +15 -0
- data/demo/core/01_assay_classes/22_respond_assay.md +15 -0
- data/demo/core/01_assay_classes/23_execution_assay.md +40 -0
- data/demo/core/01_assay_classes/23_return_assay.md +37 -0
- data/demo/core/01_assay_classes/24_rescue_assay.md +40 -0
- data/demo/core/01_assay_classes/25_raise_assay.md +40 -0
- data/demo/core/01_assay_classes/26_throw_assay.md +47 -0
- data/demo/core/01_assay_classes/27_path_assay.md +20 -0
- data/demo/core/01_assay_classes/28_directory_assay.md +21 -0
- data/demo/core/01_assay_classes/29_file_assay.md +20 -0
- data/demo/core/01_assay_classes/30_output_assay.md +39 -0
- data/demo/core/01_assay_classes/31_stdout_assay.md +21 -0
- data/demo/core/01_assay_classes/32_stderr_assay.md +21 -0
- data/demo/core/02_lookup.md +25 -0
- data/demo/core/03_assertor.md +117 -0
- data/demo/core/04_assay.md +40 -0
- data/demo/fluidity/00_intro.md +7 -0
- data/demo/fluidity/01_should.md +52 -0
- data/demo/fluidity/02_must.md +58 -0
- data/demo/fluidity/03_assert.md +71 -0
- data/demo/fluidity/04_other.md +35 -0
- data/demo/fluidity/05_object_methods.md +102 -0
- data/demo/minitest/01_minitest_extensions.rdoc +217 -0
- data/demo/minitest/02_minitest_assertions.rdoc +250 -0
- data/demo/rspec/00_introduction.md +16 -0
- data/demo/rspec/01_should.md +24 -0
- data/demo/rspec/02_matchers.md +176 -0
- data/demo/rspec/03_have.md +51 -0
- data/demo/rspec/04_expect.md +49 -0
- data/demo/rspec/applique/setup.rb +2 -0
- data/demo/testunit/01_testunit_assertions.md +266 -0
- data/lib/assay/adapter/minitest.rb +32 -21
- data/lib/assay/adapter/testunit.rb +31 -38
- data/lib/assay/ae/assert.rb +77 -0
- data/lib/assay/ae/assertor.rb +453 -0
- data/lib/assay/ae/check.rb +154 -0
- data/lib/assay/ae/core_ext.rb +146 -0
- data/lib/assay/ae/expect.rb +72 -0
- data/lib/assay/ae/must.rb +64 -0
- data/lib/assay/ae/should.rb +58 -0
- data/lib/assay/ae/subjunctive.rb +66 -0
- data/lib/assay/ae.rb +29 -0
- data/lib/assay/assertable.rb +21 -5
- data/lib/assay/assertor.rb +4 -4
- data/lib/assay/bang.rb +147 -0
- data/lib/assay/close_assay.rb +2 -2
- data/lib/assay/core_ext/kernel.rb +4 -8
- data/lib/assay/fluidity/assert.rb +27 -0
- data/lib/assay/fluidity/grammar.rb +157 -0
- data/lib/assay/fluidity/is.rb +18 -0
- data/lib/assay/fluidity/must.rb +38 -0
- data/lib/assay/fluidity/should.rb +32 -0
- data/lib/assay/fluidity/will.rb +36 -0
- data/lib/assay/fluidity.rb +6 -0
- data/lib/assay/less_assay.rb +7 -1
- data/lib/assay/less_equal_assay.rb +7 -1
- data/lib/assay/minitest/assertions.rb +495 -0
- data/lib/assay/minitest/extensions.rb +496 -0
- data/lib/assay/minitest.rb +24 -0
- data/lib/assay/more_assay.rb +7 -1
- data/lib/assay/more_equal_assay.rb +7 -1
- data/lib/assay/rspec/expect.rb +151 -0
- data/lib/assay/rspec/have.rb +201 -0
- data/lib/assay/rspec/matchers.rb +272 -0
- data/lib/assay/rspec/operatics.rb +60 -0
- data/lib/assay/rspec/should.rb +49 -0
- data/lib/assay/rspec.rb +13 -0
- data/lib/assay/testunit/assertions.rb +484 -0
- data/lib/assay/testunit.rb +9 -0
- data/lib/assay/version.rb +3 -0
- data/lib/assay.rb +7 -21
- metadata +211 -80
- data/.ruby +0 -51
- data/COPYING.rdoc +0 -38
- data/HISTORY.rdoc +0 -65
- data/QED.rdoc +0 -1143
- data/README.rdoc +0 -173
- data/lib/assay.yml +0 -51
- data/test/case_compare_assay.rb +0 -59
- data/test/case_empty_assay.rb +0 -51
- data/test/case_equal_assay.rb +0 -53
- data/test/case_equality_assay.rb +0 -55
- data/test/case_false_assay.rb +0 -48
- data/test/case_identity_assay.rb +0 -51
- data/test/case_include_assay.rb +0 -51
- data/test/case_instance_assay.rb +0 -51
- data/test/case_kind_assay.rb +0 -51
- data/test/case_less_assay.rb +0 -53
- data/test/case_less_equal_assay.rb +0 -53
- data/test/case_like_assay.rb +0 -57
- data/test/case_match_assay.rb +0 -45
- data/test/case_more_assay.rb +0 -53
- data/test/case_more_equal_assay.rb +0 -53
- data/test/case_nil_assay.rb +0 -48
- data/test/case_nomatch_assay.rb +0 -47
- data/test/case_raise_assay.rb +0 -51
- data/test/case_respond_assay.rb +0 -51
- data/test/case_throw_assay.rb +0 -51
- data/test/case_true_assay.rb +0 -48
- data/test/case_unequal_assay.rb +0 -55
- data/test/case_within_assay.rb +0 -61
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
# Assert Method
|
|
2
|
+
|
|
3
|
+
## Compatible with Test::Unit
|
|
4
|
+
|
|
5
|
+
The `assert` method is designed to be backward compatible
|
|
6
|
+
with the same method in `Test::Unit`.
|
|
7
|
+
|
|
8
|
+
Using an argument, `assert` will check that an argument evaluates
|
|
9
|
+
to true. Optionally one can send along a meaningful message should
|
|
10
|
+
the assertion fail.
|
|
11
|
+
|
|
12
|
+
assert(true, "Not true!")
|
|
13
|
+
|
|
14
|
+
expect Assertion do
|
|
15
|
+
assert(false, "Not true!")
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
## Assert with a Block
|
|
20
|
+
|
|
21
|
+
In addition +assert+ has been extended to accept a block. Like the case of the
|
|
22
|
+
argument, the block is expected to return something that evaluates as true.
|
|
23
|
+
|
|
24
|
+
assert do
|
|
25
|
+
true
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
Assertion.assert.raised? do
|
|
29
|
+
assert do
|
|
30
|
+
false
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
We should also mention that, while probably not very useful, since
|
|
35
|
+
the arity of a block can be checked, one can also pass the receiver
|
|
36
|
+
into the block as a block argument.
|
|
37
|
+
|
|
38
|
+
"hi".assert do |s|
|
|
39
|
+
/h/ =~ s
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
Given an argument as well, `assert` compares it to the block's result
|
|
43
|
+
using `#==`. Any argument counts, even `false` or `nil`.
|
|
44
|
+
|
|
45
|
+
assert(5) { 2 + 3 }
|
|
46
|
+
assert(nil) { nil }
|
|
47
|
+
|
|
48
|
+
Assertion.assert.raised? do
|
|
49
|
+
assert(false) { true }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
## Antonyms for Assert
|
|
54
|
+
|
|
55
|
+
We can state the opposite assertion using `assert!`.
|
|
56
|
+
|
|
57
|
+
10.assert! == 9
|
|
58
|
+
|
|
59
|
+
Or, because some people do not like the use of a bang method, +refute+.
|
|
60
|
+
|
|
61
|
+
10.refute == 9
|
|
62
|
+
|
|
63
|
+
These terms can be used just as +assert+ is used in all examples,
|
|
64
|
+
but with the opposite inference.
|
|
65
|
+
|
|
66
|
+
Another way to get the opposite inference, is to use +not+.
|
|
67
|
+
|
|
68
|
+
10.assert.not == 9
|
|
69
|
+
|
|
70
|
+
## Lambda Assertions
|
|
71
|
+
|
|
72
|
+
Passing +assert+ a `Proc` object, or any object that responds to `#call`,
|
|
73
|
+
will be used as if it were a block. This allows for a simple way to quickly
|
|
74
|
+
create reusable assertions.
|
|
75
|
+
|
|
76
|
+
palindrome = lambda{ |word| word == word.reverse }
|
|
77
|
+
|
|
78
|
+
"abracarba".assert palindrome
|
|
79
|
+
|
|
80
|
+
A lambda that takes no arguments is called without the receiver.
|
|
81
|
+
|
|
82
|
+
"abracarba".assert lambda { true }
|
|
83
|
+
|
|
84
|
+
The message for a failed assertion will come from calling `#to_s` on the
|
|
85
|
+
object.
|
|
86
|
+
|
|
87
|
+
A Hash or a Symbol is a value like any other, even though each has
|
|
88
|
+
`#to_proc`, so here `assert` and `refute` check only that it is truthy.
|
|
89
|
+
|
|
90
|
+
assert({a: 1})
|
|
91
|
+
|
|
92
|
+
Assertion.assert.raised? do
|
|
93
|
+
refute({a: 1})
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
## RSpec-style Assertion Matchers
|
|
97
|
+
|
|
98
|
+
If an object passed to assert responds to `#matches?` then AE will handle
|
|
99
|
+
the object as an RSpec-style mather, the receiver will be passed to the
|
|
100
|
+
`#matches?` method to determine if the assertion passes and RSpec matcher
|
|
101
|
+
message methods will be used if they are defined.
|
|
102
|
+
|
|
103
|
+
palindrome = Object.new
|
|
104
|
+
|
|
105
|
+
def palindrome.matches?(word)
|
|
106
|
+
word == word.reverse
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
"abracarba".assert palindrome
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
## Identity Assertions
|
|
113
|
+
|
|
114
|
+
Rather then the general form.
|
|
115
|
+
|
|
116
|
+
x = 10
|
|
117
|
+
x.object_id.assert == x.object_id
|
|
118
|
+
|
|
119
|
+
We can use Ruby's own `equal?`> method.
|
|
120
|
+
|
|
121
|
+
x.assert.equal?(x)
|
|
122
|
+
|
|
123
|
+
It fails for an equal but separate object.
|
|
124
|
+
|
|
125
|
+
Assertion.assert.raised? do
|
|
126
|
+
"abc".assert.equal?("abc".dup)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
And the opposite.
|
|
130
|
+
|
|
131
|
+
x.refute.equal?(11)
|
|
132
|
+
|
|
133
|
+
Assertion.assert.raised? do
|
|
134
|
+
x.refute.equal?(x)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
AE provides `identical?`> method as an alternative
|
|
138
|
+
to make it a bit more clear.
|
|
139
|
+
|
|
140
|
+
x.assert.identical?(x)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
## Equality Assertions
|
|
144
|
+
|
|
145
|
+
The most common assertion is that of value equality (`==`),
|
|
146
|
+
as we have seen throughout this document. But other forms of
|
|
147
|
+
equality can be verified as easily. We have already mentioned
|
|
148
|
+
identity. In addition there is *type equality*.
|
|
149
|
+
|
|
150
|
+
17.assert.eql? 17
|
|
151
|
+
|
|
152
|
+
Assertion.assert.raised? do
|
|
153
|
+
17.assert.eql? 17.0
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
And there is *case equality*.
|
|
157
|
+
|
|
158
|
+
Numeric.assert === 3
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
## Checking Equality with a Block
|
|
162
|
+
|
|
163
|
+
Because operators can not take blocks, and at times blocks can
|
|
164
|
+
be convenient means of supplying a value to an assertion,
|
|
165
|
+
AE has defined alternate renditions of the equality methods.
|
|
166
|
+
For equal? and eql?, the method names are the same, they simply
|
|
167
|
+
can take a block in place of an argument if need be.
|
|
168
|
+
|
|
169
|
+
For *value equality* (`==`), the method is called *eq?*.
|
|
170
|
+
|
|
171
|
+
10.assert.eq? do
|
|
172
|
+
10.0
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
And should it fail,
|
|
176
|
+
|
|
177
|
+
Assertion.assert.raised? do
|
|
178
|
+
10.assert.eq? do
|
|
179
|
+
20
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
## Case Equality
|
|
185
|
+
|
|
186
|
+
For *case equality* (`===`), it is `case?`.
|
|
187
|
+
|
|
188
|
+
Numeric.assert.case? do
|
|
189
|
+
"3".to_i
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
Assertion.assert.raised? do
|
|
193
|
+
Numeric.assert.case? do
|
|
194
|
+
"3"
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
## Regular Expressions
|
|
200
|
+
|
|
201
|
+
Regular Expressions can be used to make assertions in much the same way as equality.
|
|
202
|
+
|
|
203
|
+
/i/.assert =~ "i"
|
|
204
|
+
|
|
205
|
+
Assertion.assert.raised? do
|
|
206
|
+
/i/.assert =~ "g"
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
Conversely the String class recognizes the #=~ method as well.
|
|
210
|
+
|
|
211
|
+
"i".assert =~ /i/
|
|
212
|
+
|
|
213
|
+
Assertion.assert.raised? do
|
|
214
|
+
"i".assert =~ /g/
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
## Exception Assertions
|
|
219
|
+
|
|
220
|
+
Validating errors is easy too, as has already been shown
|
|
221
|
+
in the document to verify assertion failures.
|
|
222
|
+
|
|
223
|
+
StandardError.assert.raised? do
|
|
224
|
+
unknown_method
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
## Assertions on Object State
|
|
229
|
+
|
|
230
|
+
While testing or specifying the internal state of an object is
|
|
231
|
+
generally considered poor form, there are times when it is
|
|
232
|
+
necessary. Assert combined with +instance_eval+ makes it easy too.
|
|
233
|
+
|
|
234
|
+
class X
|
|
235
|
+
attr :a
|
|
236
|
+
def initialize(a); @a = a; end
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
x = X.new(1)
|
|
240
|
+
|
|
241
|
+
x.assert.instance_eval do
|
|
242
|
+
@a == 1
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
Assertion.assert.raised? do
|
|
246
|
+
x.assert.instance_eval do
|
|
247
|
+
@a == 2
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
## Catch/Try Assertions
|
|
253
|
+
|
|
254
|
+
Catch/Try throws can be tested via `Symbol#thrown?`.
|
|
255
|
+
|
|
256
|
+
:hookme.assert.thrown? do
|
|
257
|
+
throw :hookme
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
Alternatively, a lambda containing the potential throw
|
|
261
|
+
can be the receiver using `throws?`.
|
|
262
|
+
|
|
263
|
+
hook = lambda{ throw :hookme }
|
|
264
|
+
|
|
265
|
+
hook.assert.throws?(:hookme)
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
## Assertions on Proc Changes
|
|
269
|
+
|
|
270
|
+
I have to admit I'm not sure how this is useful,
|
|
271
|
+
but I found it in the Bacon API and ported it over
|
|
272
|
+
just for sake of thoroughness.
|
|
273
|
+
|
|
274
|
+
a = 0
|
|
275
|
+
|
|
276
|
+
l = lambda{ a }
|
|
277
|
+
|
|
278
|
+
l.assert.change?{ a +=1 }
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
## Assertion on literal True, False and Nil
|
|
282
|
+
|
|
283
|
+
Ruby already provides the #nil? method.
|
|
284
|
+
|
|
285
|
+
nil.assert.nil?
|
|
286
|
+
|
|
287
|
+
AE adds `true?` and `false?` which acts accordingly.
|
|
288
|
+
|
|
289
|
+
true.assert.true?
|
|
290
|
+
false.assert.false?
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
## Keyword Arguments
|
|
294
|
+
|
|
295
|
+
Keyword arguments are passed on to the method as keywords.
|
|
296
|
+
|
|
297
|
+
class Gate
|
|
298
|
+
def open?(strict: false)
|
|
299
|
+
!strict
|
|
300
|
+
end
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
Gate.new.assert.open?(strict: false)
|
|
304
|
+
Gate.new.refute.open?(strict: true)
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
## Comparing a Method's Result
|
|
308
|
+
|
|
309
|
+
A method called on `assert` makes the assertion itself, so its result
|
|
310
|
+
can't be compared afterwards. Here `size` returns 2, which is truthy, so
|
|
311
|
+
the assertion passes. The comparison with 3 is then made with the
|
|
312
|
+
assertion's own result, and checks nothing.
|
|
313
|
+
|
|
314
|
+
require 'stringio'
|
|
315
|
+
$stderr, stderr = StringIO.new, $stderr
|
|
316
|
+
|
|
317
|
+
[1, 2].assert.size == 3
|
|
318
|
+
|
|
319
|
+
warning, $stderr = $stderr.string, stderr
|
|
320
|
+
|
|
321
|
+
To compare a result, call the method first.
|
|
322
|
+
|
|
323
|
+
[1, 2].size.assert == 2
|
|
324
|
+
|
|
325
|
+
Because this is an easy mistake to make, AE warns when a method called
|
|
326
|
+
without arguments returns something other than true, false or nil.
|
|
327
|
+
|
|
328
|
+
warning.assert.include?("`size` returned 2")
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
## Flunk
|
|
332
|
+
|
|
333
|
+
`flunk` always fails, negated or not.
|
|
334
|
+
|
|
335
|
+
Assertion.assert.raised? do
|
|
336
|
+
flunk "not done yet"
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
Assertion.assert.raised? do
|
|
340
|
+
10.refute.flunk
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
## Send Assertions
|
|
345
|
+
|
|
346
|
+
Assert that a method can be successfully called.
|
|
347
|
+
|
|
348
|
+
"STRING".assert.send?(:upcase)
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
## Numeric Delta and Epsilon
|
|
352
|
+
|
|
353
|
+
You may wish to assert that a numeric value is with some
|
|
354
|
+
range.
|
|
355
|
+
|
|
356
|
+
3.assert.in_delta?(1,5)
|
|
357
|
+
|
|
358
|
+
Or minimum range, relative to the smaller of the two magnitudes.
|
|
359
|
+
|
|
360
|
+
3.assert.in_epsilon?(3,5)
|
|
361
|
+
-100.0.assert.in_epsilon?(-100.05, 0.01)
|
|
362
|
+
|
|
363
|
+
Assertion.assert.raised? do
|
|
364
|
+
-100.0.assert.in_epsilon?(-101.5, 0.01)
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
## Verifying Object State
|
|
369
|
+
|
|
370
|
+
Not surprisingly if underlying object state needs to be verified, +instance_eval+
|
|
371
|
+
can be used in conjunction with +assert+.
|
|
372
|
+
|
|
373
|
+
class X
|
|
374
|
+
attr :a
|
|
375
|
+
def initialize(a); @a = a; end
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
x = X.new(4)
|
|
379
|
+
|
|
380
|
+
x.instance_eval do
|
|
381
|
+
@a.assert == 4
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
Calling #instance_eval on #assert works too, as shown under "Assertions
|
|
385
|
+
on Object State" above: the block runs in the object, and its result
|
|
386
|
+
is the assertion.
|
|
387
|
+
|
|
388
|
+
Assertion.assert.raised? do
|
|
389
|
+
x.assert.instance_eval do
|
|
390
|
+
@a == "obviously wrong"
|
|
391
|
+
end
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
For some testing underlying implementation might be considered poor
|
|
395
|
+
form. You will get no argument here. It should be used thoughtfully,
|
|
396
|
+
but I would not bet against there being occasions when such validations
|
|
397
|
+
might be needed.
|
|
398
|
+
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# Subjunctives
|
|
2
|
+
|
|
3
|
+
Okay. I can hear the BDDers rumbling, "where's the *should?*"
|
|
4
|
+
AE has nothing against "should", but there are different
|
|
5
|
+
approaches for utilizing should nomenclature in specifications,
|
|
6
|
+
and AE wants to be open to these techniques. One of which
|
|
7
|
+
is how Shoulda (http://shoulda.rubyforge.org) utilizes
|
|
8
|
+
`should` in a way analogous to RSpec's use of `it`.
|
|
9
|
+
|
|
10
|
+
Even so, AE provides an optional mixin called `Subjunctive` which
|
|
11
|
+
can be used to create assertor methods with English subjunctive
|
|
12
|
+
terms, such as `should`, or `must`, `shall` and `will`.
|
|
13
|
+
To load this library use:
|
|
14
|
+
|
|
15
|
+
require 'assay/ae/subjunctive'
|
|
16
|
+
|
|
17
|
+
Then all that is required it to define a subjunctive method for all
|
|
18
|
+
objects. For example:
|
|
19
|
+
|
|
20
|
+
def will(*args, &block)
|
|
21
|
+
Assay::AE::Assertor.new(self, :backtrace=>caller).be(*args,&block)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
It's that easy. Because of their commonality AE provides two such terms,
|
|
25
|
+
`should` and +must+ as optional add-ons out-of-the-box.
|
|
26
|
+
|
|
27
|
+
require 'assay/ae/should'
|
|
28
|
+
require 'assay/ae/must'
|
|
29
|
+
|
|
30
|
+
We will use these two methods interchangeable for the rest of this
|
|
31
|
+
demonstration, but to be clear they both work exactly the same way,
|
|
32
|
+
and almost exactly like `assert`.
|
|
33
|
+
|
|
34
|
+
Keep in mind, AE "conical" functionality does not entail the subjunctive
|
|
35
|
+
forms. These are simply options you can load via your `test_helper.rb`,
|
|
36
|
+
or similar script, if you prefer these nomenclatures.
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
## Fluent Notation and Antonyms
|
|
40
|
+
|
|
41
|
+
Like `assert`, `should` and `must` can be used as higher order functions.
|
|
42
|
+
|
|
43
|
+
4.should == 4
|
|
44
|
+
4.must == 4
|
|
45
|
+
|
|
46
|
+
Antonyms provided for +should+ as `should!` (read "should not") and `shouldnt`.
|
|
47
|
+
For `must`, they are `must!` and +wont+.
|
|
48
|
+
|
|
49
|
+
4.should! == 5
|
|
50
|
+
4.shouldnt == 5
|
|
51
|
+
|
|
52
|
+
4.must! == 5
|
|
53
|
+
4.wont == 5
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
## To Be
|
|
57
|
+
|
|
58
|
+
On occasions where the English readability of a specification is hindered,
|
|
59
|
+
`be` can be used.
|
|
60
|
+
|
|
61
|
+
StandardError.must.be.raised? do
|
|
62
|
+
unknown_method
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
The `be` method is the same as `assert` with the single exception
|
|
66
|
+
that it will compare a lone argument to the receiver using `equate?`,
|
|
67
|
+
unlike `assert` which simply checks to see that the argument evaluates
|
|
68
|
+
as true.
|
|
69
|
+
|
|
70
|
+
10.should.be 10
|
|
71
|
+
10.should.be 10.0
|
|
72
|
+
10.should.be Numeric
|
|
73
|
+
|
|
74
|
+
Assertion.assert.raised? do
|
|
75
|
+
10.should.be "40"
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
## Indefinite Articles
|
|
80
|
+
|
|
81
|
+
Additional English forms are `a` and `an`, equivalent to `be` except
|
|
82
|
+
that they use `case?` (same as `#===`) instead of `equate?` when
|
|
83
|
+
acting on a single argument.
|
|
84
|
+
|
|
85
|
+
"hi".must.be.a String
|
|
86
|
+
|
|
87
|
+
Assertion.assert.raised? do
|
|
88
|
+
/x/.must.be.a /x/
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
Otherwise they are interchangeable.
|
|
92
|
+
|
|
93
|
+
"hi".must.be.an.instance_of?(String)
|
|
94
|
+
|
|
95
|
+
The indefinite articles work well when a noun follows as an arguments.
|
|
96
|
+
|
|
97
|
+
palindrome = lambda{ |x| x == x.reverse }
|
|
98
|
+
|
|
99
|
+
"abracarba".must.be.a palindrome
|
|
100
|
+
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# Expect Method
|
|
2
|
+
|
|
3
|
+
Expect is another assertion nomenclature available for use in your
|
|
4
|
+
tests or specifications. Inspired by Jay Fields' Expectations library,
|
|
5
|
+
it provides convenient syntax for creating exception and case equality
|
|
6
|
+
assertions.
|
|
7
|
+
|
|
8
|
+
require 'assay/ae/expect'
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
## Underlying Comparison
|
|
12
|
+
|
|
13
|
+
Expect uses `#===` for comparison. So providing an argument and a block
|
|
14
|
+
to #expect we can test for a somewhat broader range of compassion
|
|
15
|
+
than #assert. For example we can test for a subclass.
|
|
16
|
+
|
|
17
|
+
expect Numeric do
|
|
18
|
+
3
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
Assertion.assert.raised? do
|
|
22
|
+
expect Numeric do
|
|
23
|
+
"3"
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
Any expected value counts, even `nil` or `false`.
|
|
28
|
+
|
|
29
|
+
expect nil do
|
|
30
|
+
nil
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
Assertion.assert.raised? do
|
|
34
|
+
"abc".expect nil do
|
|
35
|
+
"abc"
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
A block that takes an argument is given the receiver.
|
|
40
|
+
|
|
41
|
+
"abc".expect(3) { |s| s.size }
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
## Exception Expectation
|
|
45
|
+
|
|
46
|
+
If the comparator is an Exception class or a instance of an Exception class,
|
|
47
|
+
then #expect will check to see if the block raises that kind of exception.
|
|
48
|
+
|
|
49
|
+
expect StandardError do
|
|
50
|
+
some_undefined_method
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
expect Assertion do
|
|
54
|
+
expect(nil)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
An instance of an Exception class stands for its class.
|
|
58
|
+
|
|
59
|
+
expect ArgumentError.new do
|
|
60
|
+
raise ArgumentError
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
An exception of another kind fails the expectation, but an interrupt or
|
|
64
|
+
an exit is never caught, so it isn't mistaken for a failure, or for
|
|
65
|
+
a pass when negated.
|
|
66
|
+
|
|
67
|
+
expect SystemExit do
|
|
68
|
+
expect! ArgumentError do
|
|
69
|
+
exit
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
This is an important distinction to note because it means `#expect` can not be used
|
|
74
|
+
if verify instances of Exception classes.
|
|
75
|
+
|
|
76
|
+
Assertion.assert.raised? do
|
|
77
|
+
expect Exception do
|
|
78
|
+
Exception.new
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
## Regex Expectations
|
|
84
|
+
|
|
85
|
+
That #expect entails `#===` also means we can check for Regexp matches.
|
|
86
|
+
|
|
87
|
+
expect /x/ do
|
|
88
|
+
"oooxooo"
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
## Expected Method
|
|
93
|
+
|
|
94
|
+
We can use #expected to make the receiver the object of expectation.
|
|
95
|
+
|
|
96
|
+
x = "dummy"
|
|
97
|
+
|
|
98
|
+
/x/.expected do
|
|
99
|
+
"x"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
## Without Block
|
|
104
|
+
|
|
105
|
+
Without a block, the receiver is compared to the argument.
|
|
106
|
+
|
|
107
|
+
x.expect String
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
## Negative Forms
|
|
111
|
+
|
|
112
|
+
Like #assert, `#expect` has a negated form called #expect!
|
|
113
|
+
|
|
114
|
+
expect! /x/ do
|
|
115
|
+
"o"
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
The pure word form for those who do not like the clever use of the
|
|
119
|
+
explimation mark is #forbid.
|
|
120
|
+
|
|
121
|
+
forbid /x/ do
|
|
122
|
+
"o"
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
## Functor, or Higher Order Function
|
|
127
|
+
|
|
128
|
+
Like #assert, #expect can be used used as a *fluid* notation.
|
|
129
|
+
|
|
130
|
+
10.expect == 10
|
|
131
|
+
|
|
132
|
+
In which case it works just like `#assert`, including negative forms.
|
|
133
|
+
|
|
134
|
+
10.expect! == 11
|
|
135
|
+
10.forbid == 11
|
|
136
|
+
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Assertion Counts
|
|
2
|
+
|
|
3
|
+
AE tracks the number of assertions made and the number that failed to pass.
|
|
4
|
+
We can reset the count using the +recount+ class method.
|
|
5
|
+
|
|
6
|
+
old_counts = Assay::AE::Assertor.recount
|
|
7
|
+
|
|
8
|
+
For example if we have one assertion pass and another fail.
|
|
9
|
+
|
|
10
|
+
assert(true)
|
|
11
|
+
|
|
12
|
+
expect Assertion do
|
|
13
|
+
assert(false)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
We will see that AE counted three assertions and one failure.
|
|
17
|
+
|
|
18
|
+
counts = Assay::AE::Assertor.counts.dup
|
|
19
|
+
|
|
20
|
+
counts[:total].assert == 3
|
|
21
|
+
counts[:pass].assert == 2
|
|
22
|
+
counts[:fail].assert == 1
|
|
23
|
+
|
|
24
|
+
The #expect call is an assertion too, which is why the total count is 3
|
|
25
|
+
rather than 2.
|
|
26
|
+
|
|
27
|
+
Now that we are done checking counts we will restore them so that
|
|
28
|
+
any other demos being run with this will tally correctly.
|
|
29
|
+
|
|
30
|
+
Assay::AE::Assertor.recount(old_counts)
|
|
31
|
+
Assay::AE::Assertor.counts[:total] += 3
|
|
32
|
+
Assay::AE::Assertor.counts[:pass] += 3
|
|
33
|
+
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Matchers
|
|
2
|
+
|
|
3
|
+
Matchers are simply Procs or objects with the proper interface that can be
|
|
4
|
+
passed to #assert or #refute (or other Assertor) as an ecapsulated test.
|
|
5
|
+
|
|
6
|
+
## Proc or #call
|
|
7
|
+
|
|
8
|
+
Passing a Proc object, or any object that responds to `#call`, will use it
|
|
9
|
+
as if it were a block of the method. This allows for a simple way to quickly
|
|
10
|
+
create reusable assertions.
|
|
11
|
+
|
|
12
|
+
palindrome = lambda{ |word| word == word.reverse }
|
|
13
|
+
|
|
14
|
+
"abracarba".assert palindrome
|
|
15
|
+
|
|
16
|
+
## #matches?
|
|
17
|
+
|
|
18
|
+
Additionally if an object responds to #matches? then the receiver
|
|
19
|
+
will be passed to this method to determine if the assertion passes.
|
|
20
|
+
|
|
21
|
+
palindrome = Object.new
|
|
22
|
+
|
|
23
|
+
def palindrome.matches?(word)
|
|
24
|
+
word == word.reverse
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
"abracarba".assert palindrome
|
|
28
|
+
|
|
29
|
+
## RSpec, Shoulda and other 3rd-Party Matchers
|
|
30
|
+
|
|
31
|
+
With tha addition of #matches?, AE supports the same interface for matchers
|
|
32
|
+
as RSpec. Any matcher library designed for use with RSpec should therefore
|
|
33
|
+
be usable with AE as well. This includes RSpecs own matchers and Shoulda's
|
|
34
|
+
excellent Rails matchers.
|