options.variadic.test.js
This file
Path
tests/options.variadic.test.js
170 lines · 5.6 KiB · JavaScript
Last changed by
More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171const commander = require('../');
describe('variadic option with required value', () => { test('when variadic with value missing then error', () => { const program = new commander.Command(); program .exitOverride() .configureOutput({ writeErr: jest.fn() }) .option('-r,--required <value...>');
expect(() => { program.parse(['--required'], { from: 'user' }); }).toThrow(); });
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' }); expect(program.opts().required).toEqual(['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' }); expect(program.opts().required).toEqual(['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' }); expect(program.opts().required).toEqual(['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' }); expect(program.opts().required).toEqual(['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' }); expect(program.opts().required).toEqual(['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' }); expect(program.opts().required).toEqual(['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' }); expect(program.opts().required).toEqual(['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(); expect(opts.required).toEqual(['one']); expect(opts.flag).toBe(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' }); expect(program.opts().required).toEqual('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' }); expect(program.opts().required).toEqual(1000); });});
// Not retesting everything, but do some tests on variadic with optionaldescribe('variadic option with optional value', () => { test('when variadic not specified then value undefined', () => { const program = new commander.Command(); program .option('-o,--optional [value...]');
program.parse([], { from: 'user' }); expect(program.opts().optional).toBeUndefined(); });
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' }); expect(program.opts().optional).toBe(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' }); expect(program.opts().optional).toEqual(['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' }); expect(program.opts().optional).toEqual(['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...]');
expect(program.options[0].variadic).toBeTruthy(); });
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,...]');
expect(program.options[0].variadic).toBeFalsy(); });});