help.wrap.test.js
This file
Path
tests/help.wrap.test.js
213 lines · 7.5 KiB · JavaScript
Last changed by
Wrap command description in help (#1804)John Geebefore adoption186f0c8
History of this fileTake it
Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
lines 1–6why, one step back1const commander = require('../');23// These are tests of the Help class, not of the Command help.4// There is some overlap with the higher level Command tests (which predate Help).56describe('wrap', () => {Test coverage (#1433)John Geebefore adoption891e23d
line 7why, one step back7 test('when string fits into width then returns input', () => {Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
line 8why, one step back8 const text = 'a '.repeat(24) + 'a';Test coverage (#1433)John Geebefore adoption891e23d
lines 9–15why, one step back9 const helper = new commander.Help();10 const wrapped = helper.wrap(text, 50, 3);11 expect(wrapped).toEqual(text);12 });1314 test('when string shorter than indent then returns input', () => {15 const text = 'a';Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
lines 16–43why, one step back16 const helper = new commander.Help();17 const wrapped = helper.wrap(text, 50, 3);18 expect(wrapped).toEqual(text);19 });2021 test('when string exceeds width then wrap', () => {22 const text = 'a '.repeat(30) + 'a';23 const helper = new commander.Help();24 const wrapped = helper.wrap(text, 50, 0);25 expect(wrapped).toEqual(`${'a '.repeat(24)}a26${'a '.repeat(5)}a`);27 });2829 test('when string exceeds width then wrap and indent', () => {30 const text = 'a '.repeat(30) + 'a';31 const helper = new commander.Help();32 const wrapped = helper.wrap(text, 50, 10);33 expect(wrapped).toEqual(`${'a '.repeat(24)}a34${' '.repeat(10)}${'a '.repeat(5)}a`);35 });3637 test('when width < 40 then do not wrap', () => {38 const text = 'a '.repeat(30) + 'a';39 const helper = new commander.Help();40 const wrapped = helper.wrap(text, 39, 0);41 expect(wrapped).toEqual(text);42 });43Wrap command description in help (#1804)John Geebefore adoption186f0c8
line 44why, one step back44 test('when text has line break then respect and indent', () => {Rework wrap to have simpler usage pattern (#1395)John Geebefore adoption4555881
line 45why, one step back45 const text = 'term description\nanother line';Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
line 46why, one step back46 const helper = new commander.Help();Rework wrap to have simpler usage pattern (#1395)John Geebefore adoption4555881
lines 47–48why, one step back47 const wrapped = helper.wrap(text, 78, 5);48 expect(wrapped).toEqual('term description\n another line');Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
lines 49–50why, one step back49 });50Wrap command description in help (#1804)John Geebefore adoption186f0c8
lines 51–71why, one step back51 test('when text has consecutive line breaks then respect and indent', () => {52 const text = 'term description\n\nanother line';53 const helper = new commander.Help();54 const wrapped = helper.wrap(text, 78, 5);55 expect(wrapped).toEqual('term description\n\n another line');56 });5758 test('when text has Windows line break then respect and indent', () => {59 const text = 'term description\r\nanother line';60 const helper = new commander.Help();61 const wrapped = helper.wrap(text, 78, 5);62 expect(wrapped).toEqual('term description\n another line');63 });6465 test('when text has Windows consecutive line breaks then respect and indent', () => {66 const text = 'term description\r\n\r\nanother line';67 const helper = new commander.Help();68 const wrapped = helper.wrap(text, 78, 5);69 expect(wrapped).toEqual('term description\n\n another line');70 });71Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
line 72why, one step back72 test('when text already formatted with line breaks and indent then do not touch', () => {Rework wrap to have simpler usage pattern (#1395)John Geebefore adoption4555881
line 73why, one step back73 const text = 'term a '.repeat(25) + '\n ' + 'a '.repeat(25) + 'a';Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
line 74why, one step back74 const helper = new commander.Help();Rework wrap to have simpler usage pattern (#1395)John Geebefore adoption4555881
line 75why, one step back75 const wrapped = helper.wrap(text, 78, 5);Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
lines 76–86why, one step back76 expect(wrapped).toEqual(text);77 });78});7980describe('wrapping by formatHelp', () => {81 // Test auto wrap and indent with some manual strings.82 // Fragile tests with complete help output.8384 test('when long option description then wrap and indent', () => {85 const program = new commander.Command();86 programRename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 87why, one step back87 .configureHelp({ helpWidth: 80 })Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
lines 88–105why, one step back88 .option('-x --extra-long-option-switch', 'kjsahdkajshkahd kajhsd akhds kashd kajhs dkha dkh aksd ka dkha kdh kasd ka kahs dkh sdkh askdh aksd kashdk ahsd kahs dkha skdh');8990 const expectedOutput =91`Usage: [options]9293Options:94 -x --extra-long-option-switch kjsahdkajshkahd kajhsd akhds kashd kajhs dkha95 dkh aksd ka dkha kdh kasd ka kahs dkh sdkh96 askdh aksd kashdk ahsd kahs dkha skdh97 -h, --help display help for command98`;99100 expect(program.helpInformation()).toBe(expectedOutput);101 });102103 test('when long option description and default then wrap and indent', () => {104 const program = new commander.Command();105 programRename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 106why, one step back106 .configureHelp({ helpWidth: 80 })Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
lines 107–120why, one step back107 .option('-x --extra-long-option <value>', 'kjsahdkajshkahd kajhsd akhds', 'aaa bbb ccc ddd eee fff ggg');108109 const expectedOutput =110`Usage: [options]111112Options:113 -x --extra-long-option <value> kjsahdkajshkahd kajhsd akhds (default: "aaa114 bbb ccc ddd eee fff ggg")115 -h, --help display help for command116`;117118 expect(program.helpInformation()).toBe(expectedOutput);119 });120Wrap command description in help (#1804)John Geebefore adoption186f0c8
line 121why, one step back121 test('when long subcommand description then wrap and indent', () => {Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
lines 122–123why, one step back122 const program = new commander.Command();123 programRename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 124why, one step back124 .configureHelp({ helpWidth: 80 })Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
lines 125–148why, one step back125 .option('-x --extra-long-option-switch', 'x')126 .command('alpha', 'Lorem mollit quis dolor ex do eu quis ad insa a commodo esse.');127128 const expectedOutput =129`Usage: [options] [command]130131Options:132 -x --extra-long-option-switch x133 -h, --help display help for command134135Commands:136 alpha Lorem mollit quis dolor ex do eu quis ad insa137 a commodo esse.138 help [command] display help for command139`;140141 expect(program.helpInformation()).toBe(expectedOutput);142 });143144 test('when not enough room then help not wrapped', () => {145 // Not wrapping if less than 40 columns available for wrapping.146 const program = new commander.Command();147 const commandDescription = 'description text of very long command which should not be automatically be wrapped. Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor labore eu.';148 programRename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 149why, one step back149 .configureHelp({ helpWidth: 60 })Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
lines 150–165why, one step back150 .command('1234567801234567890x', commandDescription);151152 const expectedOutput =153`Usage: [options] [command]154155Options:156 -h, --help display help for command157158Commands:159 1234567801234567890x ${commandDescription}160 help [command] display help for command161`;162163 expect(program.helpInformation()).toBe(expectedOutput);164 });165Wrap command description in help (#1804)John Geebefore adoption186f0c8
line 166why, one step back166 test('when option description pre-formatted then only add small indent', () => {Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
lines 167–170why, one step back167 // #396: leave custom format alone, apart from space-space indent168 const optionSpec = '-t, --time <HH:MM>';169 const program = new commander.Command();170 programRename columns to helpWidth (#1396)John Geebefore adoptioneac4787
line 171why, one step back171 .configureHelp({ helpWidth: 80 })Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
lines 172–189why, one step back172 .option(optionSpec, `select time173174Time can also be specified using special values:175 "dawn" - From night to sunrise.176`);177178 const expectedOutput =179`Usage: [options]180181Options:182 ${optionSpec} select time183 184 Time can also be specified using special values:185 "dawn" - From night to sunrise.186 187 -h, --help display help for command188`;189Wrap command description in help (#1804)John Geebefore adoption186f0c8
lines 190–210why, one step back190 expect(program.helpInformation()).toBe(expectedOutput);191 });192193 test('when command description long then wrapped', () => {194 const program = new commander.Command();195 program196 .configureHelp({ helpWidth: 80 })197 .description(`Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor labore eu Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor labore eu198After line break Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor labore eu Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor labore eu`);199 const expectedOutput = `Usage: [options]200201Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor labore202eu Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor203labore eu204After line break Do fugiat eiusmod ipsum laboris excepteur pariatur sint205ullamco tempor labore eu Do fugiat eiusmod ipsum laboris excepteur pariatur206sint ullamco tempor labore eu207208Options:209 -h, --help display help for command210`;Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
lines 211–213why, one step back211 expect(program.helpInformation()).toBe(expectedOutput);212 });213});