-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
strict.js
154 lines (130 loc) · 4.72 KB
/
strict.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
'use strict';
var parse = require('../');
var test = require('tape');
function throwsWhenStrict(args, parseOptions, testOptions) {
// does not throw by default
testOptions.t.doesNotThrow(function () {
parse(args, parseOptions);
});
// throws when strict
var strictOptions = JSON.parse(JSON.stringify(parseOptions));
strictOptions.strict = true;
testOptions.t.throws(function () {
parse(args, strictOptions);
}, testOptions.expected);
}
var kMissingString = /Missing option value/;
var kBooleanWithValue = /Unexpected option value/;
var kUnknownOption = /Unknown option/;
test('strict missing option value: long string option used alone', function (t) {
throwsWhenStrict(['--str'], { string: ['str'] }, { t: t, expected: kMissingString });
t.end();
});
test('strict missing option value: short string option used alone', function (t) {
throwsWhenStrict(['-s'], { string: ['s'] }, { t: t, expected: kMissingString });
t.end();
});
test('strict missing option value: string option alias used alone', function (t) {
throwsWhenStrict(['-s'], { string: ['str'], alias: { str: 's' } }, { t: t, expected: kMissingString });
t.end();
});
test('strict missing option value: string option followed by option (rather than value)', function (t) {
throwsWhenStrict(['--str', '-a'], { string: ['str'] }, { t: t, expected: kMissingString });
t.end();
});
test('strict missing option value: short string option used before end of short option group', function (t) {
throwsWhenStrict(['-sb'], { string: ['s'], boolean: 'b' }, { t: t, expected: kMissingString });
t.end();
});
test('strict missing option value: empty string is ok value', function (t) {
t.doesNotThrow(function () {
parse(['--str', ''], { string: ['str'] });
});
t.end();
});
test('strict missing option value: implied empty string is ok (--str=)', function (t) {
t.doesNotThrow(function () {
parse(['--str='], { string: ['str'] });
});
t.end();
});
test('strict unexpected option value: long boolean option given value (other than true/false)', function (t) {
throwsWhenStrict(['--bool=x'], { boolean: ['bool'] }, { t: t, expected: kBooleanWithValue });
t.end();
});
test('strict unexpected option value: long boolean option given true is ok', function (t) {
t.doesNotThrow(function () {
parse(['--bool=true'], { boolean: ['bool'] });
});
t.end();
});
test('strict unexpected option value: long boolean option given false is ok', function (t) {
t.doesNotThrow(function () {
parse(['--bool=false'], { boolean: ['bool'] });
});
t.end();
});
test('strict unexpected option value: short boolean option given value (other than true/false)', function (t) {
throwsWhenStrict(['--b=x'], { boolean: ['b'] }, { t: t, expected: kBooleanWithValue });
t.end();
});
test('strict unexpected option value: short boolean option given value', function (t) {
t.doesNotThrow(function () {
parse(['--b=true'], { boolean: ['b'] });
});
t.end();
});
test('strict unexpected option value: short boolean option given value', function (t) {
t.doesNotThrow(function () {
parse(['--b=false'], { boolean: ['b'] });
});
t.end();
});
test('strict unknown option: unknown option', function (t) {
throwsWhenStrict(['-u'], { }, { t: t, expected: kUnknownOption });
throwsWhenStrict(['--long'], { }, { t: t, expected: kUnknownOption });
throwsWhenStrict(['-u=x'], { }, { t: t, expected: kUnknownOption });
throwsWhenStrict(['--long=x'], { }, { t: t, expected: kUnknownOption });
t.end();
});
test('strict unknown option: opt.boolean is known', function (t) {
t.doesNotThrow(function () {
parse(['--bool'], { boolean: ['bool'], strict: true });
parse(['-b'], { boolean: ['b'], strict: true });
});
t.end();
});
test('strict unknown option: opt.string is known', function (t) {
t.doesNotThrow(function () {
parse(['--str', 'SSS'], { string: ['str'], strict: true });
parse(['-s', 'SSS'], { string: ['s'], strict: true });
});
t.end();
});
test('strict unknown option: opt.alias is known', function (t) {
t.doesNotThrow(function () {
var options = { alias: { aaa: ['a', 'AAA'] }, strict: true };
parse(['--aaa'], options);
parse(['-a'], options);
parse(['--AAA'], options);
});
t.end();
});
test('strict unknown option: opts.unknown returns false', function (t) {
// Mirror non-strict and skip argument processing if opts.unknown returns false.
// Otherwise, throw for unknown option as usual.
function unknownFn() {
}
function unknownFnTrue() {
return true;
}
function unknownFnFalse() {
return false;
}
throwsWhenStrict(['--x=y'], { unknown: unknownFn }, { t: t, expected: kUnknownOption });
throwsWhenStrict(['--x=y'], { unknown: unknownFnTrue }, { t: t, expected: kUnknownOption });
t.doesNotThrow(function () {
parse(['--x=y'], { strict: true, unknown: unknownFnFalse });
});
t.end();
});