coa 0.3.6 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
package/test/coa.js ADDED
@@ -0,0 +1,496 @@
1
+ var assert = require('chai').assert,
2
+ COA = require('..');
3
+
4
+ /**
5
+ * Mocha BDD interface.
6
+ */
7
+ /** @name describe @function */
8
+ /** @name it @function */
9
+ /** @name before @function */
10
+ /** @name after @function */
11
+ /** @name beforeEach @function */
12
+ /** @name afterEach @function */
13
+
14
+ describe('Opt', function() {
15
+
16
+ describe('Unknown option', function() {
17
+
18
+ var cmd = COA.Cmd();
19
+
20
+ it('should fail', function() {
21
+ return cmd.do(['-a'])
22
+ .then(assert.fail, emptyFn);
23
+ });
24
+
25
+ });
26
+
27
+ describe('Short options', function() {
28
+
29
+ var cmd = COA.Cmd()
30
+ .opt()
31
+ .name('a')
32
+ .short('a')
33
+ .end()
34
+ .opt()
35
+ .name('b')
36
+ .short('b')
37
+ .end()
38
+ .act(function(opts) {
39
+ return opts;
40
+ });
41
+
42
+ it('should return passed values', function() {
43
+ return cmd.do(['-a', 'a', '-b', 'b'])
44
+ .then(function(res) {
45
+ assert.deepEqual(res, { a: 'a', b: 'b' });
46
+ });
47
+ });
48
+
49
+ });
50
+
51
+ describe('Long options', function() {
52
+
53
+ var cmd = COA.Cmd()
54
+ .opt()
55
+ .name('long1')
56
+ .long('long1')
57
+ .end()
58
+ .opt()
59
+ .name('long2')
60
+ .long('long2')
61
+ .end()
62
+ .act(function(opts) {
63
+ return opts;
64
+ });
65
+
66
+ it('should return passed values', function() {
67
+ return cmd.do(['--long1', 'long value', '--long2=another long value'])
68
+ .then(function(res) {
69
+ assert.deepEqual(res, { long1: 'long value', long2: 'another long value' });
70
+ });
71
+ });
72
+
73
+ });
74
+
75
+ describe('Array option', function() {
76
+
77
+ var cmd = COA.Cmd()
78
+ .opt()
79
+ .name('a')
80
+ .short('a')
81
+ .arr()
82
+ .end()
83
+ .act(function(opts) {
84
+ return opts;
85
+ });
86
+
87
+ it('should return array of passed values', function() {
88
+ return cmd.do(['-a', '1', '-a', '2'])
89
+ .then(function(res) {
90
+ assert.deepEqual(res, { a: ['1', '2'] });
91
+ });
92
+ });
93
+
94
+ });
95
+
96
+ describe('Required option', function() {
97
+
98
+ var cmd = COA.Cmd()
99
+ .opt()
100
+ .name('a')
101
+ .short('a')
102
+ .req()
103
+ .end()
104
+ .act(function(opts) {
105
+ return opts;
106
+ });
107
+
108
+ it('should fail if not specified', function() {
109
+ return cmd.do()
110
+ .then(assert.fail, emptyFn);
111
+ });
112
+
113
+ it('should return passed value if specified', function() {
114
+ return cmd.do(['-a', 'test'])
115
+ .then(function(opts) {
116
+ assert.equal(opts.a, 'test');
117
+ });
118
+ });
119
+
120
+ });
121
+
122
+ describe('Option with default value', function() {
123
+
124
+ var cmd = COA.Cmd()
125
+ .opt()
126
+ .name('a')
127
+ .short('a')
128
+ .def('aaa')
129
+ .end()
130
+ .act(function(opts) {
131
+ return opts;
132
+ });
133
+
134
+ it('should return default value if not specified', function() {
135
+ return cmd.do()
136
+ .then(function(opts) {
137
+ assert.equal(opts.a, 'aaa');
138
+ });
139
+ });
140
+
141
+ it('should return passed value if specified', function() {
142
+ return cmd.do(['-a', 'test'])
143
+ .then(function(opts) {
144
+ assert.equal(opts.a, 'test');
145
+ });
146
+ });
147
+
148
+ });
149
+
150
+ describe('Validated / transformed option', function() {
151
+
152
+ var cmd = COA.Cmd()
153
+ .opt()
154
+ .name('a')
155
+ .short('a')
156
+ .val(function(v) {
157
+ if (v === 'invalid') return this.reject('fail');
158
+ return { value: v };
159
+ })
160
+ .end()
161
+ .act(function(opts) {
162
+ return opts;
163
+ });
164
+
165
+ it('should fail if custom checks suppose to do so', function() {
166
+ return cmd.do(['-a', 'invalid'])
167
+ .then(assert.fail, emptyFn);
168
+ });
169
+
170
+ it('should return transformed value', function() {
171
+ return cmd.do(['-a', 'test'])
172
+ .then(function(opts) {
173
+ assert.deepEqual(opts.a, { value: 'test' });
174
+ });
175
+ });
176
+
177
+ });
178
+
179
+ describe('Only option (--version case)', function() {
180
+
181
+ var ver = require('../package.json').version,
182
+ cmd = COA.Cmd()
183
+ .opt()
184
+ .name('version')
185
+ .long('version')
186
+ .flag()
187
+ .only()
188
+ .act(function() {
189
+ return ver;
190
+ })
191
+ .end()
192
+ .opt()
193
+ .name('req')
194
+ .short('r')
195
+ .req()
196
+ .end();
197
+
198
+ it('should process the only() option', function() {
199
+ return cmd.do(['--version'])
200
+ .then(assert.fail, function(res) {
201
+ assert.equal(res, ver);
202
+ });
203
+ });
204
+
205
+ });
206
+
207
+ it('input()');
208
+ it('output()');
209
+
210
+ });
211
+
212
+ describe('Arg', function() {
213
+
214
+ describe('Unknown arg', function() {
215
+
216
+ var cmd = COA.Cmd();
217
+
218
+ it('should fail', function() {
219
+ return cmd.do(['test'])
220
+ .then(assert.fail, emptyFn);
221
+ });
222
+
223
+ });
224
+
225
+ describe('Unknown arg after known', function() {
226
+
227
+ var cmd = COA.Cmd()
228
+ .arg()
229
+ .name('a')
230
+ .end();
231
+
232
+ it('should fail', function() {
233
+ return cmd.do(['test', 'unknown'])
234
+ .then(assert.fail, emptyFn);
235
+ });
236
+
237
+ });
238
+
239
+ describe('Array arg', function() {
240
+
241
+ var cmd = COA.Cmd()
242
+ .arg()
243
+ .name('a')
244
+ .arr()
245
+ .end()
246
+ .act(function(opts, args) {
247
+ return args;
248
+ });
249
+
250
+ it('should return array of passed values', function() {
251
+ return cmd.do(['value 1', 'value 2'])
252
+ .then(function(args) {
253
+ assert.deepEqual(args, { a: ['value 1', 'value 2'] });
254
+ });
255
+ });
256
+
257
+ });
258
+
259
+ describe('Required arg', function() {
260
+
261
+ var cmd = COA.Cmd()
262
+ .arg()
263
+ .name('a')
264
+ .req()
265
+ .end()
266
+ .act(function(opts, args) {
267
+ return args;
268
+ });
269
+
270
+ it('should fail if not specified', function() {
271
+ return cmd.do()
272
+ .then(assert.fail, emptyFn);
273
+ });
274
+
275
+ it('should return passed value if specified', function() {
276
+ return cmd.do(['value'])
277
+ .then(function(args) {
278
+ assert.equal(args.a, 'value');
279
+ });
280
+ });
281
+
282
+ });
283
+
284
+ describe('Args after options', function() {
285
+
286
+ var cmd = COA.Cmd()
287
+ .opt()
288
+ .name('opt')
289
+ .long('opt')
290
+ .end()
291
+ .arg()
292
+ .name('arg1')
293
+ .end()
294
+ .arg()
295
+ .name('arg2')
296
+ .arr()
297
+ .end()
298
+ .act(function(opts, args) {
299
+ return { opts: opts, args: args };
300
+ });
301
+
302
+ it('should return passed values', function() {
303
+ return cmd.do(['--opt', 'value', 'value', 'value 1', 'value 2'])
304
+ .then(function(o) {
305
+ assert.deepEqual(o, {
306
+ opts: { opt: 'value' },
307
+ args: {
308
+ arg1: 'value',
309
+ arg2: ['value 1', 'value 2']
310
+ }
311
+ });
312
+ });
313
+ });
314
+
315
+ });
316
+
317
+ describe('Raw args', function() {
318
+
319
+ var cmd = COA.Cmd()
320
+ .arg()
321
+ .name('raw')
322
+ .arr()
323
+ .end()
324
+ .act(function(opts, args) {
325
+ return args;
326
+ });
327
+
328
+ it('should return passed arg values', function() {
329
+ return cmd.do(['--', 'raw', 'arg', 'values'])
330
+ .then(function(args) {
331
+ assert.deepEqual(args, { raw: ['raw', 'arg', 'values'] });
332
+ });
333
+ });
334
+
335
+ });
336
+
337
+ });
338
+
339
+ describe('Cmd', function() {
340
+
341
+ var doTest = function(o) {
342
+ assert.deepEqual(o, {
343
+ opts: { opt: 'value' },
344
+ args: {
345
+ arg1: 'value',
346
+ arg2: ['value 1', 'value 2']
347
+ }
348
+ });
349
+ },
350
+
351
+ invokeOpts = { opt: 'value' },
352
+ invokeArgs = {
353
+ arg1: 'value',
354
+ arg2: ['value 1', 'value 2']
355
+ };
356
+
357
+ describe('Subcommand', function() {
358
+
359
+ var cmd = COA.Cmd()
360
+ .cmd()
361
+ .name('command')
362
+ .opt()
363
+ .name('opt')
364
+ .long('opt')
365
+ .end()
366
+ .arg()
367
+ .name('arg1')
368
+ .end()
369
+ .arg()
370
+ .name('arg2')
371
+ .arr()
372
+ .end()
373
+ .act(function(opts, args) {
374
+ return { opts: opts, args: args };
375
+ })
376
+ .end();
377
+
378
+ describe('when specified on command line', function() {
379
+
380
+ it('should be invoked and accept passed opts and args', function() {
381
+ return cmd.do(['command', '--opt', 'value', 'value', 'value 1', 'value 2'])
382
+ .then(doTest);
383
+ });
384
+
385
+ });
386
+
387
+ describe('when invoked using api', function() {
388
+
389
+ it('should be invoked and accept passed opts and args', function() {
390
+ return cmd.api.command(invokeOpts, invokeArgs)
391
+ .then(doTest);
392
+ });
393
+
394
+ });
395
+
396
+ describe('when invoked using invoke()', function() {
397
+
398
+ it('should be invoked and accept passed opts and args', function() {
399
+ return cmd.invoke('command', invokeOpts, invokeArgs)
400
+ .then(doTest);
401
+ });
402
+
403
+ });
404
+
405
+ describe('when unexisting command invoked using invoke()', function() {
406
+
407
+ it('should fail', function() {
408
+ return cmd.invoke('unexistent')
409
+ .then(assert.fail, emptyFn);
410
+ });
411
+
412
+ });
413
+
414
+ });
415
+
416
+ describe('External subcommand', function() {
417
+
418
+ describe('default scheme: cmd.extendable()', function() {
419
+
420
+ describe('when described as a function', function() {
421
+ var cmd = COA.Cmd()
422
+ .name('coa')
423
+ .extendable();
424
+
425
+ it('should be invoked and accept passed opts and args', function() {
426
+ return cmd.do(['test', '--opt', 'value', 'value', 'value 1', 'value 2'])
427
+ .then(doTest);
428
+ });
429
+ });
430
+
431
+ describe('when described as an COA.Cmd() object', function() {
432
+ var cmd = COA.Cmd()
433
+ .name('coa')
434
+ .extendable();
435
+
436
+ it('should be invoked and accept passed opts and args', function() {
437
+ return cmd.do(['test-obj', '--opt', 'value', 'value', 'value 1', 'value 2'])
438
+ .then(doTest);
439
+ });
440
+ });
441
+
442
+ describe('2nd level subcommand', function() {
443
+ var cmd = COA.Cmd()
444
+ .name('coa')
445
+ .cmd()
446
+ .name('test')
447
+ .extendable()
448
+ .end();
449
+
450
+ it('should be invoked and accept passed opts and args', function() {
451
+ return cmd.do(['test', 'obj', '--opt', 'value', 'value', 'value 1', 'value 2'])
452
+ .then(doTest);
453
+ });
454
+ });
455
+
456
+ });
457
+
458
+ describe("common prefix: cmd.extendable('coa-')", function() {
459
+
460
+ describe('when described as a function', function() {
461
+ var cmd = COA.Cmd()
462
+ .name('coa')
463
+ .extendable('coa-');
464
+
465
+ it('should be invoked and accept passed opts and args', function() {
466
+ return cmd.do(['test', '--opt', 'value', 'value', 'value 1', 'value 2'])
467
+ .then(doTest);
468
+ });
469
+ });
470
+
471
+ });
472
+
473
+ describe("format string: cmd.extendable('coa-%s')", function() {
474
+
475
+ describe('when described as a function', function() {
476
+ var cmd = COA.Cmd()
477
+ .name('coa')
478
+ .extendable('coa-%s');
479
+
480
+ it('should be invoked and accept passed opts and args', function() {
481
+ return cmd.do(['test', '--opt', 'value', 'value', 'value 1', 'value 2'])
482
+ .then(doTest);
483
+ });
484
+ });
485
+
486
+ });
487
+
488
+ });
489
+
490
+ it('helpful(), name(), title()');
491
+
492
+ });
493
+
494
+ function emptyFn() {
495
+ // empty function
496
+ }
package/test/common.js ADDED
@@ -0,0 +1 @@
1
+ require('mocha-as-promised')(require('mocha'));
@@ -0,0 +1,3 @@
1
+ --reporter spec
2
+ --timeout 0
3
+ --require test/common.js
@@ -1,59 +1,60 @@
1
- var vows = require('vows'),
2
- assert = require('assert'),
3
- shell = require('../lib/shell');
1
+ var assert = require('chai').assert,
2
+ shell = require('..').shell;
4
3
 
5
- vows.describe('coa/shell').addBatch({
4
+ /**
5
+ * Mocha BDD interface.
6
+ */
7
+ /** @name describe @function */
8
+ /** @name it @function */
9
+ /** @name before @function */
10
+ /** @name after @function */
11
+ /** @name beforeEach @function */
12
+ /** @name afterEach @function */
6
13
 
7
- 'The shell module': {
14
+ describe('shell', function() {
8
15
 
9
- '`escape`': {
16
+ describe('escape()', function() {
10
17
 
11
- topic: function() {
12
- return shell.escape;
13
- },
18
+ var escape = shell.escape;
14
19
 
15
- 'Should wrap values with spaces in double quotes': function(escape) {
16
- assert.equal(escape('asd abc'), '"asd abc"');
17
- },
20
+ it('Should wrap values with spaces in double quotes', function() {
21
+ assert.equal(escape('asd abc'), '"asd abc"');
22
+ });
18
23
 
19
- 'Should escape double quote "': function(escape) {
20
- assert.equal(escape('"asd'), '\\"asd');
21
- },
24
+ it('Should escape double quote "', function() {
25
+ assert.equal(escape('"asd'), '\\"asd');
26
+ });
22
27
 
23
- "Should escape single quote '": function(escape) {
24
- assert.equal(escape("'asd"), "\\'asd");
25
- },
28
+ it("Should escape single quote '", function() {
29
+ assert.equal(escape("'asd"), "\\'asd");
30
+ });
26
31
 
27
- 'Should escape backslash \\': function(escape) {
28
- assert.equal(escape('\\asd'), '\\\\asd');
29
- },
32
+ it('Should escape backslash \\', function() {
33
+ assert.equal(escape('\\asd'), '\\\\asd');
34
+ });
30
35
 
31
- 'Should escape dollar $': function(escape) {
32
- assert.equal(escape('$asd'), '\\$asd');
33
- },
36
+ it('Should escape dollar $', function() {
37
+ assert.equal(escape('$asd'), '\\$asd');
38
+ });
34
39
 
35
- 'Should escape backtick `': function(escape) {
36
- assert.equal(escape('`asd'), '\\`asd');
37
- }
40
+ it('Should escape backtick `', function() {
41
+ assert.equal(escape('`asd'), '\\`asd');
42
+ });
38
43
 
39
- },
44
+ });
40
45
 
41
- '`unescape`': {
46
+ describe('unescape()', function() {
42
47
 
43
- topic: function() {
44
- return shell.unescape;
45
- },
48
+ var unescape = shell.unescape;
46
49
 
47
- 'Should strip double quotes at the both ends': function(unescape) {
48
- assert.equal(unescape('"asd"'), 'asd');
49
- },
50
+ it('Should strip double quotes at the both ends', function() {
51
+ assert.equal(unescape('"asd"'), 'asd');
52
+ });
50
53
 
51
- 'Should not strip escaped double quotes at the both ends': function(unescape) {
52
- assert.equal(unescape('\\"asd\\"'), '"asd"');
53
- }
54
+ it('Should not strip escaped double quotes at the both ends', function() {
55
+ assert.equal(unescape('\\"asd\\"'), '"asd"');
56
+ });
54
57
 
55
- }
58
+ });
56
59
 
57
- }
58
-
59
- }).export(module);
60
+ });
package/tests/api-h.js CHANGED
@@ -1,4 +1,4 @@
1
- require('../lib/coa').Cmd()
1
+ require('..').Cmd()
2
2
  .name('bla')
3
3
  .title('Bla bla bla')
4
4
  .helpful()
@@ -6,4 +6,4 @@ require('../lib/coa').Cmd()
6
6
  .then(function(res) {
7
7
  console.log(res);
8
8
  })
9
- .end(); // Q.end()
9
+ .done(); // Q.done()
package/tests/h.js CHANGED
@@ -1,5 +1,5 @@
1
1
  var argv = process.argv.slice(2);
2
- require('../lib/coa').Cmd()
2
+ require('..').Cmd()
3
3
  .name('bla')
4
4
  .title('Bla bla bla')
5
5
  .helpful()
package/lib/coa.js DELETED
@@ -1,3 +0,0 @@
1
- // Generated by CoffeeScript 1.3.3
2
-
3
- exports.Cmd = require('./cmd').Cmd;
package/src/coa.coffee DELETED
@@ -1 +0,0 @@
1
- exports.Cmd = require('./cmd').Cmd
package/tests/a.js DELETED
@@ -1,14 +0,0 @@
1
- var argv = process.argv.slice(2);
2
- require('../lib/coa').Cmd()
3
- .name('arr')
4
- .title('Array value test')
5
- .helpful()
6
- .opt()
7
- .name('arr').title('Array')
8
- .short('a').long('arr')
9
- .arr()
10
- .act(function(opts) {
11
- console.log(opts.arr);
12
- })
13
- .end()
14
- .run(argv.length? argv : ['-a', '1', '-a', '2']);
package/tests/api-args.js DELETED
@@ -1,34 +0,0 @@
1
- require('../lib/coa').Cmd()
2
- .name('arg')
3
- .title('Args test')
4
- .helpful()
5
- .opt()
6
- .name('opt').title('Option')
7
- .long('opt').short('o')
8
- .val(function(v) {
9
- return v.toUpperCase();
10
- })
11
- .end()
12
- .opt()
13
- .name('def').title('Option with default value')
14
- .long('def').short('d')
15
- .def('default value')
16
- .end()
17
- .arg()
18
- .name('arg1').title('First arg')
19
- .end()
20
- .arg()
21
- .name('arg2').title('Second array arg')
22
- .arr()
23
- .end()
24
- .act(function(opts, args) {
25
- console.log(opts);
26
- console.log(args);
27
- })
28
- .invoke(
29
- { opt: 'value' },
30
- {
31
- arg1: 'value',
32
- arg2: ['value 1', 'value 2']
33
- })
34
- .end();