forked from jamesshore/lets_code_javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseargs.js
More file actions
150 lines (130 loc) · 4.56 KB
/
parseargs.js
File metadata and controls
150 lines (130 loc) · 4.56 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
var parseargs = require('../lib/parseargs')
, assert = require('assert')
, optsReg = [
{ full: 'directory'
, abbr: 'C'
, preempts: false
, expectValue: true
}
, { full: 'jakefile'
, abbr: 'f'
, preempts: false
, expectValue: true
}
, { full: 'tasks'
, abbr: 'T'
, preempts: true
}
, { full: 'tasks'
, abbr: 'ls'
, preempts: true
}
, { full: 'trace'
, abbr: 't'
, preempts: false
, expectValue: false
}
, { full: 'help'
, abbr: 'h'
, preempts: true
}
, { full: 'version'
, abbr: 'V'
, preempts: true
}
]
, p = new parseargs.Parser(optsReg)
, z = function (s) { return s.split(' '); }
, res;
var tests = {
'test long preemptive opt and val with equal-sign, ignore further opts': function () {
res = p.parse(z('--tasks=foo --jakefile=asdf'));
assert.equal('foo', res.opts.tasks);
assert.equal(undefined, res.opts.jakefile);
}
, 'test long preemptive opt and val without equal-sign, ignore further opts': function () {
res = p.parse(z('--tasks foo --jakefile=asdf'));
assert.equal('foo', res.opts.tasks);
assert.equal(undefined, res.opts.jakefile);
}
, 'test long preemptive opt and no val, ignore further opts': function () {
res = p.parse(z('--tasks --jakefile=asdf'));
assert.equal(true, res.opts.tasks);
assert.equal(undefined, res.opts.jakefile);
}
, 'test preemptive opt with no val, should be true': function () {
res = p.parse(z('-T'));
assert.equal(true, res.opts.tasks);
}
, 'test preemptive opt with no val, should be true and ignore further opts': function () {
res = p.parse(z('-T -f'));
assert.equal(true, res.opts.tasks);
assert.equal(undefined, res.opts.jakefile);
}
, 'test preemptive opt with val, should be val': function () {
res = p.parse(z('-T zoobie -f foo/bar/baz'));
assert.equal('zoobie', res.opts.tasks);
assert.equal(undefined, res.opts.jakefile);
}
, 'test -f expects a value, -t does not (howdy is task-name)': function () {
res = p.parse(z('-f zoobie -t howdy'));
assert.equal('zoobie', res.opts.jakefile);
assert.equal(true, res.opts.trace);
assert.equal('howdy', res.taskNames[0]);
}
, 'test different order, -f expects a value, -t does not (howdy is task-name)': function () {
res = p.parse(z('-f zoobie howdy -t'));
assert.equal('zoobie', res.opts.jakefile);
assert.equal(true, res.opts.trace);
assert.equal('howdy', res.taskNames[0]);
}
, 'test -f expects a value, -t does not (foo=bar is env var)': function () {
res = p.parse(z('-f zoobie -t foo=bar'));
assert.equal('zoobie', res.opts.jakefile);
assert.equal(true, res.opts.trace);
assert.equal('bar', res.envVars.foo);
assert.equal(undefined, res.taskName);
}
, 'test -f expects a value, -t does not (foo=bar is env-var, task-name follows)': function () {
res = p.parse(z('-f zoobie -t howdy foo=bar'));
assert.equal('zoobie', res.opts.jakefile);
assert.equal(true, res.opts.trace);
assert.equal('bar', res.envVars.foo);
assert.equal('howdy', res.taskNames[0]);
}
, 'test -t does not expect a value, -f does (throw howdy away)': function () {
res = p.parse(z('-t howdy -f zoobie'));
assert.equal(true, res.opts.trace);
assert.equal('zoobie', res.opts.jakefile);
assert.equal(undefined, res.taskName);
}
, 'test --trace does not expect a value, -f does (throw howdy away)': function () {
res = p.parse(z('--trace howdy --jakefile zoobie'));
assert.equal(true, res.opts.trace);
assert.equal('zoobie', res.opts.jakefile);
assert.equal(undefined, res.taskName);
}
, 'test --trace does not expect a value (equal), -f does (throw howdy away)': function () {
res = p.parse(z('--trace=howdy --jakefile=zoobie'));
assert.equal(true, res.opts.trace);
assert.equal('zoobie', res.opts.jakefile);
assert.equal(undefined, res.taskName);
}
/*
, 'test task-name with positional args': function () {
res = p.parse(z('foo:bar[asdf,qwer]'));
assert.equal('asdf', p.taskArgs[0]);
assert.equal('qwer', p.taskArgs[1]);
}
, 'test opts, env vars, task-name with positional args': function () {
res = p.parse(z('-f ./tests/Jakefile -t default[asdf,qwer] foo=bar'));
assert.equal('./tests/Jakefile', res.opts.jakefile);
assert.equal(true, res.opts.trace);
assert.equal('bar', res.envVars.foo);
assert.equal('default', res.taskName);
assert.equal('asdf', p.taskArgs[0]);
assert.equal('qwer', p.taskArgs[1]);
}
*/
};
module.exports = tests;