1 | chai = require('chai')
|
2 | expect = chai.expect
|
3 | Parameter = require('../lib/parameter')
|
4 | settings = require('../lib/settings')
|
5 |
|
6 | describe 'Parameter:', ->
|
7 |
|
8 | describe '#constructor()', ->
|
9 |
|
10 | it 'should throw an error if parameter is missing', ->
|
11 | expect ->
|
12 | new Parameter()
|
13 | .to.throw(Error)
|
14 |
|
15 | it 'should convert a number parameter to string', ->
|
16 | parameter = new Parameter(121)
|
17 | expect(parameter.parameter).to.equal('121')
|
18 |
|
19 | it 'should throw an error if parameter is not a string or a number', ->
|
20 | expect ->
|
21 | new Parameter([ 1, 2, 3 ])
|
22 | .to.throw(Error)
|
23 |
|
24 | it 'should return an error if required variadic and has stdin support', ->
|
25 | expect ->
|
26 | new Parameter('<|foo...>')
|
27 | .to.throw('Parameter can\'t be variadic and allow stdin')
|
28 |
|
29 | it 'should return an error if optional variadic and has stdin support', ->
|
30 | expect ->
|
31 | new Parameter('[|foo...]')
|
32 | .to.throw('Parameter can\'t be variadic and allow stdin')
|
33 |
|
34 | describe '#isRequired()', ->
|
35 |
|
36 | it 'should return true for required parameters', ->
|
37 | parameter = new Parameter('<foo>')
|
38 | expect(parameter.isRequired()).to.be.true
|
39 |
|
40 | it 'should return true for variadic required parameters', ->
|
41 | parameter = new Parameter('<foo...>')
|
42 | expect(parameter.isRequired()).to.be.true
|
43 |
|
44 | it 'should return true for multi word required parameters', ->
|
45 | parameter = new Parameter('<foo bar>')
|
46 | expect(parameter.isRequired()).to.be.true
|
47 |
|
48 | it 'should return false for optional parameters', ->
|
49 | parameter = new Parameter('[foo]')
|
50 | expect(parameter.isRequired()).to.be.false
|
51 |
|
52 | it 'should return false for variadic optional parameters', ->
|
53 | parameter = new Parameter('[foo...]')
|
54 | expect(parameter.isRequired()).to.be.false
|
55 |
|
56 | it 'should return false for invalid parameters', ->
|
57 | for input in [
|
58 | 'foo'
|
59 | '<foo]'
|
60 | '[foo>'
|
61 | ''
|
62 | ]
|
63 | parameter = new Parameter(input)
|
64 | expect(parameter.isRequired()).to.be.false
|
65 |
|
66 | describe '#isOptional()', ->
|
67 |
|
68 | it 'should return true for optional parameters', ->
|
69 | parameter = new Parameter('[foo]')
|
70 | expect(parameter.isOptional()).to.be.true
|
71 |
|
72 | it 'should return true for variadic optional parameters', ->
|
73 | parameter = new Parameter('[foo...]')
|
74 | expect(parameter.isOptional()).to.be.true
|
75 |
|
76 | it 'should return true for multi word optional parameters', ->
|
77 | parameter = new Parameter('[foo bar]')
|
78 | expect(parameter.isOptional()).to.be.true
|
79 |
|
80 | it 'should return false for required parameters', ->
|
81 | parameter = new Parameter('<foo>')
|
82 | expect(parameter.isOptional()).to.be.false
|
83 |
|
84 | it 'should return false for variadic required parameters', ->
|
85 | parameter = new Parameter('<foo...>')
|
86 | expect(parameter.isOptional()).to.be.false
|
87 |
|
88 | it 'should return false for invalid parameters', ->
|
89 | for input in [
|
90 | 'foo'
|
91 | '<foo]'
|
92 | '[foo>'
|
93 | ''
|
94 | ]
|
95 | parameter = new Parameter(input)
|
96 | expect(parameter.isOptional()).to.be.false
|
97 |
|
98 | describe '#isVariadic()', ->
|
99 |
|
100 | it 'should return true for variadic required parameters', ->
|
101 | parameter = new Parameter('<foo...>')
|
102 | expect(parameter.isVariadic()).to.be.true
|
103 |
|
104 | it 'should return true for variadic optional parameters', ->
|
105 | parameter = new Parameter('[foo...]')
|
106 | expect(parameter.isVariadic()).to.be.true
|
107 |
|
108 | it 'should return true for variadic multi word required parameters', ->
|
109 | parameter = new Parameter('<foo bar...>')
|
110 | expect(parameter.isVariadic()).to.be.true
|
111 |
|
112 | it 'should return true for variadic multi word optional parameters', ->
|
113 | parameter = new Parameter('[foo bar...]')
|
114 | expect(parameter.isVariadic()).to.be.true
|
115 |
|
116 | it 'should return false for required parameters', ->
|
117 | parameter = new Parameter('<foo>')
|
118 | expect(parameter.isVariadic()).to.be.false
|
119 |
|
120 | it 'should return false for optional parameters', ->
|
121 | parameter = new Parameter('<foo>')
|
122 | expect(parameter.isVariadic()).to.be.false
|
123 |
|
124 | it 'should return false for invalid parameters', ->
|
125 | for input in [
|
126 | 'foo'
|
127 | '<foo]'
|
128 | '[foo>'
|
129 | ''
|
130 | 'foo..'
|
131 | '<...foo>'
|
132 | ]
|
133 | parameter = new Parameter(input)
|
134 | expect(parameter.isVariadic()).to.be.false
|
135 |
|
136 | describe '#isWord()', ->
|
137 |
|
138 | it 'should return false for required parameters', ->
|
139 | parameter = new Parameter('<foo>')
|
140 | expect(parameter.isWord()).to.be.false
|
141 |
|
142 | it 'should return false for optional parameters', ->
|
143 | parameter = new Parameter('[foo]')
|
144 | expect(parameter.isWord()).to.be.false
|
145 |
|
146 | it 'should return false for multi word required parameters', ->
|
147 | parameter = new Parameter('<foo bar>')
|
148 | expect(parameter.isWord()).to.be.false
|
149 |
|
150 | it 'should return false for multi word optional parameters', ->
|
151 | parameter = new Parameter('[foo bar]')
|
152 | expect(parameter.isWord()).to.be.false
|
153 |
|
154 | it 'should return false for variadic required parameters', ->
|
155 | parameter = new Parameter('<foo...>')
|
156 | expect(parameter.isWord()).to.be.false
|
157 |
|
158 | it 'should return false for variadic optional parameters', ->
|
159 | parameter = new Parameter('[foo...]')
|
160 | expect(parameter.isWord()).to.be.false
|
161 |
|
162 | it 'should return true for everything else', ->
|
163 | for input in [
|
164 | 'foo'
|
165 | 'foo...'
|
166 | '<foo]'
|
167 | '[foo>'
|
168 | 'foo bar'
|
169 | ''
|
170 | ]
|
171 | parameter = new Parameter(input)
|
172 | expect(parameter.isWord()).to.be.true
|
173 |
|
174 | describe '#isMultiWord()', ->
|
175 |
|
176 | it 'should return true for non parameter multi words', ->
|
177 | parameter = new Parameter('foo bar')
|
178 | expect(parameter.isMultiWord()).to.be.true
|
179 |
|
180 | it 'should return true for required parameter multi words', ->
|
181 | parameter = new Parameter('<foo bar>')
|
182 | expect(parameter.isMultiWord()).to.be.true
|
183 |
|
184 | it 'should return true for optional parameter multi words', ->
|
185 | parameter = new Parameter('[foo bar]')
|
186 | expect(parameter.isMultiWord()).to.be.true
|
187 |
|
188 | it 'should return true for variadic required parameter multi words', ->
|
189 | parameter = new Parameter('<foo bar...>')
|
190 | expect(parameter.isMultiWord()).to.be.true
|
191 |
|
192 | it 'should return true for variadic optional parameter multi words', ->
|
193 | parameter = new Parameter('[foo bar...]')
|
194 | expect(parameter.isMultiWord()).to.be.true
|
195 |
|
196 | it 'should return false for single word non parameters', ->
|
197 | parameter = new Parameter('foo')
|
198 | expect(parameter.isMultiWord()).to.be.false
|
199 |
|
200 | it 'should return false for single word required parameters', ->
|
201 | parameter = new Parameter('<foo>')
|
202 | expect(parameter.isMultiWord()).to.be.false
|
203 |
|
204 | it 'should return false for single word optional parameters', ->
|
205 | parameter = new Parameter('[foo]')
|
206 | expect(parameter.isMultiWord()).to.be.false
|
207 |
|
208 | it 'should return false for single word variadic required parameters', ->
|
209 | parameter = new Parameter('<foo...>')
|
210 | expect(parameter.isMultiWord()).to.be.false
|
211 |
|
212 | it 'should return false for single word variadic optional parameters', ->
|
213 | parameter = new Parameter('[foo...]')
|
214 | expect(parameter.isMultiWord()).to.be.false
|
215 |
|
216 | describe '#allowsStdin()', ->
|
217 |
|
218 | it 'should return false for required parameters', ->
|
219 | parameter = new Parameter('<foo>')
|
220 | expect(parameter.allowsStdin()).to.be.false
|
221 |
|
222 | it 'should return false for variadic required parameters', ->
|
223 | parameter = new Parameter('<foo...>')
|
224 | expect(parameter.allowsStdin()).to.be.false
|
225 |
|
226 | it 'should return false for multi word required parameters', ->
|
227 | parameter = new Parameter('<foo bar>')
|
228 | expect(parameter.allowsStdin()).to.be.false
|
229 |
|
230 | it 'should return false for optional parameters', ->
|
231 | parameter = new Parameter('[foo]')
|
232 | expect(parameter.allowsStdin()).to.be.false
|
233 |
|
234 | it 'should return false for variadic optional parameters', ->
|
235 | parameter = new Parameter('[foo...]')
|
236 | expect(parameter.allowsStdin()).to.be.false
|
237 |
|
238 | it 'should return false for invalid parameters', ->
|
239 | for input in [
|
240 | 'foo'
|
241 | '<foo]'
|
242 | '[foo>'
|
243 | ''
|
244 | ]
|
245 | parameter = new Parameter(input)
|
246 | expect(parameter.allowsStdin()).to.be.false
|
247 |
|
248 | it 'should return true if required with |', ->
|
249 | parameter = new Parameter('<|foo>')
|
250 | expect(parameter.allowsStdin()).to.be.true
|
251 |
|
252 | it 'should return true if optional with |', ->
|
253 | parameter = new Parameter('[|foo]')
|
254 | expect(parameter.allowsStdin()).to.be.true
|
255 |
|
256 | it 'should return false if no parameter with |', ->
|
257 | parameter = new Parameter('|foo')
|
258 | expect(parameter.allowsStdin()).to.be.false
|
259 |
|
260 | it 'should return true if multi word required with |', ->
|
261 | parameter = new Parameter('<|foo bar>')
|
262 | expect(parameter.allowsStdin()).to.be.true
|
263 |
|
264 | it 'should return true if multi word optional with |', ->
|
265 | parameter = new Parameter('[|foo bar]')
|
266 | expect(parameter.allowsStdin()).to.be.true
|
267 |
|
268 | describe '#getValue()', ->
|
269 |
|
270 | it 'should get word values', ->
|
271 | parameter = new Parameter('foo')
|
272 | expect(parameter.getValue()).to.equal('foo')
|
273 |
|
274 | it 'should get multi word values', ->
|
275 | parameter = new Parameter('foo bar')
|
276 | expect(parameter.getValue()).to.equal('foo bar')
|
277 |
|
278 | it 'should get the value of required parameter', ->
|
279 | parameter = new Parameter('<foo>')
|
280 | expect(parameter.getValue()).to.equal('foo')
|
281 |
|
282 | it 'should get the value of optional parameter', ->
|
283 | parameter = new Parameter('[foo]')
|
284 | expect(parameter.getValue()).to.equal('foo')
|
285 |
|
286 | it 'should get the value of multi word required parameter', ->
|
287 | parameter = new Parameter('<foo bar>')
|
288 | expect(parameter.getValue()).to.equal('foo bar')
|
289 |
|
290 | it 'should get the value of multi word optional parameter', ->
|
291 | parameter = new Parameter('[foo bar]')
|
292 | expect(parameter.getValue()).to.equal('foo bar')
|
293 |
|
294 | it 'should get the value of variadic required parameter', ->
|
295 | parameter = new Parameter('<foo...>')
|
296 | expect(parameter.getValue()).to.equal('foo')
|
297 |
|
298 | it 'should get the value of variadic optional parameter', ->
|
299 | parameter = new Parameter('[foo...]')
|
300 | expect(parameter.getValue()).to.equal('foo')
|
301 |
|
302 | it 'should get the value of stdin optional parameter', ->
|
303 | parameter = new Parameter('[|foo]')
|
304 | expect(parameter.getValue()).to.equal('foo')
|
305 |
|
306 | it 'should get the value of stdin required parameter', ->
|
307 | parameter = new Parameter('<|foo>')
|
308 | expect(parameter.getValue()).to.equal('foo')
|
309 |
|
310 | describe '#getType()', ->
|
311 |
|
312 | it 'should return word for word parameters', ->
|
313 | parameter = new Parameter('foo')
|
314 | expect(parameter.getType()).to.equal('word')
|
315 |
|
316 | it 'should return word for multi word parameters', ->
|
317 | parameter = new Parameter('foo bar')
|
318 | expect(parameter.getType()).to.equal('word')
|
319 |
|
320 | it 'should return parameter for required parameters', ->
|
321 | parameter = new Parameter('<foo>')
|
322 | expect(parameter.getType()).to.equal('parameter')
|
323 |
|
324 | it 'should return parameter for optional parameters', ->
|
325 | parameter = new Parameter('[foo]')
|
326 | expect(parameter.getType()).to.equal('parameter')
|
327 |
|
328 | it 'should return parameter for multi word required parameters', ->
|
329 | parameter = new Parameter('<foo bar>')
|
330 | expect(parameter.getType()).to.equal('parameter')
|
331 |
|
332 | it 'should return parameter for multi word optional parameters', ->
|
333 | parameter = new Parameter('[foo bar]')
|
334 | expect(parameter.getType()).to.equal('parameter')
|
335 |
|
336 | it 'should return parameter for variadic required parameters', ->
|
337 | parameter = new Parameter('<foo...>')
|
338 | expect(parameter.getType()).to.equal('parameter')
|
339 |
|
340 | it 'should return parameter for variadic optional parameters', ->
|
341 | parameter = new Parameter('[foo...]')
|
342 | expect(parameter.getType()).to.equal('parameter')
|
343 |
|
344 | describe '#matches()', ->
|
345 |
|
346 | describe 'given a word', ->
|
347 |
|
348 | beforeEach ->
|
349 | @parameter = new Parameter('foo')
|
350 |
|
351 | it 'should return false if missing', ->
|
352 | expect(@parameter.matches('')).to.be.false
|
353 | expect(@parameter.matches()).to.be.false
|
354 |
|
355 | it 'should return true if equal to value', ->
|
356 | expect(@parameter.matches('foo')).to.be.true
|
357 |
|
358 | it 'should return false if not equal to value', ->
|
359 | expect(@parameter.matches('bar')).to.be.false
|
360 |
|
361 | it 'should return false if it shares root', ->
|
362 | expect(@parameter.matches('foo bar')).to.be.false
|
363 |
|
364 | describe 'given a required parameter', ->
|
365 |
|
366 | beforeEach ->
|
367 | @parameter = new Parameter('<foo>')
|
368 |
|
369 | it 'should return false if missing', ->
|
370 | expect(@parameter.matches('')).to.be.false
|
371 | expect(@parameter.matches()).to.be.false
|
372 |
|
373 | it 'should return true if it matches', ->
|
374 | expect(@parameter.matches('bar')).to.be.true
|
375 |
|
376 | it 'should return true if command exceeds', ->
|
377 | expect(@parameter.matches('bar baz')).to.be.true
|
378 |
|
379 | describe 'given an optional parameter', ->
|
380 |
|
381 | beforeEach ->
|
382 | @parameter = new Parameter('[foo]')
|
383 |
|
384 | it 'should return true if missing', ->
|
385 | expect(@parameter.matches('')).to.be.true
|
386 | expect(@parameter.matches()).to.be.true
|
387 |
|
388 | it 'should return true if it matches', ->
|
389 | expect(@parameter.matches('hello')).to.be.true
|
390 |
|
391 | it 'should return true if it exceeds', ->
|
392 | expect(@parameter.matches('hello world')).to.be.true
|
393 |
|
394 | describe 'given a required variadic parameter', ->
|
395 |
|
396 | beforeEach ->
|
397 | @parameter = new Parameter('<foo...>')
|
398 |
|
399 | it 'should return false if missing', ->
|
400 | expect(@parameter.matches('')).to.be.false
|
401 | expect(@parameter.matches()).to.be.false
|
402 |
|
403 | it 'should return true if one word', ->
|
404 | expect(@parameter.matches('hello')).to.be.true
|
405 |
|
406 | it 'should return true if multi word', ->
|
407 | expect(@parameter.matches('hello world')).to.be.true
|
408 |
|
409 | describe 'given an optional variadic parameter', ->
|
410 |
|
411 | beforeEach ->
|
412 | @parameter = new Parameter('[foo...]')
|
413 |
|
414 | it 'should return true if missing', ->
|
415 | expect(@parameter.matches('')).to.be.true
|
416 | expect(@parameter.matches()).to.be.true
|
417 |
|
418 | it 'should return true if one word', ->
|
419 | expect(@parameter.matches('hello')).to.be.true
|
420 |
|
421 | it 'should return true if multi word', ->
|
422 | expect(@parameter.matches('hello world')).to.be.true
|
423 |
|
424 | describe '#toString()', ->
|
425 |
|
426 | it 'should convert a word to string', ->
|
427 | parameter = new Parameter('foo')
|
428 | expect(parameter.toString()).to.equal('foo')
|
429 |
|
430 | it 'should convert a required parameter to string', ->
|
431 | parameter = new Parameter('<foo>')
|
432 | expect(parameter.toString()).to.equal('<foo>')
|
433 |
|
434 | it 'should convert an optional parameter to string', ->
|
435 | parameter = new Parameter('[foo]')
|
436 | expect(parameter.toString()).to.equal('[foo]')
|
437 |
|
438 | it 'should convert a multi word required parameter to string', ->
|
439 | parameter = new Parameter('<foo bar>')
|
440 | expect(parameter.toString()).to.equal('<foo bar>')
|
441 |
|
442 | it 'should convert a multi word optional parameter to string', ->
|
443 | parameter = new Parameter('[foo bar]')
|
444 | expect(parameter.toString()).to.equal('[foo bar]')
|
445 |
|
446 | it 'should convert a required variadic parameter to string', ->
|
447 | parameter = new Parameter('<foo...>')
|
448 | expect(parameter.toString()).to.equal('<foo...>')
|
449 |
|
450 | it 'should convert a optional variadic parameter to string', ->
|
451 | parameter = new Parameter('[foo...]')
|
452 | expect(parameter.toString()).to.equal('[foo...]')
|
453 |
|
454 | it 'should convert a wildcard to string', ->
|
455 | parameter = new Parameter(settings.signatures.wildcard)
|
456 | expect(parameter.toString()).to.equal(settings.signatures.wildcard)
|
457 |
|
458 | it 'should preserve quotes for multi word non parameters', ->
|
459 | parameter = new Parameter('foo bar')
|
460 | expect(parameter.toString()).to.equal('"foo bar"')
|