Saturday, November 14, 2009

a little Perl spice for ruby

An insightful blog post got me envying some of the brevity of Perl, so I implemented a couple useful idioms from Test::More in Ruby (which is pretty trivial since it allows opening up a class).

We'd like to have a really terse vocabulary when describing the most common test cases:

## equality
num = 8
num.should.equal 8 # bacon
num.must_equal 8 # minitest::spec
num.should == 8 # rspec
num.is 8 # clearly superior, similar to Test::More "is a, b"

## true/false
(num == 8).should.be.true # bacon
(num == 8).ok # superior, like Test::more "ok (something)"


So, for Bacon at least, this is all that needs to be done (put in your spec_helper.rb file)

class Object
def is(arg)
should.equal(arg)
end
def ok
should.equal true
end
end


The argument against this would go, "well, it's better to be more verbose and understood". The counter-argument something like, "this still makes sense and is way easier to write, so I can write more spec's which == better code".

No comments: