-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcore_spec.lua
More file actions
146 lines (113 loc) · 3.63 KB
/
core_spec.lua
File metadata and controls
146 lines (113 loc) · 3.63 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
local helpers = dofile("spec/spec_helper.lua")
describe("cliargs::core", function()
local cli
before_each(function()
cli = require("cliargs.core")()
end)
describe('#parse', function()
context('when invoked without the arguments table', function()
local global_arg
before_each(function()
global_arg = _G['arg']
end)
after_each(function()
_G['arg'] = global_arg
end)
it('uses the global _G["arg"] one', function()
_G["arg"] = {"--quiet"}
cli:option('--quiet', '...')
assert.equal(cli:parse().quiet, true)
end)
end)
it('does not mutate the argument table', function()
local arguments = { "--quiet" }
cli:option('--quiet', '...')
cli:parse(arguments)
assert.equal(#arguments, 1)
assert.equal(arguments[1], "--quiet")
end)
it("returns error strings but does not print them to STDOUT", function()
local res, err = cli:parse({ "arg1" })
assert.equal(type(res), "nil")
assert.equal(type(err), "string")
end)
end)
describe('#parse - the --__DUMP__ special option', function()
it('dumps the state and errors out', function()
stub(cli.printer, 'print')
cli:argument('OUTPUT', '...')
cli:splat('INPUTS', '...', nil, 5)
cli:option('-c, --compress=VALUE', '...')
cli:flag('-q, --quiet', '...', true)
local _, err = cli:parse({'--__DUMP__', '/tmp/out', '/tmp/in.1', '/tmp/in.2', '/tmp/in.3' })
assert.matches('======= Provided command line =============', err)
end)
end)
describe('#redefine_default', function()
it('allows me to change the default for an optargument', function()
cli:splat('ROOT', '...', 'foo')
assert.equal(cli:parse({}).ROOT, 'foo')
cli:redefine_default('ROOT', 'bar')
assert.equal(cli:parse({}).ROOT, 'bar')
end)
it('allows me to change the default for an option', function()
cli:option('-c, --compress=VALUE', '...', 'lzma')
assert.equal(cli:parse({}).compress, 'lzma')
cli:redefine_default('compress', 'bz2')
assert.equal(cli:parse({}).compress, 'bz2')
end)
it('allows me to change the default for a flag', function()
cli:flag('-q, --quiet', '...', false)
assert.equal(cli:parse({}).quiet, false)
cli:redefine_default('quiet', true)
assert.equal(cli:parse({}).quiet, true)
end)
end)
describe('#load_defaults', function()
local args, err
before_each(function()
cli:option('-c, --compress=VALUE', '...', 'lzma')
cli:flag('-q, --quiet', '...', false)
end)
it('works', function()
cli:load_defaults({
compress = 'bz2',
quiet = true
})
args, err = cli:parse({})
assert.equal(err, nil)
assert.same(args, {
c = 'bz2',
compress = 'bz2',
q = true,
quiet = true
})
end)
context('when @strict is not true', function()
it('ignores keys that could not be mapped', function()
cli:load_defaults({
compress = 'bz2',
quiet = true,
what = 'woot!'
})
args, err = cli:parse({})
assert.equal(err, nil)
assert.same(args, {
c = 'bz2',
compress = 'bz2',
q = true,
quiet = true
})
end)
end)
context('when @strict is true', function()
it('returns an error message if a key could not be mapped', function()
args, err = cli:load_defaults({
what = 'woot!'
}, true)
assert.equal(args, nil)
assert.equal(err, "Unrecognized option with the key 'what'")
end)
end)
end)
end)