source: trip-planner-front/node_modules/minimist/test/parse.js@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.5 KB
Line 
1var parse = require('../');
2var test = require('tape');
3
4test('parse args', function (t) {
5 t.deepEqual(
6 parse([ '--no-moo' ]),
7 { moo : false, _ : [] },
8 'no'
9 );
10 t.deepEqual(
11 parse([ '-v', 'a', '-v', 'b', '-v', 'c' ]),
12 { v : ['a','b','c'], _ : [] },
13 'multi'
14 );
15 t.end();
16});
17
18test('comprehensive', function (t) {
19 t.deepEqual(
20 parse([
21 '--name=meowmers', 'bare', '-cats', 'woo',
22 '-h', 'awesome', '--multi=quux',
23 '--key', 'value',
24 '-b', '--bool', '--no-meep', '--multi=baz',
25 '--', '--not-a-flag', 'eek'
26 ]),
27 {
28 c : true,
29 a : true,
30 t : true,
31 s : 'woo',
32 h : 'awesome',
33 b : true,
34 bool : true,
35 key : 'value',
36 multi : [ 'quux', 'baz' ],
37 meep : false,
38 name : 'meowmers',
39 _ : [ 'bare', '--not-a-flag', 'eek' ]
40 }
41 );
42 t.end();
43});
44
45test('flag boolean', function (t) {
46 var argv = parse([ '-t', 'moo' ], { boolean: 't' });
47 t.deepEqual(argv, { t : true, _ : [ 'moo' ] });
48 t.deepEqual(typeof argv.t, 'boolean');
49 t.end();
50});
51
52test('flag boolean value', function (t) {
53 var argv = parse(['--verbose', 'false', 'moo', '-t', 'true'], {
54 boolean: [ 't', 'verbose' ],
55 default: { verbose: true }
56 });
57
58 t.deepEqual(argv, {
59 verbose: false,
60 t: true,
61 _: ['moo']
62 });
63
64 t.deepEqual(typeof argv.verbose, 'boolean');
65 t.deepEqual(typeof argv.t, 'boolean');
66 t.end();
67});
68
69test('newlines in params' , function (t) {
70 var args = parse([ '-s', "X\nX" ])
71 t.deepEqual(args, { _ : [], s : "X\nX" });
72
73 // reproduce in bash:
74 // VALUE="new
75 // line"
76 // node program.js --s="$VALUE"
77 args = parse([ "--s=X\nX" ])
78 t.deepEqual(args, { _ : [], s : "X\nX" });
79 t.end();
80});
81
82test('strings' , function (t) {
83 var s = parse([ '-s', '0001234' ], { string: 's' }).s;
84 t.equal(s, '0001234');
85 t.equal(typeof s, 'string');
86
87 var x = parse([ '-x', '56' ], { string: 'x' }).x;
88 t.equal(x, '56');
89 t.equal(typeof x, 'string');
90 t.end();
91});
92
93test('stringArgs', function (t) {
94 var s = parse([ ' ', ' ' ], { string: '_' })._;
95 t.same(s.length, 2);
96 t.same(typeof s[0], 'string');
97 t.same(s[0], ' ');
98 t.same(typeof s[1], 'string');
99 t.same(s[1], ' ');
100 t.end();
101});
102
103test('empty strings', function(t) {
104 var s = parse([ '-s' ], { string: 's' }).s;
105 t.equal(s, '');
106 t.equal(typeof s, 'string');
107
108 var str = parse([ '--str' ], { string: 'str' }).str;
109 t.equal(str, '');
110 t.equal(typeof str, 'string');
111
112 var letters = parse([ '-art' ], {
113 string: [ 'a', 't' ]
114 });
115
116 t.equal(letters.a, '');
117 t.equal(letters.r, true);
118 t.equal(letters.t, '');
119
120 t.end();
121});
122
123
124test('string and alias', function(t) {
125 var x = parse([ '--str', '000123' ], {
126 string: 's',
127 alias: { s: 'str' }
128 });
129
130 t.equal(x.str, '000123');
131 t.equal(typeof x.str, 'string');
132 t.equal(x.s, '000123');
133 t.equal(typeof x.s, 'string');
134
135 var y = parse([ '-s', '000123' ], {
136 string: 'str',
137 alias: { str: 's' }
138 });
139
140 t.equal(y.str, '000123');
141 t.equal(typeof y.str, 'string');
142 t.equal(y.s, '000123');
143 t.equal(typeof y.s, 'string');
144 t.end();
145});
146
147test('slashBreak', function (t) {
148 t.same(
149 parse([ '-I/foo/bar/baz' ]),
150 { I : '/foo/bar/baz', _ : [] }
151 );
152 t.same(
153 parse([ '-xyz/foo/bar/baz' ]),
154 { x : true, y : true, z : '/foo/bar/baz', _ : [] }
155 );
156 t.end();
157});
158
159test('alias', function (t) {
160 var argv = parse([ '-f', '11', '--zoom', '55' ], {
161 alias: { z: 'zoom' }
162 });
163 t.equal(argv.zoom, 55);
164 t.equal(argv.z, argv.zoom);
165 t.equal(argv.f, 11);
166 t.end();
167});
168
169test('multiAlias', function (t) {
170 var argv = parse([ '-f', '11', '--zoom', '55' ], {
171 alias: { z: [ 'zm', 'zoom' ] }
172 });
173 t.equal(argv.zoom, 55);
174 t.equal(argv.z, argv.zoom);
175 t.equal(argv.z, argv.zm);
176 t.equal(argv.f, 11);
177 t.end();
178});
179
180test('nested dotted objects', function (t) {
181 var argv = parse([
182 '--foo.bar', '3', '--foo.baz', '4',
183 '--foo.quux.quibble', '5', '--foo.quux.o_O',
184 '--beep.boop'
185 ]);
186
187 t.same(argv.foo, {
188 bar : 3,
189 baz : 4,
190 quux : {
191 quibble : 5,
192 o_O : true
193 }
194 });
195 t.same(argv.beep, { boop : true });
196 t.end();
197});
Note: See TracBrowser for help on using the repository browser.