1 | chai = require('chai')
|
2 | expect = chai.expect
|
3 | Option = require('../lib/option')
|
4 | Signature = require('../lib/signature')
|
5 |
|
6 | describe 'Option:', ->
|
7 |
|
8 | describe '#constructor()', ->
|
9 |
|
10 | describe 'signature option', ->
|
11 |
|
12 | it 'should throw an error if no signature', ->
|
13 | expect ->
|
14 | new Option()
|
15 | .to.throw(Error)
|
16 |
|
17 | it 'should throw an error if is of type boolean and has parameter', ->
|
18 | expect ->
|
19 | new Option
|
20 | signature: new Signature('quiet')
|
21 | parameter: 'hello'
|
22 | boolean: true
|
23 | .to.throw(Error)
|
24 |
|
25 | it 'should throw an error if it contains a parameter in the signature', ->
|
26 | expect ->
|
27 | new Option
|
28 | signature: new Signature('quiet <hello>')
|
29 | .to.throw(Error)
|
30 |
|
31 | describe 'alias option', ->
|
32 |
|
33 | it 'should throw an error if not array or string', ->
|
34 | expect ->
|
35 | new Option
|
36 | signature: new Signature('quiet')
|
37 | alias: { q: true }
|
38 | .to.throw(Error)
|
39 |
|
40 | describe 'boolean option', ->
|
41 |
|
42 | it 'should default to false', ->
|
43 | option = new Option
|
44 | signature: new Signature('hello')
|
45 | parameter: 'name'
|
46 | expect(option.boolean).to.be.false
|
47 |
|
48 | describe 'parameter option', ->
|
49 |
|
50 | it 'should throw an error if not string', ->
|
51 | expect ->
|
52 | new Option
|
53 | signature: new Signature('hello')
|
54 | parameter: { name: 'world' }
|
55 | .to.throw(Error)
|
56 |
|
57 | it 'should throw an error if not defined and option is boolean', ->
|
58 | expect ->
|
59 | new Option
|
60 | signature: new Signature('hello')
|
61 | boolean: false
|
62 | .to.throw(Error)
|
63 |
|
64 | describe '#matches()', ->
|
65 |
|
66 | describe 'given boolean options', ->
|
67 |
|
68 | beforeEach ->
|
69 | @option = new Option
|
70 | signature: new Signature('foo')
|
71 | boolean: true
|
72 |
|
73 | it 'should return false if no value', ->
|
74 | expect(@option.matches()).to.be.false
|
75 | expect(@option.matches(null)).to.be.false
|
76 |
|
77 | it 'should return true if boolean', ->
|
78 | expect(@option.matches(true)).to.be.true
|
79 | expect(@option.matches(false)).to.be.true
|
80 |
|
81 | it 'should return false if not boolean', ->
|
82 | expect(@option.matches('hello')).to.be.false
|
83 | expect(@option.matches('19')).to.be.false
|
84 |
|
85 | describe 'given non boolean options', ->
|
86 |
|
87 | beforeEach ->
|
88 | @option = new Option
|
89 | signature: new Signature('foo')
|
90 | parameter: 'bar'
|
91 |
|
92 | it 'should return false if no value', ->
|
93 | expect(@option.matches()).to.be.false
|
94 | expect(@option.matches(null)).to.be.false
|
95 |
|
96 | it 'should return false for booleans', ->
|
97 | expect(@option.matches(true)).to.be.false
|
98 | expect(@option.matches(false)).to.be.false
|
99 |
|
100 | it 'should return true for anything else', ->
|
101 | expect(@option.matches('hello')).to.be.true
|
102 | expect(@option.matches('foo')).to.be.true
|
103 | expect(@option.matches('19')).to.be.true
|
104 |
|
105 | describe '#getOptionsValue()', ->
|
106 |
|
107 | describe 'given an option with no alias', ->
|
108 |
|
109 | beforeEach ->
|
110 | @option = new Option
|
111 | signature: new Signature('foo')
|
112 | parameter: 'bar'
|
113 |
|
114 | it 'should match with signature', ->
|
115 | options =
|
116 | foo: 'baz'
|
117 | expect(@option.getOptionsValue(options)).to.equal('baz')
|
118 |
|
119 | it 'should not match with an incorrect signature', ->
|
120 | options =
|
121 | baz: 'baz'
|
122 | expect(@option.getOptionsValue(options)).to.be.undefined
|
123 |
|
124 | describe 'given an option with one alias', ->
|
125 |
|
126 | beforeEach ->
|
127 | @option = new Option
|
128 | signature: new Signature('foo')
|
129 | parameter: 'bar'
|
130 | alias: 'f'
|
131 |
|
132 | it 'should match with signature', ->
|
133 | options =
|
134 | foo: 'baz'
|
135 | expect(@option.getOptionsValue(options)).to.equal('baz')
|
136 |
|
137 | it 'should give precedence to signature', ->
|
138 | options =
|
139 | foo: 'bar'
|
140 | f: 'baz'
|
141 | expect(@option.getOptionsValue(options)).to.equal('bar')
|
142 |
|
143 | it 'should match with alias', ->
|
144 | options =
|
145 | f: 'baz'
|
146 | expect(@option.getOptionsValue(options)).to.equal('baz')
|
147 |
|
148 | describe 'given an option with an array of aliases', ->
|
149 |
|
150 | beforeEach ->
|
151 | @option = new Option
|
152 | signature: new Signature('foo')
|
153 | parameter: 'bar'
|
154 | alias: [ 'a', 'b', 'c' ]
|
155 |
|
156 | it 'should match with signature', ->
|
157 | options =
|
158 | foo: 'baz'
|
159 | expect(@option.getOptionsValue(options)).to.equal('baz')
|
160 |
|
161 | it 'should give precedence to signature', ->
|
162 | options =
|
163 | foo: 'bar'
|
164 | a: 'baz'
|
165 | b: 'hello'
|
166 | c: 'world'
|
167 | expect(@option.getOptionsValue(options)).to.equal('bar')
|
168 |
|
169 | it 'should match with any alias', ->
|
170 | options = a: 'baz'
|
171 | expect(@option.getOptionsValue(options)).to.equal('baz')
|
172 |
|
173 | options = b: 'baz'
|
174 | expect(@option.getOptionsValue(options)).to.equal('baz')
|
175 |
|
176 | options = c: 'baz'
|
177 | expect(@option.getOptionsValue(options)).to.equal('baz')
|
178 |
|
179 | describe '#toString()', ->
|
180 |
|
181 | it 'should represent a multiletter boolean option with no aliases', ->
|
182 | option = new Option
|
183 | signature: new Signature('foo')
|
184 | boolean: true
|
185 |
|
186 | expect(option.toString()).to.equal('--foo')
|
187 |
|
188 | it 'should represent a monoletter boolean option with no aliases', ->
|
189 | option = new Option
|
190 | signature: new Signature('f')
|
191 | boolean: true
|
192 |
|
193 | expect(option.toString()).to.equal('-f')
|
194 |
|
195 | it 'should represent a multiletter parameter option with no aliases', ->
|
196 | option = new Option
|
197 | signature: new Signature('foo')
|
198 | parameter: 'bar'
|
199 |
|
200 | expect(option.toString()).to.equal('--foo <bar>')
|
201 |
|
202 | it 'should represent a monoletter parameter option with no aliases', ->
|
203 | option = new Option
|
204 | signature: new Signature('f')
|
205 | parameter: 'bar'
|
206 |
|
207 | expect(option.toString()).to.equal('-f <bar>')
|
208 |
|
209 | it 'should represent a multiletter boolean option with one multiletter alias', ->
|
210 | option = new Option
|
211 | signature: new Signature('foo')
|
212 | boolean: true
|
213 | alias: 'bar'
|
214 |
|
215 | expect(option.toString()).to.equal('--foo, --bar')
|
216 |
|
217 | it 'should represent a multiletter boolean option with one monoletter alias', ->
|
218 | option = new Option
|
219 | signature: new Signature('foo')
|
220 | boolean: true
|
221 | alias: 'f'
|
222 |
|
223 | expect(option.toString()).to.equal('--foo, -f')
|
224 |
|
225 | it 'should represent a monoletter boolean option with one monoletter alias', ->
|
226 | option = new Option
|
227 | signature: new Signature('f')
|
228 | boolean: true
|
229 | alias: 'b'
|
230 |
|
231 | expect(option.toString()).to.equal('-f, -b')
|
232 |
|
233 | it 'should represent a monoletter boolean option with one multiletter alias', ->
|
234 | option = new Option
|
235 | signature: new Signature('f')
|
236 | boolean: true
|
237 | alias: 'bar'
|
238 |
|
239 | expect(option.toString()).to.equal('-f, --bar')
|
240 |
|
241 | it 'should represent a multiletter parameter option with one multiletter alias', ->
|
242 | option = new Option
|
243 | signature: new Signature('foo')
|
244 | parameter: 'bar'
|
245 | alias: 'bar'
|
246 |
|
247 | expect(option.toString()).to.equal('--foo, --bar <bar>')
|
248 |
|
249 | it 'should represent a multiletter parameter option with one monoletter alias', ->
|
250 | option = new Option
|
251 | signature: new Signature('foo')
|
252 | parameter: 'bar'
|
253 | alias: 'b'
|
254 |
|
255 | expect(option.toString()).to.equal('--foo, -b <bar>')
|
256 |
|
257 | it 'should represent a monoletter parameter option with one monoletter alias', ->
|
258 | option = new Option
|
259 | signature: new Signature('f')
|
260 | parameter: 'bar'
|
261 | alias: 'b'
|
262 |
|
263 | expect(option.toString()).to.equal('-f, -b <bar>')
|
264 |
|
265 | it 'should represent a monoletter parameter option with one multiletter alias', ->
|
266 | option = new Option
|
267 | signature: new Signature('f')
|
268 | parameter: 'bar'
|
269 | alias: 'bar'
|
270 |
|
271 | expect(option.toString()).to.equal('-f, --bar <bar>')
|
272 |
|
273 | it 'should represent a multiletter boolean option with aliases', ->
|
274 | option = new Option
|
275 | signature: new Signature('foo')
|
276 | boolean: true
|
277 | alias: [ 'f', 'bar' ]
|
278 |
|
279 | expect(option.toString()).to.equal('--foo, -f, --bar')
|
280 |
|
281 | it 'should represent a multiletter parameter option with aliases', ->
|
282 | option = new Option
|
283 | signature: new Signature('foo')
|
284 | parameter: 'bar'
|
285 | alias: [ 'f', 'baz' ]
|
286 |
|
287 | expect(option.toString()).to.equal('--foo, -f, --baz <bar>')
|