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 | var util = require('util/');
|
29 |
|
30 | var pSlice = Array.prototype.slice;
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | var assert = module.exports = ok;
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | assert.AssertionError = function AssertionError(options) {
|
44 | this.name = 'AssertionError';
|
45 | this.actual = options.actual;
|
46 | this.expected = options.expected;
|
47 | this.operator = options.operator;
|
48 | if (options.message) {
|
49 | this.message = options.message;
|
50 | this.generatedMessage = false;
|
51 | } else {
|
52 | this.message = getMessage(this);
|
53 | this.generatedMessage = true;
|
54 | }
|
55 | var stackStartFunction = options.stackStartFunction || fail;
|
56 |
|
57 | if (Error.captureStackTrace) {
|
58 | Error.captureStackTrace(this, stackStartFunction);
|
59 | }
|
60 | };
|
61 |
|
62 |
|
63 | util.inherits(assert.AssertionError, Error);
|
64 |
|
65 | function replacer(key, value) {
|
66 | if (util.isUndefined(value)) {
|
67 | return '' + value;
|
68 | }
|
69 | if (util.isNumber(value) && (isNaN(value) || !isFinite(value))) {
|
70 | return value.toString();
|
71 | }
|
72 | if (util.isFunction(value) || util.isRegExp(value)) {
|
73 | return value.toString();
|
74 | }
|
75 | return value;
|
76 | }
|
77 |
|
78 | function truncate(s, n) {
|
79 | if (util.isString(s)) {
|
80 | return s.length < n ? s : s.slice(0, n);
|
81 | } else {
|
82 | return s;
|
83 | }
|
84 | }
|
85 |
|
86 | function getMessage(self) {
|
87 | return truncate(JSON.stringify(self.actual, replacer), 128) + ' ' +
|
88 | self.operator + ' ' +
|
89 | truncate(JSON.stringify(self.expected, replacer), 128);
|
90 | }
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | function fail(actual, expected, message, operator, stackStartFunction) {
|
104 | throw new assert.AssertionError({
|
105 | message: message,
|
106 | actual: actual,
|
107 | expected: expected,
|
108 | operator: operator,
|
109 | stackStartFunction: stackStartFunction
|
110 | });
|
111 | }
|
112 |
|
113 |
|
114 | assert.fail = fail;
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | function ok(value, message) {
|
124 | if (!value) fail(value, true, message, '==', assert.ok);
|
125 | }
|
126 | assert.ok = ok;
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | assert.equal = function equal(actual, expected, message) {
|
133 | if (actual != expected) fail(actual, expected, message, '==', assert.equal);
|
134 | };
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | assert.notEqual = function notEqual(actual, expected, message) {
|
140 | if (actual == expected) {
|
141 | fail(actual, expected, message, '!=', assert.notEqual);
|
142 | }
|
143 | };
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | assert.deepEqual = function deepEqual(actual, expected, message) {
|
149 | if (!_deepEqual(actual, expected)) {
|
150 | fail(actual, expected, message, 'deepEqual', assert.deepEqual);
|
151 | }
|
152 | };
|
153 |
|
154 | function _deepEqual(actual, expected) {
|
155 |
|
156 | if (actual === expected) {
|
157 | return true;
|
158 |
|
159 | } else if (util.isBuffer(actual) && util.isBuffer(expected)) {
|
160 | if (actual.length != expected.length) return false;
|
161 |
|
162 | for (var i = 0; i < actual.length; i++) {
|
163 | if (actual[i] !== expected[i]) return false;
|
164 | }
|
165 |
|
166 | return true;
|
167 |
|
168 |
|
169 |
|
170 | } else if (util.isDate(actual) && util.isDate(expected)) {
|
171 | return actual.getTime() === expected.getTime();
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | } else if (util.isRegExp(actual) && util.isRegExp(expected)) {
|
177 | return actual.source === expected.source &&
|
178 | actual.global === expected.global &&
|
179 | actual.multiline === expected.multiline &&
|
180 | actual.lastIndex === expected.lastIndex &&
|
181 | actual.ignoreCase === expected.ignoreCase;
|
182 |
|
183 |
|
184 |
|
185 | } else if (!util.isObject(actual) && !util.isObject(expected)) {
|
186 | return actual == expected;
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | } else {
|
195 | return objEquiv(actual, expected);
|
196 | }
|
197 | }
|
198 |
|
199 | function isArguments(object) {
|
200 | return Object.prototype.toString.call(object) == '[object Arguments]';
|
201 | }
|
202 |
|
203 | function objEquiv(a, b) {
|
204 | if (util.isNullOrUndefined(a) || util.isNullOrUndefined(b))
|
205 | return false;
|
206 |
|
207 | if (a.prototype !== b.prototype) return false;
|
208 |
|
209 |
|
210 | if (isArguments(a)) {
|
211 | if (!isArguments(b)) {
|
212 | return false;
|
213 | }
|
214 | a = pSlice.call(a);
|
215 | b = pSlice.call(b);
|
216 | return _deepEqual(a, b);
|
217 | }
|
218 | try {
|
219 | var ka = Object.keys(a),
|
220 | kb = Object.keys(b),
|
221 | key, i;
|
222 | } catch (e) {
|
223 | return false;
|
224 | }
|
225 |
|
226 |
|
227 | if (ka.length != kb.length)
|
228 | return false;
|
229 |
|
230 | ka.sort();
|
231 | kb.sort();
|
232 |
|
233 | for (i = ka.length - 1; i >= 0; i--) {
|
234 | if (ka[i] != kb[i])
|
235 | return false;
|
236 | }
|
237 |
|
238 |
|
239 | for (i = ka.length - 1; i >= 0; i--) {
|
240 | key = ka[i];
|
241 | if (!_deepEqual(a[key], b[key])) return false;
|
242 | }
|
243 | return true;
|
244 | }
|
245 |
|
246 |
|
247 |
|
248 |
|
249 | assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
|
250 | if (_deepEqual(actual, expected)) {
|
251 | fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
|
252 | }
|
253 | };
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | assert.strictEqual = function strictEqual(actual, expected, message) {
|
259 | if (actual !== expected) {
|
260 | fail(actual, expected, message, '===', assert.strictEqual);
|
261 | }
|
262 | };
|
263 |
|
264 |
|
265 |
|
266 |
|
267 | assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
|
268 | if (actual === expected) {
|
269 | fail(actual, expected, message, '!==', assert.notStrictEqual);
|
270 | }
|
271 | };
|
272 |
|
273 | function expectedException(actual, expected) {
|
274 | if (!actual || !expected) {
|
275 | return false;
|
276 | }
|
277 |
|
278 | if (Object.prototype.toString.call(expected) == '[object RegExp]') {
|
279 | return expected.test(actual);
|
280 | } else if (actual instanceof expected) {
|
281 | return true;
|
282 | } else if (expected.call({}, actual) === true) {
|
283 | return true;
|
284 | }
|
285 |
|
286 | return false;
|
287 | }
|
288 |
|
289 | function _throws(shouldThrow, block, expected, message) {
|
290 | var actual;
|
291 |
|
292 | if (util.isString(expected)) {
|
293 | message = expected;
|
294 | expected = null;
|
295 | }
|
296 |
|
297 | try {
|
298 | block();
|
299 | } catch (e) {
|
300 | actual = e;
|
301 | }
|
302 |
|
303 | message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
|
304 | (message ? ' ' + message : '.');
|
305 |
|
306 | if (shouldThrow && !actual) {
|
307 | fail(actual, expected, 'Missing expected exception' + message);
|
308 | }
|
309 |
|
310 | if (!shouldThrow && expectedException(actual, expected)) {
|
311 | fail(actual, expected, 'Got unwanted exception' + message);
|
312 | }
|
313 |
|
314 | if ((shouldThrow && actual && expected &&
|
315 | !expectedException(actual, expected)) || (!shouldThrow && actual)) {
|
316 | throw actual;
|
317 | }
|
318 | }
|
319 |
|
320 |
|
321 |
|
322 |
|
323 | assert.throws = function(block, /*optional*/error, /*optional*/message) {
|
324 | _throws.apply(this, [true].concat(pSlice.call(arguments)));
|
325 | };
|
326 |
|
327 |
|
328 | assert.doesNotThrow = function(block, /*optional*/message) {
|
329 | _throws.apply(this, [false].concat(pSlice.call(arguments)));
|
330 | };
|
331 |
|
332 | assert.ifError = function(err) { if (err) {throw err;}};
|