Skip to content
Myr

command.argumentVariations.test.js

This file

Path

tests/command.argumentVariations.test.js

104 lines · 3.7 KiB · JavaScript

Last changed by

Merge remote-tracking branch 'origin/develop' into release/12.xJohn Geebefore adoption9afbabd

History of this file

Take it

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

lines 1–6why, one step back
1const 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) => {

Merge remote-tracking branch 'origin/develop' into release/12.xJohn Geebefore adoption9afbabd

line 7why, one step back
7 const argument = cmd.registeredArguments[0];

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

lines 8–17why, one step back
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) => {

Merge remote-tracking branch 'origin/develop' into release/12.xJohn Geebefore adoption9afbabd

line 18why, one step back
18 const argument = cmd.registeredArguments[0];

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

lines 19–28why, one step back
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) => {

Merge remote-tracking branch 'origin/develop' into release/12.xJohn Geebefore adoption9afbabd

line 29why, one step back
29 const argument = cmd.registeredArguments[0];

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

lines 30–39why, one step back
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) => {

Merge remote-tracking branch 'origin/develop' into release/12.xJohn Geebefore adoption9afbabd

line 40why, one step back
40 const argument = cmd.registeredArguments[0];

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

lines 41–50why, one step back
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) => {

Merge remote-tracking branch 'origin/develop' into release/12.xJohn Geebefore adoption9afbabd

line 51why, one step back
51 const argument = cmd.registeredArguments[0];

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

lines 52–61why, one step back
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) => {

Merge remote-tracking branch 'origin/develop' into release/12.xJohn Geebefore adoption9afbabd

line 62why, one step back
62 const argument = cmd.registeredArguments[0];

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

lines 63–81why, one step back
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) => {

Merge remote-tracking branch 'origin/develop' into release/12.xJohn Geebefore adoption9afbabd

lines 82–83why, one step back
82 expect(cmd.registeredArguments[0].name()).toEqual('first');
83 expect(cmd.registeredArguments[1].name()).toEqual('second');

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

lines 84–101why, one step back
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'));

Merge remote-tracking branch 'origin/develop' into release/12.xJohn Geebefore adoption9afbabd

line 102why, one step back
102 const argNames = cmd.registeredArguments.map(arg => arg.name());

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

lines 103–104why, one step back
103 expect(argNames).toEqual(['arg1', 'arg2', 'arg3', 'arg4', 'arg5', 'arg6']);
104});