command.argumentVariations.test.js
This file
Path
tests/command.argumentVariations.test.js
135 lines · 4.9 KiB · JavaScript
Last changed by
Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136const commander = require('../');const { test, describe } = require('node:test');const assert = require('node:assert/strict');
// Do some low-level checks that the multiple ways of specifying command arguments produce same internal result,// and not exhaustively testing all methods elsewhere.
describe('Command arguments added using different methods', () => { describe('when add "<arg>" then argument required', () => { getSingleArgCases('<explicit-required>').forEach(([methodName, cmd]) => { test(`using ${methodName}`, () => { const argument = cmd.registeredArguments[0]; assert.equal(argument._name, 'explicit-required'); assert.equal(argument.required, true); assert.equal(argument.variadic, false); assert.equal(argument.description, ''); }); }); });
describe('when add "arg" then argument required', () => { getSingleArgCases('implicit-required').forEach(([methodName, cmd]) => { test(`using ${methodName}`, () => { const argument = cmd.registeredArguments[0]; assert.equal(argument._name, 'implicit-required'); assert.equal(argument.required, true); assert.equal(argument.variadic, false); assert.equal(argument.description, ''); }); }); });
describe('when add "[arg]" then argument optional', () => { getSingleArgCases('[optional]').forEach(([methodName, cmd]) => { test(`using ${methodName}`, () => { const argument = cmd.registeredArguments[0]; assert.equal(argument._name, 'optional'); assert.equal(argument.required, false); assert.equal(argument.variadic, false); assert.equal(argument.description, ''); }); }); });
describe('when add "<arg...>" then argument required and variadic', () => { getSingleArgCases('<explicit-required...>').forEach(([methodName, cmd]) => { test(`using ${methodName}`, () => { const argument = cmd.registeredArguments[0]; assert.equal(argument._name, 'explicit-required'); assert.equal(argument.required, true); assert.equal(argument.variadic, true); assert.equal(argument.description, ''); }); }); });
describe('when add "arg..." then argument required and variadic', () => { getSingleArgCases('implicit-required...').forEach(([methodName, cmd]) => { test(`using ${methodName}`, () => { const argument = cmd.registeredArguments[0]; assert.equal(argument._name, 'implicit-required'); assert.equal(argument.required, true); assert.equal(argument.variadic, true); assert.equal(argument.description, ''); }); }); });
describe('when add "[arg...]" then argument optional and variadic', () => { getSingleArgCases('[optional...]').forEach(([methodName, cmd]) => { test(`using ${methodName}`, () => { const argument = cmd.registeredArguments[0]; assert.equal(argument._name, 'optional'); assert.equal(argument.required, false); assert.equal(argument.variadic, true); assert.equal(argument.description, ''); }); }); });
function getSingleArgCases(arg) { return [ ['.arguments', new commander.Command().arguments(arg)], ['.argument', new commander.Command().argument(arg)], [ '.addArgument', new commander.Command('add-argument').addArgument( new commander.Argument(arg), ), ], ['.command', new commander.Command().command(`command ${arg}`)], ]; }
describe('when add two arguments then two arguments', () => { getMultipleArgCases('<first>', '[second]').forEach(([methodName, cmd]) => { test(`using ${methodName}`, () => { assert.equal(cmd.registeredArguments[0].name(), 'first'); assert.equal(cmd.registeredArguments[1].name(), 'second'); }); }); });
function getMultipleArgCases(arg1, arg2) { return [ ['.arguments', new commander.Command().arguments(`${arg1} ${arg2}`)], ['.argument', new commander.Command().argument(arg1).argument(arg2)], [ '.addArgument', new commander.Command('add-argument') .addArgument(new commander.Argument(arg1)) .addArgument(new commander.Argument(arg2)), ], ['.command', new commander.Command().command(`command ${arg1} ${arg2}`)], ]; }
test('when add arguments using multiple methods then all added', () => { // This is not a key use case, but explicitly test that additive behaviour. const program = new commander.Command(); const cmd = program.command('sub <arg1> <arg2>'); cmd.arguments('<arg3> <arg4>'); cmd.argument('<arg5>'); cmd.addArgument(new commander.Argument('arg6')); const argNames = cmd.registeredArguments.map((arg) => arg.name()); assert.deepEqual(argNames, [ 'arg1', 'arg2', 'arg3', 'arg4', 'arg5', 'arg6', ]); });});