Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[patch]: test for boolean in setarg should include aliases #34

Merged
merged 2 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ module.exports = function (args, opts) {
o = {};
}
if (o === Array.prototype) { o = []; }
if (o[lastKey] === undefined || flags.bools[lastKey] || typeof o[lastKey] === 'boolean') {
if (o[lastKey] === undefined || isBooleanKey(lastKey) || typeof o[lastKey] === 'boolean') {
o[lastKey] = value;
} else if (Array.isArray(o[lastKey])) {
o[lastKey].push(value);
Expand Down
122 changes: 122 additions & 0 deletions test/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
'use strict';

var parse = require('../');
var test = require('tape');

test('repeated auto strings accumulate in array', function (t) {
var argv = parse(['-s', 'foo', '-s', 'bar']);

t.deepEqual(argv, {
s: ['foo', 'bar'],
_: [],
});

t.end();
});

test('repeated declared strings accumulate in array', function (t) {
var argv = parse(['-s', 'foo', '-s', 'bar', '-s'], { string: ['s'] });

t.deepEqual(argv, {
s: ['foo', 'bar', ''],
_: [],
});

t.end();
});

test('repeated auto booleans overwrite', function (t) {
var argv = parse(['--bool', '--bool']);

t.deepEqual(argv, {
bool: true,
_: [],
});

t.end();
});

test('repeated declared booleans overwrite', function (t) {
var argv = parse(['--bool', 'moo', '--bool'], { boolean: ['bool'] });

t.deepEqual(argv, {
bool: true,
_: ['moo'],
});

t.end();
});

test('auto string overwrites auto bool', function (t) {
// Testing for coverage of existing behaviour rather than because this is by design.
var argv = parse(['--mixed', '--mixed', 'str']);

t.deepEqual(argv, {
mixed: 'str',
_: [],
});

t.end();
});

test('auto bool accumulates with auto string', function (t) {
// Testing for coverage of existing behaviour rather than because this is by design.
var argv = parse(['--mixed', 'str', '--mixed']);

t.deepEqual(argv, {
mixed: ['str', true],
_: [],
});

t.end();
});

test('declared boolean overwrites string', function (t) {
var options = {
boolean: ['b'],
};

// Verify the setup, that can get a string into the option. (Can't do this for long options.)
var argv1 = parse(['-b=xyz'], options);
t.deepEqual(argv1, {
b: 'xyz',
_: [],
});

// Check that declared boolean overwrites string, and does not accumulate into array.
var argv2 = parse(['-b=xyz', '-b'], options);

t.deepEqual(argv2, {
b: true,
_: [],
});

t.end();
});

test('declared boolean alias overwrites string', function (t) {
// https://github.com/minimistjs/minimist/issues/31
var options = {
boolean: ['b'],
alias: { b: 'B' },
};

// Verify the setup, that can get a string into the option. (Can't do this for long options.)
var argv1 = parse(['-B=xyz'], options);
t.deepEqual(argv1, {
b: 'xyz',
B: 'xyz',
_: [],
});

// Check that declared boolean overwrites string, and does not accumulate into array.
var argv2 = parse(['-B=xyz', '-B'], options);

t.deepEqual(argv2, {
b: true,
B: true,
_: [],
});

t.end();
});