/
argsert.js
143 lines (108 loc) · 3.65 KB
/
argsert.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
/* global describe, it */
const argsert = require('../lib/argsert')
const checkOutput = require('./helpers/utils').checkOutput
require('chai').should()
describe('Argsert', function () {
it('does not warn if optional argument is not provided', function () {
var o = checkOutput(function () {
argsert('[object]', [].slice.call(arguments))
})
o.warnings.length.should.equal(0)
})
it('warn if wrong type is provided for optional argument', function () {
var o = checkOutput(function () {
function foo (opts) {
argsert('[object|number]', [].slice.call(arguments))
}
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', function () {
var o = checkOutput(function () {
function foo (opts) {
argsert('[object]', [].slice.call(arguments))
}
foo({foo: 'bar'})
})
o.warnings.length.should.equal(0)
})
it('warns if required argument is not provided', function () {
var o = checkOutput(function () {
argsert('<object>', [].slice.call(arguments))
})
o.warnings[0].should.match(/Not enough arguments provided. Expected 1 but received 0./)
})
it('warns if required argument is of wrong type', function () {
var o = checkOutput(function () {
function foo (opts) {
argsert('<object>', [].slice.call(arguments))
}
foo('bar')
})
o.warnings[0].should.match(/Invalid first argument. Expected object but received string./)
})
it('supports a combination of required and optional arguments', function () {
var o = checkOutput(function () {
function foo (opts) {
argsert('<array> <string|object> [string|object]', [].slice.call(arguments))
}
foo([], 'foo', {})
})
o.warnings.length.should.equal(0)
})
it('warns if too many arguments are provided', function () {
var o = checkOutput(function () {
function foo (expected) {
argsert('<array> [batman]', [].slice.call(arguments))
}
foo([], 33, 99)
})
o.warnings[0].should.match(/Too many arguments provided. Expected max 2 but received 3./)
})
it('configures function to accept 0 parameters, if only arguments object is provided', function () {
var o = checkOutput(function () {
function foo (expected) {
argsert([].slice.call(arguments))
}
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', function () {
var o = checkOutput(function () {
function foo (opts) {
argsert('<*>', [].slice.call(arguments))
}
foo('bar')
})
o.warnings.length.should.equal(0)
})
it('should ignore trailing undefined values', function () {
var o = checkOutput(function () {
function foo (opts) {
argsert('<*>', [].slice.call(arguments))
}
foo('bar', undefined, undefined)
})
o.warnings.length.should.equal(0)
})
it('should not ignore undefined values that are not trailing', function () {
var o = checkOutput(function () {
function foo (opts) {
argsert('<*>', [].slice.call(arguments))
}
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', function () {
var o = checkOutput(function () {
function foo (arg) {
argsert('<null>', [].slice.call(arguments))
}
foo(null)
})
o.warnings.length.should.equal(0)
})
})