diff --git a/es5/definition.js b/es5/definition.js index ae28cd0..a083233 100644 --- a/es5/definition.js +++ b/es5/definition.js @@ -4,6 +4,8 @@ var _createClass = function () { function defineProperties(target, props) { for function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } +var t = require('typical'); + var OptionDefinition = function () { function OptionDefinition(definition) { _classCallCheck(this, OptionDefinition); @@ -41,19 +43,15 @@ var OptionDefinition = function () { }, { key: 'isBoolean', value: function isBoolean() { - if (!this.type) { + if (this.type) { + return this.type === Boolean || t.isFunction(this.type) && this.type.name === 'Boolean'; + } else { return false; - } else { // Fall back to ES5 Method - var result = /^function\s+([\w\$]+)\s*\(/.exec( this.type.toString() ); - if (result && result.length>=2 && result[1] == "Boolean") { - return true; - } } - return false; } }]); return OptionDefinition; }(); -module.exports = OptionDefinition; +module.exports = OptionDefinition; \ No newline at end of file diff --git a/lib/definition.js b/lib/definition.js index a53953c..c6492ec 100644 --- a/lib/definition.js +++ b/lib/definition.js @@ -1,4 +1,6 @@ 'use strict' +const t = require('typical') + /** * @module definition */ @@ -236,17 +238,11 @@ class OptionDefinition { } isBoolean () { - if (!this.type) { - return false; - } else if (this.type.name == "Boolean") { // First try ES6 Method - return true; - } else { // Fall back to ES5 Method - var result = /^function\s+([\w\$]+)\s*\(/.exec( this.type.toString() ); - if (result && result.length>=2 && result[1] == "Boolean") { - return true; - } + if (this.type) { + return this.type === Boolean || (t.isFunction(this.type) && this.type.name === 'Boolean') + } else { + return false } - return false; } } diff --git a/package.json b/package.json index 7cdcbe6..aa709ce 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "core-assert": "~0.2.0", "coveralls": "^2.11.12", "jsdoc-to-markdown": "^1.3.6", - "test-runner": "~0.1.4", + "test-runner": "~0.1.6", "test-value": "^2.0.0" }, "dependencies": { diff --git a/test/default-option.js b/test/default-option.js index 2be4b2d..d82cbb5 100644 --- a/test/default-option.js +++ b/test/default-option.js @@ -32,7 +32,6 @@ test('defaultOption: after a boolean', function () { cliArgs(definitions, [ '--one', 'sfsgf' ]), { one: true, two: 'sfsgf' } ) - }) test('defaultOption: multiple defaultOption values between other arg/value pairs', function () { @@ -71,5 +70,4 @@ test('defaultOption: floating args present but no defaultOption', function () { cliArgs(definitions, [ 'aaa', '--one', 'aaa', 'aaa' ]), { one: true } ) - }) diff --git a/test/exceptions.js b/test/exceptions.js index 9887155..c67d63a 100644 --- a/test/exceptions.js +++ b/test/exceptions.js @@ -29,7 +29,6 @@ test('err-invalid-definition: throws if dev set a numeric alias', function () { } catch (err) { a.strictEqual(err.name, 'INVALID_ALIAS') } - }) test('err-invalid-definition: throws if dev set an alias of "-"', function () { @@ -44,7 +43,6 @@ test('err-invalid-definition: throws if dev set an alias of "-"', function () { } catch (err) { a.strictEqual(err.name, 'INVALID_ALIAS') } - }) test('err-invalid-definition: multi-character alias', function () { @@ -59,7 +57,6 @@ test('err-invalid-definition: multi-character alias', function () { } catch (err) { a.strictEqual(err.name, 'INVALID_ALIAS') } - }) test('err-invalid-definition: invalid type values', function () { @@ -88,7 +85,6 @@ test('err-invalid-definition: invalid type values', function () { a.doesNotThrow(function () { cliArgs([ { name: 'one', type: function () {} } ], argv) }, /invalid/i) - }) test('err-invalid-definition: value without option definition', function () { @@ -128,7 +124,6 @@ test('err-invalid-definition: value without option definition', function () { } catch (err) { a.strictEqual(err.name, 'UNKNOWN_OPTION', 'getOpts') } - }) test('err-invalid-definition: duplicate name', function () { @@ -144,7 +139,6 @@ test('err-invalid-definition: duplicate name', function () { } catch (err) { a.strictEqual(err.name, 'DUPLICATE_NAME') } - }) test('err-invalid-definition: duplicate alias', function () { @@ -160,7 +154,6 @@ test('err-invalid-definition: duplicate alias', function () { } catch (err) { a.strictEqual(err.name, 'DUPLICATE_ALIAS') } - }) test('err-invalid-definition: multiple defaultOption', function () { @@ -176,5 +169,4 @@ test('err-invalid-definition: multiple defaultOption', function () { } catch (err) { a.strictEqual(err.name, 'DUPLICATE_DEFAULT_OPTION') } - }) diff --git a/test/type-boolean.js b/test/type-boolean.js index 2a9f2ee..8b837ee 100644 --- a/test/type-boolean.js +++ b/test/type-boolean.js @@ -3,11 +3,11 @@ var test = require('test-runner') var cliArgs = require('../') var a = require('core-assert') -var optionDefinitions = [ - { name: 'one', type: Boolean } -] - test('type-boolean: different values', function () { + var optionDefinitions = [ + { name: 'one', type: Boolean } + ] + a.deepStrictEqual( cliArgs(optionDefinitions, [ '--one' ]), { one: true } @@ -25,3 +25,33 @@ test('type-boolean: different values', function () { { one: true } ) }) + +var origBoolean = Boolean + +/* test in contexts which override the standard global Boolean constructor */ +test('type-boolean: global Boolean overridden', function () { + function Boolean () { + return origBoolean.apply(origBoolean, arguments) + } + + var optionDefinitions = [ + { name: 'one', type: Boolean } + ] + + a.deepStrictEqual( + cliArgs(optionDefinitions, [ '--one', 'true' ]), + { one: true } + ) + a.deepStrictEqual( + cliArgs(optionDefinitions, [ '--one', 'false' ]), + { one: true } + ) + a.deepStrictEqual( + cliArgs(optionDefinitions, [ '--one', 'sfsgf' ]), + { one: true } + ) + a.deepStrictEqual( + cliArgs(optionDefinitions, [ '--one' ]), + { one: true } + ) +}) diff --git a/test/type-other.js b/test/type-other.js index 888e4cf..0f4ad27 100644 --- a/test/type-other.js +++ b/test/type-other.js @@ -3,13 +3,13 @@ var test = require('test-runner') var cliArgs = require('../') var a = require('core-assert') -var optionDefinitions = [ - { name: 'file', type: function (file) { - return file - }} -] - test('type-other: different values', function () { + var optionDefinitions = [ + { name: 'file', type: function (file) { + return file + }} + ] + a.deepStrictEqual( cliArgs(optionDefinitions, [ '--file', 'one.js' ]), { file: 'one.js' } @@ -19,3 +19,14 @@ test('type-other: different values', function () { { file: null } ) }) + +test('type-other: broken custom type function', function () { + var optionDefinitions = [ + { name: 'file', type: function (file) { + lasdfjsfakn + }} + ] + a.throws(function () { + cliArgs(optionDefinitions, [ '--file', 'one.js' ]) + }) +}) diff --git a/test/type-string.js b/test/type-string.js index ff6e6a3..426f193 100644 --- a/test/type-string.js +++ b/test/type-string.js @@ -28,5 +28,4 @@ test.skip('type-string: pass a value resembling an option', function () { cliArgs(optionDefinitions, [ '--one', '--yeah' ]), { one: '--yeah' } ) - t.end() })