I promised to show you how the tests work. I just ginned up a trivial testing setup. Probably at some future time I’ll do something more fancy but this was enough for today:
function state_entry()
run_tests()
end
local test_correct
local test_wrong
function assert_equals(result, expected)
local correct = result == expected
if correct then
test_correct += 1
else
test_wrong += 1
print(`{result} == {expected} {correct}`)
end
end
function assert_nearly_equal(result, expected, delta)
correct = ll.Fabs(result-expected) <= delta
if correct then
test_correct += 1
else
test_wrong += 1
print(`{result} nearly_equal {expected} {correct}`)
end
end
function run_tests()
test_correct = 0
test_wrong = 0
p1 = Person:new("Janet", "V", "Rossini", 1.78, 55)
p2 = Person:new("Froggie", "T", "Gremlin", 1., 20)
assert_equals(p1:name_full(), "Janet V. Rossini")
assert_equals(p1:name_last_first(), "Rossini, Janet V.")
assert_nearly_equal(p1:bmi(), 17.35, 0.05)
assert_equals(p2:name_full(), "Froggie T. Gremlin")
assert_nearly_equal(p2:bmi(), 20, 0.001)
print(`Tests: correct {test_correct}, wrong {test_wrong}`)
end
-- simulate state_entry to get things going.
state_entry()
With those simple functions in place, my tests run every time I save the script and I get instant feedback on whether everything is working. When I work, I go in very small steps, and when I’m at my best, I create a tiny test and then make it work, repeating until I have lots of stuff done. It takes a bit more effort to set things up like this, but once it’s in place, the testing is automatic and I don’t have to print things and read them quite so much.
Clearly this code could be better, and more powerful. As SLua comes along and I learn more of it, maybe I’ll improve this simple scheme, but it’s pretty useful already and I just wrote it this morning!