Merge remote-tracking branch 'lethosor/test-mocks' into develop

develop
lethosor 2021-04-10 16:12:33 -04:00
commit 855ab1a0a3
No known key found for this signature in database
GPG Key ID: 76A269552F4F58C1
3 changed files with 220 additions and 12 deletions

@ -3,6 +3,7 @@
local expect = require 'test_util.expect' local expect = require 'test_util.expect'
local json = require 'json' local json = require 'json'
local mock = require 'test_util.mock'
local script = require 'gui.script' local script = require 'gui.script'
local utils = require 'utils' local utils = require 'utils'
@ -187,6 +188,7 @@ local function build_test_env()
mode = 'none', mode = 'none',
}, },
expect = {}, expect = {},
mock = mock,
delay = delay, delay = delay,
require = clean_require, require = clean_require,
reqscript = clean_reqscript, reqscript = clean_reqscript,
@ -279,19 +281,20 @@ local function sort_tests(tests)
end end
local function wrap_test(func) local function wrap_test(func)
local saved_printerr, saved_run_script = dfhack.printerr, dfhack.run_script local saved_printerr = dfhack.printerr
local printerr_called = false local printerr_called = false
dfhack.printerr = function(msg) local printerr_wrapper = function(msg)
if msg == nil then return end if msg == nil then return end
saved_printerr(msg) saved_printerr(msg)
printerr_called = true printerr_called = true
end end
dfhack.run_script = clean_run_script
return dfhack.with_finalize( return mock.patch(
function() {
dfhack.printerr = saved_printerr {dfhack, 'printerr', printerr_wrapper},
dfhack.run_script = saved_run_script {dfhack, 'run_script', clean_run_script},
end, {dfhack, 'reqscript', clean_reqscript},
},
function() function()
local ok, err = pcall(func) local ok, err = pcall(func)
if printerr_called then if printerr_called then

@ -0,0 +1,69 @@
local mock = mkmodule('test_util.mock')
--[[
Usage:
patch(table, key, value, callback)
patch({
{table, key, value},
{table2, key2, value2}
}, callback)
]]
function mock.patch(...)
local args = {...}
if #args == 4 then
args = {{
{args[1], args[2], args[3]}
}, args[4]}
end
if #args ~= 2 then
error('expected 2 or 4 arguments')
end
local callback = args[2]
local patches = {}
for _, v in ipairs(args[1]) do
local p = {
table = v[1],
key = v[2],
new_value = v[3],
}
p.old_value = p.table[p.key]
-- no-op to ensure that the value can be restored by the finalizer below
p.table[p.key] = p.old_value
table.insert(patches, p)
end
return dfhack.with_finalize(
function()
for _, p in ipairs(patches) do
p.table[p.key] = p.old_value
end
end,
function()
for _, p in ipairs(patches) do
p.table[p.key] = p.new_value
end
return callback()
end
)
end
function mock.func(return_value)
local f = {
return_value = return_value,
call_count = 0,
call_args = {},
}
setmetatable(f, {
__call = function(self, ...)
self.call_count = self.call_count + 1
table.insert(self.call_args, {...})
return self.return_value
end,
})
return f
end
return mock

@ -0,0 +1,136 @@
local mock = require('test_util.mock')
local test_table = {
func1 = function()
return 1
end,
func2 = function()
return 2
end,
}
function test.patch_single()
expect.eq(test_table.func1(), 1)
mock.patch(test_table, 'func1', function() return 3 end, function()
expect.eq(test_table.func1(), 3)
end)
expect.eq(test_table.func1(), 1)
end
function test.patch_single_nested()
expect.eq(test_table.func1(), 1)
mock.patch(test_table, 'func1', function() return 3 end, function()
expect.eq(test_table.func1(), 3)
mock.patch(test_table, 'func1', function() return 5 end, function()
expect.eq(test_table.func1(), 5)
end)
expect.eq(test_table.func1(), 3)
end)
expect.eq(test_table.func1(), 1)
end
function test.patch_multiple()
expect.eq(test_table.func1(), 1)
expect.eq(test_table.func2(), 2)
mock.patch({
{test_table, 'func1', function() return 3 end},
{test_table, 'func2', function() return 4 end},
}, function()
expect.eq(test_table.func1(), 3)
expect.eq(test_table.func2(), 4)
end)
expect.eq(test_table.func1(), 1)
expect.eq(test_table.func2(), 2)
end
function test.patch_nil_old_value()
local t = {}
mock.patch(t, 1, 2, function()
expect.eq(t[1], 2)
end)
expect.eq(t[1], nil)
expect.eq(#t, 0)
end
function test.patch_nil_new_value()
local t = {2}
mock.patch(t, 1, nil, function()
expect.eq(t[1], nil)
expect.eq(#t, 0)
end)
expect.eq(t[1], 2)
end
function test.patch_nil_key()
local called = false
expect.error_match('table index is nil', function()
mock.patch({}, nil, 'value', function()
called = true
end)
end)
expect.false_(called)
end
function test.patch_nil_table()
local called = false
expect.error(function()
mock.patch(nil, 1, 2, function()
called = true
end)
end)
expect.false_(called)
end
function test.patch_complex_key()
local key = {'key'}
local t = {[key] = 'value'}
expect.eq(t[key], 'value')
mock.patch(t, key, 2, function()
expect.eq(t[key], 2)
end)
expect.eq(t[key], 'value')
end
function test.patch_callback_return_value()
local a, b = mock.patch({}, 'k', 'v', function()
return 3, 4
end)
expect.eq(a, 3)
expect.eq(b, 4)
end
function test.func_call_count()
local f = mock.func()
expect.eq(f.call_count, 0)
f()
expect.eq(f.call_count, 1)
f()
expect.eq(f.call_count, 2)
end
function test.func_call_args()
local f = mock.func()
expect.eq(#f.call_args, 0)
f()
f(7)
expect.eq(#f.call_args, 2)
expect.eq(#f.call_args[1], 0)
expect.eq(#f.call_args[2], 1)
expect.eq(f.call_args[2][1], 7)
end
function test.func_call_args_nil()
local f = mock.func()
f(nil)
f(2, nil, 4)
expect.table_eq(f.call_args[1], {nil})
expect.table_eq(f.call_args[2], {2, nil, 4})
expect.eq(#f.call_args[2], 3)
end
function test.func_call_return_value()
local f = mock.func(7)
expect.eq(f(), 7)
f.return_value = 9
expect.eq(f(), 9)
end