From 62db1ea4176c34e7bdc579df91035d57607b1c72 Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Sat, 29 Jan 2022 12:36:08 +0000 Subject: [PATCH] additional testing for duplicate defaultOptions --- dist/index.js | 2 +- dist/index.mjs | 2 +- dist/tests.js | 30 +++++++++++++++++++++++++- test/exceptions-invalid-definition.mjs | 14 ++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/dist/index.js b/dist/index.js index 43d961f..90a0c5e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -929,7 +929,7 @@ class Definitions extends Array { ); } - const duplicateDefaultOption = hasDuplicates(this.map(def => def.defaultOption)); + const duplicateDefaultOption = this.filter(def => def.defaultOption === true).length > 1; if (duplicateDefaultOption) { halt( 'INVALID_DEFINITIONS', diff --git a/dist/index.mjs b/dist/index.mjs index f55dfee..00683e4 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -925,7 +925,7 @@ class Definitions extends Array { ); } - const duplicateDefaultOption = hasDuplicates(this.map(def => def.defaultOption)); + const duplicateDefaultOption = this.filter(def => def.defaultOption === true).length > 1; if (duplicateDefaultOption) { halt( 'INVALID_DEFINITIONS', diff --git a/dist/tests.js b/dist/tests.js index 7e4cb46..996cc72 100644 --- a/dist/tests.js +++ b/dist/tests.js @@ -931,7 +931,7 @@ class Definitions extends Array { ); } - const duplicateDefaultOption = hasDuplicates(this.map(def => def.defaultOption)); + const duplicateDefaultOption = this.filter(def => def.defaultOption === true).length > 1; if (duplicateDefaultOption) { halt( 'INVALID_DEFINITIONS', @@ -2350,6 +2350,20 @@ runner$c.test('defaultOption: multiple-defaultOption values spread out', functio }); }); +runner$c.test('defaultOption: can be false', function () { + const optionDefinitions = [ + { name: 'one', defaultOption: false }, + { name: 'two', defaultOption: false }, + { name: 'files', defaultOption: true, multiple: true } + ]; + const argv = ['--one', '1', 'file1', 'file2', '--two', '2']; + a.deepStrictEqual(commandLineArgs(optionDefinitions, { argv }), { + one: '1', + two: '2', + files: ['file1', 'file2'] + }); +}); + runner$c.test('defaultOption: multiple-defaultOption values spread out 2', function () { const optionDefinitions = [ { name: 'one', type: Boolean }, @@ -2761,6 +2775,20 @@ runner$h.test('err-invalid-definition: multiple defaultOption', function () { ); }); +runner$h.test('err-invalid-definition: multiple defaultOptions 2', function () { + const optionDefinitions = [ + { name: 'one', defaultOption: undefined }, + { name: 'two', defaultOption: false }, + { name: 'files', defaultOption: true, multiple: true }, + { name: 'files2', defaultOption: true } + ]; + const argv = ['--one', '1', 'file1', 'file2', '--two', '2']; + a.throws( + () => commandLineArgs(optionDefinitions, { argv }), + err => err.name === 'INVALID_DEFINITIONS' + ); +}); + runner$h.test('err-invalid-defaultOption: defaultOption on a Boolean type', function () { const optionDefinitions = [ { name: 'one', type: Boolean, defaultOption: true } diff --git a/test/exceptions-invalid-definition.mjs b/test/exceptions-invalid-definition.mjs index e228a90..a47e32b 100644 --- a/test/exceptions-invalid-definition.mjs +++ b/test/exceptions-invalid-definition.mjs @@ -165,6 +165,20 @@ runner.test('err-invalid-definition: multiple defaultOption', function () { ) }) +runner.test('err-invalid-definition: multiple defaultOptions 2', function () { + const optionDefinitions = [ + { name: 'one', defaultOption: undefined }, + { name: 'two', defaultOption: false }, + { name: 'files', defaultOption: true, multiple: true }, + { name: 'files2', defaultOption: true } + ] + const argv = ['--one', '1', 'file1', 'file2', '--two', '2'] + a.throws( + () => commandLineArgs(optionDefinitions, { argv }), + err => err.name === 'INVALID_DEFINITIONS' + ) +}) + runner.test('err-invalid-defaultOption: defaultOption on a Boolean type', function () { const optionDefinitions = [ { name: 'one', type: Boolean, defaultOption: true }