chromatic wrote:
On Tuesday 12 February 2008 10:55:21 chromatic wrote:
On Tuesday 12 February 2008 10:06:14 Eric Wilhelm wrote:
How will you print the assertion code without a source filter?
"Show Source on Exception" is fairly easy:
http://www.oreillynet.com/onlamp/blog/2007/10/adding_show_source_to_perl_ex
c.html
Making that work with anonymous functions is trickier, but doable.
Of course, in the case of Test::Exception, the assertion functions already
have the code reference that might throw an exception, in which case the
problem isn't even tricky.
Data::Dump::Streamer can decompile a code reference, complete with attached
lexicals. But as has been pointed out by Yuval, the real trick is to show the
value of all variables used in the block.
It's an interesting idea, but at the end of the article is the Fine Print
about how assert {2.0} doesn't quite work:
-------
When an assertion passes, Ruby only evaluates it once. However, when an
assertion fails, the module RubyNodeReflector will re-evaluate each element in
your block. (You knew there was a “gotcha”, right?;) This effect will hammer
your side-effects, and will disable boolean short-circuiting. So once again
sloppy developer tests help inspire us to write clean and decoupled code!
--------
What he puts down to sloppiness I call flexibility. I wouldn't want to
restrict all tests to only those without side-effects, too limiting. You all
know I'm a stickler for allowing any possible test.
Consider a simple test of post increment.
i = 0; # deliberately set wrong to cause a failure
assert { 1 == i++ }
When that fails, because it re-evaluates each element in the code block, you
will see something like this:
assert{ 1 == (i++) } --> false - should pass
i ==> 1
That's just my supposition, I can get assert2 to run. Ruby can't find the
installed gem. :(
--
Defender of Lexical Encapsulation