Commit 742f8f99 authored by Jamie A. Jennings's avatar Jamie A. Jennings

Now running unit tests in infix.lua for rpl_1_3 and the infix-to-prefix code. All tests passing.

parent e348b57d
......@@ -432,7 +432,50 @@ function slurp(filename)
return txt
end
function indent(str, col)
return str:gsub('\n', '\n' .. string.rep(" ", col))
end
right_column = 4
function run_unittests(rosie, test_rplx, args, filename)
io.stdout:write(filename, '\n')
local test_engine, pkgname = unittest.make_test_engine(rosie, args, filename)
if not test_engine then
io.write(indent(pkgname, right_column), "\n") -- error message
return false
end
unittest.write_test_result("File compiled successfully")
local test_lines = unittest.extract_tests_from_file(filename)
if #test_lines == 0 then
unittest.write_test_result("No tests found")
return true
end
local failures, errors, blocked, passed, total = 0, 0, 0, 0, 0
for _, line in pairs(test_lines) do
local ee, ff, bb, pp, tt, msgs = unittest.do_line(test_rplx, line, test_engine, pkgname)
errors = errors + ee
failures = failures + ff
blocked = blocked + bb
passed = passed + pp
total = total + tt
for _, msg in ipairs(msgs) do unittest.write_test_result(msg) end
end -- for each line of tests
if failures == 0 and errors == 0 and blocked == 0 then
unittest.write_test_result("All ", tostring(total), " tests passed")
else
unittest.write_test_result("Tests: ", tostring(total),
" Errors: ", tostring(errors),
" Failures: ", tostring(failures),
" Blocked: ", tostring(blocked),
" Passed: ", tostring(passed))
end
return true, errors, failures, blocked, passed, total
end
function test1(filename)
assert(test_rplx, "no test_rplx. maybe setup() not run?")
io.stdout:write("Testing " .. filename .. " ... ")
io.stdout:flush()
-- NOTE: using globals here, for ease of debugging
......@@ -447,7 +490,9 @@ function test1(filename)
else
error("load failed. inspect 'err' for details.")
end
print("(not testing match() yet).")
-- print("(not testing match() yet).")
ok, ee, ff, bb, pp, tt = run_unittests(rosie, test_rplx, {norcfile=true}, filename)
assert(ok)
end
-- -- Parse
-- local messages = {}
......@@ -509,8 +554,9 @@ function test()
end
function setup()
rpl_definition_file = "rosie/rpl_1_3"
oe = rosie.engine.new()
assert((oe:import('rosie/rpl_1_3', '.')))
assert((oe:import(rpl_definition_file, '.')))
exp = assert((oe:compile('rpl_expression')))
stmt = assert((oe:compile('rpl_statements')))
preparse = assert((oe:compile('preparse')))
......@@ -520,6 +566,10 @@ function setup()
parse_expression = rosie.env.compile.make_parse_expression(exp, infix.to_prefix)
e.compiler.parse_block = parse_block
e.compiler.parse_expression = parse_expression
unittest = import('unittest')
test_rplx = unittest.setup(oe, rpl_definition_file)
assert(test_rplx)
end
return infix
......@@ -25,15 +25,13 @@ end
local split = util.split
local function find_test_lines(str)
local num = 0
local lines = {}
for _,line in pairs(split(str, "\n")) do
for _, line in pairs(split(str, "\n")) do
if startswith(line,'-- test') or startswith(line,'--test') then
table.insert(lines, line)
num = num + 1
end
end
return num, lines
return lines
end
-- Create a fresh engine and load it up with patterns for parsing out all the
......@@ -155,9 +153,14 @@ end
function unittest.extract_tests_from_file(filename)
local f, msg = io.open(filename, 'r')
if not f then error(msg); end
local num_patterns, test_lines = find_test_lines(f:read('*a'))
local str = f:read('*a')
if not str then
f:close()
error("could not read file: " .. filename)
end
local test_lines = find_test_lines(str)
f:close()
return num_patterns, test_lines
return test_lines
end
-- Do tests of inclusion/exclusion
......@@ -335,8 +338,8 @@ function unittest.run(rosie, test_rplx, args, filename)
if args.verbose then
unittest.write_test_result("File compiled successfully")
end
local num_patterns, test_lines = unittest.extract_tests_from_file(filename)
if num_patterns == 0 then
local test_lines = unittest.extract_tests_from_file(filename)
if #test_lines == 0 then
unittest.write_test_result("No tests found")
return true
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment