-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathoptions.preset.test.js
59 lines (51 loc) · 2.25 KB
/
options.preset.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
const { Command, Option } = require('../');
test('when boolean option with string preset used then value is preset', () => {
const program = new Command();
program.addOption(new Option('-d, --debug').preset('foo'));
program.parse(['-d'], { from: 'user' });
expect(program.opts().debug).toBe('foo');
});
test('when boolean option with number preset used then value is preset', () => {
const program = new Command();
program.addOption(new Option('-d, --debug').preset(80));
program.parse(['-d'], { from: 'user' });
expect(program.opts().debug).toBe(80);
});
test('when optional with string preset used then value is preset', () => {
const program = new Command();
program.addOption(new Option('-p, --port [port]').preset('foo'));
program.parse(['-p'], { from: 'user' });
expect(program.opts().port).toBe('foo');
});
test('when optional with number preset used then value is preset', () => {
const program = new Command();
program.addOption(new Option('-p, --port [port]').preset(80));
program.parse(['-p'], { from: 'user' });
expect(program.opts().port).toBe(80);
});
test('when optional with string preset used with option-argument then value is as specified', () => {
const program = new Command();
program.addOption(new Option('-p, --port [port]').preset('foo'));
program.parse(['-p', '1234'], { from: 'user' });
expect(program.opts().port).toBe('1234');
});
test('when optional with preset and coerce used then preset is coerced', () => {
const program = new Command();
program.addOption(
new Option('-p, --port [port]').preset('4').argParser(parseFloat),
);
program.parse(['-p'], { from: 'user' });
expect(program.opts().port).toBe(4);
});
test('when optional with preset and variadic used then preset is concatenated', () => {
const program = new Command();
program.addOption(new Option('-n, --name [name...]').preset('two'));
program.parse(['-n', 'one', '-n', '-n', 'three'], { from: 'user' });
expect(program.opts().name).toEqual(['one', 'two', 'three']);
});
test('when negated with string preset used then value is preset', () => {
const program = new Command();
program.addOption(new Option('--no-colour').preset('foo'));
program.parse(['--no-colour'], { from: 'user' });
expect(program.opts().colour).toBe('foo');
});