Debian Codegraph @

mri/test/index.js

raw
1 const test = require('tape');
2 const fn = require('../lib');
3
4 test('parse args', t => {
5 const res1 = fn(['--no-moo']);
6 const res2 = fn(['-v', 'a', '-v', 'b', '-v', 'c']);
7 t.deepEqual(res1, { moo: false, _: [] }, 'no');
8 t.deepEqual(res2, { v: ['a', 'b', 'c'], _: [] }, 'multi');
9 t.end();
10 });
11
12 test('comprehensive', t => {
13 t.deepEqual(
14 fn([
15 '--name=meowmers',
16 'bare',
17 '-cats',
18 'woo',
19 '-h',
20 'awesome',
21 '--multi=quux',
22 '--key',
23 'value',
24 '-b',
25 '--bool',
26 '--no-meep',
27 '--multi=baz',
28 '--number=-123',
29 '--zeronum=0',
30 '--',
31 '--not-a-flag',
32 'eek'
33 ]),
34 {
35 c: true,
36 a: true,
37 t: true,
38 s: 'woo',
39 h: 'awesome',
40 b: true,
41 bool: true,
42 key: 'value',
43 multi: ['quux', 'baz'],
44 meep: false,
45 name: 'meowmers',
46 number: -123,
47 zeronum: 0,
48 _: ['bare', '--not-a-flag', 'eek']
49 }
50 );
51 t.end();
52 });
53
54 test('flag boolean', t => {
55 const res = fn(['-t', 'moo'], { boolean: 't' });
56 t.deepEqual(res, { t: true, _: ['moo'] });
57 t.deepEqual(typeof res.t, 'boolean');
58 t.end();
59 });
60
61 test('flag boolean value', t => {
62 const res = fn(['--verbose', 'false', 'moo', '-t', 'true'], {
63 boolean: ['t', 'verbose'],
64 default: { verbose: true }
65 });
66
67 t.deepEqual(res, { verbose: false, t: true, _: ['moo'] });
68
69 t.deepEqual(typeof res.verbose, 'boolean');
70 t.deepEqual(typeof res.t, 'boolean');
71 t.end();
72 });
73
74 test('flag default value', t => {
75 const res = fn(['--foo'], {
76 default: { bar: true }
77 });
78 t.deepEqual(res, { foo: true, bar: true, _: [] });
79 t.equal(typeof res.foo, 'boolean');
80 t.equal(typeof res.bar, 'boolean');
81 t.end();
82 });
83
84 test('flag default and alias', t => {
85 const res = fn(['--foo'], {
86 default: { bar: true },
87 alias: { bar: 'b' }
88 });
89 t.deepEqual(res, { foo: true, bar: true, b: true, _: [] });
90 t.equal(typeof res.foo, 'boolean');
91 t.equal(typeof res.bar, 'boolean');
92 t.equal(typeof res.b, 'boolean');
93 t.end();
94 });
95
96 test('flag default string w/ alias', t => {
97 t.deepEqual(
98 fn(['--arg', '01'], {
99 alias: { a: ['arg'] },
100 default: { arg: '' },
101 }),
102 { _: [], arg: '01', a: '01' }
103 );
104
105 t.deepEqual(
106 fn(['-a', '01'], {
107 alias: { a: ['arg'] },
108 default: { arg: '' },
109 }),
110 { _: [], arg: '01', a: '01' }
111 );
112
113 // ---
114
115 t.deepEqual(
116 fn(['-a', '01'], {
117 alias: { arg: ['a'] },
118 default: { a: '' },
119 }),
120 { _: [], arg: '01', a: '01' }
121 );
122
123 t.deepEqual(
124 fn(['--arg', '01'], {
125 alias: { arg: ['a'] },
126 default: { a: '' },
127 }),
128 { _: [], arg: '01', a: '01' }
129 );
130
131 // ---
132
133 t.deepEqual(
134 fn(['-a', '01'], {
135 alias: { arg: ['a'] },
136 default: { arg: '' },
137 }),
138 { _: [], arg: '01', a: '01' }
139 );
140
141 t.end();
142 });
143
144 // test('newlines in params' , t => {
145 // var args = fn(['-s', "X\nX"])
146 // t.deepEqual(args, { _ : [], s : "X\nX" });
147
148 // // reproduce in bash:
149 // // VALUE="new
150 // // line"
151 // // node program.js --s="$VALUE"
152 // args = fn(["--s=X\nX"])
153 // t.deepEqual(args, { _ : [], s : "X\nX" });
154 // t.end();
155 // });
156
157 test('strings', t => {
158 const s = fn(['-s', '0001234'], { string: 's' }).s;
159 t.equal(typeof s, 'string');
160 t.equal(s, '0001234');
161
162 const x = fn(['-x', '56'], { string: 'x' }).x;
163 t.equal(typeof x, 'string');
164 t.equal(x, '56');
165 t.end();
166 });
167
168 test('stringArgs', t => {
169 const s = fn([' ', ' '], { string: '_' })._;
170 t.same(s.length, 2);
171 t.same(typeof s[0], 'string');
172 t.same(s[0], ' ');
173 t.same(typeof s[1], 'string');
174 t.same(s[1], ' ');
175 t.end();
176 });
177
178 test('empty strings', t => {
179 const s = fn(['-s'], { string: 's' }).s;
180 t.equal(typeof s, 'string');
181 t.equal(s, '');
182
183 const str = fn(['--str'], { string: 'str' }).str;
184 t.equal(typeof str, 'string');
185 t.equal(str, '');
186
187 const letters = fn(['-art'], { string: ['a', 't'] });
188
189 t.equal(letters.r, true);
190 t.equal(letters.a, '');
191 t.equal(letters.t, '');
192
193 t.end();
194 });
195
196 test('string and alias', t => {
197 const x = fn(['--str', '000123'], {
198 string: 's',
199 alias: { s: 'str' }
200 });
201
202 t.equal(x.str, '000123');
203 t.equal(typeof x.str, 'string');
204 t.equal(x.s, '000123');
205 t.equal(typeof x.s, 'string');
206
207 const y = fn(['-s', '000123'], {
208 string: 'str',
209 alias: { str: 's' }
210 });
211
212 t.equal(typeof y.str, 'string');
213 t.equal(typeof y.s, 'string');
214 t.equal(y.str, '000123');
215 t.equal(y.s, '000123');
216 t.end();
217 });
218
219 // test('slashBreak', t => {
220 // t.same(
221 // fn(['-I/foo/bar/baz']),
222 // { I : '/foo/bar/baz', _ : [] }
223 // );
224 // t.same(
225 // fn(['-xyz/foo/bar/baz']),
226 // { x : true, y : true, z : '/foo/bar/baz', _ : [] }
227 // );
228 // t.end();
229 // });
230
231 test('alias', t => {
232 const argv = fn(['-f', '11', '--zoom', '55'], {
233 alias: { z: 'zoom' }
234 });
235 t.equal(argv.zoom, 55);
236 t.equal(argv.z, argv.zoom);
237 t.equal(argv.f, 11);
238 t.end();
239 });
240
241 test('multiAlias', t => {
242 const argv = fn(['-f', '11', '--zoom', '55'], {
243 alias: { z: ['zm', 'zoom'] }
244 });
245 t.equal(argv.zoom, 55);
246 t.equal(argv.z, argv.zoom);
247 t.equal(argv.z, argv.zm);
248 t.equal(argv.f, 11);
249 t.end();
250 });
251
252 // test('nested dotted objects', t => {
253 // const argv = fn([
254 // '--foo.bar', '3', '--foo.baz', '4',
255 // '--foo.quux.quibble', '5', '--foo.quux.o_O',
256 // '--beep.boop'
257 // ]);
258
259 // t.same(argv.foo, {
260 // bar : 3,
261 // baz : 4,
262 // quux : {
263 // quibble : 5,
264 // o_O : true
265 // }
266 // });
267 // t.same(argv.beep, { boop : true });
268 // t.end();
269 // });
270
271 test('parse with modifier functions' , t => {
272 const argv = fn(['-b', '123'], { boolean:'b' });
273 t.same(argv, { b:true, _:[123] });
274 t.end();
275 });
276
277 test('flag default null value', t => {
278 const argv = fn(['--foo'], { default: { bar:null }});
279 t.same(argv, { foo:true, bar:null, _:[] });
280 t.end();
281 });
282
283 test('flag boolean with default', t => {
284 const foo = fn(['-t'], { default: { t:true }});
285 t.same(foo, { t:true, _:[] });
286 t.is(typeof foo.t, 'boolean');
287
288 const bar = fn(['-t'], { default: { t:false }});
289 t.same(bar, { t:true, _:[] });
290 t.is(typeof bar.t, 'boolean');
291
292 const baz = fn(['--no-two'], { default: { two:true }});
293 t.same(baz, { two:false, _:[] });
294 t.is(typeof baz.two, 'boolean');
295 t.end();
296 });
297
298 test('flag boolean with default & alias', t => {
299 const alias = { t: ['tt'], two:['toot'] };
300
301 const foo = fn(['-t'], { alias, default: { t:true }});
302 t.same(foo, { t:true, tt:true, _:[] });
303 t.is(typeof foo.t, 'boolean');
304
305 const bar = fn(['-t'], { alias, default: { t:false }});
306 t.same(bar, { t:true, tt:true, _:[] });
307 t.is(typeof bar.t, 'boolean');
308
309 const baz = fn(['--no-two'], { alias, default: { two:true }});
310 t.same(baz, { two:false, toot:false, _:[] });
311 t.is(typeof baz.two, 'boolean');
312
313 t.end();
314 });
315
316 test('flag boolean with default string & alias', t => {
317 const foo = fn(['-t'], { default: { t:'hi' }});
318 t.same(foo, { t:'', _:[] });
319 t.is(typeof foo.t, 'string');
320
321 const bar = fn(['-t'], { alias:{ t:'tt' }, default: { t:'boo' }});
322 t.same(bar, { t:'', tt:'', _:[] });
323 t.is(typeof bar.t, 'string');
324
325 // --no-* overrides
326 const baz = fn(['--no-two'], { default: { two:'hi' }});
327 t.same(baz, { two:false, _:[] });
328 t.is(typeof baz.two, 'boolean');
329 t.end();
330 });