-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
261 lines (247 loc) · 9.25 KB
/
test.js
File metadata and controls
261 lines (247 loc) · 9.25 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*eslint-env mocha */
'use strict';
const assert = require('assert');
const icli = require('.');
const commandConfig = {
cmd: 'burger',
description: 'create your burger',
throwErrors: true,
parameters: [{
cmdSpec: '[name]',
type: 'input',
validate: (v) => { return /^[a-zA-Z0-9 -]+$/.test(v); },
question: {
message: 'How do you want to name your burger?'
}
}, {
cmdSpec: '-s, --sauces <sauces-list>',
description: 'A comma-separated list of sauces',
type: 'checkbox',
choices: ['bbq', 'ketchup', 'mayonnaise', 'mustard', 'spicy'],
question: {
message: 'Choose your sauce(s)'
}
}, {
cmdSpec: '--vege',
description: 'Check if vegetarian',
type: 'bool',
default: false,
question: {
message: 'Are you vegetarian?'
}
}, {
cmdSpec: '-b, --bacon <none|simple|double|triple>',
description: 'Select the quantity of bacon',
type: 'list',
choices: () => {
return new Promise(resolve => {
setTimeout(() => { resolve(['none', 'simple', 'double', 'triple']); }, 200);
});
},
default: 'simple',
question: {
message: 'What quantity of bacon do you want?',
when: (answers, cmdParameterValues) => {
return cmdParameterValues.vege !== true && answers.vege !== true && cmdParameterValues.bacon === undefined;
}
}
}, {
cmdSpec: '--salad',
description: 'Add salad',
type: 'confirm',
default: false,
question: {
message: 'Do you want some salad?'
}
}, {
cmdSpec: '--tomato',
description: 'Add tomato',
type: 'boolean',
question: {
message: 'Do you want some tomato?'
}
}, {
cmdSpec: '--steaks <quantity>',
description: 'Number of steaks',
type: 'integer',
question: {
message: 'How many steaks do you want?',
when: (answers, cmdParameterValues) => {
return cmdParameterValues.vege !== true && answers.vege !== true && cmdParameterValues.bacon === undefined;
}
}
}, {
cmdSpec: '--drink-size <quantity>',
description: 'Size of the drink im ml',
type: 'int',
question: {
message: 'What size do you want your drink?'
}
}, {
cmdSpec: '-p, --price <estimated-price>',
description: 'Price you are willing to pay',
type: 'number',
question: {
message: 'How much are you willing to pay?'
}
}, {
cmdSpec: '--satisfaction <comment>',
description: 'Tell us if you are happy',
type: 'input',
question: {
validate: () => { return true; }
}
}, {
cmdSpec: '-u',
description: 'Useless option'
}]
};
describe('Comquirer', function() {
beforeEach(() => {
icli.reset();
});
it('should colorize portions of text', () => {
assert.equal(icli.format.cmd('a command'), '\x1b[33ma command\x1b[0m', 'commands are colorised');
assert.equal(icli.format.info('an info'), '\x1b[36man info\x1b[0m', 'information is colorised');
assert.equal(icli.format.error('an error'), '\x1b[31man error\x1b[0m', 'errors are colorised');
assert.equal(icli.format.success('a success message'), '\x1b[32ma success message\x1b[0m', 'success messages are colorised');
assert.equal(icli.format.custom('a custom message', '\x1b[35m'), '\x1b[35ma custom message\x1b[0m', 'it is possible to customize text format');
assert.equal(icli.format.ok('ok'), '\x1b[32mok\x1b[0m', '"ok" messages are colorised');
assert.equal(icli.format.ko('ko'), '\x1b[31mko\x1b[0m', '"ko" messages are colorised');
});
it('should perform syntax highlight', () => {
// eslint-disable-next-line max-len
assert.equal(icli.highlight('{ a: \'b\', c: 123 }'), '\u001b[33m{\u001b[39m \u001b[37ma\u001b[39m\u001b[93m:\u001b[39m \u001b[92m\'b\'\u001b[39m\u001b[32m,\u001b[39m \u001b[37mc\u001b[39m\u001b[93m:\u001b[39m \u001b[34m123\u001b[39m \u001b[33m}\u001b[39m');
});
it('should transform a configuration object into a command and a prompt', function() {
const command = [
'/path/to/node', 'file.js', 'burger',
'my-burger',
'--tomato',
'--salad',
'--steaks', '3',
'--drink-size', '200',
'-p', '12.5',
'--bacon', 'double',
'--sauces', 'mustard,ketchup',
'--vege',
'--satisfaction', 'very happy'
];
commandConfig.execute = (parameters) => {
assert.equal(parameters.name, command[3], 'the "name" argument is set correctly');
assert.equal(parameters.tomato, true, 'the "tomato" option is set correctly');
assert.equal(parameters.salad, true, 'the "salad" option is set correctly');
assert.equal(parameters.steaks, 3, 'the "steaks" option is set correctly');
assert.equal(parameters.drinkSize, 200, 'the "drink-size" option is set correctly');
assert.equal(parameters.bacon, 'double', 'the "bacon" option is set correctly');
assert.equal(parameters.sauces.join(','), 'mustard,ketchup', 'the "sauces" option is set correctly');
return parameters;
};
icli.createSubCommand(commandConfig);
return icli.parse(command).then(burger => {
assert.equal(burger.name, command[3], 'the "name" argument is set correctly');
assert.equal(burger.tomato, true, 'the "tomato" option is set correctly');
assert.equal(burger.salad, true, 'the "salad" option is set correctly');
assert.equal(burger.steaks, 3, 'the "steaks" option is set correctly');
assert.equal(burger.drinkSize, 200, 'the "drink-size" option is set correctly');
assert.equal(burger.bacon, 'double', 'the "bacon" option is set correctly');
assert.equal(burger.sauces.join(','), 'mustard,ketchup', 'the "sauces" option is set correctly');
});
});
it('should accept the execution function as a second argument', function() {
const command = [
'/path/to/node', 'file.js', 'burger',
'my-burger',
'--tomato',
'--salad',
'--steaks', '4',
'--drink-size', '200',
'-p', '12.5',
'--bacon', 'double',
'--sauces', 'mustard,ketchup',
'--vege',
'--satisfaction', 'very happy'
];
delete commandConfig.execute;
icli.createSubCommand(commandConfig, parameters => {
assert.equal(parameters.name, command[3], 'the "name" argument is set correctly');
assert.equal(parameters.tomato, true, 'the "tomato" option is set correctly');
assert.equal(parameters.salad, true, 'the "salad" option is set correctly');
assert.equal(parameters.steaks, 4, 'the "steaks" option is set correctly');
assert.equal(parameters.drinkSize, 200, 'the "drink-size" option is set correctly');
assert.equal(parameters.bacon, 'double', 'the "bacon" option is set correctly');
assert.equal(parameters.sauces.join(','), 'mustard,ketchup', 'the "sauces" option is set correctly');
return parameters;
});
return icli.parse(command).then(burger => {
assert.equal(burger.name, command[3], 'the "name" argument is set correctly');
assert.equal(burger.tomato, true, 'the "tomato" option is set correctly');
assert.equal(burger.salad, true, 'the "salad" option is set correctly');
assert.equal(burger.steaks, 4, 'the "steaks" option is set correctly');
assert.equal(burger.drinkSize, 200, 'the "drink-size" option is set correctly');
assert.equal(burger.bacon, 'double', 'the "bacon" option is set correctly');
assert.equal(burger.sauces.join(','), 'mustard,ketchup', 'the "sauces" option is set correctly');
});
});
it('should reject the Promise returned by createSubCommand() if needed', function() {
const command = [
'/path/to/node', 'file.js', 'burger',
'my-burger',
'--tomato',
'--salad',
'--steaks', '3',
'--drink-size', '200',
'-p', '12.5',
'--bacon', 'double',
'--sauces', 'mustard,ketchup',
'--vege',
'--satisfaction', 'very happy'
];
commandConfig.execute = (parameters) => {
return Promise.reject(new Error('Fail for some reason'));
};
icli.createSubCommand(commandConfig);
return icli.parse(command).then(() => {
throw new Error('This code should not be reached');
})
.catch(e => {
// We expect to catch an error here
assert.equal(e.message, 'Fail for some reason');
return Promise.resolve();
});
});
it('should reject the Promise returned by createSubCommand() if something went wrong during the parsing', function() {
const command = [
'/path/to/node', 'file.js', 'burger',
'my_burger',
'--tomato',
'--salad',
'--steaks', '3',
'--drink-size', 'xxx',
'-p', '12.5o',
'--bacon', 'zzz',
'--sauces', 'xxx,yyy',
'--vege',
'--satisfaction', 'very happy'
];
commandConfig.commanderActionHook = function() {
return arguments;
};
commandConfig.inquirerPromptHook = (answers, commandParameterValues) => {
return Promise.resolve([answers, commandParameterValues]);
};
commandConfig.execute = parameters => {
throw new Error('This code should not be reached 1');
};
icli.createSubCommand(commandConfig);
icli.parse(command).then(() => {
throw new Error('This code should not be reached 2');
})
.catch(e => {
// We expect to catch an error here
const messages = e.message.split('\n');
assert.equal(messages.length, 6);
return Promise.resolve();
});
});
});