assert{ 2.0 }

assert{ 2.0 } is the industry's most aggressive TDD
system—for Ruby, or any other language. Each time it fails, it analyzes the
reason and presents a complete, formatted report. This makes the cause very
easy to rapidly identify and fix. assert{ 2.0 } is like a
debugger's "inspect variable" system, and it makes your TDD cycle more
effective.

Here's an example of the output diagnostic when assert{ 2.0 }
fails. The first line reflects the source of the entire assertion:

The second line, --> nil, is the return value of the asserted expression.
The next lines contain the complete source and re-evaluation of each
terminal (z) and expression (z =~ /=begon/) in the assertion's block.

The diagnostic lines are formatted to scan easily, and they use "pretty_inspect()"
to wrap complex outputs. The diagostic contains the name and value of every variable and
expression in the asserted block.

These simple techniques free your assertions from restricting patterns, such
as assert_equal() or
assert_match() (or their syntax-sugary equivalents!).
The more creative your assertions, the more elaborate their diagnostics.

Classic assert( boolean )

assert{} will pass thru to the original assert()
from Test::Unit::TestCase. When you drop require 'assert2'
into your tests, all your legacy assert() calls will still perform
correctly:

deftest_assert_classicassert_flunk/(false. is not true)|(Failed assertion)/doassertfalseendend

Error Handling

assert{} interprets program errors and decorates their diagnostics:

deftest_error_handlingassert_flunk/ZeroDivisionError: divided by 0/doassert{1/0}# would you believe some math professors frown upon that?!endend

Compound Assertions

assert{} correctly interprets and diagnoses complex boolean
expressions, so you can interrogate related variables in one expression:

deftest_error_handlingx,y=42,43assert_flunk/x\s+--> 42.*y\s+--> 43/mdo# FIXME take out the \s+assert{x==42andy==42}endend

Warning: Put Assertions on Separate Lines

assert{} works by exploiting marginal features in Ruby's internal parser.
To reflect source, assertions must find clean beginnings and endings to statements.

Do not, for example, put two assertions in one line, because the first one will
confuse the second one:

deftest_put_assertions_on_separate_linesx=42assert_flunk/not like this/do# but the outer assertion did not fail!assert('not like this'){assert{x==43}}endend

Warning: Assertions Repeat their Side-Effects

assert{} works by exploiting marginal features in Ruby's interpreter.
To reflect the value of captured expressions, they must be evaluated again.

When an assertion passes, it will only evaluate once, as a normal block. When assertions
fail, however, their expressions will evaluate twice (or more!). Their
side-effects might interfere with your diagnosis.

What about Ruby 1.8.7?

FIXME redo!

assert{ 2.0 } uses RubyNode, which works with 1.8.6 and lower.
assert{ 2.1 } uses Ripper, which is built into Ruby 1.9 and up.
Ruby 1.8.7 includes some backports from 1.9 that break the internal API that
RubyNode used. This means the assert{ 2.0 } project cannot support
a Ruby 1.8 greater than 1.8.6!