Skip to content
Myr

argument.variadic.test.js

This file

Path

tests/argument.variadic.test.js

106 lines · 3.4 KiB · JavaScript

Last changed by

Choices for arguments (#1525)John Geebefore adoption708527e

History of this file

Take it

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

lines 1–83why, one step back
1const commander = require('../');
23// Testing variadic arguments. Testing all the action arguments, but could test just variadicArg.45describe('variadic argument', () => {6 test('when no extra arguments specified for program then variadic arg is empty array', () => {7 const actionMock = jest.fn();8 const program = new commander.Command();9 program10 .argument('<id>')11 .argument('[variadicArg...]')12 .action(actionMock);1314 program.parse(['node', 'test', 'id']);1516 expect(actionMock).toHaveBeenCalledWith('id', [], program.opts(), program);17 });1819 test('when extra arguments specified for program then variadic arg is array of values', () => {20 const actionMock = jest.fn();21 const program = new commander.Command();22 program23 .addArgument(new commander.Argument('<id>'))24 .argument('[variadicArg...]')25 .action(actionMock);26 const extraArguments = ['a', 'b', 'c'];2728 program.parse(['node', 'test', 'id', ...extraArguments]);2930 expect(actionMock).toHaveBeenCalledWith('id', extraArguments, program.opts(), program);31 });3233 test('when no extra arguments specified for command then variadic arg is empty array', () => {34 const actionMock = jest.fn();35 const program = new commander.Command();36 const cmd = program37 .command('sub [variadicArg...]')38 .action(actionMock);3940 program.parse(['node', 'test', 'sub']);4142 expect(actionMock).toHaveBeenCalledWith([], cmd.opts(), cmd);43 });4445 test('when extra arguments specified for command then variadic arg is array of values', () => {46 const actionMock = jest.fn();47 const program = new commander.Command();48 const cmd = program49 .command('sub [variadicArg...]')50 .action(actionMock);51 const extraArguments = ['a', 'b', 'c'];5253 program.parse(['node', 'test', 'sub', ...extraArguments]);5455 expect(actionMock).toHaveBeenCalledWith(extraArguments, cmd.opts(), cmd);56 });5758 test('when program variadic argument not last then error', () => {59 const program = new commander.Command();6061 expect(() => {62 program63 .argument('<variadicArg...>')64 .argument('[optionalArg]');65 }).toThrow("only the last argument can be variadic 'variadicArg'");66 });6768 test('when command variadic argument not last then error', () => {69 const program = new commander.Command();7071 expect(() => {72 program.command('sub <variadicArg...> [optionalArg]');73 }).toThrow("only the last argument can be variadic 'variadicArg'");74 });7576 test('when variadic argument then usage shows variadic', () => {77 const program = new commander.Command();78 program79 .name('foo')80 .argument('[args...]');8182 expect(program.usage()).toBe('[options] [args...]');83 });

Choices for arguments (#1525)John Geebefore adoption708527e

lines 84–105why, one step back
84
85 test('when variadic used with choices and one value then set in array', () => {86 const program = new commander.Command();87 let passedArg;88 program89 .addArgument(new commander.Argument('<value...>').choices(['one', 'two']))90 .action((value) => { passedArg = value; });9192 program.parse(['one'], { from: 'user' });93 expect(passedArg).toEqual(['one']);94 });9596 test('when variadic used with choices and two values then set in array', () => {97 const program = new commander.Command();98 let passedArg;99 program100 .addArgument(new commander.Argument('<value...>').choices(['one', 'two']))101 .action((value) => { passedArg = value; });102103 program.parse(['one', 'two'], { from: 'user' });104 expect(passedArg).toEqual(['one', 'two']);105 });

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 106why, one step back
106});