forked from tj/commander.js
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.values.test.js
83 lines (71 loc) · 2.94 KB
/
options.values.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
const commander = require('../');
// Test the ways values can be specified for options.
// See also references on "Utility Conventions" in command.parseOptions.test.js
// options with required values can eat values starting with a dash, including just dash sometimes used as alias for stdin
//
// option with required value, using describe.each to test all matrix of values and tests
describe.each([['str'], ['80'], ['-'], ['-5'], ['--flag']])(
'option with required value specified as %s', (value) => {
function createPortProgram() {
const program = new commander.Command();
program
.option('-p,--port <number>', 'specify port');
return program;
}
test('when short flag followed by value then value is as specified', () => {
const program = createPortProgram();
program.parse(['node', 'test', '-p', value]);
expect(program.port).toBe(value);
});
test('when short flag concatenated with value then value is as specified', () => {
const program = createPortProgram();
program.parse(['node', 'test', `-p${value}`]);
expect(program.port).toBe(value);
});
test('when long flag followed by value then value is as specified', () => {
const program = createPortProgram();
program.parse(['node', 'test', '--port', value]);
expect(program.port).toBe(value);
});
test('when long flag = value then value is as specified', () => {
const program = createPortProgram();
program.parse(['node', 'test', `--port=${value}`]);
expect(program.port).toBe(value);
});
});
// option with optional value
describe('option with optional value', () => {
function createPortProgram() {
const program = new commander.Command();
program
.option('-p,--port [number]', 'specify port');
return program;
}
test('when short flag followed by value then value is as specified', () => {
const program = createPortProgram();
program.parse('node test -p 80'.split(' '));
expect(program.port).toBe('80');
});
test('when short flag concatenated with value then value is as specified', () => {
const program = createPortProgram();
program.parse('node test -p80'.split(' '));
expect(program.port).toBe('80');
});
test('when long flag followed by value then value is as specified', () => {
const program = createPortProgram();
program.parse('node test --port 80'.split(' '));
expect(program.port).toBe('80');
});
test('when long flag = value then value is as specified', () => {
const program = createPortProgram();
program.parse('node test --port=80'.split(' '));
expect(program.port).toBe('80');
});
test('when long flag followed empty string then value is empty string', () => {
const program = createPortProgram();
program
.option('-c, --cheese [type]', 'optionally specify the type of cheese');
program.parse(['node', 'test', '--cheese', '']);
expect(program.cheese).toBe('');
});
});