forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock.lua
More file actions
157 lines (130 loc) · 3.75 KB
/
Copy pathmock.lua
File metadata and controls
157 lines (130 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
local mock = mkmodule('test_util.mock')
function _patch_impl(patches_raw, callback, restore_only)
local patches = {}
for _, v in ipairs(patches_raw) 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()
if not restore_only then
for _, p in ipairs(patches) do
p.table[p.key] = p.new_value
end
end
return callback()
end
)
end
--[[
Replaces `table[key]` with `value`, calls `callback()`, then restores the
original value of `table[key]`.
Usage:
patch(table, key, value, callback)
patch({
{table, key, value},
{table2, key2, value2},
}, callback)
]]
function mock.patch(...)
local args = {...}
local patches
local callback
if #args == 4 then
patches = {{args[1], args[2], args[3]}}
callback = args[4]
elseif #args == 2 then
patches = args[1]
callback = args[2]
else
error('expected 2 or 4 arguments')
end
return _patch_impl(patches, callback)
end
--[[
Restores the original value of `table[key]` after calling `callback()`.
Equivalent to: patch(table, key, table[key], callback)
Usage:
restore(table, key, callback)
restore({
{table, key},
{table2, key2},
}, callback)
]]
function mock.restore(...)
local args = {...}
local patches
local callback
if #args == 3 then
patches = {{args[1], args[2]}}
callback = args[3]
elseif #args == 2 then
patches = args[1]
callback = args[2]
else
error('expected 2 or 3 arguments')
end
return _patch_impl(patches, callback, true)
end
--[[
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 = {
call_count = 0,
call_args = {},
}
setmetatable(f, {
__call = function(self, ...)
self.call_count = self.call_count + 1
local args = {...}
for i,v in ipairs(args) do
if type(v) == 'table' then
-- just a shallow copy, but it offers some ability to
-- inspect original values in tables that were altered after
-- the call
args[i] = copyall(v)
end
end
table.insert(self.call_args, args)
return callback(...)
end,
})
return f
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