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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
-- vim:foldmethod=marker
local repl = require 'repl'
pcall(require, 'luarocks.loader')
require 'Test.More'
local utils = require 'test-utils'
plan(25)
local clone = repl:clone()
do -- basic tests {{{
local with_plugin = clone:clone()
local has_called_normal
local has_called_override
function with_plugin:foo()
has_called_normal = true
end
with_plugin:loadplugin(function()
function override:foo()
has_called_override = true
end
end)
with_plugin:foo()
ok(not has_called_normal)
ok(has_called_override)
local line_no
local _, err = pcall(function()
with_plugin:loadplugin(function()
line_no = utils.next_line_number()
function override:nonexistent()
end
end)
end)
like(err, string.format("%d: The 'nonexistent' method does not exist", line_no))
_, err = pcall(function()
with_plugin:loadplugin(function()
line_no = utils.next_line_number()
override.foo = 17
end)
end)
like(err, string.format('%d: 17 is not a function', line_no))
end -- }}}
do -- arguments tests {{{
local with_plugin = clone:clone()
local orig_args
local got_args
function with_plugin:foo(...)
orig_args = {
n = select('#', ...),
...,
}
end
with_plugin:loadplugin(function()
function override:foo(...)
got_args = {
n = select('#', ...),
...,
}
end
end)
with_plugin:foo()
is(got_args.n, 0)
ok(not orig_args)
with_plugin:foo(1, 2, 3)
is(got_args.n, 3)
is(got_args[1], 1)
is(got_args[2], 2)
is(got_args[3], 3)
ok(not orig_args)
with_plugin:foo(1, nil, nil, nil, 5)
is(got_args.n, 5)
is(got_args[1], 1)
is(got_args[2], nil)
is(got_args[3], nil)
is(got_args[4], nil)
is(got_args[5], 5)
ok(not orig_args)
end -- }}}
do -- exception tests {{{
local with_plugin = clone:clone()
local has_called_original
function with_plugin:foo()
has_called_original = true
end
with_plugin:loadplugin(function()
function override:foo()
error 'uh-oh'
end
end)
local _, err = pcall(with_plugin.foo, with_plugin)
like(err, 'uh%-oh')
ok(not has_called_original)
end -- }}}
do -- return value tests {{{
local with_plugin = clone:clone()
function with_plugin:foo()
return 17
end
function with_plugin:bar()
return 18, 19, 20
end
function with_plugin:baz()
return 1, nil, nil, nil, 5
end
with_plugin:loadplugin(function()
function override:foo()
return 18
end
function override:bar()
return 19, 20, 21
end
function override:baz()
return 1, nil, nil, 4
end
end)
local result = with_plugin:foo()
is(result, 18)
local results = utils.gather_results(with_plugin:bar())
utils.cmp_tables(results, { n = 3, 19, 20, 21 })
results = utils.gather_results(with_plugin:baz())
utils.cmp_tables(results, { n = 4, 1, nil, nil, 4 })
end -- }}}
do -- multiple advice, multiple plugins {{{
local with_plugin = clone:clone()
local calls = {}
function with_plugin:foo()
calls[#calls + 1] = 'original'
end
with_plugin:loadplugin(function()
function override:foo()
calls[#calls + 1] = 'first'
end
end)
with_plugin:loadplugin(function()
function override:foo()
calls[#calls + 1] = 'second'
end
end)
with_plugin:foo()
utils.cmp_tables(calls, { 'second' })
end
do -- multiple advice, single plugin {{{
local with_plugin = clone:clone()
local calls = {}
function with_plugin:foo()
calls[#calls + 1] = 'original'
end
with_plugin:loadplugin(function()
function override:foo()
calls[#calls + 1] = 'first'
end
function override:foo()
calls[#calls + 1] = 'second'
end
end)
with_plugin:foo()
utils.cmp_tables(calls, { 'second' })
end
|