unit tests first draft

develop
myk002 2021-08-07 00:09:48 -07:00
parent 9fc71ef6e1
commit 3f2795e80a
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
2 changed files with 100 additions and 3 deletions

@ -172,18 +172,18 @@ local function safe_iterate(table, iterate_fn, elem_cb)
end
local function print_element(k, v)
print(string.format("%-23s\t = %s", tostring(k), tostring(v)))
dfhack.println(string.format("%-23s\t = %s", tostring(k), tostring(v)))
end
function printall(table)
if not safe_iterate(table, pairs, print_element) then
print(tostring(table))
dfhack.println(tostring(table))
end
end
function printall_ipairs(table)
if not safe_iterate(table, ipairs, print_element) then
print(tostring(table))
dfhack.println(tostring(table))
end
end

@ -0,0 +1,97 @@
-- tests print-related functions added by dfhack.lua
local dfhack = dfhack
local mock_print = mock.func()
local function test_wrapper(test_fn)
mock.patch({{dfhack, 'print', mock_print},
{dfhack, 'println', mock_print}},
test_fn)
mock_print = mock.func()
end
config.wrapper = test_wrapper
function test.printall_table()
printall({a='b'})
expect.eq(1, mock_print.call_count)
expect.true_(mock_print.call_args[1][1]:find('a%s+= b'))
end
function test.printall_string()
printall('a')
expect.eq(1, mock_print.call_count)
expect.eq('a', mock_print.call_args[1][1])
end
function test.printall_number()
printall(10)
expect.eq(1, mock_print.call_count)
expect.eq('10', mock_print.call_args[1][1])
end
function test.printall_nil()
printall(nil)
expect.eq(1, mock_print.call_count)
expect.eq('nil', mock_print.call_args[1][1])
end
function test.printall_boolean()
printall(false)
expect.eq(1, mock_print.call_count)
expect.eq('false', mock_print.call_args[1][1])
end
function test.printall_function()
printall(function() end)
expect.eq(1, mock_print.call_count)
expect.true_(mock_print.call_args[1][1]:find('^function: 0x'))
end
function test.printall_userdata()
-- TODO
end
function test.printall_ipairs_table()
printall_ipairs({'a', 'b'})
expect.eq(2, mock_print.call_count)
expect.true_(mock_print.call_args[1][1]:find('1%s+= a'))
expect.true_(mock_print.call_args[2][1]:find('2%s+= b'))
end
function test.printall_ipairs_string()
printall_ipairs('a')
expect.eq(1, mock_print.call_count)
expect.eq('a', mock_print.call_args[1][1])
end
function test.printall_ipairs_number()
printall_ipairs(10)
expect.eq(1, mock_print.call_count)
expect.eq('10', mock_print.call_args[1][1])
end
function test.printall_ipairs_nil()
printall_ipairs(nil)
expect.eq(1, mock_print.call_count)
expect.eq('nil', mock_print.call_args[1][1])
end
function test.printall_ipairs_boolean()
printall_ipairs(false)
expect.eq(1, mock_print.call_count)
expect.eq('false', mock_print.call_args[1][1])
end
function test.printall_ipairs_function()
printall_ipairs(function() end)
expect.eq(1, mock_print.call_count)
expect.true_(mock_print.call_args[1][1]:find('^function: 0x'))
end
function test.printall_ipairs_userdata()
-- TODO
end
function test.printall_recurse()
-- TODO
end