forked from tj/commander.js
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.bool.test.js
128 lines (115 loc) · 4.25 KB
/
options.bool.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
const commander = require('../');
// Test simple flag and negatable flag
// boolean flag on program
describe('boolean flag on program', () => {
test('when boolean flag not specified then value is undefined', () => {
const program = new commander.Command();
program
.option('--pepper', 'add pepper');
program.parse(['node', 'test']);
expect(program.pepper).toBeUndefined();
});
test('when boolean flag specified then value is true', () => {
const program = new commander.Command();
program
.option('--pepper', 'add pepper');
program.parse(['node', 'test', '--pepper']);
expect(program.pepper).toBe(true);
});
test('when negatable boolean flag not specified then value is true', () => {
const program = new commander.Command();
program
.option('--no-cheese', 'remove cheese');
program.parse(['node', 'test']);
expect(program.cheese).toBe(true);
});
test('when negatable boolean flag specified then value is false', () => {
const program = new commander.Command();
program
.option('--no-cheese', 'remove cheese');
program.parse(['node', 'test', '--no-cheese']);
expect(program.cheese).toBe(false);
});
});
// boolean flag on command
describe('boolean flag on command', () => {
test('when boolean flag not specified then value is undefined', () => {
let subCommand;
const program = new commander.Command();
program
.command('sub')
.option('--pepper', 'add pepper')
.action((cmd) => { subCommand = cmd; });
program.parse(['node', 'test', 'sub']);
expect(subCommand.pepper).toBeUndefined();
});
test('when boolean flag specified then value is true', () => {
let subCommand;
const program = new commander.Command();
program
.command('sub')
.option('--pepper', 'add pepper')
.action((cmd) => { subCommand = cmd; });
program.parse(['node', 'test', 'sub', '--pepper']);
expect(subCommand.pepper).toBe(true);
});
test('when negatable boolean flag not specified then value is true', () => {
let subCommand;
const program = new commander.Command();
program
.command('sub')
.option('--no-cheese', 'remove cheese')
.action((cmd) => { subCommand = cmd; });
program.parse(['node', 'test', 'sub']);
expect(subCommand.cheese).toBe(true);
});
test('when negatable boolean flag specified then value is false', () => {
let subCommand;
const program = new commander.Command();
program
.command('sub')
.option('--no-cheese', 'remove cheese')
.action((cmd) => { subCommand = cmd; });
program.parse(['node', 'test', 'sub', '--no-cheese']);
expect(subCommand.cheese).toBe(false);
});
});
// This is a somewhat undocumented special behaviour which appears in some examples.
// When a flag has a non-boolean default, it is used as the value (only) when the flag is specified.
//
// boolean flag with non-boolean default
describe('boolean flag with non-boolean default', () => {
test('when flag not specified then value is undefined', () => {
const flagValue = 'black';
const program = new commander.Command();
program
.option('--olives', 'Add olives? Sorry we only have black.', flagValue);
program.parse(['node', 'test']);
expect(program.olives).toBeUndefined();
});
test('when flag specified then value is "default" value', () => {
const flagValue = 'black';
const program = new commander.Command();
program
.option('-v, --olives', 'Add olives? Sorry we only have black.', flagValue);
program.parse(['node', 'test', '--olives']);
expect(program.olives).toBe(flagValue);
});
});
// Regression test for #1301 with `-no-` in middle of option
describe('regression test for -no- in middle of option flag', () => {
test('when flag not specified then value is undefined', () => {
const program = new commander.Command();
program
.option('--module-no-parse');
program.parse(['node', 'test']);
expect(program.moduleNoParse).toBeUndefined();
});
test('when flag specified then value is true', () => {
const program = new commander.Command();
program
.option('--module-no-parse');
program.parse(['node', 'test', '--module-no-parse']);
expect(program.moduleNoParse).toEqual(true);
});
});