Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Assay is one library again. The notations that were spun off as separate
gems in 0.4 (Fluidity, RSpecial, Assay MiniTest and Assay TestUnit) are
back, each loaded on its own, and Bang joins them. Assay needs Ruby 2.7 or newer.
back, each loaded on its own, and Bang and AE join them. Assay needs Ruby 2.7 or newer.

Changes:

Expand All @@ -17,6 +17,11 @@ Changes:
* Add `assay/bang`, from the Bang gem, rebuilt on Assay's classes:
`x.equal!(y)` fails with an `EqualAssay`. A query method Assay has
no class for still works (`10.even!`) and fails with an `Assertion`.
* Add `assay/ae`, the notation of the AE gem (`x.assert == y`,
`expect`, and optionally `should`, `must` and `ok`/`no`), built on
Assay's classes: a failure is of the Assay class for the method
where there is one. Its module is `Assay::AE`. The AE gem itself
carries on unchanged.
* Fix a Hash under test being taken for options, which made comparing
two hashes raise ArgumentError in every notation. Only a trailing
Hash whose keys are all `:message` or `:backtrace` is options now.
Expand All @@ -27,6 +32,13 @@ Changes:
* Fix `assert(false)` passing silently once `assay/fluidity/assert` was
loaded. Worse, Assay itself called that `assert`, so no assay could
fail either.
* Fix fluent assertions that used a method every object has, such as
`x.should.be.kind_of?(String)`, `x.assert.nil?` or `x.assert != y`.
The grammar object answered them itself, so they never failed. This
goes back to the Fluidity gem. Likewise `expect(x).to.kind_of?(String)`
in the RSpec notation, which is now a NoMethodError.
* The fluent notation passes keyword arguments on to an object's own
query method.
* Fix the fluent fallback to an object's own query method ignoring
negation: `x.should_not.be.no` now fails when `x.no?` is true.
* Fix `will_not` and `wont`. They were never defined, and loading the
Expand Down
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ assertions, each loaded on its own:
| `assay/testunit` | Test::Unit's `assert_equal` |
| `assay/rspec` | RSpec's `should eq` and `expect(x).to eq` |
| `assay/fluidity` | Assay's own: `x.should.equal y`, `x.assert == y` |
| `assay/ae` | AE's functor: `x.assert == y`, `a.assert.include?(e)` |
| `assay/bang` | Query methods as assertions: `x.equal!(y)`, `x.empty!` |

Whichever you choose, a failure is an instance of the same assay class,
Expand Down Expand Up @@ -120,6 +121,42 @@ And to satisfy the technical aficionados there is `assert`.
alone (`assay/fluidity/should`), and `is` and `will` are available the
same way (`assay/fluidity/is`, `assay/fluidity/will`).

#### AE

The AE notation, from the [AE](https://github.com/rubyworks/ae) gem,
needs no vocabulary of its own: call any method of the object under
test through `assert`, and its result is asserted.

require 'assay/ae'

x.assert == y
a.assert.include?(e)
x.refute.nil?
"Daffy Duck".assert.start_with?("Daffy")

Where Assay has a class for the method, as it does for the first three,
that class is the failure raised; otherwise it is a plain `Assertion`.
`assert` still takes a truth value, or a block, as it does in Test::Unit.

assert(x > 3, "too small")
assert{ x > 3 }

`expect` tests a block's result by case equality, or that it raises.

expect ArgumentError do
Integer("x")
end

The subjunctive verbs and the `ok`/`no` checks are loaded separately.

require 'assay/ae/should' # x.should == y, x.should.be.a String
require 'assay/ae/must' # x.must == y
require 'assay/ae/check' # check{ |a, b| a == b }; ok 1, 1

AE and Fluidity both define `assert`, `should` and `must` on every
object, so load one or the other. Likewise load `assay/ae` or the AE
gem, not both: the gem stands on its own and raises only `Assertion`.

#### Bang

Bang turns a query method into an assertion: call it with `!` in place
Expand Down Expand Up @@ -227,6 +264,11 @@ Bang's failures are now assay classes where there is one, not
`Assay::Assertions`, `Assay::Matchers` and `Assay::Extensions` are as
they were.

The AE gem is not retired. `assay/ae` is the same notation built on
Assay's classes, under `Assay::AE` rather than `AE`. It leaves out
`ae/legacy` (use `assay/testunit`), `ae/pry` and AE's framework
adapters (Assay has its own, above).

### Learning More

There is more to learn about Assay, mainly the variety of assay classes
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ end

# Each notation extends Object in its own way, so each set of demos runs
# in a process of its own.
DEMOS = %w[core minitest testunit rspec fluidity bang]
DEMOS = %w[core minitest testunit rspec fluidity ae bang]

desc "Run the QED demos"
task :demo do
Expand Down
98 changes: 98 additions & 0 deletions demo/ae/01_overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# The AE Notation

AE (Assertive Expressive) is a notation designed around the concept of
an Assertor. The Assertor is an Assertion Functor, or Higher-Order
Function, which reroutes method calls while monitoring them for failing
conditions. It began as a library of its own, the AE gem, and is here
built on Assay's assertion classes.


## What It Provides

Requiring the notation

require 'assay/ae'

loads the +Assertor+ class, the method +assert+ and its antonyms
+assert!+ and +refute+, the method +expect+, and a set of core
extensions that make writing certain types of assertions easier.


## Assertion and Assertor Classes

The +Assertion+ class is at the heart of it. The +Assertion+ class
is a subclass of Exception. When an assertion is made and fails, it
is an Assertion that is raised: either an instance of Assertion
itself, or of the Assay class for the method used, each of which is
a subclass of Assertion.

expect Assertion do
msg = "my failure message"
assert false, msg
end

Like any raised exception, the last Assertion message is available
via `$!`.

(FYI, in Test::Unit the equivalent class was called +AssertionFailedError+.)

Assertions themselves are not generally used in creating tests or
behavior specifications. Rather they are used to create additional
types of assertion methods.

As mentioned above the +Assertor+ class is a type of Higher-Order
function, or Functor, which intercedes with a normal message
invocation to monitor for failed conditions, upon which is raises
Assertion exceptions.


## Assertion Methods

The three methods, +assert+, <tt>assert!</tt> and +refute+ all
return an Assertor instance when used fluidly, i.e. magic-dot
notation, higher-order notation, functor notation, whatever you
prefer to call it.

assert(Assay::AE::Assertor === assert)

Through the use of +method_missing+, the Assertor allows us to write
statements like:

1.assert == 1

If the operation evaluates to false or nil, then an Assertion error
is raised.

expect Assertion do
1.assert == 2
end

Assay has a class for `==`, so more exactly it is an EqualAssay.

expect EqualAssay do
1.assert == 2
end

The methods <tt>assert!</tt> and +refute+ are just like +assert+
expect they purport the negative condition. Patterned after Ruby's
own use of "<tt>!</tt>" as meaning +not+, <tt>assert!</tt> should be
read "assert not". While +refute+ exists for the sake of those who
find the use of a bang method for this purpose unsuited to them.


## How It Works

An Assertor essentially sits in wait for a method call (via
method_missing). When that happens it applies the method to the
original receiver, but wrapped in a clause that raises an
Assertion should the statement fail. If we wanted to be
pedantic, we could write our assertions like:

raise Assertion.new("1 != 1") unless 1 == 1

Instead of

1.assert == 1

Obviously using Assertor methods are whole lot more concise.

Loading
Loading