deno.land / x / yargs@v17.6.0-deno / test / argsert.cjs

نووسراو ببینە
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* global describe, it */const {argsert} = require('../build/index.cjs');const {checkOutput} = require('./helpers/utils.cjs');const {expect, should} = require('chai');
should();
describe('Argsert', () => { it('does not warn if optional argument is not provided', () => { const o = checkOutput((...args) => { argsert('[object]', [].slice.call(args)); });
o.warnings.length.should.equal(0); });
it('warn if wrong type is provided for optional argument', () => { const o = checkOutput(() => { function foo(...args) { argsert('[object|number]', [].slice.call(args)); }
foo('hello'); });
o.warnings[0].should.match( /Invalid first argument. Expected object or number but received string./ ); });
it('does not warn if optional argument is valid', () => { const o = checkOutput(() => { function foo(...args) { argsert('[object]', [].slice.call(args)); }
foo({foo: 'bar'}); });
o.warnings.length.should.equal(0); });
it('warns if required argument is not provided', () => { const o = checkOutput((...args) => { argsert('<object>', [].slice.call(args)); });
o.warnings[0].should.match( /Not enough arguments provided. Expected 1 but received 0./ ); });
it('warns if required argument is of wrong type', () => { const o = checkOutput(() => { function foo(...args) { argsert('<object>', [].slice.call(args)); }
foo('bar'); });
o.warnings[0].should.match( /Invalid first argument. Expected object but received string./ ); });
it('supports a combination of required and optional arguments', () => { const o = checkOutput(() => { function foo(...args) { argsert('<array> <string|object> [string|object]', [].slice.call(args)); }
foo([], 'foo', {}); });
o.warnings.length.should.equal(0); });
it('warns if too many arguments are provided', () => { const o = checkOutput(() => { function foo(...args) { argsert('<array> [batman]', [].slice.call(args)); }
foo([], 33, 99); });
o.warnings[0].should.match( /Too many arguments provided. Expected max 2 but received 3./ ); });
it('warn with argument position if wrong type is provided for argument', () => { const o = checkOutput(() => { function foo(...args) { argsert('<string> <string> <string>', [].slice.call(args)); }
foo('hello', 'ayy', {}); });
o.warnings[0].should.match( /Invalid third argument. Expected string but received obj./ ); });
it('warn with generic argument position if wrong type is provided for seventh or greater argument', () => { const o = checkOutput(() => { function foo(...args) { argsert( '<string> <string> <string> <string> <string> <string> <string>', [].slice.call(args) ); }
foo('a', 'b', 'c', 'd', 'e', 'f', 10); });
o.warnings[0].should.match( /Invalid manyith argument. Expected string but received number./ ); });
it('configures function to accept 0 parameters, if only arguments object is provided', () => { const o = checkOutput(() => { function foo(...args) { argsert([].slice.call(args)); }
foo(99); });
o.warnings[0].should.match( /Too many arguments provided. Expected max 0 but received 1./ ); });
it('allows for any type if * is provided', () => { const o = checkOutput(() => { function foo(...args) { argsert('<*>', [].slice.call(args)); }
foo('bar'); });
o.warnings.length.should.equal(0); });
it('should ignore trailing undefined values', () => { const o = checkOutput(() => { function foo(...args) { argsert('<*>', [].slice.call(args)); }
foo('bar', undefined, undefined); });
o.warnings.length.should.equal(0); });
it('should not ignore undefined values that are not trailing', () => { const o = checkOutput(() => { function foo(...args) { argsert('<*>', [].slice.call(args)); }
foo('bar', undefined, undefined, 33); });
o.warnings[0].should.match( /Too many arguments provided. Expected max 1 but received 4./ ); });
it('supports null as special type', () => { const o = checkOutput(() => { function foo(...args) { argsert('<null>', [].slice.call(args)); } foo(null); });
o.warnings.length.should.equal(0); });
// See: https://github.com/yargs/yargs/issues/1666 it('should throw on warnings, when under test', () => { function foo(...args) { argsert('<*>', [].slice.call(args)); }
expect(() => { foo('bar', undefined, undefined, 33); }).to.throw(); });});
yargs

Version Info

Tagged at
a year ago