Runtime Assertions in Ruby

I’ve published my first gem on rubyforge: assit. It does runtime assertions for Ruby.

When I got started with Ruby, I was flabbergasted that there weren’t any runtime assertions at all. I have no idea why – even though there are one gadzillion different unit testing whatnots, a decent debugger and even a profiler. But this basic debugging tool, that has been there since the dawning days of C, is flatly missing; although it’d be even more useful in a language that doesn’t do any static checking.

So I’ve written my own little thing, and put it on for everyone to use. Getting started is easy: Just do


sudo gem install assit

and you’ll be ready to use it.

In the code, it’ll work like this:


require 'assit'

# Just some examples, there are more assertions
assit(defined?(assit)) # Yes, this is self-referential ;-)

x = 2 + 3
assit_equal(5, x, "There seems to be a problem with the addition")

foo = Foo.new
assit_kind_of(Foo, foo)

When the assertion fails, an action will be taken – at the moment you can either print to stderr, or raise an error. But I’m planning for some more actions, and you can configure which one to use.

It’s also possible to disable assit: All the assertion methods will be replaced with calls to an empty method, so that the runtime overhead is minimal.

Technical details

I decide to let assit add it’s methods to the basic Object class, so that it will be really easy to use. The drawback is that you cannot use the same method names anywhere in your code. That’s why all the methods start with “assit_” – it’s still similiar to “assert”, but unlikely to be a “real” method name. Also, it doesn’t conflict with the unit testing assertions.

The assertion can be disabled at startup. In this case it will not load the assertion methods, but create aliases to an “empty” method that just returns nil. The only overhead will be the call to the dynamically created method (which should be o.k. by Ruby standards).

If I’ve time, there will be more to come; for example I’d really like an action that automatically breaks to the debugger if an assertion fails.

Advertisements

2 thoughts on “Runtime Assertions in Ruby

  1. What’s wrong with runit/assert.rb?

    >> require “runit/assert.rb”
    => true
    >> include RUNIT::Assert
    => Object
    >> assert
    assert assert_match assert_nothing_raised
    assert_block assert_matches assert_nothing_thrown
    assert_equal assert_nil assert_operator
    assert_equal_float assert_no_exception assert_raise
    assert_exception assert_no_match assert_raises
    assert_fail assert_not_equal assert_respond_to
    assert_in_delta assert_not_match assert_same
    assert_instance_of assert_not_nil assert_send
    assert_kind_of assert_not_same assert_throws

    Like

  2. Ok, I admit that I didn’t see it before. Mainly because it’s quite impossible to find. (Even if you Google for “runit assert” you only get a whopping 350something results. Still, after five minutes of looking at it, I still don’t think it cuts it:

    * From the name I gather it’s a unit testing thing. Of course I cant be sure ’cause…
    * There is exactly zero documentation. Nada. Nil.
    * You can’t really include it in “Object” – it uses “standard” names like “assert” and will clash badly with your unit tests.
    * You can’t customize the behaviour and you can’t disable it. (Or at least, as for as I can’t say, see above)

    The latter is fine for unit tests, but not so fine if you intend if you want to leave the assert instructions in your production code.

    Like

Comments are closed.

Blog at WordPress.com.

Up ↑

%d bloggers like this: