UNPKG

5.05 kBJavaScriptView Raw
1
2!function(exports) {
3 var empty = {}
4 , toStr = empty.toString
5 , hasOwn = empty.hasOwnProperty
6 , isArray = Array.isArray
7
8 module.exports = assert
9
10 function assert(value, message, x, _stackStart) {
11 if (!value) {
12 if (!message) {
13 message = stringify(value) + " == true"
14 } else if (isArray(message)) {
15 message = message[1] +
16 "\nexpected: " + stringify(message[2], 160) +
17 "\nactual: " + stringify(message[0], 160)
18 }
19 throw new AssertionError(message, _stackStart || assert)
20 }
21 }
22 assert.ok = assert
23
24 assert.notOk = function notOk(value, message, x, _stackStart) {
25 assert(
26 !value,
27 message || [value, "==", null],
28 null,
29 _stackStart || notOk
30 )
31 }
32
33 assert.equal =
34 assert.deepEqual = function assertEqual(actual, expected, message, _stackStart) {
35 assert(
36 _deepEqual(actual, expected, []),
37 message || [actual, "==", expected],
38 null,
39 _stackStart || assertEqual
40 )
41 }
42
43
44 assert.notEqual =
45 assert.notDeepEqual = function notEqual(actual, expected, message, _stackStart) {
46 assert(
47 !_deepEqual(actual, expected, []),
48 message || [actual, "!=", expected],
49 null,
50 _stackStart || notEqual
51 )
52 }
53
54 assert.strictEqual = function strictEqual(actual, expected, message, _stackStart) {
55 assert(
56 actual === expected,
57 message || [actual, "===", expected],
58 null,
59 _stackStart || strictEqual
60 )
61 }
62
63 assert.notStrictEqual = function notStrictEqual(actual, expected, message, _stackStart) {
64 assert(
65 actual !== expected,
66 message || [actual, "!==", expected],
67 null,
68 _stackStart || notStrictEqual
69 )
70 }
71
72 assert["throws"] = function assertThrows(fn, message, x, _stackStart) {
73 var actual = false
74 , expected = true
75 try {
76 fn()
77 } catch(e) {
78 actual = true
79 }
80 assert(actual, message || "throws", null, _stackStart || assertThrows)
81 }
82
83 assert.type = function assertType(thing, expected, x, _stackStart) {
84 var actual = type(thing)
85 assert(
86 actual === expected,
87 "type should be " + expected + ", got " + actual,
88 null,
89 _stackStart || assertType
90 )
91 }
92
93 assert.anyOf = function anyOf(a, b, x, _stackStart) {
94 assert(
95 isArray(b) && b.indexOf(a) != -1,
96 "should be one from " + stringify(b) + ", got " + a,
97 null,
98 _stackStart || anyOf
99 )
100 }
101
102
103 function AssertionError(message, _stackStart) {
104 this.name = "AssertionError"
105 this.message = message
106 if (Error.captureStackTrace) {
107 Error.captureStackTrace(this, _stackStart || AssertionError)
108 } else {
109 this.stack = this.toString() + "\n" + (new Error()).stack
110 }
111 }
112 AssertionError.prototype = Object.create(Error.prototype)
113
114
115 function _deepEqual(actual, expected, circ) {
116 if (
117 actual === expected ||
118 // null == undefined
119 expected === null && actual == expected ||
120 // make NaN equal to NaN
121 actual !== actual && expected !== expected
122 ) return true
123
124 var key, aKeys, len
125 , aType = typeof actual
126
127 if (
128 aType !== "object" ||
129 actual == null ||
130 aType !== typeof expected ||
131 (aType = type(actual)) != type(expected) ||
132 actual.constructor !== expected.constructor ||
133 (aType == "date" && actual.getTime() !== expected.getTime()) ||
134 (aType == "regexp" && ""+actual !== ""+expected)
135 ) {
136 return false
137 }
138
139 key = circ.indexOf(actual)
140 if (key > -1) return true
141 circ.push(actual)
142
143 if (aType == "array" || aType == "arguments") {
144 len = actual.length
145 if (len !== expected.length) return false
146 for (; len--; ) {
147 if (!_deepEqual(actual[len], expected[len], circ)) return false
148 }
149 } else {
150 aKeys = Object.keys(actual)
151 len = aKeys.length
152 if (len !== Object.keys(expected).length) return false
153 for (; len--; ) {
154 key = aKeys[len]
155 if (
156 !hasOwn.call(expected, key) ||
157 !_deepEqual(actual[key], expected[key], circ)
158 ) return false
159 }
160 }
161 return true
162 }
163
164 function type(obj) {
165 // Standard clearly states that NaN is a number
166 // but this is not useful for testing.
167 return obj !== obj ? "nan" : toStr.call(obj).slice(8, -1).toLowerCase()
168 }
169
170 function stringify(item, maxLen) {
171 var max = maxLen > 9 ? maxLen : 70
172 , str = _stringify(item, max, [])
173 return str.length > max ? str.slice(0, max - 3) + ".." + str.slice(-1) : str
174 }
175
176 function _stringify(item, max, circ) {
177 var i, tmp
178 , left = max
179 , t = type(item)
180 , str =
181 t === "string" ? JSON.stringify(item) :
182 t === "function" ? ("" + item).split(/n | *\{/)[1] :
183 (!item || t === "number" || t === "regexp" || item === true) ? "" + item :
184 item.toJSON ? item.toJSON() :
185 item
186
187 if (typeof str == "object") {
188 if (circ.indexOf(str) > -1) return "[Circular]"
189 circ.push(str)
190 tmp = []
191 for (i in str) if (hasOwn.call(str, i)) {
192 i = (t === "object" ? i + ":" : "") + _stringify(str[i], left, circ)
193 tmp.push(i)
194 left -= i.length
195 if (left < 0) break
196 }
197 str =
198 t === "array" ? "[" + tmp + "]" :
199 t === "arguments" ? t + "[" + tmp + "]" :
200 "{" + tmp + "}"
201
202 if (t === "object" && item.constructor !== Object) {
203 str = item.constructor.name + str
204 }
205 }
206
207 return str
208 }
209}(this)
210