ae 1.8.0 → 1.8.1
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.
- data/.ruby +2 -2
- data/HISTORY.rdoc +9 -0
- data/QED.rdoc +736 -0
- data/lib/ae.yml +2 -2
- data/lib/ae/ansi.rb +26 -0
- data/qed/08_check.rdoc +63 -0
- metadata +11 -7
data/.ruby
CHANGED
@@ -40,9 +40,9 @@ revision: 0
|
|
40
40
|
created: '2008-08-17'
|
41
41
|
summary: Assertive Expressive
|
42
42
|
title: AE
|
43
|
-
version: 1.8.
|
43
|
+
version: 1.8.1
|
44
44
|
name: ae
|
45
45
|
description: ! "Assertive Expressive is an assertions library specifically designed
|
46
46
|
\nfor reuse by other test frameworks."
|
47
47
|
organization: Rubyworks
|
48
|
-
date: '2011-11-
|
48
|
+
date: '2011-11-04'
|
data/HISTORY.rdoc
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
= RELEASE HISTORY
|
2
2
|
|
3
|
+
== 1.8.1 / 2011-12-04
|
4
|
+
|
5
|
+
Fixed missing ae/ansi.rb file from distribution.
|
6
|
+
|
7
|
+
Changes:
|
8
|
+
|
9
|
+
* Update manifest, missing ae/ansi.rb
|
10
|
+
|
11
|
+
|
3
12
|
== 1.8.0 / 2011-12-03 / Checkered Flag
|
4
13
|
|
5
14
|
This new release improves support for Proc-based assertions and
|
data/QED.rdoc
ADDED
@@ -0,0 +1,736 @@
|
|
1
|
+
= Introduction
|
2
|
+
|
3
|
+
AE is an assertions framework for Ruby. It's designed
|
4
|
+
around the concept of an Assertor. The Assertor is an
|
5
|
+
Assertion Functor, or Higher-Order Function, which
|
6
|
+
reroutes method calls while monitoring them for failing
|
7
|
+
conditions.
|
8
|
+
|
9
|
+
|
10
|
+
== What AE Provides
|
11
|
+
|
12
|
+
Requiring the AE library.
|
13
|
+
|
14
|
+
require 'ae'
|
15
|
+
|
16
|
+
Loads two classes, +Assertion+ and +Assertor+, the Kernel
|
17
|
+
method +assert+ and it's antonyms +assert!+ and +refute+
|
18
|
+
and a set of core extensions that make writing certain types
|
19
|
+
of assertions easier.
|
20
|
+
|
21
|
+
|
22
|
+
== Assertion and Assertor Classes
|
23
|
+
|
24
|
+
The +Assertion+ class is at the heart of AE. All other AE
|
25
|
+
methods depend on it. The +Assertion+ class is a subclass
|
26
|
+
of Exception. When an assertion is made and fails, it is
|
27
|
+
an instance of Assertion that is raised.
|
28
|
+
|
29
|
+
expect Assertion do
|
30
|
+
msg = "my failure message"
|
31
|
+
assert false, msg
|
32
|
+
end
|
33
|
+
|
34
|
+
Like any raised exception, the last Assertion message is available
|
35
|
+
via <tt>$!</tt>.
|
36
|
+
|
37
|
+
(FYI, in Test::Unit the equivalent class was called +AssertionFailedError+.)
|
38
|
+
|
39
|
+
Assertions themselves are not generally used in creating tests or
|
40
|
+
behavior specifications. Rather they are used to create additional
|
41
|
+
types of assertion methods.
|
42
|
+
|
43
|
+
As mentioned above the +Assertor+ class is a type of Higher-Order
|
44
|
+
function, or Functor, which intercedes with a normal message
|
45
|
+
invocation to monitor for failed conditions, upon which is raises
|
46
|
+
Assertion exceptions.
|
47
|
+
|
48
|
+
|
49
|
+
== Assertion Methods
|
50
|
+
|
51
|
+
The three methods, +assert+, <tt>assert!</tt> and +refute+ all
|
52
|
+
return an Assertor instance when used fluidly, i.e. magic-dot
|
53
|
+
notation, higher-order notation, functor notation, whatever you
|
54
|
+
prefer to call it.
|
55
|
+
|
56
|
+
assert(AE::Assertor === assert)
|
57
|
+
|
58
|
+
Through the use of +method_missing+, the Assertor allows us to write
|
59
|
+
statements like:
|
60
|
+
|
61
|
+
1.assert == 1
|
62
|
+
|
63
|
+
If the operation evaluates to false or nil, then an Assertion error
|
64
|
+
is raised.
|
65
|
+
|
66
|
+
expect Assertion do
|
67
|
+
1.assert == 2
|
68
|
+
end
|
69
|
+
|
70
|
+
The methods <tt>assert!</tt> and +refute+ are just like +assert+
|
71
|
+
expect they purport the negative condition. Patterned after Ruby's
|
72
|
+
own use of "<tt>!</tt>" as meaning +not+, <tt>assert!</tt> should be
|
73
|
+
read "assert not". While +refute+ exists for the sake of those who
|
74
|
+
find the use of a bang method for this purpose unsuited to them.
|
75
|
+
|
76
|
+
|
77
|
+
== How It Works
|
78
|
+
|
79
|
+
An Assertor essentially sits in wait for a method call (via
|
80
|
+
method_missing). When that happens it applies the method to the
|
81
|
+
original receiver, but wrapped in a clause that raises an
|
82
|
+
Assertion should the statement fail. If we wanted to be
|
83
|
+
pedantic, we could write our assertions like:
|
84
|
+
|
85
|
+
raise Assertion.new("1 != 1") unless 1 == 1
|
86
|
+
|
87
|
+
Instead of
|
88
|
+
|
89
|
+
1.assert == 1
|
90
|
+
|
91
|
+
Obviously using Assertor methods are whole lot more concise.
|
92
|
+
|
93
|
+
|
94
|
+
= Assertion Class
|
95
|
+
|
96
|
+
The Assertion class is a subclass of Exception and is the error raised when
|
97
|
+
and assertion fails.
|
98
|
+
|
99
|
+
= Assert Method
|
100
|
+
|
101
|
+
== Compatible with Test::Unit
|
102
|
+
|
103
|
+
The +assert+ method is designed to be backward compatible
|
104
|
+
with the same method in <tt>Test::Unit</tt>.
|
105
|
+
|
106
|
+
Using an argument, +assert+ will check that an argument evaluates
|
107
|
+
to true. Optionally one can send along a meaningful message should
|
108
|
+
the assertion fail.
|
109
|
+
|
110
|
+
assert(true, "Not true!")
|
111
|
+
|
112
|
+
expect Assertion do
|
113
|
+
assert(false, "Not true!")
|
114
|
+
end
|
115
|
+
|
116
|
+
|
117
|
+
== Assert with a Block
|
118
|
+
|
119
|
+
In addition +assert+ has been extended to accept a block. Like the case of the
|
120
|
+
argument, the block is expected to return something that evaluates as true.
|
121
|
+
|
122
|
+
assert do
|
123
|
+
true
|
124
|
+
end
|
125
|
+
|
126
|
+
Assertion.assert.raised? do
|
127
|
+
assert do
|
128
|
+
false
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
We should also mention that, while probably not very useful, since
|
133
|
+
the arity of a block can be checked, one can also pass the receiver
|
134
|
+
into the block as a block argument.
|
135
|
+
|
136
|
+
"hi".assert do |s|
|
137
|
+
/h/ =~ s
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
== Antonyms for Assert
|
142
|
+
|
143
|
+
We can state the opposite assertion using <tt>assert!</tt>.
|
144
|
+
|
145
|
+
10.assert! == 9
|
146
|
+
|
147
|
+
Or, because some people do not like the use of a bang method, +refute+.
|
148
|
+
|
149
|
+
10.refute == 9
|
150
|
+
|
151
|
+
These terms can be used just as +assert+ is used in all examples,
|
152
|
+
but with the opposite inference.
|
153
|
+
|
154
|
+
Another way to get the opposite inference, is to use +not+.
|
155
|
+
|
156
|
+
10.assert.not == 9
|
157
|
+
|
158
|
+
== Lambda Assertions
|
159
|
+
|
160
|
+
Passing +assert+ a `Proc` object, or any object that responds to `#call`,
|
161
|
+
will be used as if it were a block. This allows for a simple way to quickly
|
162
|
+
create reusable assertions.
|
163
|
+
|
164
|
+
palindrome = lambda{ |word| word == word.reverse }
|
165
|
+
|
166
|
+
"abracarba".assert palindrome
|
167
|
+
|
168
|
+
The message for a failed assertion will come from calling `#to_s` on the
|
169
|
+
object.
|
170
|
+
|
171
|
+
== RSpec-style Assertion Matchers
|
172
|
+
|
173
|
+
If an object passed to assert responds to `#matches?` then AE will handle
|
174
|
+
the object as an RSpec-style mather, the receiver will be passed to the
|
175
|
+
`#matches?` method to determine if the assertion passes and RSpec matcher
|
176
|
+
message methods will be used if they are defined.
|
177
|
+
|
178
|
+
palindrome = Object.new
|
179
|
+
|
180
|
+
def palindrome.matches?(word)
|
181
|
+
word == word.reverse
|
182
|
+
end
|
183
|
+
|
184
|
+
"abracarba".assert palindrome
|
185
|
+
|
186
|
+
|
187
|
+
== Identity Assertions
|
188
|
+
|
189
|
+
Rather then the general form.
|
190
|
+
|
191
|
+
x = 10
|
192
|
+
x.assert.object_id == x.object_id
|
193
|
+
|
194
|
+
We can use Ruby's own <tt>equal?</tt> method.
|
195
|
+
|
196
|
+
x.assert.equal?(x)
|
197
|
+
|
198
|
+
AE provides <tt>identical?</tt> method as an alternative
|
199
|
+
to make it a bit more clear.
|
200
|
+
|
201
|
+
x.assert.identical?(x)
|
202
|
+
|
203
|
+
|
204
|
+
== Equality Assertions
|
205
|
+
|
206
|
+
The most common assertion is that of value equality (<tt>==</tt>),
|
207
|
+
as we have seen throughout this document. But other forms of
|
208
|
+
equality can be verified as easily. We have already mentioned
|
209
|
+
identity. In addition there is <i>type equality</i>.
|
210
|
+
|
211
|
+
17.assert.eql? 17
|
212
|
+
|
213
|
+
Assertion.assert.raised? do
|
214
|
+
17.assert.eql? 17.0
|
215
|
+
end
|
216
|
+
|
217
|
+
And there is <i>case equality</i>.
|
218
|
+
|
219
|
+
Numeric.assert === 3
|
220
|
+
|
221
|
+
|
222
|
+
== Checking Equality with a Block
|
223
|
+
|
224
|
+
Because operators can not take blocks, and at times blocks can
|
225
|
+
be convenient means of supplying a value to an assertion,
|
226
|
+
AE has defined alternate renditions of the equality methods.
|
227
|
+
For equal? and eql?, the method names are the same, they simply
|
228
|
+
can take a block in place of an argument if need be.
|
229
|
+
|
230
|
+
For <i>value equality</i> (<tt>==</tt>), the method is called <tt>eq?</tt>.
|
231
|
+
|
232
|
+
10.assert.eq? do
|
233
|
+
10.0
|
234
|
+
end
|
235
|
+
|
236
|
+
And should it fail...
|
237
|
+
|
238
|
+
Assertion.assert.raised? do
|
239
|
+
10.assert.eq? do
|
240
|
+
20
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
== Case Equality
|
245
|
+
|
246
|
+
For <i>case equality</i> (<tt>===</tt>), it is <tt>case?</tt>.
|
247
|
+
|
248
|
+
Numeric.assert.case? do
|
249
|
+
"3".to_i
|
250
|
+
end
|
251
|
+
|
252
|
+
Assertion.assert.raised? do
|
253
|
+
Numeric.assert.case? do
|
254
|
+
"3"
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
|
259
|
+
== Regular Expressions
|
260
|
+
|
261
|
+
Regular Expressions can be used to make assertions in much the same way as equality.
|
262
|
+
|
263
|
+
/i/.assert =~ "i"
|
264
|
+
|
265
|
+
Assertion.assert.raised? do
|
266
|
+
/i/.assert =~ "g"
|
267
|
+
end
|
268
|
+
|
269
|
+
Conversely the String class recognizes the #=~ method as well.
|
270
|
+
|
271
|
+
"i".assert =~ /i/
|
272
|
+
|
273
|
+
Assertion.assert.raised? do
|
274
|
+
"i".assert =~ /g/
|
275
|
+
end
|
276
|
+
|
277
|
+
|
278
|
+
== Exception Assertions
|
279
|
+
|
280
|
+
Validating errors is easy too, as has already been shown
|
281
|
+
in the document to verify assertion failures.
|
282
|
+
|
283
|
+
StandardError.assert.raised? do
|
284
|
+
unknown_method
|
285
|
+
end
|
286
|
+
|
287
|
+
|
288
|
+
== Assertions on Object State
|
289
|
+
|
290
|
+
While testing or specifying the internal state of an object is
|
291
|
+
generally considered poor form, there are times when it is
|
292
|
+
necessary. Assert combined with +instance_eval+ makes it easy too.
|
293
|
+
|
294
|
+
class X
|
295
|
+
attr :a
|
296
|
+
def initialize(a); @a = a; end
|
297
|
+
end
|
298
|
+
|
299
|
+
x = X.new(1)
|
300
|
+
|
301
|
+
x.assert.instance_eval do
|
302
|
+
@a == 1
|
303
|
+
end
|
304
|
+
|
305
|
+
|
306
|
+
== Catch/Try Assertions
|
307
|
+
|
308
|
+
Catch/Try throws can be tested via <tt>Symbol#thrown?</tt>.
|
309
|
+
|
310
|
+
:hookme.assert.thrown? do
|
311
|
+
throw :hookme
|
312
|
+
end
|
313
|
+
|
314
|
+
Alternatively, a lambda containing the potential throw
|
315
|
+
can be the receiver using <tt>throws?</tt>.
|
316
|
+
|
317
|
+
hook = lambda{ throw :hookme }
|
318
|
+
|
319
|
+
hook.assert.throws?(:hookme)
|
320
|
+
|
321
|
+
|
322
|
+
== Assertions on Proc Changes
|
323
|
+
|
324
|
+
I have to admit I'm not sure how this is useful,
|
325
|
+
but I found it in the Bacon API and ported it over
|
326
|
+
just for sake of thoroughness.
|
327
|
+
|
328
|
+
a = 0
|
329
|
+
|
330
|
+
l = lambda{ a }
|
331
|
+
|
332
|
+
l.assert.change?{ a +=1 }
|
333
|
+
|
334
|
+
|
335
|
+
== Assertion on literal True, False and Nil
|
336
|
+
|
337
|
+
Ruby already provides the #nil? method.
|
338
|
+
|
339
|
+
nil.assert.nil?
|
340
|
+
|
341
|
+
AE adds <tt>true?</tt> and <tt>false?</tt> which acts accordingly.
|
342
|
+
|
343
|
+
true.assert.true?
|
344
|
+
false.assert.false?
|
345
|
+
|
346
|
+
|
347
|
+
== Send Assertions
|
348
|
+
|
349
|
+
Assert that a method can be successfully called.
|
350
|
+
|
351
|
+
"STRING".assert.send?(:upcase)
|
352
|
+
|
353
|
+
|
354
|
+
== Numeric Delta and Epsilon
|
355
|
+
|
356
|
+
You may wish to assert that a numeric value is with some
|
357
|
+
range.
|
358
|
+
|
359
|
+
3.in_delta?(1,5)
|
360
|
+
|
361
|
+
Or minimum range.
|
362
|
+
|
363
|
+
3.in_epsilon?(3,5)
|
364
|
+
|
365
|
+
|
366
|
+
== Verifying Object State
|
367
|
+
|
368
|
+
Not surprisingly if underlying object state needs to be verified, +instance_eval+
|
369
|
+
can be used in conjunction with +assert+.
|
370
|
+
|
371
|
+
class X
|
372
|
+
attr :a
|
373
|
+
def initialize(a); @a = a; end
|
374
|
+
end
|
375
|
+
|
376
|
+
x = X.new(4)
|
377
|
+
|
378
|
+
x.instance_eval do
|
379
|
+
@a.assert == 4
|
380
|
+
end
|
381
|
+
|
382
|
+
However #instance_eval is a reserved method for the underlying Assertor class,
|
383
|
+
so it cannot be used on #assert, e.g.
|
384
|
+
|
385
|
+
x.assert.instance_eval do
|
386
|
+
@a == "obvisouly wrong"
|
387
|
+
end
|
388
|
+
|
389
|
+
AE offers an optional helper method for times when testing underlying private
|
390
|
+
or protected methods is important, called #pry. See the QED on pry for more
|
391
|
+
information.
|
392
|
+
|
393
|
+
For some testing underlying implementation might be considered poor
|
394
|
+
form. You will get no argument here. It should be used thoughtfully,
|
395
|
+
but I would not bet against there being occasions when such validations
|
396
|
+
might be needed.
|
397
|
+
|
398
|
+
= Subjunctives
|
399
|
+
|
400
|
+
Okay. I can hear the BDDers rumbling, "where's the *should?*"
|
401
|
+
AE has nothing against "should", but there are different
|
402
|
+
approaches for utilizing should nomenclature in specifications,
|
403
|
+
and AE wants to be open to these techniques. One of which
|
404
|
+
is how Shoulda (http://shoulda.rubyforge.org) utilizes
|
405
|
+
+should+ in a way analogous to RSpec's use of +it+.
|
406
|
+
|
407
|
+
Even so, AE provides an optional mixin called +Subjunctive+ which
|
408
|
+
can be used to create assertor methods with English subjunctive
|
409
|
+
terms, such as +should+, or +must+, +shall+ and +will+.
|
410
|
+
To load this library use:
|
411
|
+
|
412
|
+
require 'ae/subjunctive'
|
413
|
+
|
414
|
+
Then all that is required it to define a subjunctive method for all
|
415
|
+
objects. For example:
|
416
|
+
|
417
|
+
def will(*args, &block)
|
418
|
+
Assertor.new(self, :backtrace=>caller).be(*args,&block)
|
419
|
+
end
|
420
|
+
|
421
|
+
It's that easy. Because of their commonality AE provides two such terms,
|
422
|
+
+should+ and +must+ as optional add-ons out-of-the-box.
|
423
|
+
|
424
|
+
require 'ae/should'
|
425
|
+
require 'ae/must'
|
426
|
+
|
427
|
+
We will use these two methods interchangeable for the rest of this
|
428
|
+
demonstration, but to be clear they both work exactly the same way,
|
429
|
+
and almost exactly like +assert+.
|
430
|
+
|
431
|
+
Keep in mind, AE "conical" functionality does not entail the subjunctive
|
432
|
+
forms. These are simply options you can load via your <tt>test_helper.rb</tt>,
|
433
|
+
or similar script, if you prefer these nomenclatures.
|
434
|
+
|
435
|
+
|
436
|
+
== Fluent Notation and Antonyms
|
437
|
+
|
438
|
+
Like +assert+, +should+ and +must+ can be used as higher order functions.
|
439
|
+
|
440
|
+
4.should == 4
|
441
|
+
4.must == 4
|
442
|
+
|
443
|
+
Antonyms provided for +should+ as <tt>should!</tt> (read "should not") and +shouldnt+.
|
444
|
+
For +must+ +must+, they are <tt>must!</tt> and +wont+.
|
445
|
+
|
446
|
+
4.should! == 5
|
447
|
+
4.shouldnt == 5
|
448
|
+
|
449
|
+
4.must! == 5
|
450
|
+
4.wont == 5
|
451
|
+
|
452
|
+
|
453
|
+
== To Be
|
454
|
+
|
455
|
+
On occasions where the English readability of a specification is hindered,
|
456
|
+
+be+ can be used.
|
457
|
+
|
458
|
+
StandardError.must.be.raised? do
|
459
|
+
unknown_method
|
460
|
+
end
|
461
|
+
|
462
|
+
The +be+ method is the same as +assert+ with the single exception
|
463
|
+
that it will compare a lone argument to the receiver using +equate?+,
|
464
|
+
unlike +assert+ which simply checks to see that the argument evaluates
|
465
|
+
as true.
|
466
|
+
|
467
|
+
10.should.be 10
|
468
|
+
10.should.be 10.0
|
469
|
+
10.should.be Numeric
|
470
|
+
|
471
|
+
Assertion.assert.raised? do
|
472
|
+
10.should.be "40"
|
473
|
+
end
|
474
|
+
|
475
|
+
|
476
|
+
== Indefinite Articles
|
477
|
+
|
478
|
+
Additional English forms are +a+ and +an+, equivalent to +be+ except
|
479
|
+
that they use <tt>case?</tt> (same as <tt>#===</tt>) instead of
|
480
|
+
<tt>equate?</tt> when acting on a single argument.
|
481
|
+
|
482
|
+
"hi".must.be.a String
|
483
|
+
|
484
|
+
Assertion.assert.raised? do
|
485
|
+
/x/.must.be.a /x/
|
486
|
+
end
|
487
|
+
|
488
|
+
Otherwise they are interchangeable.
|
489
|
+
|
490
|
+
"hi".must.be.an.instance_of?(String)
|
491
|
+
|
492
|
+
The indefinite articles work well when a noun follows as an arguments.
|
493
|
+
|
494
|
+
palindrome = lambda{ |x| x == x.reverse }
|
495
|
+
|
496
|
+
"abracarba".must.be.a palindrome
|
497
|
+
|
498
|
+
|
499
|
+
= Expect Method
|
500
|
+
|
501
|
+
Expect is another assertion nomenclature available for use in your
|
502
|
+
tests or specifications. Inspired by Jay Fields' Expectations library,
|
503
|
+
it provides convenient syntax for creating exception and case equality
|
504
|
+
assertions.
|
505
|
+
|
506
|
+
require 'ae/expect'
|
507
|
+
|
508
|
+
|
509
|
+
== Underlying Comparison
|
510
|
+
|
511
|
+
Expect uses #=== for comparison. So providing an argument and a block to
|
512
|
+
#expect we can test for a somewhat broader range of compassion than #assert.
|
513
|
+
For example we can test for a subclass.
|
514
|
+
|
515
|
+
expect Numeric do
|
516
|
+
3
|
517
|
+
end
|
518
|
+
|
519
|
+
Assertion.assert.raised? do
|
520
|
+
expect Numeric do
|
521
|
+
"3"
|
522
|
+
end
|
523
|
+
end
|
524
|
+
|
525
|
+
|
526
|
+
== Exception Expectation
|
527
|
+
|
528
|
+
If the comparator is an Exception class or a instance of an Exception class,
|
529
|
+
then #expect will check to see if the block raises that kind of exception.
|
530
|
+
|
531
|
+
expect StandardError do
|
532
|
+
some_undefined_method
|
533
|
+
end
|
534
|
+
|
535
|
+
expect Assertion do
|
536
|
+
expect(nil)
|
537
|
+
end
|
538
|
+
|
539
|
+
This is an important distinction to note because it means #expect can not be used
|
540
|
+
if verify instances of Exception classes.
|
541
|
+
|
542
|
+
Assertion.assert.raised? do
|
543
|
+
expect Exception do
|
544
|
+
Exception.new
|
545
|
+
end
|
546
|
+
end
|
547
|
+
|
548
|
+
|
549
|
+
== Regex Expectations
|
550
|
+
|
551
|
+
That #expect entails #=== also means we can check for Regexp matches.
|
552
|
+
|
553
|
+
expect /x/ do
|
554
|
+
"oooxooo"
|
555
|
+
end
|
556
|
+
|
557
|
+
|
558
|
+
== Expected Method
|
559
|
+
|
560
|
+
We can use #expected to make the receiver the object of expectation.
|
561
|
+
|
562
|
+
x = "dummy"
|
563
|
+
|
564
|
+
/x/.expected do
|
565
|
+
"x"
|
566
|
+
end
|
567
|
+
|
568
|
+
|
569
|
+
== Without Block
|
570
|
+
|
571
|
+
Without a block, the receiver is compared to the argument.
|
572
|
+
|
573
|
+
x.expect String
|
574
|
+
|
575
|
+
|
576
|
+
== Negative Forms
|
577
|
+
|
578
|
+
Like #assert, #expect has a negated form called #expect!
|
579
|
+
|
580
|
+
expect! /x/ do
|
581
|
+
"o"
|
582
|
+
end
|
583
|
+
|
584
|
+
The pure word form for those who do not like the clever use of the
|
585
|
+
explimation mark is #forbid.
|
586
|
+
|
587
|
+
forbid /x/ do
|
588
|
+
"o"
|
589
|
+
end
|
590
|
+
|
591
|
+
|
592
|
+
== Functor, or Higher Order Function
|
593
|
+
|
594
|
+
Like #assert, #expect can be used used as a *fluid* notation.
|
595
|
+
|
596
|
+
10.expect == 10
|
597
|
+
|
598
|
+
In which case it works just like #assert, including negative forms.
|
599
|
+
|
600
|
+
10.expect! == 11
|
601
|
+
10.forbid == 11
|
602
|
+
|
603
|
+
|
604
|
+
= Assertion Counts
|
605
|
+
|
606
|
+
AE tracks the number of assertions made and the number that failed to pass.
|
607
|
+
We can reset the count using the +recount+ class method.
|
608
|
+
|
609
|
+
old_counts = AE::Assertor.recount
|
610
|
+
|
611
|
+
For example if we have one assertion pass and another fail.
|
612
|
+
|
613
|
+
assert(true)
|
614
|
+
|
615
|
+
expect Assertion do
|
616
|
+
assert(false)
|
617
|
+
end
|
618
|
+
|
619
|
+
We will see that AE counted three assertions and one failure.
|
620
|
+
|
621
|
+
counts = AE::Assertor.counts.dup
|
622
|
+
|
623
|
+
counts[:total].assert == 3
|
624
|
+
counts[:pass].assert == 2
|
625
|
+
counts[:fail].assert == 1
|
626
|
+
|
627
|
+
The #expect call is an assertion too, which is why the total count is 3
|
628
|
+
rather than 2.
|
629
|
+
|
630
|
+
Now that we are done checking counts we will restore them so that
|
631
|
+
any other demos being run with this will tally correctly.
|
632
|
+
|
633
|
+
AE::Assertor.recount(old_counts)
|
634
|
+
AE::Assertor.counts[:total] += 3
|
635
|
+
AE::Assertor.counts[:pass] += 3
|
636
|
+
|
637
|
+
|
638
|
+
= Matchers
|
639
|
+
|
640
|
+
Matchers are simply Procs or objects with the proper interface that can be
|
641
|
+
passed to #assert or #refute (or other Assertor) as an ecapsulated test.
|
642
|
+
|
643
|
+
== Proc or #to_proc
|
644
|
+
|
645
|
+
Passing a Proc object or an object that responds to :to_proc, will use it
|
646
|
+
as if it were a block of the method. This allows for a simple way to quickly
|
647
|
+
create reusable assertions.
|
648
|
+
|
649
|
+
palindrome = lambda{ |word| word == word.reverse }
|
650
|
+
|
651
|
+
"abracarba".assert palindrome
|
652
|
+
|
653
|
+
== #matches?
|
654
|
+
|
655
|
+
Additionally if an object responds to #matches? then the receiver
|
656
|
+
will be passed to this method to determine if the assertion passes.
|
657
|
+
|
658
|
+
palindrome = Object.new
|
659
|
+
|
660
|
+
def palindrome.matches?(word)
|
661
|
+
word == word.reverse
|
662
|
+
end
|
663
|
+
|
664
|
+
"abracarba".assert palindrome
|
665
|
+
|
666
|
+
== RSpec, Shoulda and other 3rd-Party Matchers
|
667
|
+
|
668
|
+
With tha addition of #matches?, AE supports the same interface for matchers
|
669
|
+
as RSpec. Any matcher library designed for use with RSpec should therefore
|
670
|
+
be usable with AE as well. This includes RSpecs own matchers and Shoulda's
|
671
|
+
excellent Rails matchers.
|
672
|
+
|
673
|
+
== Check Ok/No
|
674
|
+
|
675
|
+
The Check library is an optional library that can be used
|
676
|
+
to conveniently speed-up construction of reptitive assertions.
|
677
|
+
|
678
|
+
To use it, first require the library, then include the mixin
|
679
|
+
into the namespace in which you will utilize it.
|
680
|
+
|
681
|
+
require 'ae/check'
|
682
|
+
|
683
|
+
include AE::Check
|
684
|
+
|
685
|
+
Now we can define ok/no check procedures. A one-off procedure is
|
686
|
+
defined with a block only.
|
687
|
+
|
688
|
+
check do |x, y|
|
689
|
+
x == y
|
690
|
+
end
|
691
|
+
|
692
|
+
ok 1,1
|
693
|
+
|
694
|
+
no 1,2
|
695
|
+
|
696
|
+
To define reusable check procedures, give the procedure a name.
|
697
|
+
|
698
|
+
check :palindrome do |x|
|
699
|
+
x.reverse == x
|
700
|
+
end
|
701
|
+
|
702
|
+
This will also cause the current check method to be set.
|
703
|
+
Later in the code, the check procedure can be reset to this
|
704
|
+
by just passing the name.
|
705
|
+
|
706
|
+
check :palindrome
|
707
|
+
|
708
|
+
ok 'abracarba'
|
709
|
+
|
710
|
+
no 'foolishness'
|
711
|
+
|
712
|
+
The Check mixin comes preloaded with a few standard checks.
|
713
|
+
By default the `:equality` procedure is used.
|
714
|
+
|
715
|
+
check :equality
|
716
|
+
|
717
|
+
ok 1=>1.0
|
718
|
+
|
719
|
+
Notice the use of the hash argument here. This is a useful construct for
|
720
|
+
many check procedures becuase it it akin to the `#=>` pattern we often
|
721
|
+
see in code examples and it also allows for multiple assertions in one call.
|
722
|
+
For instance, in the case of `:equality`, multiple entries convey a
|
723
|
+
meaning of logical-or.
|
724
|
+
|
725
|
+
ok 1=>2, 1=>1
|
726
|
+
|
727
|
+
This would pass becuase the second assertion of equality is true.
|
728
|
+
|
729
|
+
Another built in check is `:case_equality` which uses `#===` instead of `#==`
|
730
|
+
to make the comparison.
|
731
|
+
|
732
|
+
check :case_equality
|
733
|
+
|
734
|
+
ok 1=>Integer
|
735
|
+
|
736
|
+
|
data/lib/ae.yml
CHANGED
@@ -40,9 +40,9 @@ revision: 0
|
|
40
40
|
created: '2008-08-17'
|
41
41
|
summary: Assertive Expressive
|
42
42
|
title: AE
|
43
|
-
version: 1.8.
|
43
|
+
version: 1.8.1
|
44
44
|
name: ae
|
45
45
|
description: ! "Assertive Expressive is an assertions library specifically designed
|
46
46
|
\nfor reuse by other test frameworks."
|
47
47
|
organization: Rubyworks
|
48
|
-
date: '2011-11-
|
48
|
+
date: '2011-11-04'
|
data/lib/ae/ansi.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'ansi/diff'
|
2
|
+
|
3
|
+
module AE
|
4
|
+
|
5
|
+
# Default ANSI mode is "on".
|
6
|
+
@ansi = true
|
7
|
+
|
8
|
+
# ANSI mode.
|
9
|
+
#
|
10
|
+
# @return [Boolean] ANSI mode.
|
11
|
+
def self.ansi?
|
12
|
+
@ansi
|
13
|
+
end
|
14
|
+
|
15
|
+
# To turn of ANSI colorized error messages off, set
|
16
|
+
# ansi to +false+ in your test helper.
|
17
|
+
#
|
18
|
+
# @example
|
19
|
+
# AE.ansi = false
|
20
|
+
#
|
21
|
+
def self.ansi=(boolean)
|
22
|
+
@ansi = boolean
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
data/qed/08_check.rdoc
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
== Check Ok/No
|
2
|
+
|
3
|
+
The Check library is an optional library that can be used
|
4
|
+
to conveniently speed-up construction of reptitive assertions.
|
5
|
+
|
6
|
+
To use it, first require the library, then include the mixin
|
7
|
+
into the namespace in which you will utilize it.
|
8
|
+
|
9
|
+
require 'ae/check'
|
10
|
+
|
11
|
+
include AE::Check
|
12
|
+
|
13
|
+
Now we can define ok/no check procedures. A one-off procedure is
|
14
|
+
defined with a block only.
|
15
|
+
|
16
|
+
check do |x, y|
|
17
|
+
x == y
|
18
|
+
end
|
19
|
+
|
20
|
+
ok 1,1
|
21
|
+
|
22
|
+
no 1,2
|
23
|
+
|
24
|
+
To define reusable check procedures, give the procedure a name.
|
25
|
+
|
26
|
+
check :palindrome do |x|
|
27
|
+
x.reverse == x
|
28
|
+
end
|
29
|
+
|
30
|
+
This will also cause the current check method to be set.
|
31
|
+
Later in the code, the check procedure can be reset to this
|
32
|
+
by just passing the name.
|
33
|
+
|
34
|
+
check :palindrome
|
35
|
+
|
36
|
+
ok 'abracarba'
|
37
|
+
|
38
|
+
no 'foolishness'
|
39
|
+
|
40
|
+
The Check mixin comes preloaded with a few standard checks.
|
41
|
+
By default the `:equality` procedure is used.
|
42
|
+
|
43
|
+
check :equality
|
44
|
+
|
45
|
+
ok 1=>1.0
|
46
|
+
|
47
|
+
Notice the use of the hash argument here. This is a useful construct for
|
48
|
+
many check procedures becuase it it akin to the `#=>` pattern we often
|
49
|
+
see in code examples and it also allows for multiple assertions in one call.
|
50
|
+
For instance, in the case of `:equality`, multiple entries convey a
|
51
|
+
meaning of logical-or.
|
52
|
+
|
53
|
+
ok 1=>2, 1=>1
|
54
|
+
|
55
|
+
This would pass becuase the second assertion of equality is true.
|
56
|
+
|
57
|
+
Another built in check is `:case_equality` which uses `#===` instead of `#==`
|
58
|
+
to make the comparison.
|
59
|
+
|
60
|
+
check :case_equality
|
61
|
+
|
62
|
+
ok 1=>Integer
|
63
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ae
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.8.
|
4
|
+
version: 1.8.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-11-04 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ansi
|
16
|
-
requirement: &
|
16
|
+
requirement: &31611960 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *31611960
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: detroit
|
27
|
-
requirement: &
|
27
|
+
requirement: &31611420 !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: *31611420
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: qed
|
38
|
-
requirement: &
|
38
|
+
requirement: &31610920 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *31610920
|
47
47
|
description: ! "Assertive Expressive is an assertions library specifically designed
|
48
48
|
\nfor reuse by other test frameworks."
|
49
49
|
email:
|
@@ -53,6 +53,7 @@ extensions: []
|
|
53
53
|
extra_rdoc_files:
|
54
54
|
- HISTORY.rdoc
|
55
55
|
- README.rdoc
|
56
|
+
- QED.rdoc
|
56
57
|
- NOTICE.rdoc
|
57
58
|
files:
|
58
59
|
- .ruby
|
@@ -61,6 +62,7 @@ files:
|
|
61
62
|
- lib/ae/adapters/minitest.rb
|
62
63
|
- lib/ae/adapters/rspec.rb
|
63
64
|
- lib/ae/adapters/testunit.rb
|
65
|
+
- lib/ae/ansi.rb
|
64
66
|
- lib/ae/assert.rb
|
65
67
|
- lib/ae/assertion.rb
|
66
68
|
- lib/ae/assertor.rb
|
@@ -85,8 +87,10 @@ files:
|
|
85
87
|
- qed/05_expect.rdoc
|
86
88
|
- qed/06_counts.rdoc
|
87
89
|
- qed/07_matchers.rdoc
|
90
|
+
- qed/08_check.rdoc
|
88
91
|
- HISTORY.rdoc
|
89
92
|
- README.rdoc
|
93
|
+
- QED.rdoc
|
90
94
|
- NOTICE.rdoc
|
91
95
|
homepage: http://rubyworks.github.com/ae
|
92
96
|
licenses:
|