forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargparse.lua
More file actions
299 lines (248 loc) · 12 KB
/
Copy pathargparse.lua
File metadata and controls
299 lines (248 loc) · 12 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
config.target = 'core'
local argparse = require('argparse')
local guidm = require('gui.dwarfmode')
function test.processArgs()
local validArgs = {opt1=true, opt2=true}
expect.table_eq({}, argparse.processArgs({}, validArgs))
expect.table_eq({opt1=''}, argparse.processArgs({'-opt1'}, validArgs))
expect.table_eq({opt1=''}, argparse.processArgs({'--opt1'}, validArgs))
expect.table_eq({opt1='arg'},
argparse.processArgs({'-opt1', 'arg'}, validArgs))
expect.table_eq({opt1='arg'},
argparse.processArgs({'--opt1', 'arg'}, validArgs))
expect.table_eq({opt1='', opt2=''},
argparse.processArgs({'--opt1', '-opt2'}, validArgs))
expect.table_eq({opt1='', opt2=''},
argparse.processArgs({'--opt1', '--opt2'},validArgs))
expect.table_eq({opt1='', opt2='arg'},
argparse.processArgs({'--opt1', '-opt2', 'arg'}, validArgs))
expect.table_eq({opt1='', opt2='arg'},
argparse.processArgs({'--opt1', '--opt2', 'arg'},validArgs))
expect.table_eq({opt1={}},
argparse.processArgs({'-opt1', '[', ']'}, validArgs))
expect.table_eq({opt1={'a'}},
argparse.processArgs({'--opt1', '[', 'a', ']'}, validArgs))
expect.table_eq({opt1={'a', '[', 'nested', 'string', ']'}},
argparse.processArgs({'-opt1', '[', 'a', '[', 'nested',
'string', ']', ']'},
validArgs))
expect.table_eq({opt1='-value'},
argparse.processArgs({'-opt1', '\\-value'}, validArgs))
expect.table_eq({opt1='--value'},
argparse.processArgs({'-opt1', '\\--value'}, validArgs))
expect.table_eq({unvalidated_opt='value'},
argparse.processArgs({'-unvalidated_opt', 'value'}, nil))
expect.error_match('invalid arg',
function() argparse.processArgs({'-opt3'}, validArgs) end)
expect.error_match('duplicate arg',
function() argparse.processArgs({'-opt1', '--opt1'}, validArgs) end)
expect.error_match('error parsing arg',
function() argparse.processArgs({'justastring'}, validArgs) end)
end
function test.processArgsGetopt_happy_path()
local quiet, verbose, name
local function process(args, expected_q, expected_v, expected_n)
quiet, verbose, name = false, false, nil
local nonoptions = argparse.processArgsGetopt(args, {
{'q', handler=function() quiet = true end},
{'v', 'verbose', handler=function() verbose = true end},
{'n', 'name', hasArg=true,
handler=function(optarg) name = optarg end},
})
expect.eq(expected_q, quiet)
expect.eq(expected_v, verbose)
expect.eq(expected_n, name)
return nonoptions
end
local args = {}
expect.table_eq({}, process(args, false, false, nil))
args = {'-q'}
expect.table_eq({}, process(args, true, false, nil))
args = {'-v'}
expect.table_eq({}, process(args, false, true, nil))
args = {'--verbose'}
expect.table_eq({}, process(args, false, true, nil))
args = {'-n', 'foo'}
expect.table_eq({}, process(args, false, false, 'foo'))
args = {'-n', 'foo'}
expect.table_eq({}, process(args, false, false, 'foo'))
args = {'-nfoo'}
expect.table_eq({}, process(args, false, false, 'foo'))
args = {'--name', 'foo'}
expect.table_eq({}, process(args, false, false, 'foo'))
args = {'--name=foo'}
expect.table_eq({}, process(args, false, false, 'foo'))
args = {'-vqnfoo'}
expect.table_eq({}, process(args, true, true, 'foo'))
args = {'nonopt1', '-nfoo', 'nonopt2', '-1', '-10', '-0v'}
expect.table_eq({'nonopt1', 'nonopt2', '-1', '-10', '-0v'},
process(args, false, false, 'foo'))
args = {'nonopt1', '--', '-nfoo', '--nonopt2', 'nonopt3'}
expect.table_eq({'nonopt1', '-nfoo', '--nonopt2', 'nonopt3'},
process(args, false, false, nil))
end
function test.processArgsGetopt_action_errors()
expect.error_match('at least one of sh_opt and long_opt',
function()
argparse.processArgsGetopt({}, {{handler=function() end}})
end)
expect.error_match('option letter not found',
function() argparse.processArgsGetopt({}, {{'notoneletter'}}) end)
expect.error_match('option letter not found',
function() argparse.processArgsGetopt({}, {{function() end}}) end)
expect.error_match('long option name',
function() argparse.processArgsGetopt({}, {{'', ''}}) end)
expect.error_match('long option name',
function() argparse.processArgsGetopt({}, {{nil, ''}}) end)
expect.error_match('long option name',
function() argparse.processArgsGetopt({}, {{'a', ''}}) end)
expect.error_match('handler missing',
function() argparse.processArgsGetopt({}, {{'r'}}) end)
end
function test.processArgsGetopt_parsing_errors()
expect.error_match('Unknown option',
function() argparse.processArgsGetopt({'-abc'},
{{'a', handler=function() end}})
end,
'use undefined short option')
expect.error_match('Unknown option',
function() argparse.processArgsGetopt({'--abc'},
{{'a', handler=function() end}})
end,
'use undefined long option')
expect.error_match('Bad usage',
function() argparse.processArgsGetopt({'--ab=c'},
{{'a', 'ab', handler=function() end}})
end,
'pass value to param that does not take one')
expect.error_match('Missing value',
function() argparse.processArgsGetopt({'-a'},
{{'a', 'ab', hasArg=true,
handler=function() end}})
end,
'fail to pass value to short param that requires one')
expect.error_match('Missing value',
function() argparse.processArgsGetopt({'--ab'},
{{'a', 'ab', hasArg=true,
handler=function() end}})
end,
'fail to pass value to long param that requires one')
end
function test.processArgsGetopt_long_opt_without_short_opt()
local var = false
local nonoptions = argparse.processArgsGetopt(
{'--long'},
{{'', 'long', handler=function() var = true end}})
expect.true_(var)
expect.table_eq({}, nonoptions)
var = false
nonoptions = argparse.processArgsGetopt(
{'--long'},
{{nil, 'long', handler=function() var = true end}})
expect.true_(var)
expect.table_eq({}, nonoptions)
end
function test.stringList()
expect.table_eq({'happy', 'path'}, argparse.stringList(' happy , path'),
'ensure elements are trimmed')
expect.table_eq({'empty', '', 'elem'}, argparse.stringList('empty,,elem'),
'ensure empty elements are preserved')
expect.error_match('expected 5 elements',
function() argparse.stringList('a,b,c,d', '', 5) end,
'too few elements')
expect.error_match('expected 5 elements',
function() argparse.stringList('a,b,c,d,e,f', '', 5) end,
'too many elements')
expect.error_match('^expected',
function() argparse.stringList('', '', 5) end,
'no arg name printed when none supplied')
expect.error_match('^argname',
function() argparse.stringList('', 'argname', 5) end,
'arg name printed when supplied')
end
function test.numberList()
expect.table_eq({5, 4, 0, -1, 0.5, -0.5},
argparse.numberList(' 5,4, 0, -1 , 000.5000, -0.50'),
'happy path')
expect.error_match('invalid number',
function() argparse.numberList('1,b,3') end,
'letter not number')
expect.error_match('invalid number',
function() argparse.numberList('1,,3') end,
'blank number')
expect.error_match('invalid number',
function() argparse.numberList('1,2,') end,
'blank number at end')
expect.error_match('invalid number',
function() argparse.numberList('1-1') end,
'bad number format')
expect.error_match('^expected',
function() argparse.numberList('', '', 5) end,
'no arg name printed when none supplied')
expect.error_match('^argname',
function() argparse.numberList('', 'argname', 5) end,
'arg name printed when supplied')
end
function test.coords()
mock.patch(dfhack.maps, "isValidTilePos", mock.func(true),
function()
expect.table_eq({x=0, y=4, z=3}, argparse.coords('0,4 , 3'),
'happy path')
expect.error_match('expected non%-negative integer',
function() argparse.coords('1,-2,3') end,
'negative coordinate')
mock.patch(guidm, 'getCursorPos', mock.func({x=1, y=2, z=3}),
function()
expect.table_eq({x=1, y=2, z=3}, argparse.coords('here'))
end)
mock.patch(guidm, 'getCursorPos', mock.func(),
function()
expect.error_match('cursor is not active',
function() argparse.coords('here') end,
'inactive cursor')
end)
end)
mock.patch(dfhack.maps, "isValidTilePos", mock.func(false),
function()
expect.error_match('not on current map',
function() argparse.coords('0,4,300') end)
expect.table_eq({x=0, y=4, z=300},
argparse.coords('0,4,300', nil, true))
mock.patch(guidm, 'getCursorPos', mock.func({x=1, y=2, z=300}),
function()
expect.error_match('not on current map',
function() argparse.coords('here') end)
end)
mock.patch(guidm, 'getCursorPos', mock.func({x=1, y=2, z=300}),
function()
expect.table_eq({x=1, y=2, z=300},
argparse.coords('here', nil, true))
end)
end)
end
function test.positiveInt()
expect.eq(5, argparse.positiveInt(5))
expect.eq(5, argparse.positiveInt('5'))
expect.eq(5, argparse.positiveInt('5.0'))
expect.eq(1, argparse.positiveInt('1'))
expect.error_match('expected positive integer',
function() argparse.positiveInt('0') end)
expect.error_match('expected positive integer',
function() argparse.positiveInt('5.01') end)
expect.error_match('expected positive integer',
function() argparse.positiveInt(-1) end)
end
function test.nonnegativeInt()
expect.eq(5, argparse.nonnegativeInt(5))
expect.eq(5, argparse.nonnegativeInt('5'))
expect.eq(5, argparse.nonnegativeInt('5.0'))
expect.eq(1, argparse.nonnegativeInt('1'))
expect.eq(0, argparse.nonnegativeInt('0'))
expect.eq(0, argparse.nonnegativeInt('-0'))
expect.error_match('expected non%-negative integer',
function() argparse.nonnegativeInt('-0.01') end)
expect.error_match('expected non%-negative integer',
function() argparse.nonnegativeInt(-5) end)
expect.error_match('expected non%-negative integer',
function() argparse.nonnegativeInt(-1) end)
end