/
options.custom-processing.test.js
161 lines (142 loc) · 5.6 KB
/
options.custom-processing.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
const commander = require('../');
function myParseInt(value, dummyPrevious) {
// parseInt takes a string and a radix
return parseInt(value, 10);
}
function increaseVerbosity(dummyValue, previous) {
return previous + 1;
}
function collect(value, previous) {
return previous.concat([value]);
}
function collectVoid(value, previous) {
previous.concat([value]);
}
function commaSeparatedList(value, dummyPrevious) {
return value.split(',');
}
test('when option not specified then callback not called', () => {
const mockCoercion = jest.fn();
const program = new commander.Command();
program
.option('-i, --integer <n>', 'number', mockCoercion);
program.parse(['node', 'test']);
expect(mockCoercion).not.toHaveBeenCalled();
});
test('when option not specified then value is undefined', () => {
const program = new commander.Command();
program
.option('-i, --integer <n>', 'number', myParseInt);
program.parse(['node', 'test']);
expect(program.integer).toBeUndefined();
});
test('when starting value is defined and option not specified then callback not called', () => {
const mockCoercion = jest.fn();
const program = new commander.Command();
program
.option('-i, --integer <n>', 'number', mockCoercion, 1);
program.parse(['node', 'test']);
expect(mockCoercion).not.toHaveBeenCalled();
});
test('when starting value is defined and option not specified then value is starting value', () => {
// NB: Can not specify a starting value for a boolean flag! Discovered when writing this test...
const startingValue = 1;
const program = new commander.Command();
program
.option('-i, --integer <n>', 'number', myParseInt, startingValue);
program.parse(['node', 'test']);
expect(program.integer).toBe(startingValue);
});
test('when option specified then callback called with value', () => {
const mockCoercion = jest.fn();
const value = '1';
const program = new commander.Command();
program
.option('-i, --integer <n>', 'number', mockCoercion);
program.parse(['node', 'test', '-i', value]);
expect(mockCoercion).toHaveBeenCalledWith(value, undefined);
});
test('when option specified then value is as returned from callback ', () => {
const callbackResult = 2;
const program = new commander.Command();
program
.option('-i, --integer <n>', 'number', () => {
return callbackResult;
});
program.parse(['node', 'test', '-i', '0']);
expect(program.integer).toBe(callbackResult);
});
test('when starting value is defined and option specified then callback called with value and starting value', () => {
const mockCoercion = jest.fn();
const startingValue = 1;
const value = '2';
const program = new commander.Command();
program
.option('-i, --integer <n>', 'number', mockCoercion, startingValue);
program.parse(['node', 'test', '-i', value]);
expect(mockCoercion).toHaveBeenCalledWith(value, startingValue);
});
test('when option specified multiple times then callback called with value and previousValue', () => {
const mockCoercion = jest.fn().mockImplementation(() => {
return 'callback';
});
const program = new commander.Command();
program
.option('-i, --integer <n>', 'number', mockCoercion);
program.parse(['node', 'test', '-i', '1', '-i', '2']);
expect(mockCoercion).toHaveBeenCalledTimes(2);
expect(mockCoercion).toHaveBeenNthCalledWith(1, '1', undefined);
expect(mockCoercion).toHaveBeenNthCalledWith(2, '2', 'callback');
});
// Now some functional tests like the examples in the README!
test('when parseFloat "1e2" then value is 100', () => {
const program = new commander.Command();
program
.option('-f, --float <number>', 'float argument', parseFloat);
program.parse(['node', 'test', '-f', '1e2']);
expect(program.float).toBe(100);
});
test('when myParseInt "1" then value is 1', () => {
const program = new commander.Command();
program
.option('-i, --integer <number>', 'integer argument', myParseInt);
program.parse(['node', 'test', '-i', '1']);
expect(program.integer).toBe(1);
});
test('when increaseVerbosity -v -v -v then value is 3', () => {
const program = new commander.Command();
program
.option('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0);
program.parse(['node', 'test', '-v', '-v', '-v']);
expect(program.verbose).toBe(3);
});
test('when collect -c a -c b -c c then value is [a, b, c]', () => {
const program = new commander.Command();
program
.option('-c, --collect <value>', 'repeatable value', collect, []);
program.parse(['node', 'test', '-c', 'a', '-c', 'b', '-c', 'c']);
expect(program.collect).toEqual(['a', 'b', 'c']);
});
// freeze void returning collector behaviour
test('when collect returning Void -c a -c b -c c then value is void', () => {
const program = new commander.Command();
program
.option('-c, --collect <value>', 'repeatable value', collectVoid, []);
program.parse(['node', 'test', '-c', 'a', '-c', 'b', '-c', 'c']);
expect(program.collect).toBeUndefined();
});
// freeze void returning collector behaviour
test('when collect returning Void -c a -c b -c c -c d then value is []', () => {
const program = new commander.Command();
program
.option('-c, --collect <value>', 'repeatable value', collectVoid, []);
program.parse(['node', 'test', '-c', 'a', '-c', 'b', '-c', 'c', '-c', 'd']);
expect(program.collect).toEqual([]);
});
test('when commaSeparatedList x,y,z then value is [x, y, z]', () => {
const program = new commander.Command();
program
.option('-l, --list <items>', 'comma separated list', commaSeparatedList);
program.parse(['node', 'test', '--list', 'x,y,z']);
expect(program.list).toEqual(['x', 'y', 'z']);
});