Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More `mocha` tests on `Cmd`

  • Loading branch information...
commit d1f408c6c56925fab76fe42d44b3568978ca44e9 1 parent c2a97b7
@arikon arikon authored
Showing with 78 additions and 82 deletions.
  1. +78 −7 test/coa.js
  2. +0 −34 tests/api-args.js
  3. +0 −27 tests/api-cmd.js
  4. +0 −14 tests/cmd.js
View
85 test/coa.js
@@ -1,6 +1,5 @@
var assert = require('chai').assert,
- COA = require('..'),
- Q = require('q');
+ COA = require('..');
/**
* Mocha BDD interface.
@@ -65,7 +64,7 @@ describe('Opt', function() {
});
it('should return passed values', function() {
- return cmd.do(['--long1', 'long value', '--long2', 'another long value'])
+ return cmd.do(['--long1', 'long value', '--long2=another long value'])
.then(function(res) {
assert.deepEqual(res, { long1: 'long value', long2: 'another long value' });
});
@@ -155,7 +154,7 @@ describe('Opt', function() {
.name('a')
.short('a')
.val(function(v) {
- if (v === 'invalid') return Q.reject('fail');
+ if (v === 'invalid') return this.reject('fail');
return { value: v };
})
.end()
@@ -339,10 +338,82 @@ describe('Arg', function() {
describe('Cmd', function() {
+ describe('Subcommand', function() {
+
+ var cmd = COA.Cmd()
+ .cmd()
+ .name('command')
+ .opt()
+ .name('opt')
+ .long('opt')
+ .end()
+ .arg()
+ .name('arg1')
+ .end()
+ .arg()
+ .name('arg2')
+ .arr()
+ .end()
+ .act(function(opts, args) {
+ return { opts: opts, args: args };
+ })
+ .end(),
+
+ doTest = function(o) {
+ assert.deepEqual(o, {
+ opts: { opt: 'value' },
+ args: {
+ arg1: 'value',
+ arg2: ['value 1', 'value 2']
+ }
+ });
+ },
+
+ invokeOpts = { opt: 'value' },
+ invokeArgs = {
+ arg1: 'value',
+ arg2: ['value 1', 'value 2']
+ };
+
+ describe('when specified on command line', function() {
+
+ it('should be invoked and accept passed opts and args', function() {
+ return cmd.do(['command', '--opt', 'value', 'value', 'value 1', 'value 2'])
+ .then(doTest);
+ });
+
+ });
+
+ describe('when invoked using api', function() {
+
+ it('should be invoked and accept passed opts and args', function() {
+ return cmd.api.command(invokeOpts, invokeArgs)
+ .then(doTest);
+ });
+
+ });
+
+ describe('when invoked using invoke()', function() {
+
+ it('should be invoked and accept passed opts and args', function() {
+ return cmd.invoke('command', invokeOpts, invokeArgs)
+ .then(doTest);
+ });
+
+ });
+
+ describe('when unexisting command invoked using invoke()', function() {
+
+ it('should fail', function() {
+ return cmd.invoke('unexistent')
+ .then(assert.fail, emptyFn);
+ });
+
+ });
+
+ });
+
it('helpful(), name(), title()');
- it('cmd()');
- it('api');
- it('invoke()');
});
View
34 tests/api-args.js
@@ -1,34 +0,0 @@
-require('../lib/coa').Cmd()
- .name('arg')
- .title('Args test')
- .helpful()
- .opt()
- .name('opt').title('Option')
- .long('opt').short('o')
- .val(function(v) {
- return v.toUpperCase();
- })
- .end()
- .opt()
- .name('def').title('Option with default value')
- .long('def').short('d')
- .def('default value')
- .end()
- .arg()
- .name('arg1').title('First arg')
- .end()
- .arg()
- .name('arg2').title('Second array arg')
- .arr()
- .end()
- .act(function(opts, args) {
- console.log(opts);
- console.log(args);
- })
- .invoke(
- { opt: 'value' },
- {
- arg1: 'value',
- arg2: ['value 1', 'value 2']
- })
- .end();
View
27 tests/api-cmd.js
@@ -1,27 +0,0 @@
-var program = require('../lib/coa').Cmd()
- .name('cmd')
- .title('Command test')
- .helpful()
- .cmd()
- .name('command')
- .title('Do command work')
- .helpful()
- .cmd()
- .name('subcommand')
- .title('Do subcommand work')
- .helpful()
- .act(function() {
- console.log('Doing subcommand work...');
- })
- .end()
- .act(function() {
- console.log('Doing a lot of command work...');
- })
- .end()
- .act(function() {
- console.log('Doing nothing...');
- });
-
-program.api().end();
-program.api.command().end();
-program.api.command.subcommand().end();
View
14 tests/cmd.js
@@ -1,14 +0,0 @@
-var argv = process.argv.slice(2);
-require('../lib/coa').Cmd()
- .name('cmd')
- .title('Command test')
- .helpful()
- .cmd()
- .name('command')
- .title('Do command work')
- .helpful()
- .act(function() {
- console.log('Doing command work...');
- })
- .end()
- .run(argv.length? argv : ['command']);
Please sign in to comment.
Something went wrong with that request. Please try again.