command.argumentVariations.test.js
This file
Path
tests/command.argumentVariations.test.js
104 lines · 3.6 KiB · JavaScript
Last changed by
More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
History of this fileTake it
More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
lines 1–104why, one step back1const commander = require('../');23// Do some low-level checks that the multiple ways of specifying command arguments produce same internal result,4// and not exhaustively testing all methods elsewhere.56test.each(getSingleArgCases('<explicit-required>'))('when add "<arg>" using %s then argument required', (methodName, cmd) => {7 const argument = cmd._args[0];8 const expectedShape = {9 _name: 'explicit-required',10 required: true,11 variadic: false,12 description: ''13 };14 expect(argument).toEqual(expectedShape);15});1617test.each(getSingleArgCases('implicit-required'))('when add "arg" using %s then argument required', (methodName, cmd) => {18 const argument = cmd._args[0];19 const expectedShape = {20 _name: 'implicit-required',21 required: true,22 variadic: false,23 description: ''24 };25 expect(argument).toEqual(expectedShape);26});2728test.each(getSingleArgCases('[optional]'))('when add "[arg]" using %s then argument optional', (methodName, cmd) => {29 const argument = cmd._args[0];30 const expectedShape = {31 _name: 'optional',32 required: false,33 variadic: false,34 description: ''35 };36 expect(argument).toEqual(expectedShape);37});3839test.each(getSingleArgCases('<explicit-required...>'))('when add "<arg...>" using %s then argument required and variadic', (methodName, cmd) => {40 const argument = cmd._args[0];41 const expectedShape = {42 _name: 'explicit-required',43 required: true,44 variadic: true,45 description: ''46 };47 expect(argument).toEqual(expectedShape);48});4950test.each(getSingleArgCases('implicit-required...'))('when add "arg..." using %s then argument required and variadic', (methodName, cmd) => {51 const argument = cmd._args[0];52 const expectedShape = {53 _name: 'implicit-required',54 required: true,55 variadic: true,56 description: ''57 };58 expect(argument).toEqual(expectedShape);59});6061test.each(getSingleArgCases('[optional...]'))('when add "[arg...]" using %s then argument optional and variadic', (methodName, cmd) => {62 const argument = cmd._args[0];63 const expectedShape = {64 _name: 'optional',65 required: false,66 variadic: true,67 description: ''68 };69 expect(argument).toEqual(expectedShape);70});7172function getSingleArgCases(arg) {73 return [74 ['.arguments', new commander.Command().arguments(arg)],75 ['.argument', new commander.Command().argument(arg)],76 ['.addArgument', new commander.Command('add-argument').addArgument(new commander.Argument(arg))],77 ['.command', new commander.Command().command(`command ${arg}`)]78 ];79}8081test.each(getMultipleArgCases('<first>', '[second]'))('when add two arguments using %s then two arguments', (methodName, cmd) => {82 expect(cmd._args[0].name()).toEqual('first');83 expect(cmd._args[1].name()).toEqual('second');84});8586function getMultipleArgCases(arg1, arg2) {87 return [88 ['.arguments', new commander.Command().arguments(`${arg1} ${arg2}`)],89 ['.argument', new commander.Command().argument(arg1).argument(arg2)],90 ['.addArgument', new commander.Command('add-argument').addArgument(new commander.Argument(arg1)).addArgument(new commander.Argument(arg2))],91 ['.command', new commander.Command().command(`command ${arg1} ${arg2}`)]92 ];93}9495test('when add arguments using multiple methods then all added', () => {96 // This is not a key use case, but explicitly test that additive behaviour.97 const program = new commander.Command();98 const cmd = program.command('sub <arg1> <arg2>');99 cmd.arguments('<arg3> <arg4>');100 cmd.argument('<arg5>');101 cmd.addArgument(new commander.Argument('arg6'));102 const argNames = cmd._args.map(arg => arg.name());103 expect(argNames).toEqual(['arg1', 'arg2', 'arg3', 'arg4', 'arg5', 'arg6']);104});