# Assert{ 2.0 } available with assert\_xpath

**URL:** <https://www.ruby-forum.com/t/assert-2-0-available-with-assert-xpath/127826>\
**Category:** Rails\
**Created:** [February 1, 2008, 5:00pm UTC](https://www.ruby-forum.com/t/assert-2-0-available-with-assert-xpath/127826 "2008-02-01T17:00:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [February 1, 2008, 5:00pm UTC](https://www.ruby-forum.com/t/assert-2-0-available-with-assert-xpath/127826/1 "2008-02-01T17:00:55Z")

</div>

Railsers:

My assert{ 2.0 } project will soon have its own package. It’s available  
now as a  
“technology preview”, bundled with assert\_xpath.

It’s better than the classic test assertions because when it fails it  
prints out  
its complete expression, and the intermediate value of each of its  
variables.

Install its dependency rubynode with it, like this:

gem install rubynode  
piston import [yar\_wiki - Revision 475: /](http://phlip.svnrepository.com/svn/yar_wiki/)  
vendor/plugins

The rest of assert\_xpath (and its yar\_wiki vehicle) are documented here:

[http://assertxpath.rubyforge.org/](http://assertxpath.rubyforge.org/)

(Non-Rails uses can toss these two files into their library paths…

[http://phlip.svnrepository.com/svn/yar\_wiki/lib/assert\_2\_0.rb](http://phlip.svnrepository.com/svn/yar_wiki/lib/assert_2_0.rb)  
[http://phlip.svnrepository.com/svn/yar\_wiki/lib/ruby\_reflector.rb](http://phlip.svnrepository.com/svn/yar_wiki/lib/ruby_reflector.rb)

…then include Assert2\_0 into their test suites to use the library.)

assert{ 2.0 } is for any Test::Unit::TestCase, not just Rails; that’s  
why I will  
repackage it soon. Its source files are already portable; their only  
dependencies are Ruby and rubynode.

To use it, simply replace any of your calls to assert, assert\_equal,  
assert\_match, assert\_kind\_of, assert\_operator, assert\_not\_nil, etc, in  
your  
developer tests. Replace them with assert{ raw ruby equivalent } (and  
replace  
assertions like assert\_nil, assert\_no\_match, assert\_not\_equal with  
assert{}'s  
evil twin, deny{}).

You can write whatever you like inside - including sick complex  
expressions -  
so long as their last statement return a positive (or negative) value.

The classic assertions only exist for one reason - to print out their  
values  
when they fail. And then they don’t even reflect their variable names,  
either.

When assert{} fails, it prints its complete expression, with each  
intermediate term and its value, like this:

assert{ “a topic” == ( topics[“first”] ) } → false  
topics → {“first”=\>“wrong topic”}  
topics[“first”] → “wrong topic”

The library is not published because I have not checked every single  
Ruby  
op\_code off my list yet. (I had no idea there were so many!) If I’m  
missing  
one, you get a clean error message that RubyReflector does not have a  
method  
“\_foo”, where “foo” is one of the opcode prefixes from Ruby’s internal  
token  
table. If that happens to you (simplify your assertion!), and report  
“foo” to  
me, so I can move it to the top of my do-list.

Warning: When the assertion succeeds, it only evaluates its expression  
once. But  
when it fails, it evaluates the expression several more times, to  
extract the  
intermediate values. This will defeat boolean short-circuiting, and will  
hammer  
any side-effects in your methods. As usual, sloppy testing requires  
decoupled  
code, but I can’t fix these without tweaking Ruby’s evaluation kernel!

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [February 1, 2008, 8:13pm UTC](https://www.ruby-forum.com/t/assert-2-0-available-with-assert-xpath/127826/2 "2008-02-01T20:13:56Z")

</div>

On Feb 1, 2008, at 07:59 , Phlip wrote:

> My assert{ 2.0 } project will soon have its own package. It’s  
> available now as a “technology preview”, bundled with assert\_xpath.

please stop emailing ruby-core about this package. ruby-talk is much  
more appropriate.
