ae 1.8.2 → 1.9.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.
- checksums.yaml +7 -0
- data/HISTORY.md +71 -0
- data/LICENSE.txt +24 -0
- data/README.md +34 -14
- data/demo/01_overview.md +92 -0
- data/demo/02_assertion.md +4 -0
- data/demo/03_assert.md +402 -0
- data/demo/04_subjunctive.md +100 -0
- data/demo/05_expect.md +136 -0
- data/demo/06_counts.md +33 -0
- data/demo/07_matchers.md +34 -0
- data/demo/08_check.md +61 -0
- data/demo/09_legacy.md +122 -0
- data/demo/10_adapters.md +48 -0
- data/demo/applique/ae.rb +1 -0
- data/lib/ae/adapter.rb +3 -4
- data/lib/ae/adapters/minitest.rb +35 -36
- data/lib/ae/adapters/testunit.rb +42 -56
- data/lib/ae/ansi.rb +9 -7
- data/lib/ae/assert.rb +1 -2
- data/lib/ae/assertor.rb +118 -57
- data/lib/ae/check.rb +14 -6
- data/lib/ae/core_ext/helpers.rb +22 -20
- data/lib/ae/legacy.rb +38 -28
- data/lib/ae/must.rb +2 -2
- data/lib/ae/pry.rb +8 -11
- data/lib/ae/should.rb +2 -2
- data/lib/ae/version.rb +1 -19
- metadata +67 -48
- data/.ruby +0 -0
- data/.yardopts +0 -7
- data/DEMO.rdoc +0 -736
- data/lib/ae.yml +0 -61
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 07e5fefa83c88e06d6d8a94bdc072d9eea673ef345edbe75db64ab173f1e71fc
|
|
4
|
+
data.tar.gz: baad7c8b171ddf06a6382d8ce2da3394229290867bf3c0bd697d119eb94421ed
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 78893c43cd04011a98d2bc73bbde6652f536f1fd0c8f5107ab7b6b13dd6ac9ff0f1d530a5e58b1b3c2889366d4591ee691b46a0d5dbf61d573cb767204ad8496
|
|
7
|
+
data.tar.gz: 9dab4ff9008cf59f9f544617a496b079341d0f86adfc19334a429807db9bd46fae29b1a4e41875c1721cb06d028d9e3451967bb17027845f7b1a6a34035dc065
|
data/HISTORY.md
CHANGED
|
@@ -1,5 +1,76 @@
|
|
|
1
1
|
# RELEASE HISTORY
|
|
2
2
|
|
|
3
|
+
## 1.9.1 / 2026-11-10
|
|
4
|
+
|
|
5
|
+
This release fixes assertions that checked nothing, or less than they
|
|
6
|
+
appeared to. So some tests that passed before may now fail, but each
|
|
7
|
+
of those was not checking what it said.
|
|
8
|
+
|
|
9
|
+
Changes:
|
|
10
|
+
|
|
11
|
+
* Fix `x.assert.equal?(y)`, which always passed. Ruby's BasicObject,
|
|
12
|
+
Assertor's superclass, has its own public `equal?`, so the call
|
|
13
|
+
never became an assertion. Now it does, like `==`, `!=` and `!`.
|
|
14
|
+
* `x.assert.instance_eval { ... }` and `instance_exec` go to the
|
|
15
|
+
object, so the block's result is the assertion. They used to run in
|
|
16
|
+
the Assertor and check nothing.
|
|
17
|
+
* Warn about `x.assert.size == 3`, which checks only that `size` is
|
|
18
|
+
truthy. The warning comes when a method called with no arguments
|
|
19
|
+
returns something other than true, false or nil. Write
|
|
20
|
+
`x.size.assert == 3` instead.
|
|
21
|
+
* Pass keyword arguments on as keywords.
|
|
22
|
+
* `assert(expected) { ... }` and `expect(expected) { ... }` compare
|
|
23
|
+
with any expected value, including `false` and `nil`.
|
|
24
|
+
* A Hash or Symbol is no longer used as a proc because it has
|
|
25
|
+
`#to_proc`, so `refute({a: 1})` fails. A lambda that takes no
|
|
26
|
+
arguments works as a matcher.
|
|
27
|
+
* Fix `flunk`, which raised NoMethodError, and passed when negated.
|
|
28
|
+
* Fix `expect` with a block that takes the receiver, and with an
|
|
29
|
+
exception instance. It no longer catches an interrupt or an exit.
|
|
30
|
+
* Fix the legacy Test::Unit assertions: `assert(false)` never failed,
|
|
31
|
+
`assert_block` was inverted, `assert_raises` and `assert_throws`
|
|
32
|
+
were broken, and `assert_instance_of` accepted a subclass. They are
|
|
33
|
+
counted now, and raise the test framework's assertion error.
|
|
34
|
+
* The minitest and test-unit adapters add AE's assertions to the
|
|
35
|
+
test's count, and `ae/adapter` finds current minitest, which no
|
|
36
|
+
longer defines `MiniTest`.
|
|
37
|
+
* Build failure messages only for failures. A passing `==` on two
|
|
38
|
+
1000-element arrays took five seconds. A broken `#inspect` no
|
|
39
|
+
longer breaks the message, and very large color diffs are skipped.
|
|
40
|
+
* Color diffs are on only when standard output is a terminal and
|
|
41
|
+
NO_COLOR is not set. `AE.ansi = true` or `false` still decides.
|
|
42
|
+
* `Kernel#pry` no longer caches, which mixed up equal objects and
|
|
43
|
+
kept them all alive. It passes keywords, and sends `==` and the
|
|
44
|
+
like to the object.
|
|
45
|
+
* Fix `in_epsilon?` for negative numbers, `match?` for objects without
|
|
46
|
+
`=~`, and `throws?`, `thrown?` and `send?`, which hid errors raised
|
|
47
|
+
by the code under test.
|
|
48
|
+
* Fix `AE::Check.define` and `Check::Proc#message`, and report a failed
|
|
49
|
+
`ok` or `no` at the line that called it.
|
|
50
|
+
* Add demos for the legacy assertions and the adapters, and fix demos
|
|
51
|
+
that asserted nothing.
|
|
52
|
+
* Test on Ruby 4.0.
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
## 1.9.0 / 2026-03-30
|
|
56
|
+
|
|
57
|
+
Maintenance release. Modernized project tooling and cleaned up documentation.
|
|
58
|
+
|
|
59
|
+
Changes:
|
|
60
|
+
|
|
61
|
+
* Replace custom Indexer system with standard gemspec.
|
|
62
|
+
* Replace Travis CI with GitHub Actions.
|
|
63
|
+
* Replace Assembly/detroit with Rakefile.
|
|
64
|
+
* Simplify version.rb to use a plain constant.
|
|
65
|
+
* Update minitest adapter for minitest 5+.
|
|
66
|
+
* Update testunit adapter for modern test-unit gem.
|
|
67
|
+
* Fix typos and update URLs to HTTPS.
|
|
68
|
+
* Add LICENSE.txt.
|
|
69
|
+
* Move site from gh-pages to docs/.
|
|
70
|
+
* Remove obsolete files (var/, etc/, MANIFEST, DEMO.rdoc).
|
|
71
|
+
* Clean up .gitignore.
|
|
72
|
+
|
|
73
|
+
|
|
3
74
|
## 1.8.2 / 2013-02-18
|
|
4
75
|
|
|
5
76
|
This release primarily fixes one bug --the assertions count
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
(BSD-2-Clause License)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2008 Thomas Sawyer. All rights reserved.
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice,
|
|
9
|
+
this list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright
|
|
12
|
+
notice, this list of conditions and the following disclaimer in the
|
|
13
|
+
documentation and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
16
|
+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
18
|
+
COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
19
|
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
20
|
+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
21
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
22
|
+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
23
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
24
|
+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
# Assertive Expressive
|
|
2
2
|
|
|
3
|
-
[
|
|
4
|
-
[
|
|
5
|
-
[
|
|
6
|
-
[
|
|
7
|
-
|
|
8
|
-
[
|
|
9
|
-
[
|
|
10
|
-
[](http://travis-ci.org/rubyworks/ae)
|
|
11
|
-
[](http://badge.fury.io/rb/ae)
|
|
3
|
+
[Website](https://rubyworks.github.io/ae) /
|
|
4
|
+
[API](https://rubydoc.info/gems/ae) /
|
|
5
|
+
[Report Issue](https://github.com/rubyworks/ae/issues) /
|
|
6
|
+
[Source Code](https://github.com/rubyworks/ae)
|
|
7
|
+
|
|
8
|
+
[](https://rubygems.org/gems/ae)
|
|
9
|
+
[](https://github.com/rubyworks/ae/actions/workflows/test.yml)
|
|
12
10
|
|
|
13
11
|
|
|
14
12
|
## About
|
|
@@ -25,13 +23,13 @@ intended for reuse by any TDD, BDD or similar system.
|
|
|
25
23
|
* Core extensions are standardized around Ruby Facets.
|
|
26
24
|
* But Facets is not a dependency; the extensions are built-in.
|
|
27
25
|
* Easily extensible allowing for alternate notations.
|
|
28
|
-
* Eats
|
|
26
|
+
* Eats its own dog food.
|
|
29
27
|
|
|
30
28
|
|
|
31
29
|
## Synopsis
|
|
32
30
|
|
|
33
|
-
AE defines the method `assert`. It
|
|
34
|
-
as defined by Test::Unit and
|
|
31
|
+
AE defines the method `assert`. It is compatible with the method
|
|
32
|
+
as defined by Test::Unit and Minitest, which verifies truth of a
|
|
35
33
|
single argument (and can accept an optional failure message).
|
|
36
34
|
|
|
37
35
|
assert(true)
|
|
@@ -66,6 +64,14 @@ to make an assertion. Eg.
|
|
|
66
64
|
|
|
67
65
|
"Daffy Duck".assert.daffy?
|
|
68
66
|
|
|
67
|
+
The method's result is what gets asserted, so to compare a result,
|
|
68
|
+
call the method before `assert`:
|
|
69
|
+
|
|
70
|
+
x.size.assert == 3
|
|
71
|
+
|
|
72
|
+
Written as `x.assert.size == 3`, it would check only that `size`
|
|
73
|
+
is truthy, and AE warns about that.
|
|
74
|
+
|
|
69
75
|
When an assertion fails an Assertion exception is raised. Any test
|
|
70
76
|
framework can catch this exception and process it accordingly.
|
|
71
77
|
Technically the framework should check to see that the exception
|
|
@@ -154,6 +160,20 @@ and do:
|
|
|
154
160
|
Windows users use 'ruby setup.rb all'.
|
|
155
161
|
|
|
156
162
|
|
|
163
|
+
## Contributing
|
|
164
|
+
|
|
165
|
+
If you would like to contribute code to the AE project, for the upstream
|
|
166
|
+
repository and create a branch for you changes. When your changes are ready
|
|
167
|
+
for review (and no, they do not have to 100% perfect if you still have some issues
|
|
168
|
+
you need help working out).
|
|
169
|
+
|
|
170
|
+
It you need to personally discuss some ideas or issue you try to get up with us
|
|
171
|
+
via the mailing list or the IRC channel.
|
|
172
|
+
|
|
173
|
+
* [Source Code](https://github.com/rubyworks/ae) /
|
|
174
|
+
* [Mailing List](https://groups.google.com/group/rubyworks-mailinglist)
|
|
175
|
+
|
|
176
|
+
|
|
157
177
|
## Copyrights & License
|
|
158
178
|
|
|
159
179
|
Copyright (c) 2008 Rubyworks. All rights reserved.
|
|
@@ -162,7 +182,7 @@ Unless otherwise provided for by the originating author, this
|
|
|
162
182
|
program is distributed under the terms of the *BSD-2-Clause* license.
|
|
163
183
|
Portions of this program may be copyrighted by others.
|
|
164
184
|
|
|
165
|
-
See the NOTICE.
|
|
185
|
+
See the NOTICE.md file for details.
|
|
166
186
|
|
|
167
|
-
AE is a [Rubyworks](
|
|
187
|
+
AE is a [Rubyworks](https://rubyworks.github.io) project.
|
|
168
188
|
|
data/demo/01_overview.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
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 `$!`.
|
|
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
|
+
|
data/demo/03_assert.md
ADDED
|
@@ -0,0 +1,402 @@
|
|
|
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
|
+
AE offers an optional helper method for times when testing underlying private
|
|
395
|
+
or protected methods is important, called #pry. See the QED on pry for more
|
|
396
|
+
information.
|
|
397
|
+
|
|
398
|
+
For some testing underlying implementation might be considered poor
|
|
399
|
+
form. You will get no argument here. It should be used thoughtfully,
|
|
400
|
+
but I would not bet against there being occasions when such validations
|
|
401
|
+
might be needed.
|
|
402
|
+
|