help.sortOptions.test.js
This file
Path
tests/help.sortOptions.test.js
78 lines · 3.0 KiB · JavaScript
Last changed by
WYSIWYG sort (#1400)John Geebefore adoption9b087c8
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879const commander = require('../');
// These are tests of the Help class, not of the Command help.// There is some overlap with the higher level Command tests (which predate Help).
describe('sortOptions', () => { test('when unsorted then options in order added', () => { const program = new commander.Command(); program .option('--zzz', 'desc') .option('--aaa', 'desc') .option('--bbb', 'desc'); const helper = program.createHelp(); const visibleOptionNames = helper.visibleOptions(program).map(option => option.name()); expect(visibleOptionNames).toEqual(['zzz', 'aaa', 'bbb', 'help']); });
test('when sortOptions:true then options sorted alphabetically', () => { const program = new commander.Command(); program .configureHelp({ sortOptions: true }) .option('--zzz', 'desc') .option('--aaa', 'desc') .option('--bbb', 'desc'); const helper = program.createHelp(); const visibleOptionNames = helper.visibleOptions(program).map(cmd => cmd.name()); expect(visibleOptionNames).toEqual(['aaa', 'bbb', 'help', 'zzz']); });
test('when both short and long flags then sort on short flag', () => { const program = new commander.Command(); program .configureHelp({ sortOptions: true }) .option('-m,--zzz', 'desc') .option('-n,--aaa', 'desc') .option('-o,--bbb', 'desc'); const helper = program.createHelp(); const visibleOptionNames = helper.visibleOptions(program).map(cmd => cmd.name()); expect(visibleOptionNames).toEqual(['help', 'zzz', 'aaa', 'bbb']); });
test('when lone short and long flags then sort on lone flag', () => { const program = new commander.Command(); program .configureHelp({ sortOptions: true }) .option('--zzz', 'desc') .option('--aaa', 'desc') .option('-b', 'desc'); const helper = program.createHelp(); const visibleOptionNames = helper.visibleOptions(program).map(cmd => cmd.name()); expect(visibleOptionNames).toEqual(['aaa', 'b', 'help', 'zzz']); });
test('when mixed case flags then sort is case insensitive', () => { const program = new commander.Command(); program .configureHelp({ sortOptions: true }) .option('-B', 'desc') .option('-a', 'desc') .option('-c', 'desc'); const helper = program.createHelp(); const visibleOptionNames = helper.visibleOptions(program).map(cmd => cmd.name()); expect(visibleOptionNames).toEqual(['a', 'B', 'c', 'help']); });
test('when negated option then sort negated option separately', () => { const program = new commander.Command(); program .configureHelp({ sortOptions: true }) .option('--bbb', 'desc') .option('--ccc', 'desc') .option('--no-bbb', 'desc') .option('--aaa', 'desc'); const helper = program.createHelp(); const visibleOptionNames = helper.visibleOptions(program).map(cmd => cmd.name()); expect(visibleOptionNames).toEqual(['aaa', 'bbb', 'ccc', 'help', 'no-bbb']); });});