A while ago I invented an assertion, assert{ 2.0 }, that reflects its
expression and all intermediate values when it fails:
x = 7; y = 10; assert { x == 7 && y == 11 }
==>
Expected ((x == 7) and (y == 11)), but
(x == 7) is true
x is 7
(y == 11) is false
y is 10
Actually, that's not my assertion, it's Wrong.rb, by Alex Chaffee. His
diagnostic itself is slightly different, but note that it reflects the
variable names and values. That's the point of assert_equal() and its
ilk. You _can't_ say == in your assertion, despite the readability
benefits, because assertions must compete with debugging.
An assertion, when it fails, should work like debugging with a "local
variables watch" feature.
http://github.com/alexch/wrong
Today I successfully auditioned pytest, and it has a better version of
this technique. It leverages Python, which has different reflection
systems. Its faults look like this:
def test_function():
> assert f() == 4
E assert 3 == 4
E + where 3 = f()
Open question: What other assertion systems have user interfaces this awesome?
Oh, also, with pytest...
A> you don't need any icky { } around your statement -
just write assert! and...
B> even asserts in the middle of your code reflect their values.
Get it here: http://pytest.org/assert.html
--
Phlip
http://c2.com/cgi/wiki?ZeekLand