helpGroup.test.js
This file
Path
tests/helpGroup.test.js
255 lines · 9.2 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256import { test, describe } from 'node:test';import assert from 'node:assert/strict';import { Command, Option } from '../index.js';
// Similar tests for Option.helpGroup() and Command.helpGroup(),// and for Command.optionsGroup() and Command.commandsGroup().
describe('Option.helpGroup', () => { test('when add one option with helpGroup then help contains group', () => { const program = new Command(); program.addOption(new Option('--alpha').helpGroup('Greek:')); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Greek:\n *--alpha/); });
test('when add two options with helpGroup then help contains group', () => { const program = new Command(); program.addOption(new Option('--alpha').helpGroup('Greek:')); program.addOption(new Option('--beta').helpGroup('Greek:')); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Greek:\n *--alpha\n *--beta/); });});
describe('Command.helpGroup', () => { test('when add one command with helpGroup then help contains group', () => { const program = new Command(); program.command('alpha').helpGroup('Greek:'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Greek:\n *alpha/); });
test('when add two commands with helpGroup then help contains group', () => { const program = new Command(); program.command('alpha').helpGroup('Greek:'); program.command('beta').helpGroup('Greek:'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Greek:\n *alpha\n *beta/); });});
describe('Command.optionsGroup', () => { test('when add one option then help contains group', () => { const program = new Command(); program.optionsGroup('Greek:'); program.option('--alpha'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Greek:\n *--alpha/); });
test('when add two options then help contains group with two options', () => { const program = new Command(); program.optionsGroup('Greek:'); program.option('--alpha'); program.option('--beta'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Greek:\n *--alpha\n *--beta/); });
test('when add options with different groups then help contains two groups', () => { const program = new Command(); program.optionsGroup('Greek:'); program.option('--alpha'); program.optionsGroup('Latin:'); program.option('--unus'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Greek:\n *--alpha/); assert.match(helpInfo, /Latin:\n *--unus/); });
test('when implicit help option then help option not affected', () => { const program = new Command(); program.optionsGroup('Greek:'); program.option('--alpha'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Options:\n *-h, --help/); });
test('when option with helpGroup then helpGroup wins', () => { const program = new Command(); program.optionsGroup('Greek:'); program.addOption(new Option('--unus').helpGroup('Latin:')); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Latin:\n *--unus/); });
test('when add no options with heading then heading does not appear', () => { const program = new Command(); program.optionsGroup('Greek:'); const helpInfo = program.helpInformation(); assert.doesNotMatch(helpInfo, /Greek/); });
test('when add no visible options with heading then heading does not appear', () => { const program = new Command(); program.optionsGroup('Greek:'); program.addOption(new Option('--alpha').hideHelp()); const helpInfo = program.helpInformation(); assert.doesNotMatch(helpInfo, /Greek/); });
test('when .helpOption(flags) then help option in group', () => { const program = new Command(); program.optionsGroup('Greek:'); program.helpOption('--assist'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Greek:\n *--assist/); });
test('when .helpOption(true) then help option in group', () => { const program = new Command(); program.optionsGroup('Greek:'); program.helpOption(true); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Greek:\n *-h, --help/); });
test('when .version(str) then version option in group', () => { const program = new Command(); program.optionsGroup('Greek:'); program.version('1.2.3'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Greek:\n *-V, --version/); });
test('when set sortOptions then options are sorted within groups', () => { const program = new Command(); program.configureHelp({ sortOptions: true }); program.optionsGroup('Latin:'); program.option('--unus'); program.option('--duo'); program.optionsGroup('Greek:'); program.option('--beta'); program.option('--alpha'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Latin:\n *--duo\n *--unus/); assert.match(helpInfo, /Greek:\n *--alpha\n *--beta/); });
test('when set sortOptions then groups are in order added not sorted', () => { const program = new Command(); program.configureHelp({ sortOptions: true }); program.addOption(new Option('--bbb').helpGroup('BBB:')); program.addOption(new Option('--ccc').helpGroup('CCC:')); program.addOption(new Option('--aaa').helpGroup('AAA:')); const helpInfo = program.helpInformation(); assert.match( helpInfo, /BBB:\n *--bbb.*\n\nCCC:\n *--ccc.*\n\nAAA:\n *--aaa/, ); });});
describe('Command.commandsGroup', () => { test('when add one command then help contains group', () => { const program = new Command(); program.commandsGroup('Greek:'); program.command('alpha'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Greek:\n *alpha/); });
test('when add two commands then help contains group with two commands', () => { const program = new Command(); program.commandsGroup('Greek:'); program.command('alpha'); program.command('beta'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Greek:\n *alpha\n *beta/); });
test('when add commands with different groups then help contains two groups', () => { const program = new Command(); program.commandsGroup('Greek:'); program.command('alpha'); program.commandsGroup('Latin:'); program.command('unus'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Greek:\n *alpha/); assert.match(helpInfo, /Latin:\n *unus/); });
test('when implicit help command then help command not affected', () => { const program = new Command(); program.commandsGroup('Greek:'); program.command('alpha'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Commands:\n *help/); });
test('when command with custom helpGroup then helpGroup wins', () => { const program = new Command(); program.commandsGroup('Greek:'); program.command('unus').helpGroup('Latin:'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Latin:\n *unus/); });
test('when add no commands with heading then heading does not appear', () => { const program = new Command(); program.commandsGroup('Greek:'); const helpInfo = program.helpInformation(); assert.doesNotMatch(helpInfo, /Greek/); });
test('when add no visible command with heading then heading does not appear', () => { const program = new Command(); program.commandsGroup('Greek:'); program.command('alpha', { hidden: true }); const helpInfo = program.helpInformation(); assert.doesNotMatch(helpInfo, /Greek/); });
test('when .helpCommand(name) then help command in group', () => { const program = new Command(); program.command('foo'); program.commandsGroup('Greek:'); program.helpCommand('assist'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Greek:\n *assist/); });
test('when .helpCommand(true) then help command in group', () => { const program = new Command(); program.command('foo'); program.commandsGroup('Greek:'); program.helpCommand(true); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Greek:\n *help/); });
test('when set sortCommands then commands are sorted within groups', () => { const program = new Command(); program.configureHelp({ sortSubcommands: true }); program.commandsGroup('Latin:'); program.command('unus'); program.command('duo'); program.commandsGroup('Greek:'); program.command('beta'); program.command('alpha'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /Latin:\n *duo\n *unus/); assert.match(helpInfo, /Greek:\n *alpha\n *beta/); });
test('when set sortOptions then groups are in order added not sorted', () => { const program = new Command(); program.configureHelp({ sortSubcommands: true }); program.command('bbb').helpGroup('BBB:'); program.command('ccc').helpGroup('CCC:'); program.command('aaa').helpGroup('AAA:'); const helpInfo = program.helpInformation(); assert.match(helpInfo, /BBB:\n *bbb.*\n\nCCC:\n *ccc.*\n\nAAA:\n *aaa/); });});