options.variadic.test.js
This file
Path
tests/options.variadic.test.js
191 lines · 6.7 KiB · JavaScript
Last changed by
Merge from develop into release/15.x (with minor conflicts and test migrations)John Geebefore adoption26e3ba3
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192import * as commander from '../index.js';import { createTestCommand } from './testHelpers.js';import { test, describe } from 'node:test';import assert from 'node:assert/strict';
describe('variadic options', () => { describe('variadic option with required option-argument', () => { test('when variadic with value missing then error', () => { const program = createTestCommand(); program.option('-r,--required <value...>');
assert.throws(() => { program.parse(['--required'], { from: 'user' }); }); });
test('when variadic with one value then set in array', () => { const program = new commander.Command(); program.option('-r,--required <value...>');
program.parse(['--required', 'one'], { from: 'user' }); assert.deepEqual(program.opts().required, ['one']); });
test('when variadic with two values then set in array', () => { const program = new commander.Command(); program.option('-r,--required <value...>');
program.parse(['--required', 'one', 'two'], { from: 'user' }); assert.deepEqual(program.opts().required, ['one', 'two']); });
test('when variadic with repeated values then set in array', () => { const program = new commander.Command(); program.option('-r,--required <value...>');
program.parse(['--required', 'one', '--required', 'two'], { from: 'user', }); assert.deepEqual(program.opts().required, ['one', 'two']); });
test('when variadic used with choices and one value then set in array', () => { const program = new commander.Command(); program.addOption( new commander.Option('-r,--required <value...>').choices([ 'one', 'two', ]), );
program.parse(['--required', 'one'], { from: 'user' }); assert.deepEqual(program.opts().required, ['one']); });
test('when variadic used with choices and two values then set in array', () => { const program = new commander.Command(); program.addOption( new commander.Option('-r,--required <value...>').choices([ 'one', 'two', ]), );
program.parse(['--required', 'one', 'two'], { from: 'user' }); assert.deepEqual(program.opts().required, ['one', 'two']); });
test('when variadic with short combined argument then not variadic', () => { const program = new commander.Command(); program.option('-r,--required <value...>').argument('[arg]');
program.parse(['-rone', 'operand'], { from: 'user' }); assert.deepEqual(program.opts().required, ['one']); });
test('when variadic with long combined argument then not variadic', () => { const program = new commander.Command(); program.option('-r,--required <value...>').argument('[arg]');
program.parse(['--required=one', 'operand'], { from: 'user' }); assert.deepEqual(program.opts().required, ['one']); });
test('when variadic with value followed by option then option not eaten', () => { const program = new commander.Command(); program .option('-r,--required <value...>') .option('-f, --flag') .argument('[arg]');
program.parse(['-r', 'one', '-f'], { from: 'user' }); const opts = program.opts(); assert.deepEqual(opts.required, ['one']); assert.equal(opts.flag, true); });
test('when variadic with no value and default then set to default', () => { const program = new commander.Command(); program.option( '-r,--required <value...>', 'variadic description', 'default', );
program.parse([], { from: 'user' }); assert.equal(program.opts().required, 'default'); });
test('when variadic with coercion then coercion sets value', () => { const program = new commander.Command(); program.option( '-r,--required <value...>', 'variadic description', parseFloat, );
// variadic processing reads the multiple values, but up to custom coercion what it does. program.parse(['--required', '1e2', '1e3'], { from: 'user' }); assert.equal(program.opts().required, 1000); }); });
// Not retesting everything, but do some tests on variadic with optional describe('variadic option with optional option-argument', () => { test('when variadic not specified then value undefined', () => { const program = new commander.Command(); program.option('-o,--optional [value...]');
program.parse([], { from: 'user' }); assert.equal(program.opts().optional, undefined); });
test('when variadic used as boolean flag then value true', () => { const program = new commander.Command(); program.option('-o,--optional [value...]');
program.parse(['--optional'], { from: 'user' }); assert.equal(program.opts().optional, true); });
test('when variadic with one value then set in array', () => { const program = new commander.Command(); program.option('-o,--optional [value...]');
program.parse(['--optional', 'one'], { from: 'user' }); assert.deepEqual(program.opts().optional, ['one']); });
test('when variadic with two values then set in array', () => { const program = new commander.Command(); program.option('-o,--optional [value...]');
program.parse(['--optional', 'one', 'two'], { from: 'user' }); assert.deepEqual(program.opts().optional, ['one', 'two']); }); });
describe('variadic special cases', () => { test('when option flags has word character before dots then is variadic', () => { const program = new commander.Command(); program.option('-c,--comma [value...]');
assert.equal(program.options[0].variadic, true); });
test('when option flags has special characters before dots then not variadic', () => { // This might be used to describe coercion for comma separated values, and is not variadic. const program = new commander.Command(); program.option('-c,--comma [value,...]');
assert.equal(program.options[0].variadic, false); }); });
test('when option has default array then specified value is used instead of default (not appended)', () => { const program = new commander.Command(); program.option('-c,--comma [value...]', 'values', ['default']); program.parse(['--comma', 'CCC'], { from: 'user' });
assert.deepEqual(program.opts().comma, ['CCC']); });
test('when option has default array then specified value is used instead of default (not appended)', () => { const program = new commander.Command(); program.option('-c,--comma [value...]', 'values', ['default']); program.parse(['--comma', 'CCC'], { from: 'user' });
assert.deepEqual(program.opts().comma, ['CCC']); });});