Building a unit test framework ==> from lisp to ruby

P

Piergiuliano Bossi

On the way to learn ruby effectively, I have tried to develop the same
framework as the one described by Peter Seibel in [1]. This was also the
cause of my questions in [2].

In terms of pure framework, my ruby version is more compact (only 11
lines of code) and it looks more understandable to me (I'm not really a
lisper, anyway), although I miss the code=data feature of lisp somehow.

I know that every test expression is evaluated twice, but I don't mind
very much about it, unless the double computation can be removed without
adding new lines of code.

This simple exercise has made me appreciate ruby for its conjunction of
power and simplicity.

I'd like to have your feedback on the attached version.
Thanks, Giuliano


[1] - http://tinyurl.com/2b99d
[2] - rubytalk:92492 ==>
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/92492

--
If you want to send me an email in the address you have to write 'p',
then a dot, followed by 'bossi' at 'quinary', another dot and 'com' at last

def stack
methods = caller[3..caller.size].collect{|m| m.split(/[`']/).last}
methods.select {|m| m.index('test_') == 0}.reverse.join " "
end
def check(*tests)
tests.collect! {|t| puts "#{eval(t) ? 'pass' : 'FAIL'} ... (#{stack}) #{t.strip}"; eval(t)}
combine_results(*tests)
end
def combine_results(*tests)
tests.inject(true) {|acc, t| acc && t}
end

def test_plus
check(
"1 + 2 == 3",
"1 + 2 + 3 == 6",
"-1 + -3 == -4")
end
def test_multiply
check(
"2 * 2 == 4",
"3 * 5 == 15")
end

def test_aritmetic
combine_results(
test_plus,
test_multiply)
end

def test_math
test_aritmetic
end

result = test_math
puts "result=#{result}"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top