|
|
@ -32,12 +32,17 @@ function _patch_impl(patches_raw, callback, restore_only)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--[[
|
|
|
|
--[[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Replaces `table[key]` with `value`, calls `callback()`, then restores the
|
|
|
|
|
|
|
|
original value of `table[key]`.
|
|
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
Usage:
|
|
|
|
patch(table, key, value, callback)
|
|
|
|
patch(table, key, value, callback)
|
|
|
|
patch({
|
|
|
|
patch({
|
|
|
|
{table, key, value},
|
|
|
|
{table, key, value},
|
|
|
|
{table2, key2, value2},
|
|
|
|
{table2, key2, value2},
|
|
|
|
}, callback)
|
|
|
|
}, callback)
|
|
|
|
|
|
|
|
|
|
|
|
]]
|
|
|
|
]]
|
|
|
|
function mock.patch(...)
|
|
|
|
function mock.patch(...)
|
|
|
|
local args = {...}
|
|
|
|
local args = {...}
|
|
|
@ -57,12 +62,18 @@ function mock.patch(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--[[
|
|
|
|
--[[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Restores the original value of `table[key]` after calling `callback()`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Equivalent to: patch(table, key, table[key], callback)
|
|
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
Usage:
|
|
|
|
restore(table, key, callback)
|
|
|
|
restore(table, key, callback)
|
|
|
|
restore({
|
|
|
|
restore({
|
|
|
|
{table, key},
|
|
|
|
{table, key},
|
|
|
|
{table2, key2},
|
|
|
|
{table2, key2},
|
|
|
|
}, callback)
|
|
|
|
}, callback)
|
|
|
|
|
|
|
|
|
|
|
|
]]
|
|
|
|
]]
|
|
|
|
function mock.restore(...)
|
|
|
|
function mock.restore(...)
|
|
|
|
local args = {...}
|
|
|
|
local args = {...}
|
|
|
@ -81,9 +92,19 @@ function mock.restore(...)
|
|
|
|
return _patch_impl(patches, callback, true)
|
|
|
|
return _patch_impl(patches, callback, true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function mock.func(...)
|
|
|
|
--[[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Returns a callable object that tracks the arguments it is called with, then
|
|
|
|
|
|
|
|
passes those arguments to `callback()`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The returned object has the following properties:
|
|
|
|
|
|
|
|
- `call_count`: the number of times the object has been called
|
|
|
|
|
|
|
|
- `call_args`: a table of function arguments (shallow-copied) corresponding
|
|
|
|
|
|
|
|
to each time the object was called
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
]]
|
|
|
|
|
|
|
|
function mock.observe_func(callback)
|
|
|
|
local f = {
|
|
|
|
local f = {
|
|
|
|
return_values = {...},
|
|
|
|
|
|
|
|
call_count = 0,
|
|
|
|
call_count = 0,
|
|
|
|
call_args = {},
|
|
|
|
call_args = {},
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -101,11 +122,36 @@ function mock.func(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.insert(self.call_args, args)
|
|
|
|
table.insert(self.call_args, args)
|
|
|
|
return table.unpack(self.return_values)
|
|
|
|
return callback(...)
|
|
|
|
end,
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
return f
|
|
|
|
return f
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--[[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Returns a callable object similar to `mock.observe_func()`, but which when
|
|
|
|
|
|
|
|
called, only returns the given `return_value`(s) with no additional side effects.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Intended for use by `patch()`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
func(return_value [, return_value2 ...])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
See `observe_func()` for a description of the return value.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The return value also has an additional `return_values` field, which is a table
|
|
|
|
|
|
|
|
of values returned when the object is called. This can be modified.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
]]
|
|
|
|
|
|
|
|
function mock.func(...)
|
|
|
|
|
|
|
|
local f
|
|
|
|
|
|
|
|
f = mock.observe_func(function()
|
|
|
|
|
|
|
|
return table.unpack(f.return_values)
|
|
|
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
f.return_values = {...}
|
|
|
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return mock
|
|
|
|
return mock
|
|
|
|