Permalink
Browse files

Rewrite bunch of `Opt` and `Arg` tests to `mocha`; add convenient `Cm…

…d.do()` method to run from tests (Ref #22)
  • Loading branch information...
1 parent c9668ce commit 10073705abb5e34ebb22d0836b9dfd83b0b18aa4 @arikon arikon committed Oct 28, 2012
Showing with 346 additions and 143 deletions.
  1. +12 −1 lib/cmd.js
  2. +9 −1 src/cmd.coffee
  3. +325 −0 test/coa.js
  4. +0 −14 tests/a.js
  5. +0 −21 tests/args.js
  6. +0 −25 tests/def.js
  7. +0 −21 tests/l.js
  8. +0 −13 tests/rarg.js
  9. +0 −10 tests/req.js
  10. +0 −23 tests/v.js
  11. +0 −14 tests/val.js
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -342,9 +342,17 @@ exports.Cmd = class Cmd
@_exit res.stack ? res.toString(), res.exitCode ? code
else
@_exit()
- Q.when(@_do(@_parseArr argv), cb(0), cb(1)).end()
+ Q.when(@do(argv), cb(0), cb(1)).end()
@
+ ###*
+ Convenient function to run command from tests.
+ @param {Array} argv
+ @returns {Q.Promise}
+ ###
+ do: (argv) ->
+ @_do(@_parseArr argv || [])
+
###*
Invoke specified (or current) command using provided
options and arguments.
View
@@ -0,0 +1,325 @@
+var assert = require('chai').assert,
+ COA = require('..'),
+ Q = require('q');
+
+/**
+ * Mocha BDD interface.
+ */
+/** @name describe @function */
+/** @name it @function */
+/** @name before @function */
+/** @name after @function */
+/** @name beforeEach @function */
+/** @name afterEach @function */
+
+describe('Opt', function() {
+
+ describe('Unknown option', function() {
+
+ var cmd = COA.Cmd();
+
+ it('should fail', function() {
+ return cmd.do(['-a'])
+ .then(assert.fail, emptyFn);
+ });
+
+ });
+
+ describe('Short options', function() {
+
+ var cmd = COA.Cmd()
+ .opt()
+ .name('a')
+ .short('a')
+ .end()
+ .opt()
+ .name('b')
+ .short('b')
+ .end()
+ .act(function(opts) {
+ return opts;
+ });
+
+ it('should return passed values', function() {
+ return cmd.do(['-a', 'a', '-b', 'b'])
+ .then(function(res) {
+ assert.deepEqual(res, { a: 'a', b: 'b' });
+ });
+ });
+
+ });
+
+ describe('Long options', function() {
+
+ var cmd = COA.Cmd()
+ .opt()
+ .name('long1')
+ .long('long1')
+ .end()
+ .opt()
+ .name('long2')
+ .long('long2')
+ .end()
+ .act(function(opts) {
+ return opts;
+ });
+
+ it('should return passed values', function() {
+ return cmd.do(['--long1', 'long value', '--long2', 'another long value'])
+ .then(function(res) {
+ assert.deepEqual(res, { long1: 'long value', long2: 'another long value' });
+ });
+ });
+
+ });
+
+ describe('Array option', function() {
+
+ var cmd = COA.Cmd()
+ .opt()
+ .name('a')
+ .short('a')
+ .arr()
+ .end()
+ .act(function(opts) {
+ return opts;
+ });
+
+ it('should return array of passed values', function() {
+ return cmd.do(['-a', '1', '-a', '2'])
+ .then(function(res) {
+ assert.deepEqual(res, { a: ['1', '2'] });
+ });
+ });
+
+ });
+
+ describe('Required option', function() {
+
+ var cmd = COA.Cmd()
+ .opt()
+ .name('a')
+ .short('a')
+ .req()
+ .end()
+ .act(function(opts) {
+ return opts;
+ });
+
+ it('should fail if not specified', function() {
+ return cmd.do()
+ .then(assert.fail, emptyFn);
+ });
+
+ it('should return passed value if specified', function() {
+ return cmd.do(['-a', 'test'])
+ .then(function(opts) {
+ assert.equal(opts.a, 'test');
+ });
+ });
+
+ });
+
+ describe('Option with default value', function() {
+
+ var cmd = COA.Cmd()
+ .opt()
+ .name('a')
+ .short('a')
+ .def('aaa')
+ .end()
+ .act(function(opts) {
+ return opts;
+ });
+
+ it('should return default value if not specified', function() {
+ return cmd.do()
+ .then(function(opts) {
+ assert.equal(opts.a, 'aaa');
+ });
+ });
+
+ it('should return passed value if specified', function() {
+ return cmd.do(['-a', 'test'])
+ .then(function(opts) {
+ assert.equal(opts.a, 'test');
+ });
+ });
+
+ });
+
+ describe('Validated / transformed option', function() {
+
+ var cmd = COA.Cmd()
+ .opt()
+ .name('a')
+ .short('a')
+ .val(function(v) {
+ if (v === 'invalid') return Q.reject('fail');
+ return { value: v };
+ })
+ .end()
+ .act(function(opts) {
+ return opts;
+ });
+
+ it('should fail if custom checks suppose to do so', function() {
+ return cmd.do(['-a', 'invalid'])
+ .then(assert.fail, emptyFn);
+ });
+
+ it('should return transformed value', function() {
+ return cmd.do(['-a', 'test'])
+ .then(function(opts) {
+ assert.deepEqual(opts.a, { value: 'test' });
+ });
+ });
+
+ });
+
+ describe('Only option (--version case)', function() {
+
+ var ver = require('../package.json').version,
+ cmd = COA.Cmd()
+ .opt()
+ .name('version')
+ .long('version')
+ .flag()
+ .only()
+ .act(function() {
+ return ver;
+ })
+ .end()
+ .opt()
+ .name('req')
+ .short('r')
+ .req()
+ .end();
+
+ it('should process the only() option', function() {
+ return cmd.do(['--version'])
+ .then(assert.fail, function(res) {
+ assert.equal(res, ver);
+ });
+ });
+
+ });
+
+});
+
+describe('Arg', function() {
+
+ describe('Unknown arg', function() {
+
+ var cmd = COA.Cmd();
+
+ it('should fail', function() {
+ return cmd.do(['test'])
+ .then(assert.fail, emptyFn);
+ });
+
+ });
+
+ describe('Unknown arg after known', function() {
+
+ var cmd = COA.Cmd()
+ .arg()
+ .name('a')
+ .end();
+
+ it('should fail', function() {
+ return cmd.do(['test', 'unknown'])
+ .then(assert.fail, emptyFn);
+ });
+
+ });
+
+ describe('Array arg', function() {
+
+ var cmd = COA.Cmd()
+ .arg()
+ .name('a')
+ .arr()
+ .end();
+
+ it('should return array of passed values');
+
+ });
+
+ describe('Required arg', function() {
+
+ var cmd = COA.Cmd()
+ .arg()
+ .name('a')
+ .req()
+ .end();
+
+ it('should fail if not specified');
+ it('should return passed value if specified');
+
+ });
+
+ describe('Args after options', function() {
+
+ var cmd = COA.Cmd()
+ .opt()
+ .name('opt')
+ .long('opt')
+ .end()
+ .arg()
+ .name('arg1')
+ .end()
+ .arg()
+ .name('arg2')
+ .arr()
+ .end()
+ .act(function(opts, args) {
+ return { opts: opts, args: args };
+ });
+
+ it('should return passed values', function() {
+ return cmd.do(['--opt', 'value', 'value', 'value 1', 'value 2'])
+ .then(function(o) {
+ assert.deepEqual(o, {
+ opts: { opt: 'value' },
+ args: {
+ arg1: 'value',
+ arg2: ['value 1', 'value 2']
+ }
+ });
+ });
+ });
+
+ });
+
+ describe('Raw args', function() {
+
+ var cmd = COA.Cmd()
+ .arg()
+ .name('raw')
+ .arr()
+ .end()
+ .act(function(opts, args) {
+ return args;
+ });
+
+ it('should return passed arg values', function() {
+ return cmd.do(['--', 'raw', 'arg', 'values'])
+ .then(function(args) {
+ assert.deepEqual(args, { raw: ['raw', 'arg', 'values'] });
+ });
+ });
+
+ });
+
+});
+
+describe('Cmd', function() {
+
+ it('TODO');
+
+});
+
+function emptyFn() {
+ // empty function
+}
View
@@ -1,14 +0,0 @@
-var argv = process.argv.slice(2);
-require('../lib/coa').Cmd()
- .name('arr')
- .title('Array value test')
- .helpful()
- .opt()
- .name('arr').title('Array')
- .short('a').long('arr')
- .arr()
- .act(function(opts) {
- console.log(opts.arr);
- })
- .end()
- .run(argv.length? argv : ['-a', '1', '-a', '2']);
Oops, something went wrong.

0 comments on commit 1007370

Please sign in to comment.