Permalink
Browse files

partial tests cleanup

  • Loading branch information...
puzrin committed Jan 8, 2013
1 parent 20b8c14 commit e702d5d73d976f4e2cfd8ee3f334ce2769a4575a
Showing with 736 additions and 724 deletions.
  1. +17 −21 test/appendConstDefault.js
  2. +143 −119 test/base.js
  3. +55 −50 test/childgroups.js
  4. +55 −52 test/choices.js
  5. +42 −43 test/constant.js
  6. +30 −34 test/helpargs.js
  7. +13 −13 test/namespace.js
  8. +48 −48 test/negarg.js
  9. +17 −21 test/optionalstar.js
  10. +26 −27 test/parents.js
  11. +12 −17 test/positionalappend.js
  12. +66 −67 test/sub_commands.js
  13. +177 −173 test/testgroup.js
  14. +35 −39 test/testoptionlike.js
View
@@ -5,28 +5,24 @@
var assert = require('assert');
var ArgumentParser = require('../lib/argparse').ArgumentParser;
-describe('ArgumentParser', function () {
- describe('....', function () {
- var parser;
- var args;
- it("TestOptionalsActionAppendConstWithDefault", function () {
- // Tests the append_const action for an Optional
- parser = new ArgumentParser({debug: true});
- parser.addArgument(['-b'], {action: 'appendConst', constant: 'Exception', defaultValue: ['X']});
- parser.addArgument(['-c'], {action: 'append', dest: 'b'});
-
- args = parser.parseArgs([]);
- assert.deepEqual(args, {b: ['X']});
- args = parser.parseArgs('-b'.split(' '));
- assert.deepEqual(args, {b: ['X', 'Exception']});
- args = parser.parseArgs('-b -cx -b -cyz'.split(' '));
- assert.deepEqual(args, {b: ['X', 'Exception', 'x', 'Exception', 'yz']});
- // problem is b not getting 'X' default
- });
+describe('....', function () {
+ var parser;
+ var args;
+
+ it("TestOptionalsActionAppendConstWithDefault", function () {
+ // Tests the append_const action for an Optional
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-b'], {action: 'appendConst', constant: 'Exception', defaultValue: ['X']});
+ parser.addArgument(['-c'], {action: 'append', dest: 'b'});
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, {b: ['X']});
+ args = parser.parseArgs('-b'.split(' '));
+ assert.deepEqual(args, {b: ['X', 'Exception']});
+ args = parser.parseArgs('-b -cx -b -cyz'.split(' '));
+ assert.deepEqual(args, {b: ['X', 'Exception', 'x', 'Exception', 'yz']});
+ // problem is b not getting 'X' default
});
});
-/*
-
-*/
View
@@ -1,145 +1,169 @@
-/*global describe, it, beforeEach*/
+/*global describe, it*/
'use strict';
var assert = require('assert');
var ArgumentParser = require('../lib/argparse').ArgumentParser;
-describe('ArgumentParser', function () {
- describe('base', function () {
- var parser;
- var args;
- beforeEach(function () {
- parser = new ArgumentParser({debug: true});
- parser.addArgument([ '-f', '--foo' ]);
- });
- it("should parse argument in short form", function () {
- args = parser.parseArgs('-f 1'.split(' '));
- assert.equal(args.foo, 1);
- args = parser.parseArgs('-f=1'.split(' '));
- assert.equal(args.foo, 1);
- args = parser.parseArgs('-f1'.split(' '));
- assert.equal(args.foo, 1);
- });
+describe('base', function () {
+ var parser;
+ var args;
- it("should parse argument in long form", function () {
- args = parser.parseArgs('--foo 1'.split(' '));
- assert.equal(args.foo, 1);
- args = parser.parseArgs('--foo=1'.split(' '));
- assert.equal(args.foo, 1);
- });
+ it("should parse argument in short form", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
- it("should parse multiple arguments", function () {
- parser.addArgument(['--bar' ]);
- args = parser.parseArgs('--foo 5 --bar 6'.split(' '));
- assert.equal(args.foo, 5);
- assert.equal(args.bar, 6);
- });
+ args = parser.parseArgs('-f 1'.split(' '));
+ assert.equal(args.foo, 1);
+ args = parser.parseArgs('-f=1'.split(' '));
+ assert.equal(args.foo, 1);
+ args = parser.parseArgs('-f1'.split(' '));
+ assert.equal(args.foo, 1);
+ });
- it("should check argument type", function () {
- parser.addArgument(['--bar' ], { type: 'int' });
- assert.throws(function () {
- parser.parseArgs('--bar bar'.split(' '));
- });
- assert.doesNotThrow(function () {
- parser.parseArgs('--bar 1'.split(' '));
- });
- });
+ it("should parse argument in long form", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
- it("should not drop down with empty args (without positional arguments)", function () {
- assert.doesNotThrow(function () {
- parser.parseArgs([]);
- });
- });
+ args = parser.parseArgs('--foo 1'.split(' '));
+ assert.equal(args.foo, 1);
+ args = parser.parseArgs('--foo=1'.split(' '));
+ assert.equal(args.foo, 1);
+ });
- it("should drop down with empty args (positional arguments)", function () {
- parser.addArgument([ 'baz']);
- assert.throws(
- function () {parser.parseArgs([]); },
- /too few arguments/
- );
- });
+ it("should parse multiple arguments", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument(['--bar']);
- it("should support pseudo-argument", function () {
- parser.addArgument([ 'bar' ], { nargs: '+' });
- args = parser.parseArgs([ '-f', 'foo', '--', '-f', 'bar' ]);
- assert.equal(args.foo, 'foo');
- assert.equal(args.bar.length, 2);
- });
+ args = parser.parseArgs('--foo 5 --bar 6'.split(' '));
+ assert.equal(args.foo, 5);
+ assert.equal(args.bar, 6);
+ });
- it("should support #setDefaults", function () {
- parser.setDefaults({bar: 1});
- args = parser.parseArgs([]);
- assert.equal(args.bar, 1);
- });
+ it("should check argument type", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument(['--bar' ], { type: 'int' });
- it("should throw TypeError with conflicting options", function () {
- assert.throws(
- function () {
- parser.addArgument(['-f']);
- },
- /Conflicting option string/
- );
- assert.throws(
- function () {
- parser.addArgument(['--foo']);
- },
- /Conflicting option string/
- );
- assert.throws(
- function () {
- parser.addArgument(['-f', '--flame']);
- },
- /Conflicting option string/
- );
- assert.throws(
- function () {
- parser.addArgument(['-m', '--foo']);
- },
- /Conflicting option string/
- );
+ assert.throws(function () {
+ parser.parseArgs('--bar bar'.split(' '));
+ });
+ assert.doesNotThrow(function () {
+ parser.parseArgs('--bar 1'.split(' '));
});
+ });
- it("should parse negative arguments", function () {
- parser.addArgument([ 'bar' ], { type: 'int', });
- args = parser.parseArgs(['-1']);
+ it("should not drop down with empty args (without positional arguments)", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
- assert.equal(args.bar, -1);
- });
+ assert.doesNotThrow(function () { parser.parseArgs([]); });
+ });
- it("should infer option destination from long and short options", function () {
- //parser.addArgument(['-f', '--foo']); // from long option
- parser.addArgument(['-g']); // from short option
- parser.addArgument(['-x'], { dest: 'xxx' });// from dest keyword
-
- args = parser.parseArgs(['-f', '1']);
- assert.deepEqual(args, { foo: '1', g: null, xxx: null});
- args = parser.parseArgs(['-g', '2']);
- assert.deepEqual(args, { foo: null, g: '2', xxx: null});
- args = parser.parseArgs(['-f', 1, '-g', 2, '-x', 3]);
- assert.deepEqual(args, { foo: 1, g: 2, xxx: 3});
- });
+ it("should drop down with empty args (positional arguments)", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument([ 'baz']);
- it("should accept 0 defaultValue", function () {
- parser.addArgument(['bar'], { nargs: '?', defaultValue: 0});
- args = parser.parseArgs([]);
- assert.equal(args.bar, 0);
- // could also test for '', and false
- });
+ assert.throws(
+ function () {parser.parseArgs([]); },
+ /too few arguments/
+ );
+ });
- it("should accept defaultValue for nargs:'*'", function () {
- parser.addArgument(['bar'], { nargs: '*', defaultValue: 42});
- args = parser.parseArgs([]);
- assert.equal(args.bar, 42);
- });
+ it("should support pseudo-argument", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument([ 'bar' ], { nargs: '+' });
- it("getDefault() should get defaults", function () {
- parser.addArgument(['-g', '--goo'], {defaultValue: 42});
- assert.equal(parser.getDefault('goo'), 42);
- assert.equal(parser.getDefault('help'), require('../lib/const').SUPPRESS);
- });
+ args = parser.parseArgs([ '-f', 'foo', '--', '-f', 'bar' ]);
+ assert.equal(args.foo, 'foo');
+ assert.equal(args.bar.length, 2);
+ });
+
+ it("should support #setDefaults", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.setDefaults({bar: 1});
+
+ args = parser.parseArgs([]);
+ assert.equal(args.bar, 1);
+ });
+
+ it("should throw TypeError with conflicting options", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+
+ assert.throws(
+ function () { parser.addArgument(['-f']); },
+ /Conflicting option string/
+ );
+ assert.throws(
+ function () { parser.addArgument(['--foo']); },
+ /Conflicting option string/
+ );
+ assert.throws(
+ function () { parser.addArgument(['-f', '--flame']); },
+ /Conflicting option string/
+ );
+ assert.throws(
+ function () { parser.addArgument(['-m', '--foo']); },
+ /Conflicting option string/
+ );
+ });
+
+ it("should parse negative arguments", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument([ 'bar' ], { type: 'int', });
+
+ args = parser.parseArgs(['-1']);
+ assert.equal(args.bar, -1);
+ });
+
+ it("should infer option destination from long and short options", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']); // from long option
+ parser.addArgument(['-g']); // from short option
+ parser.addArgument(['-x'], { dest: 'xxx' });// from dest keyword
+
+ args = parser.parseArgs(['-f', '1']);
+ assert.deepEqual(args, { foo: '1', g: null, xxx: null});
+ args = parser.parseArgs(['-g', '2']);
+ assert.deepEqual(args, { foo: null, g: '2', xxx: null});
+ args = parser.parseArgs(['-f', 1, '-g', 2, '-x', 3]);
+ assert.deepEqual(args, { foo: 1, g: 2, xxx: 3});
+ });
+
+ it("should accept 0 defaultValue", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument(['bar'], { nargs: '?', defaultValue: 0});
+
+ args = parser.parseArgs([]);
+ assert.equal(args.bar, 0);
+ // could also test for '', and false
+ });
+
+ it("should accept defaultValue for nargs:'*'", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument(['bar'], { nargs: '*', defaultValue: 42});
+
+ args = parser.parseArgs([]);
+ assert.equal(args.bar, 42);
+ });
+
+ it("getDefault() should get defaults", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument(['-g', '--goo'], {defaultValue: 42});
+
+ assert.equal(parser.getDefault('goo'), 42);
+ assert.equal(parser.getDefault('help'), require('../lib/const').SUPPRESS);
});
});
Oops, something went wrong.

0 comments on commit e702d5d

Please sign in to comment.