2020-03-24 23:43:09 -06:00
|
|
|
local json = require 'json'
|
2018-07-19 09:26:38 -06:00
|
|
|
local script = require 'gui.script'
|
2018-07-19 10:40:25 -06:00
|
|
|
local utils = require 'utils'
|
2018-07-19 09:26:38 -06:00
|
|
|
|
2018-07-18 12:29:13 -06:00
|
|
|
local args = {...}
|
|
|
|
local done_command = args[1]
|
|
|
|
|
2020-03-31 22:26:51 -06:00
|
|
|
local CONFIG_FILE = 'test_config.json'
|
2020-03-24 23:43:09 -06:00
|
|
|
local STATUS_FILE = 'test_status.json'
|
|
|
|
local TestStatus = {
|
|
|
|
PENDING = 'pending',
|
|
|
|
PASSED = 'passed',
|
|
|
|
FAILED = 'failed',
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:34:44 -06:00
|
|
|
local VALID_MODES = utils.invert{'none', 'fortress'}
|
|
|
|
|
2018-07-18 12:29:13 -06:00
|
|
|
expect = {}
|
2018-07-19 10:40:25 -06:00
|
|
|
function expect.true_(value, comment)
|
|
|
|
return not not value, comment, 'expected true'
|
2018-07-18 12:29:13 -06:00
|
|
|
end
|
2018-07-19 10:40:25 -06:00
|
|
|
function expect.false_(value, comment)
|
|
|
|
return not value, comment, 'expected false'
|
2018-07-18 12:29:13 -06:00
|
|
|
end
|
2018-07-19 10:40:25 -06:00
|
|
|
function expect.eq(a, b, comment)
|
|
|
|
return a == b, comment, ('%s ~= %s'):format(a, b)
|
2018-07-18 12:29:13 -06:00
|
|
|
end
|
2018-07-19 10:40:25 -06:00
|
|
|
function expect.ne(a, b, comment)
|
|
|
|
return a ~= b, comment, ('%s == %s'):format(a, b)
|
2018-07-18 12:29:13 -06:00
|
|
|
end
|
2020-03-27 00:01:03 -06:00
|
|
|
function expect.lt(a, b, comment)
|
|
|
|
return a < b, comment, ('%s >= %s'):format(a, b)
|
|
|
|
end
|
|
|
|
function expect.le(a, b, comment)
|
|
|
|
return a <= b, comment, ('%s > %s'):format(a, b)
|
|
|
|
end
|
|
|
|
function expect.gt(a, b, comment)
|
|
|
|
return a > b, comment, ('%s <= %s'):format(a, b)
|
|
|
|
end
|
|
|
|
function expect.ge(a, b, comment)
|
|
|
|
return a >= b, comment, ('%s < %s'):format(a, b)
|
|
|
|
end
|
2020-03-26 23:38:40 -06:00
|
|
|
function expect.table_eq(a, b, comment)
|
|
|
|
local checked = {}
|
|
|
|
for k, v in pairs(a) do
|
|
|
|
if a[k] ~= b[k] then
|
|
|
|
return false, comment, ('key "%s": %s ~= %s'):format(k, a[k], b[k])
|
|
|
|
end
|
|
|
|
checked[k] = true
|
|
|
|
end
|
|
|
|
for k in pairs(b) do
|
|
|
|
if not checked[k] then
|
|
|
|
return false, comment, ('key "%s": %s ~= %s'):format(k, a[k], b[k])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2018-07-18 12:29:13 -06:00
|
|
|
function expect.error(func, ...)
|
|
|
|
local ok, ret = pcall(func, ...)
|
|
|
|
if ok then
|
|
|
|
return false, 'no error raised by function call'
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-19 09:26:38 -06:00
|
|
|
function delay(frames)
|
|
|
|
frames = frames or 1
|
|
|
|
script.sleep(frames, 'frames')
|
|
|
|
end
|
|
|
|
|
2020-03-31 22:26:51 -06:00
|
|
|
function load_test_config(config_file)
|
|
|
|
local config = {}
|
|
|
|
if dfhack.filesystem.isfile(config_file) then
|
|
|
|
config = json.decode_file(config_file)
|
|
|
|
end
|
|
|
|
|
|
|
|
if not config.test_dir then
|
|
|
|
config.test_dir = dfhack.getHackPath() .. 'scripts/test'
|
|
|
|
end
|
|
|
|
if not dfhack.filesystem.isdir(config.test_dir) then
|
|
|
|
error('Invalid test folder: ' .. config.test_dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
return config
|
|
|
|
end
|
|
|
|
|
2018-07-18 12:29:13 -06:00
|
|
|
function build_test_env()
|
|
|
|
local env = {
|
2018-07-19 10:40:25 -06:00
|
|
|
test = utils.OrderedTable(),
|
2020-03-26 22:34:44 -06:00
|
|
|
config = {
|
|
|
|
mode = 'none',
|
|
|
|
},
|
2018-07-18 12:29:13 -06:00
|
|
|
expect = {},
|
2018-07-19 09:26:38 -06:00
|
|
|
delay = delay,
|
2018-07-18 12:29:13 -06:00
|
|
|
}
|
|
|
|
local private = {
|
|
|
|
checks = 0,
|
|
|
|
checks_ok = 0,
|
|
|
|
}
|
|
|
|
for name, func in pairs(expect) do
|
|
|
|
env.expect[name] = function(...)
|
|
|
|
private.checks = private.checks + 1
|
2018-07-19 10:40:25 -06:00
|
|
|
local ret = {func(...)}
|
|
|
|
local ok = table.remove(ret, 1)
|
|
|
|
local msg = ''
|
|
|
|
for _, part in pairs(ret) do
|
|
|
|
if part then
|
|
|
|
msg = msg .. ': ' .. tostring(part)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
msg = msg:sub(3) -- strip leading ': '
|
2018-07-18 12:29:13 -06:00
|
|
|
if ok then
|
|
|
|
private.checks_ok = private.checks_ok + 1
|
|
|
|
else
|
|
|
|
dfhack.printerr('Check failed! ' .. (msg or '(no message)'))
|
|
|
|
local info = debug.getinfo(2)
|
|
|
|
dfhack.printerr((' at %s:%d'):format(info.short_src, info.currentline))
|
|
|
|
print('')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
setmetatable(env, {__index = _G})
|
|
|
|
return env, private
|
|
|
|
end
|
|
|
|
|
2020-03-31 22:26:51 -06:00
|
|
|
function get_test_files(test_dir)
|
2018-07-18 12:29:13 -06:00
|
|
|
local files = {}
|
2020-03-31 22:26:51 -06:00
|
|
|
for _, entry in ipairs(dfhack.filesystem.listdir_recursive(test_dir)) do
|
2018-07-18 12:29:13 -06:00
|
|
|
if not entry.isdir and not entry.path:match('main.lua') then
|
|
|
|
table.insert(files, entry.path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(files)
|
|
|
|
return files
|
|
|
|
end
|
|
|
|
|
2020-03-24 23:43:09 -06:00
|
|
|
function load_test_status()
|
|
|
|
if dfhack.filesystem.isfile(STATUS_FILE) then
|
|
|
|
return json.decode_file(STATUS_FILE)
|
|
|
|
end
|
2018-02-04 14:00:53 -07:00
|
|
|
end
|
|
|
|
|
2020-03-24 23:43:09 -06:00
|
|
|
function save_test_status(status)
|
|
|
|
json.encode_file(status, STATUS_FILE)
|
|
|
|
end
|
|
|
|
|
|
|
|
function finish_tests()
|
2018-07-18 12:29:13 -06:00
|
|
|
if done_command then
|
|
|
|
dfhack.run_command(done_command)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-26 21:26:43 -06:00
|
|
|
function load_tests(file, tests)
|
|
|
|
local short_filename = file:sub(file:find('test'), -1)
|
|
|
|
print('Loading file: ' .. short_filename)
|
|
|
|
local env, env_private = build_test_env()
|
|
|
|
local code, err = loadfile(file, 't', env)
|
|
|
|
if not code then
|
|
|
|
dfhack.printerr('Failed to load file: ' .. tostring(err))
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
local ok, err = pcall(code)
|
|
|
|
if not ok then
|
|
|
|
dfhack.printerr('Error when running file: ' .. tostring(err))
|
|
|
|
return false
|
|
|
|
else
|
2020-03-26 22:34:44 -06:00
|
|
|
if not VALID_MODES[env.config.mode] then
|
|
|
|
dfhack.printerr('Invalid config.mode: ' .. env.config.mode)
|
|
|
|
return false
|
|
|
|
end
|
2020-03-26 21:26:43 -06:00
|
|
|
for name, test_func in pairs(env.test) do
|
|
|
|
local test_data = {
|
|
|
|
full_name = short_filename .. ':' .. name,
|
|
|
|
func = test_func,
|
|
|
|
private = env_private,
|
2020-03-26 22:34:44 -06:00
|
|
|
config = env.config,
|
2020-03-26 21:26:43 -06:00
|
|
|
}
|
|
|
|
test_data.name = test_data.full_name:gsub('test/', ''):gsub('.lua', '')
|
|
|
|
table.insert(tests, test_data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function run_test(test, status, counts)
|
2020-03-26 22:49:34 -06:00
|
|
|
test.private.checks = 0
|
|
|
|
test.private.checks_ok = 0
|
|
|
|
counts.tests = counts.tests + 1
|
|
|
|
local ok, err = pcall(test.func)
|
|
|
|
local passed = false
|
|
|
|
if not ok then
|
|
|
|
dfhack.printerr('test errored: ' .. test.name .. ': ' .. tostring(err))
|
|
|
|
elseif test.private.checks ~= test.private.checks_ok then
|
|
|
|
dfhack.printerr('test failed: ' .. test.name)
|
2020-03-26 21:26:43 -06:00
|
|
|
else
|
2020-03-26 22:49:34 -06:00
|
|
|
print('test passed: ' .. test.name)
|
|
|
|
passed = true
|
|
|
|
counts.tests_ok = counts.tests_ok + 1
|
2020-03-26 21:26:43 -06:00
|
|
|
end
|
2020-03-26 22:49:34 -06:00
|
|
|
counts.checks = counts.checks + (tonumber(test.private.checks) or 0)
|
|
|
|
counts.checks_ok = counts.checks_ok + (tonumber(test.private.checks_ok) or 0)
|
|
|
|
return passed
|
2020-03-26 21:26:43 -06:00
|
|
|
end
|
|
|
|
|
2018-07-18 12:29:13 -06:00
|
|
|
function main()
|
2020-03-31 22:26:51 -06:00
|
|
|
local config = load_test_config(CONFIG_FILE)
|
|
|
|
local files = get_test_files(config.test_dir)
|
2018-07-18 12:29:13 -06:00
|
|
|
|
|
|
|
local counts = {
|
|
|
|
tests = 0,
|
|
|
|
tests_ok = 0,
|
|
|
|
checks = 0,
|
|
|
|
checks_ok = 0,
|
|
|
|
file_errors = 0,
|
|
|
|
}
|
|
|
|
local passed = true
|
|
|
|
|
2018-07-19 09:26:38 -06:00
|
|
|
print('Looking for title screen...')
|
|
|
|
for i = 0, 100 do
|
|
|
|
local scr = dfhack.gui.getCurViewscreen()
|
|
|
|
if df.viewscreen_titlest:is_instance(scr) then
|
|
|
|
print('Found title screen')
|
|
|
|
break
|
|
|
|
else
|
|
|
|
scr:feed_key(df.interface_key.LEAVESCREEN)
|
|
|
|
delay(10)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not df.viewscreen_titlest:is_instance(dfhack.gui.getCurViewscreen()) then
|
|
|
|
qerror('Could not find title screen')
|
|
|
|
end
|
|
|
|
|
2020-03-24 23:43:09 -06:00
|
|
|
print('Loading tests')
|
|
|
|
local tests = {}
|
2018-07-18 12:29:13 -06:00
|
|
|
for _, file in ipairs(files) do
|
2020-03-26 21:26:43 -06:00
|
|
|
if not load_tests(file, tests) then
|
2018-07-18 12:29:13 -06:00
|
|
|
passed = false
|
|
|
|
counts.file_errors = counts.file_errors + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-26 22:49:34 -06:00
|
|
|
print('Filtering tests')
|
2020-03-24 23:43:09 -06:00
|
|
|
local status = load_test_status() or {}
|
2020-03-26 22:49:34 -06:00
|
|
|
for i = #tests, 1, -1 do
|
|
|
|
local test = tests[i]
|
2020-03-24 23:43:09 -06:00
|
|
|
if not status[test.full_name] then
|
|
|
|
status[test.full_name] = TestStatus.PENDING
|
2020-03-26 22:49:34 -06:00
|
|
|
elseif status[test.full_name] ~= TestStatus.PENDING then
|
|
|
|
print('skipping test: ' .. test.name .. ': state = ' .. status[test.full_name] .. ')')
|
|
|
|
table.remove(tests, i)
|
2020-03-24 23:43:09 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-26 22:49:34 -06:00
|
|
|
print('Running ' .. #tests .. ' tests')
|
2020-03-24 23:43:09 -06:00
|
|
|
for _, test in pairs(tests) do
|
2020-03-26 22:49:34 -06:00
|
|
|
local passed = run_test(test, status, counts)
|
|
|
|
status[test.full_name] = passed and TestStatus.PASSED or TestStatus.FAILED
|
|
|
|
save_test_status(status)
|
2020-03-24 23:43:09 -06:00
|
|
|
end
|
|
|
|
|
2018-07-18 12:29:13 -06:00
|
|
|
print('\nTest summary:')
|
|
|
|
print(('%d/%d tests passed'):format(counts.tests_ok, counts.tests))
|
|
|
|
print(('%d/%d checks passed'):format(counts.checks_ok, counts.checks))
|
|
|
|
print(('%d test files failed to load'):format(counts.file_errors))
|
|
|
|
end
|
|
|
|
|
2020-03-26 21:33:39 -06:00
|
|
|
script.start(function()
|
|
|
|
dfhack.with_finalize(finish_tests, main)
|
|
|
|
end)
|