dfhack/test/library/print.lua

112 lines
2.9 KiB
Lua

2021-08-07 01:09:48 -06:00
-- 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')
2021-08-14 18:36:38 -06:00
expect.eq(0, mock_print.call_count)
2021-08-07 01:09:48 -06:00
end
function test.printall_number()
printall(10)
2021-08-14 18:36:38 -06:00
expect.eq(0, mock_print.call_count)
2021-08-07 01:09:48 -06:00
end
function test.printall_nil()
printall(nil)
2021-08-14 18:36:38 -06:00
expect.eq(0, mock_print.call_count)
2021-08-07 01:09:48 -06:00
end
function test.printall_boolean()
printall(false)
2021-08-14 18:36:38 -06:00
expect.eq(0, mock_print.call_count)
2021-08-07 01:09:48 -06:00
end
function test.printall_function()
printall(function() end)
2021-08-14 18:36:38 -06:00
expect.eq(0, mock_print.call_count)
2021-08-07 01:09:48 -06:00
end
2021-08-14 23:15:26 -06:00
local function new_int_vector()
-- create a vector of integers by cloning one from world. we do it this way
-- because we can't allocate typed vectors from lua directly.
local vector = df.global.world.busy_buildings:new()
vector:resize(0)
return vector
end
2021-08-07 01:09:48 -06:00
function test.printall_userdata()
2021-08-14 23:15:26 -06:00
local utable = new_int_vector()
dfhack.with_temp_object(utable, function()
utable:insert(0, 10)
utable:insert(1, 20)
printall(utable)
expect.eq(2, mock_print.call_count)
expect.true_(mock_print.call_args[1][1]:find('0%s+= 10'))
expect.true_(mock_print.call_args[2][1]:find('1%s+= 20'))
end)
2021-08-07 01:09:48 -06:00
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')
2021-08-14 18:36:38 -06:00
expect.eq(0, mock_print.call_count)
2021-08-07 01:09:48 -06:00
end
function test.printall_ipairs_number()
printall_ipairs(10)
2021-08-14 18:36:38 -06:00
expect.eq(0, mock_print.call_count)
2021-08-07 01:09:48 -06:00
end
function test.printall_ipairs_nil()
printall_ipairs(nil)
2021-08-14 18:36:38 -06:00
expect.eq(0, mock_print.call_count)
2021-08-07 01:09:48 -06:00
end
function test.printall_ipairs_boolean()
printall_ipairs(false)
2021-08-14 18:36:38 -06:00
expect.eq(0, mock_print.call_count)
2021-08-07 01:09:48 -06:00
end
function test.printall_ipairs_function()
printall_ipairs(function() end)
2021-08-14 18:36:38 -06:00
expect.eq(0, mock_print.call_count)
2021-08-07 01:09:48 -06:00
end
function test.printall_ipairs_userdata()
2021-08-14 23:15:26 -06:00
local utable = new_int_vector()
dfhack.with_temp_object(utable, function()
utable:insert(0, 10)
utable:insert(1, 20)
printall_ipairs(utable)
expect.eq(2, mock_print.call_count)
expect.true_(mock_print.call_args[1][1]:find('0%s+= 10'))
expect.true_(mock_print.call_args[2][1]:find('1%s+= 20'))
end)
2021-08-07 01:09:48 -06:00
end
function test.printall_recurse()
-- TODO
end