forked from tj/commander.js
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.variadic.test.js
156 lines (124 loc) · 5.1 KB
/
options.variadic.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
const commander = require('../');
describe('variadic option with required value', () => {
test('when variadic with value missing then error', () => {
// Optional. Use internal knowledge to suppress output to keep test output clean.
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });
const program = new commander.Command();
program
.exitOverride()
.option('-r,--required <value...>');
expect(() => {
program.parse(['--required'], { from: 'user' });
}).toThrow();
consoleErrorSpy.mockRestore();
});
test('when variadic with one value then set in array', () => {
const program = new commander.Command();
program
.option('-r,--required <value...>');
program.parse(['--required', 'one'], { from: 'user' });
expect(program.opts().required).toEqual(['one']);
});
test('when variadic with two values then set in array', () => {
const program = new commander.Command();
program
.option('-r,--required <value...>');
program.parse(['--required', 'one', 'two'], { from: 'user' });
expect(program.opts().required).toEqual(['one', 'two']);
});
test('when variadic with repeated values then set in array', () => {
const program = new commander.Command();
program
.option('-r,--required <value...>');
program.parse(['--required', 'one', '--required', 'two'], { from: 'user' });
expect(program.opts().required).toEqual(['one', 'two']);
});
test('when variadic with short combined argument then not variadic', () => {
const program = new commander.Command();
program
.option('-r,--required <value...>')
.arguments('[arg]');
program.parse(['-rone', 'operand'], { from: 'user' });
expect(program.opts().required).toEqual(['one']);
});
test('when variadic with long combined argument then not variadic', () => {
const program = new commander.Command();
program
.option('-r,--required <value...>')
.arguments('[arg]');
program.parse(['--required=one', 'operand'], { from: 'user' });
expect(program.opts().required).toEqual(['one']);
});
test('when variadic with value followed by option then option not eaten', () => {
const program = new commander.Command();
program
.option('-r,--required <value...>')
.option('-f, --flag')
.arguments('[arg]');
program.parse(['-r', 'one', '-f'], { from: 'user' });
const opts = program.opts();
expect(opts.required).toEqual(['one']);
expect(opts.flag).toBe(true);
});
test('when variadic with no value and default then set to default', () => {
const program = new commander.Command();
program
.option('-r,--required <value...>', 'variadic description', 'default');
program.parse([], { from: 'user' });
expect(program.opts().required).toEqual('default');
});
test('when variadic with coercion then coercion sets value', () => {
const program = new commander.Command();
program
.option('-r,--required <value...>', 'variadic description', parseFloat);
// variadic processing reads the multiple values, but up to custom coercion what it does.
program.parse(['--required', '1e2', '1e3'], { from: 'user' });
expect(program.opts().required).toEqual(1000);
});
});
// Not retesting everything, but do some tests on variadic with optional
describe('variadic option with optional value', () => {
test('when variadic not specified then value undefined', () => {
const program = new commander.Command();
program
.option('-o,--optional [value...]');
program.parse([], { from: 'user' });
expect(program.opts().optional).toBeUndefined();
});
test('when variadic used as boolean flag then value true', () => {
const program = new commander.Command();
program
.option('-o,--optional [value...]');
program.parse(['--optional'], { from: 'user' });
expect(program.opts().optional).toBe(true);
});
test('when variadic with one value then set in array', () => {
const program = new commander.Command();
program
.option('-o,--optional [value...]');
program.parse(['--optional', 'one'], { from: 'user' });
expect(program.opts().optional).toEqual(['one']);
});
test('when variadic with two values then set in array', () => {
const program = new commander.Command();
program
.option('-o,--optional [value...]');
program.parse(['--optional', 'one', 'two'], { from: 'user' });
expect(program.opts().optional).toEqual(['one', 'two']);
});
});
describe('variadic special cases', () => {
test('when option flags has word character before dots then is variadic', () => {
const program = new commander.Command();
program
.option('-c,--comma [value...]');
expect(program.options[0].variadic).toBeTruthy();
});
test('when option flags has special characters before dots then not variadic', () => {
// This might be used to describe coercion for comma separated values, and is not variadic.
const program = new commander.Command();
program
.option('-c,--comma [value,...]');
expect(program.options[0].variadic).toBeFalsy();
});
});