-
Notifications
You must be signed in to change notification settings - Fork 1
/
CommandTest.js
181 lines (163 loc) · 4.91 KB
/
CommandTest.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var assert = require('assert'),
Cli = require('./../lib/Cli'),
Command = require('./../lib/Command');
describe("Commands", function () {
it("Should parse basic command", function () {
var cli = new Cli();
cli
.commandGroup({
name: 'cmd1',
description: 'main route for the program',
commands: [
new Command({name: 'test1', description: 'The first command option'}),
new Command({name: 'test2', description: 'The second command option'})
]
})
.parse(['node', 'cli-test.js', 'test1']);
assert.equal(cli.params.cmd1, 'test1');
cli.parse(['node', 'cli-test.js', 'test2']);
assert.equal(cli.params.cmd1, 'test2');
});
it("Should parse basic command, multiple commands", function () {
var cli = new Cli();
cli
.commandGroup({
name: 'cmd1',
description: 'main route for the program',
commands: [
new Command({name: 'test1', description: 'The first command option'}),
new Command({name: 'test2', description: 'The second command option'})
],
required: true
})
.commandGroup({
name: 'cmd2',
description: 'secondary route for the program',
commands: [
new Command({name: 'testA', description: 'The first command option'}),
new Command({name: 'testB', description: 'The second command option'})
],
required: true
})
.parse(['node', 'cli-test.js', 'test1', 'testB']);
assert.equal(cli.params.cmd1, 'test1');
assert.equal(cli.params.cmd2, 'testB');
});
it("Should error on missing command", function () {
var cli = new Cli();
cli
.commandGroup({
name: 'cmd1',
description: 'main route for the program',
commands: [
new Command({name: 'test1', description: 'The first command option'}),
new Command({name: 'test2', description: 'The second command option'})
],
required: true
});
assert.throws(function () {
cli.parse(['node', 'cli-test.js', 'test3']);
});
});
it("Should execute command callbacks", function () {
var cli = new Cli();
var resultCommand = {};
var resultCli = {};
var resultGroup = {};
cli
.commandGroup({
name: 'cmd1',
description: 'main route for the program',
commands: [
new Command({
name: 'test1', description: 'The first command option', callback: function (cli, command) {
resultCli = cli;
resultCommand = command;
}
}),
new Command({
name: 'test2', description: 'The second command option', callback: function (cli, command) {
resultCli = cli;
resultCommand = 'Hello World';
}
})
],
callback: function (cli, command) {
resultGroup = command;
}.bind(this)
})
.parse(['node', 'cli-test.js', 'test1']);
assert.equal(cli.params.cmd1, 'test1');
assert.ok(resultCli instanceof Cli, 'resultCli was not instance of Cli');
assert.ok(resultCommand instanceof Command, 'resultCommand was not instance of Command');
assert.equal(resultCommand.name, 'test1');
assert.equal(resultGroup.name, 'test1');
cli.parse(['node', 'cli-test.js', 'test2']);
assert.equal(cli.params.cmd1, 'test2');
assert.ok(resultCli instanceof Cli, 'resultCli was not instance of Cli');
assert.equal(resultCommand, 'Hello World');
assert.equal(resultGroup.name, 'test2');
});
it("Should make a tree of commands easily", function () {
var cli = new Cli();
var finalResult;
cli
.commandGroup({
name: 'cmd1',
description: 'main route for the program',
commands: [
new Command({
name: 'test1',
description: 'The first command option',
callback: function(cli, command) {
// Append additional subgroups when this one is chosen.
cli.commandGroup({
name: 'test1Sub',
description: 'the sub command to test1',
commands: [
new Command({
name: 'foo',
description: 'Foo should equal bar',
callback: function(cli, command) {
finalResult = 'bar';
}
})
],
required: true
});
}
}),
new Command({
name: 'test2',
description: 'The second command option',
callback: function(cli, command) {
finalResult = 'hello';
}
})
],
required: true
});
cli.parse(['node', 'cli-test.js', 'test2']);
assert.equal(cli.params.cmd1, 'test2');
assert.equal(finalResult, 'hello');
cli.parse(['node', 'cli-test.js', 'test1', 'foo']);
assert.equal(cli.params.cmd1, 'test1');
assert.equal(cli.params.test1Sub, 'foo');
assert.equal(finalResult, 'bar');
});
it("Should error on extra command", function () {
var cli = new Cli();
cli
.commandGroup({
name: 'cmd1',
description: 'main route for the program',
commands: [
new Command('test1', 'The first command option'),
new Command('test2', 'The second command option')
]
});
assert.throws(function () {
cli.parse(['node', 'cli-test.js', 'test3']);
}, 'Didn\'t throw error for extra params as it should');
});
});