From 679f897fe91c0799ff4f7c9b18343de830ca58ce Mon Sep 17 00:00:00 2001 From: usmonster Date: Sun, 29 Apr 2018 18:08:37 +0200 Subject: [PATCH] fix tests - Many tests were not properly invoking assertion functions. Now they are. - Fixed one test then was then failing because of side-effects from previous test setups. - Made small changes in some setups to avoid potential false negatives. (Also: made a tiny unrelated code change to ensure Options always has boolean properties; does not change any behavior, but may help with debugging.) --- index.js | 6 +++--- test/test.command.name.js | 4 ++-- test/test.command.name.set.js | 2 +- test/test.literal.args.js | 2 +- test/test.options.args.optional.js | 2 +- test/test.options.bool.js | 4 ++-- test/test.options.bool.no.js | 2 +- test/test.options.bool.small.combined.js | 4 ++-- test/test.options.bool.small.js | 4 ++-- test/test.options.commands.js | 23 ++++++++++++----------- test/test.options.func.js | 12 ++++++------ test/test.options.large-only.js | 2 +- 12 files changed, 34 insertions(+), 33 deletions(-) diff --git a/index.js b/index.js index fb648beec..0f54b5ef7 100644 --- a/index.js +++ b/index.js @@ -43,9 +43,9 @@ exports.Option = Option; function Option(flags, description) { this.flags = flags; - this.required = ~flags.indexOf('<'); - this.optional = ~flags.indexOf('['); - this.bool = !~flags.indexOf('-no-'); + this.required = flags.indexOf('<') >= 0; + this.optional = flags.indexOf('[') >= 0; + this.bool = flags.indexOf('-no-') === -1; flags = flags.split(/[ ,|]+/); if (flags.length > 1 && !/^[[<]/.test(flags[1])) this.short = flags.shift(); this.long = flags.shift(); diff --git a/test/test.command.name.js b/test/test.command.name.js index 16e064e46..4f6a36d0e 100644 --- a/test/test.command.name.js +++ b/test/test.command.name.js @@ -10,7 +10,7 @@ program program.parse(['node', 'test']); -program.name.should.be.a.Function; +program.name.should.be.a.Function(); program.name().should.equal('test'); program.commands[0].name().should.equal('mycommand'); program.commands[1].name().should.equal('help'); @@ -21,4 +21,4 @@ output[0].should.containEql([ ' mycommand [options] this is my command' ].join('\n')); -sinon.restore(); \ No newline at end of file +sinon.restore(); diff --git a/test/test.command.name.set.js b/test/test.command.name.set.js index e69c46ea0..a251e67c1 100644 --- a/test/test.command.name.set.js +++ b/test/test.command.name.set.js @@ -7,7 +7,7 @@ sinon.stub(process.stdout, 'write'); program.name('foobar').description('This is a test.'); -program.name.should.be.a.Function; +program.name.should.be.a.Function(); program.name().should.equal('foobar'); program.description().should.equal('This is a test.'); diff --git a/test/test.literal.args.js b/test/test.literal.args.js index 156c42cae..9cc938948 100644 --- a/test/test.literal.args.js +++ b/test/test.literal.args.js @@ -11,7 +11,7 @@ program .option('-b, --bar', 'add some bar'); program.parse(['node', 'test', '--foo', '--', '--bar', 'baz']); -program.foo.should.be.true; +program.foo.should.be.true(); should.equal(undefined, program.bar); program.args.should.eql(['--bar', 'baz']); diff --git a/test/test.options.args.optional.js b/test/test.options.args.optional.js index aacaccc9f..d36b384be 100644 --- a/test/test.options.args.optional.js +++ b/test/test.options.args.optional.js @@ -10,4 +10,4 @@ program .option('-c, --cheese [type]', 'optionally specify the type of cheese'); program.parse(['node', 'test', '--cheese']); -program.cheese.should.be.true; +program.cheese.should.be.true(); diff --git a/test/test.options.bool.js b/test/test.options.bool.js index 338a8caff..508b84b09 100644 --- a/test/test.options.bool.js +++ b/test/test.options.bool.js @@ -11,5 +11,5 @@ program .option('-c, --no-cheese', 'remove cheese'); program.parse(['node', 'test', '--pepper']); -program.pepper.should.be.true; -program.cheese.should.be.true; +program.pepper.should.be.true(); +program.cheese.should.be.true(); diff --git a/test/test.options.bool.no.js b/test/test.options.bool.no.js index bb7b8993f..c8c954439 100644 --- a/test/test.options.bool.no.js +++ b/test/test.options.bool.no.js @@ -12,4 +12,4 @@ program program.parse(['node', 'test', '--no-cheese']); should.equal(undefined, program.pepper); -program.cheese.should.be.false; +program.cheese.should.be.false(); diff --git a/test/test.options.bool.small.combined.js b/test/test.options.bool.small.combined.js index 0b1a3bfc1..9ee3a793c 100644 --- a/test/test.options.bool.small.combined.js +++ b/test/test.options.bool.small.combined.js @@ -11,5 +11,5 @@ program .option('-c, --no-cheese', 'remove cheese'); program.parse(['node', 'test', '-pc']); -program.pepper.should.be.true; -program.cheese.should.be.false; +program.pepper.should.be.true(); +program.cheese.should.be.false(); diff --git a/test/test.options.bool.small.js b/test/test.options.bool.small.js index 2818bfd4c..45c6a76b8 100644 --- a/test/test.options.bool.small.js +++ b/test/test.options.bool.small.js @@ -11,5 +11,5 @@ program .option('-c, --no-cheese', 'remove cheese'); program.parse(['node', 'test', '-p', '-c']); -program.pepper.should.be.true; -program.cheese.should.be.false; +program.pepper.should.be.true(); +program.cheese.should.be.false(); diff --git a/test/test.options.commands.js b/test/test.options.commands.js index f5c34d2ae..c33d093c0 100644 --- a/test/test.options.commands.js +++ b/test/test.options.commands.js @@ -47,15 +47,15 @@ program program.parse(['node', 'test', '--config', 'conf']); program.config.should.equal("conf"); -program.commands[0].should.not.have.property.setup_mode; -program.commands[1].should.not.have.property.exec_mode; +program.commands[0].should.not.have.property('setup_mode'); +program.commands[1].should.not.have.property('exec_mode'); envValue.should.equal(""); cmdValue.should.equal(""); -program.parse(['node', 'test', '--config', 'conf1', 'setup', '--setup_mode', 'mode3', 'env1']); +program.parse(['node', 'test', '--config', 'conf1', 'setup', '--setup_mode', 'mode2', 'env1']); program.config.should.equal("conf1"); -program.commands[0].setup_mode.should.equal("mode3"); -program.commands[0].should.not.have.property.host; +program.commands[0].setup_mode.should.equal("mode2"); +program.commands[0].should.not.have.property('host'); envValue.should.equal("env1"); program.parse(['node', 'test', '--config', 'conf2', 'setup', '--setup_mode', 'mode3', '-o', 'host1', 'env2']); @@ -72,7 +72,7 @@ envValue.should.equal("env3"); program.parse(['node', 'test', '--config', 'conf4', 'exec', '--exec_mode', 'mode1', 'exec1']); program.config.should.equal("conf4"); program.commands[1].exec_mode.should.equal("mode1"); -program.commands[1].should.not.have.property.target; +program.commands[1].should.not.have.property('target'); cmdValue.should.equal("exec1"); program.parse(['node', 'test', '--config', 'conf5', 'exec', '-e', 'mode2', 'exec2']); @@ -80,16 +80,17 @@ program.config.should.equal("conf5"); program.commands[1].exec_mode.should.equal("mode2"); cmdValue.should.equal("exec2"); -program.parse(['node', 'test', '--config', 'conf6', 'exec', '--target', 'target1', '-e', 'mode2', 'exec3']); +program.parse(['node', 'test', '--config', 'conf6', 'exec', '--target', 'target1', '-e', 'mode6', 'exec3']); program.config.should.equal("conf6"); -program.commands[1].exec_mode.should.equal("mode2"); +program.commands[1].exec_mode.should.equal("mode6"); program.commands[1].target.should.equal("target1"); cmdValue.should.equal("exec3"); +delete program.commands[1].target; program.parse(['node', 'test', '--config', 'conf7', 'ex', '-e', 'mode3', 'exec4']); program.config.should.equal("conf7"); program.commands[1].exec_mode.should.equal("mode3"); -program.commands[1].should.not.have.property.target; +program.commands[1].should.not.have.property('target'); cmdValue.should.equal("exec4"); // Make sure we still catch errors with required values for options @@ -120,5 +121,5 @@ catch (ex) { } process.exit = oldProcessExit; -exceptionOccurred.should.be.true; -customHelp.should.be.true; +exceptionOccurred.should.be.true(); +customHelp.should.be.true(); diff --git a/test/test.options.func.js b/test/test.options.func.js index 1acb39124..eb8c3bd07 100644 --- a/test/test.options.func.js +++ b/test/test.options.func.js @@ -11,13 +11,13 @@ program .option('-q, --quux ', 'add some quux'); program.parse(['node', 'test', '--foo', '--bar', '--no-magic', '--camel-case', '--quux', 'value']); -program.opts.should.be.a.Function; +program.opts.should.be.a.Function(); var opts = program.opts(); -opts.should.be.an.Object; +opts.should.be.an.Object(); opts.version.should.equal('0.0.1'); -opts.foo.should.be.true; -opts.bar.should.be.true; -opts.magic.should.be.false; -opts.camelCase.should.be.true; +opts.foo.should.be.true(); +opts.bar.should.be.true(); +opts.magic.should.be.false(); +opts.camelCase.should.be.true(); opts.quux.should.equal('value'); diff --git a/test/test.options.large-only.js b/test/test.options.large-only.js index 6f7e328f9..4f07ed9d8 100644 --- a/test/test.options.large-only.js +++ b/test/test.options.large-only.js @@ -10,4 +10,4 @@ program .option('--verbose', 'do stuff'); program.parse(['node', 'test', '--verbose']); -program.verbose.should.be.true; +program.verbose.should.be.true();