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
8 changes: 7 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. Assay needs Ruby 2.7 or newer.
back, each loaded on its own, and Bang joins them. Assay needs Ruby 2.7 or newer.

Changes:

Expand All @@ -14,6 +14,12 @@ Changes:
gem. Its module is `Assay::RSpec`.
* Add `assay/minitest` and `assay/testunit`, the assertion methods of
those frameworks, from the Assay MiniTest and Assay TestUnit gems.
* 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`.
* 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.
* Rewrite the Minitest and Test::Unit adapters for Minitest 5 and 6 and
for test-unit 3. The old ones patched classes those frameworks no
longer have.
Expand Down
27 changes: 27 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/bang` | Query methods as assertions: `x.equal!(y)`, `x.empty!` |

Whichever you choose, a failure is an instance of the same assay class,
and any test framework that follows the
Expand Down Expand Up @@ -119,6 +120,28 @@ 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`).

#### Bang

Bang turns a query method into an assertion: call it with `!` in place
of `?`, and prefix `not_` to negate it.

require 'assay/bang'

10.kind_of!(Integer)
10.equal!(5 + 5)
10.not_equal!(20)

Where Assay has a class for the assertion, as it does for these, that
class is the failure raised. Any other query method an object has works
too, and fails with a plain `Assertion`.

10.even!
10.between!(1, 20)

For a procedure there are `raises!`, `rescues!` and `throws!`.

lambda{ raise ArgumentError }.raises!(ArgumentError)

#### RSpec

require 'assay/rspec'
Expand Down Expand Up @@ -195,8 +218,12 @@ gems are retired:
| gem `rspecial`, `require 'rspecial'` | `require 'assay/rspec'` |
| gem `assay-minitest` | `require 'assay/minitest'` |
| gem `assay-testunit` | `require 'assay/testunit'` |
| gem `bang`, `require 'bang'` | `require 'assay/bang'` |

`Fluidity` is now `Assay::Fluidity`, and `RSpecial` is `Assay::RSpec`.
Bang's failures are now assay classes where there is one, not
`Bang::Assertion`, and the query methods it added to Object (such as
`equal_to?` and `raises?`) are gone.
`Assay::Assertions`, `Assay::Matchers` and `Assay::Extensions` are as
they were.

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]
DEMOS = %w[core minitest testunit rspec fluidity bang]

desc "Run the QED demos"
task :demo do
Expand Down
220 changes: 220 additions & 0 deletions demo/bang/01_bang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
# Bang

Bang turns a query method into an assertion: call it with `!` in place
of `?`. Where Assay has a class for the assertion, that is the failure
raised.

require 'assay/bang'

## true!

true.true!

expect ::TrueAssay do
false.true!
end

false.not_true!

## false!

false.false!

expect ::FalseAssay do
true.false!
end

true.not_false!

## nil!

nil.nil!

expect ::NilAssay do
true.nil!
end

true.not_nil!

## equal!

1.equal!(1)

expect ::EqualAssay do
1.equal!(2)
end

1.not_equal!(2)

## eql!

1.eql!(1)

expect ::EqualityAssay do
1.eql!(1.0)
end

1.not_eql!(1.0)

## identical!

Ruby's built-in query method for this is `#equal?`, but obviously that conflicts
with our use of `#equal!` to test equality using `#==`. So instead we use `#identical!`
which makes more sense really.

:a.identical!(:a)

expect ::IdentityAssay do
"a".identical!("a")
end

:a.not_identical!('a')

## instance_of!

1.instance_of!(Integer)

expect ::InstanceAssay do
1.instance_of!(String)
end

1.not_instance_of!(String)

## kind_of!

1.kind_of!(Integer)

expect ::KindAssay do
1.kind_of!(String)
end

1.not_kind_of!(String)

## within!

1.within!(2, 1.5)

expect ::WithinAssay do
1.within!(2, 0.5)
end

1.not_within!(2, 0.5)

## close!

1.close!(2, 1.5)

expect ::CloseAssay do
1.close!(5, 0.5)
end

1.not_close!(5, 0.5)

## match!

"abc".match!(/a/)

expect ::MatchAssay do
"abc".match!(/x/)
end

"abc".not_match!(/g/)

## respond_to!

"string".respond_to!(:upcase)

expect ::RespondAssay do
"string".respond_to!(:not_a_method)
end

"string".not_respond_to!(:not_a_method)

## satisfy!

5.satisfy!{ |x| x > 3 }

expect ::ExecutionAssay do
5.satisfy!{ |x| x < 3 }
end

5.not_satisfy!{ |x| x < 3 }

## raises!

::ArgumentError.raised!{ raise ::ArgumentError }

An extension to Proc class can also be used.

procedure = lambda{ raise ::ArgumentError }

procedure.raises!(::ArgumentError)

expect ::RaiseAssay do
procedure.raises!(::TypeError)
end

## rescues!

::ArgumentError.rescued!{ raise ::ArgumentError }

An extension to Proc class can also be used.

procedure = lambda{ raise ::ArgumentError }

procedure.rescues!(::StandardError)

expect ::RescueAssay do
procedure.rescues!(::TypeError)
end

## throws!

:foo.thrown!{ throw :foo }

An extension to Proc class can also be used.

procedure = lambda{ throw :foo }

procedure.throws!(:foo)

expect ::ThrowAssay do
procedure.throws!(:bar)
end

## Other query methods

A query method that Assay has no class for works the same way, by
calling the object's own method. It fails with a plain `Assertion`.

10.even!
10.not_odd!
10.between!(1, 20)

expect ::Assertion do
10.odd!
end

expect ::Assertion do
10.not_between!(1, 20)
end

Hashes can be compared like anything else.

{:a=>1}.equal!({:a=>1})

expect ::EqualAssay do
{:a=>1}.equal!({:a=>2})
end

A bang method that means nothing is still a NoMethodError, and Ruby's
own bang methods are left alone.

expect ::NoMethodError do
10.no_such_thing!
end

"abc".upcase!.equal!("ABC")

10.respond_to?(:even!).true!
10.respond_to?(:no_such_thing!).false!
18 changes: 18 additions & 0 deletions demo/core/03_assertor.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,21 @@ Error messages are handle by RSpec by `#failure_message_for_should` and
assertor.failure_message_for_should(1)
assertor.failure_message_for_should_not(2)


## Hashes

A Hash under test is not mistaken for options. Only a trailing Hash
whose keys are all `:message` or `:backtrace` is taken as options.

EqualAssay.assert!({:a=>1}, {:a=>1})
EqualAssay.assert!({}, {})

expect ::EqualAssay do
EqualAssay.assert!({:a=>1}, {:a=>2})
end

EqualAssay[{:a=>1}] =~ {:a=>1}

expect ::EqualAssay do
EqualAssay.assert!(1, 2, :message=>"my message")
end
20 changes: 18 additions & 2 deletions lib/assay/assertable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def fail?(subject, *criteria, &block)
# Test the assertion, raising the exception if failing.
#
def assert!(*arguments, &block)
options = (Hash === arguments.last ? arguments.pop : {})
options = extract_options(arguments)

backtrace = options[:backtrace] || caller
message = options[:message] || assert_message(*arguments, &block)
Expand All @@ -98,7 +98,7 @@ def assert!(*arguments, &block)
# Test the inverse assertion, raising the exception if not failing.
#
def refute!(*arguments, &block)
options = (Hash === arguments.last ? arguments.pop : {})
options = extract_options(arguments)

backtrace = options[:backtrace] || caller
message = options[:message] || refute_message(*arguments, &block)
Expand Down Expand Up @@ -138,6 +138,22 @@ def refute_message(*arguments, &block)

private

#
# Take the options off the end of an argument list, if there are any.
# A trailing Hash counts as options only if every key is `:message` or
# `:backtrace`, so that a Hash under test isn't mistaken for them.
# To be certain, pass options explicitly, even if empty of use:
# `EqualAssay.assert!(a, b, :backtrace=>caller)`.
#
def extract_options(arguments)
last = arguments.last
if Hash === last && !last.empty? && last.keys.all?{ |k| k == :message || k == :backtrace }
arguments.pop
else
{}
end
end

##
## Increment global `$ASSERTION_COUNTS` variable.
##
Expand Down
8 changes: 4 additions & 4 deletions lib/assay/assertor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def assert!(subject, &block)
arguments, block = complete_criteria(subject, &block)

if @not
@assay.refute!(*arguments, &block)
@assay.refute!(*arguments, {:backtrace=>caller}, &block)
else
@assay.assert!(*arguments, &block)
@assay.assert!(*arguments, {:backtrace=>caller}, &block)
end
end

Expand All @@ -81,9 +81,9 @@ def refute!(subject, &block)
arguments, block = complete_criteria(subject, &block)

if @not
@assay.assert!(*arguments, &block)
@assay.assert!(*arguments, {:backtrace=>caller}, &block)
else
@assay.refute!(*arguments, &block)
@assay.refute!(*arguments, {:backtrace=>caller}, &block)
end
end

Expand Down
Loading
Loading