-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
help.sortOptions.test.js
90 lines (83 loc) · 3.06 KB
/
help.sortOptions.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
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('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']);
});
});