/
help.wrap.test.js
213 lines (173 loc) · 7.51 KB
/
help.wrap.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const 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('wrap', () => {
test('when string fits into width then returns input', () => {
const text = 'a '.repeat(24) + 'a';
const helper = new commander.Help();
const wrapped = helper.wrap(text, 50, 3);
expect(wrapped).toEqual(text);
});
test('when string shorter than indent then returns input', () => {
const text = 'a';
const helper = new commander.Help();
const wrapped = helper.wrap(text, 50, 3);
expect(wrapped).toEqual(text);
});
test('when string exceeds width then wrap', () => {
const text = 'a '.repeat(30) + 'a';
const helper = new commander.Help();
const wrapped = helper.wrap(text, 50, 0);
expect(wrapped).toEqual(`${'a '.repeat(24)}a
${'a '.repeat(5)}a`);
});
test('when string exceeds width then wrap and indent', () => {
const text = 'a '.repeat(30) + 'a';
const helper = new commander.Help();
const wrapped = helper.wrap(text, 50, 10);
expect(wrapped).toEqual(`${'a '.repeat(24)}a
${' '.repeat(10)}${'a '.repeat(5)}a`);
});
test('when width < 40 then do not wrap', () => {
const text = 'a '.repeat(30) + 'a';
const helper = new commander.Help();
const wrapped = helper.wrap(text, 39, 0);
expect(wrapped).toEqual(text);
});
test('when text has line break then respect and indent', () => {
const text = 'term description\nanother line';
const helper = new commander.Help();
const wrapped = helper.wrap(text, 78, 5);
expect(wrapped).toEqual('term description\n another line');
});
test('when text has consecutive line breaks then respect and indent', () => {
const text = 'term description\n\nanother line';
const helper = new commander.Help();
const wrapped = helper.wrap(text, 78, 5);
expect(wrapped).toEqual('term description\n\n another line');
});
test('when text has Windows line break then respect and indent', () => {
const text = 'term description\r\nanother line';
const helper = new commander.Help();
const wrapped = helper.wrap(text, 78, 5);
expect(wrapped).toEqual('term description\n another line');
});
test('when text has Windows consecutive line breaks then respect and indent', () => {
const text = 'term description\r\n\r\nanother line';
const helper = new commander.Help();
const wrapped = helper.wrap(text, 78, 5);
expect(wrapped).toEqual('term description\n\n another line');
});
test('when text already formatted with line breaks and indent then do not touch', () => {
const text = 'term a '.repeat(25) + '\n ' + 'a '.repeat(25) + 'a';
const helper = new commander.Help();
const wrapped = helper.wrap(text, 78, 5);
expect(wrapped).toEqual(text);
});
});
describe('wrapping by formatHelp', () => {
// Test auto wrap and indent with some manual strings.
// Fragile tests with complete help output.
test('when long option description then wrap and indent', () => {
const program = new commander.Command();
program
.configureHelp({ helpWidth: 80 })
.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');
const expectedOutput =
`Usage: [options]
Options:
-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
-h, --help display help for command
`;
expect(program.helpInformation()).toBe(expectedOutput);
});
test('when long option description and default then wrap and indent', () => {
const program = new commander.Command();
program
.configureHelp({ helpWidth: 80 })
.option('-x --extra-long-option <value>', 'kjsahdkajshkahd kajhsd akhds', 'aaa bbb ccc ddd eee fff ggg');
const expectedOutput =
`Usage: [options]
Options:
-x --extra-long-option <value> kjsahdkajshkahd kajhsd akhds (default: "aaa
bbb ccc ddd eee fff ggg")
-h, --help display help for command
`;
expect(program.helpInformation()).toBe(expectedOutput);
});
test('when long subcommand description then wrap and indent', () => {
const program = new commander.Command();
program
.configureHelp({ helpWidth: 80 })
.option('-x --extra-long-option-switch', 'x')
.command('alpha', 'Lorem mollit quis dolor ex do eu quis ad insa a commodo esse.');
const expectedOutput =
`Usage: [options] [command]
Options:
-x --extra-long-option-switch x
-h, --help display help for command
Commands:
alpha Lorem mollit quis dolor ex do eu quis ad insa
a commodo esse.
help [command] display help for command
`;
expect(program.helpInformation()).toBe(expectedOutput);
});
test('when not enough room then help not wrapped', () => {
// Not wrapping if less than 40 columns available for wrapping.
const program = new commander.Command();
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.';
program
.configureHelp({ helpWidth: 60 })
.command('1234567801234567890x', commandDescription);
const expectedOutput =
`Usage: [options] [command]
Options:
-h, --help display help for command
Commands:
1234567801234567890x ${commandDescription}
help [command] display help for command
`;
expect(program.helpInformation()).toBe(expectedOutput);
});
test('when option description pre-formatted then only add small indent', () => {
// #396: leave custom format alone, apart from space-space indent
const optionSpec = '-t, --time <HH:MM>';
const program = new commander.Command();
program
.configureHelp({ helpWidth: 80 })
.option(optionSpec, `select time
Time can also be specified using special values:
"dawn" - From night to sunrise.
`);
const expectedOutput =
`Usage: [options]
Options:
${optionSpec} select time
Time can also be specified using special values:
"dawn" - From night to sunrise.
-h, --help display help for command
`;
expect(program.helpInformation()).toBe(expectedOutput);
});
test('when command description long then wrapped', () => {
const program = new commander.Command();
program
.configureHelp({ helpWidth: 80 })
.description(`Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor labore eu Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor labore eu
After 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`);
const expectedOutput = `Usage: [options]
Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor labore
eu Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor
labore eu
After 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
Options:
-h, --help display help for command
`;
expect(program.helpInformation()).toBe(expectedOutput);
});
});