Alex Young
6/13/2007 8:04:00 AM
Hi all,
It's always bothered me having code and unit tests in different places -
keeping unit tests near the code they are testing *has* to be a good
thing, right?
Given the above, I stumbled on the following idiom:
$ cat testable.rb
module Test; module Unit; class TestCase; end; end; end
module Testable
def test_case(&block)
Class.new(Test::Unit::TestCase).module_eval &block
end
end
$ cat builtin_tests.rb
require 'testable'
class MyWorkingClass
extend Testable
def foo(a,b)
return a+b
end
test_case {
def test_foo_null
assert_equal 2, MyWorkingClass.new.foo(1,1)
end
}
end
if __FILE__ == $0
puts MyWorkingClass.new.foo(1,1)
end
$ ruby builtin_tests.rb
2
$ testrb builtin_tests.rb
Loaded suite builtin_tests.rb
Started