forked from tj/commander.js
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
args.variadic.test.js
71 lines (54 loc) · 2.31 KB
/
args.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
const commander = require('../');
// Testing variadic arguments. Testing all the action arguments, but could test just variadicArg.
describe('variadic argument', () => {
test('when no extra arguments specified for program then variadic arg is empty array', () => {
const actionMock = jest.fn();
const program = new commander.Command();
program
.arguments('<id> [variadicArg...]')
.action(actionMock);
program.parse(['node', 'test', 'id']);
expect(actionMock).toHaveBeenCalledWith('id', [], program);
});
test('when extra arguments specified for program then variadic arg is array of values', () => {
const actionMock = jest.fn();
const program = new commander.Command();
program
.arguments('<id> [variadicArg...]')
.action(actionMock);
const extraArguments = ['a', 'b', 'c'];
program.parse(['node', 'test', 'id', ...extraArguments]);
expect(actionMock).toHaveBeenCalledWith('id', extraArguments, program);
});
test('when no extra arguments specified for command then variadic arg is empty array', () => {
const actionMock = jest.fn();
const program = new commander.Command();
const cmd = program
.command('sub [variadicArg...]')
.action(actionMock);
program.parse(['node', 'test', 'sub']);
expect(actionMock).toHaveBeenCalledWith([], cmd);
});
test('when extra arguments specified for command then variadic arg is array of values', () => {
const actionMock = jest.fn();
const program = new commander.Command();
const cmd = program
.command('sub [variadicArg...]')
.action(actionMock);
const extraArguments = ['a', 'b', 'c'];
program.parse(['node', 'test', 'sub', ...extraArguments]);
expect(actionMock).toHaveBeenCalledWith(extraArguments, cmd);
});
test('when program variadic argument not last then error', () => {
const program = new commander.Command();
expect(() => {
program.arguments('<variadicArg...> [optionalArg]');
}).toThrow("only the last argument can be variadic 'variadicArg'");
});
test('when command variadic argument not last then error', () => {
const program = new commander.Command();
expect(() => {
program.command('sub <variadicArg...> [optionalArg]');
}).toThrow("only the last argument can be variadic 'variadicArg'");
});
});