Advertisement
|
_why just
blogged
about my xmp code annotator for Ruby
and shot the following idea
You know what would be wild? To use this as a way of writing test cases. You
write the code that needs to be tested and annotate it with the asserts.
So here is a rough implementation... the enhanced^3 xmp filter will now turn
require 'test/unit'
class Foo
class StillTooSmallWheresYourManhoodDude < Exception; end
attr_reader :b
def initialize; @a = [] end
def foo(x); @a << x end
def bar; @a.join(" ") end
def baz; @a.size end
def foobar; raise StillTooSmallWheresYourManhoodDude unless @a.size > 6; "OK" end
end
class TestFoo < Test::Unit::TestCase
def setup; @f = Foo.new end
def test_basic_foo
@f.foo 1
@f.bar # =>
@f.baz # =>
@f.b # =>
end
def test_more_foo
@f.foobar # =>
10.times{|i| @f.foo i }
@f.bar # =>
@f.foobar # =>
end
end
into
require 'test/unit'
class Foo
class StillTooSmallWheresYourManhoodDude < Exception; end
attr_reader :b
def initialize; @a = [] end
def foo(x); @a << x end
def bar; @a.join(" ") end
def baz; @a.size end
def foobar; raise StillTooSmallWheresYourManhoodDude unless @a.size > 6; "OK" end
end
class TestFoo < Test::Unit::TestCase
def setup; @f = Foo.new end
def test_basic_foo
@f.foo 1
assert_equal("1", @f.bar)
assert_equal(1, @f.baz)
assert_nil(@f.b)
end
def test_more_foo
assert_raise(Foo::StillTooSmallWheresYourManhoodDude){@f.foobar}
10.times{|i| @f.foo i }
assert_equal("0 1 2 3 4 5 6 7 8 9", @f.bar)
assert_equal("OK", @f.foobar)
end
end
# >> "Loaded suite -\nStarted\n..\nFinished in 0.000819 seconds.\n\n2 tests, 0 assertions, 0 failures, 0 errors\n"
Read more...
Read: xmp redux: expanding test assertions for profit