Skip to content

Commit

Permalink
additional testing for duplicate defaultOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Jan 29, 2022
1 parent 8920ca5 commit 62db1ea
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
30 changes: 29 additions & 1 deletion dist/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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 }
Expand Down
14 changes: 14 additions & 0 deletions test/exceptions-invalid-definition.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 62db1ea

Please # to comment.