UNPKG

11 kBJavaScriptView Raw
1// http://wiki.commonjs.org/wiki/Unit_Testing/1.0
2//
3// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
4//
5// Originally from narwhal.js (http://narwhaljs.org)
6// Copyright (c) 2009 Thomas Robinson <280north.com>
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and associated documentation files (the 'Software'), to
10// deal in the Software without restriction, including without limitation the
11// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12// sell copies of the Software, and to permit persons to whom the Software is
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in
16// all copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25// when used in node, this will actually load the util module we depend on
26// versus loading the builtin util module as happens otherwise
27// this is a bug in node module loading as far as I am concerned
28var util = require('util/');
29
30var pSlice = Array.prototype.slice;
31
32// 1. The assert module provides functions that throw
33// AssertionError's when particular conditions are not met. The
34// assert module must conform to the following interface.
35
36var assert = module.exports = ok;
37
38// 2. The AssertionError is defined in assert.
39// new assert.AssertionError({ message: message,
40// actual: actual,
41// expected: expected })
42
43assert.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// assert.AssertionError instanceof Error
63util.inherits(assert.AssertionError, Error);
64
65function 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
78function 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
86function 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// At present only the three keys mentioned above are used and
93// understood by the spec. Implementations or sub modules can pass
94// other keys to the AssertionError's constructor - they will be
95// ignored.
96
97// 3. All of the following functions must throw an AssertionError
98// when a corresponding condition is not met, with a message that
99// may be undefined if not provided. All assertion methods provide
100// both the actual and expected values to the assertion error for
101// display purposes.
102
103function 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// EXTENSION! allows for well behaved errors defined elsewhere.
114assert.fail = fail;
115
116// 4. Pure assertion tests whether a value is truthy, as determined
117// by !!guard.
118// assert.ok(guard, message_opt);
119// This statement is equivalent to assert.equal(true, !!guard,
120// message_opt);. To test strictly for the value true, use
121// assert.strictEqual(true, guard, message_opt);.
122
123function ok(value, message) {
124 if (!value) fail(value, true, message, '==', assert.ok);
125}
126assert.ok = ok;
127
128// 5. The equality assertion tests shallow, coercive equality with
129// ==.
130// assert.equal(actual, expected, message_opt);
131
132assert.equal = function equal(actual, expected, message) {
133 if (actual != expected) fail(actual, expected, message, '==', assert.equal);
134};
135
136// 6. The non-equality assertion tests for whether two objects are not equal
137// with != assert.notEqual(actual, expected, message_opt);
138
139assert.notEqual = function notEqual(actual, expected, message) {
140 if (actual == expected) {
141 fail(actual, expected, message, '!=', assert.notEqual);
142 }
143};
144
145// 7. The equivalence assertion tests a deep equality relation.
146// assert.deepEqual(actual, expected, message_opt);
147
148assert.deepEqual = function deepEqual(actual, expected, message) {
149 if (!_deepEqual(actual, expected)) {
150 fail(actual, expected, message, 'deepEqual', assert.deepEqual);
151 }
152};
153
154function _deepEqual(actual, expected) {
155 // 7.1. All identical values are equivalent, as determined by ===.
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 // 7.2. If the expected value is a Date object, the actual value is
169 // equivalent if it is also a Date object that refers to the same time.
170 } else if (util.isDate(actual) && util.isDate(expected)) {
171 return actual.getTime() === expected.getTime();
172
173 // 7.3 If the expected value is a RegExp object, the actual value is
174 // equivalent if it is also a RegExp object with the same source and
175 // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
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 // 7.4. Other pairs that do not both pass typeof value == 'object',
184 // equivalence is determined by ==.
185 } else if (!util.isObject(actual) && !util.isObject(expected)) {
186 return actual == expected;
187
188 // 7.5 For all other Object pairs, including Array objects, equivalence is
189 // determined by having the same number of owned properties (as verified
190 // with Object.prototype.hasOwnProperty.call), the same set of keys
191 // (although not necessarily the same order), equivalent values for every
192 // corresponding key, and an identical 'prototype' property. Note: this
193 // accounts for both named and indexed properties on Arrays.
194 } else {
195 return objEquiv(actual, expected);
196 }
197}
198
199function isArguments(object) {
200 return Object.prototype.toString.call(object) == '[object Arguments]';
201}
202
203function objEquiv(a, b) {
204 if (util.isNullOrUndefined(a) || util.isNullOrUndefined(b))
205 return false;
206 // an identical 'prototype' property.
207 if (a.prototype !== b.prototype) return false;
208 //~~~I've managed to break Object.keys through screwy arguments passing.
209 // Converting to array solves the problem.
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) {//happens when one is a string literal and the other isn't
223 return false;
224 }
225 // having the same number of owned properties (keys incorporates
226 // hasOwnProperty)
227 if (ka.length != kb.length)
228 return false;
229 //the same set of keys (although not necessarily the same order),
230 ka.sort();
231 kb.sort();
232 //~~~cheap key test
233 for (i = ka.length - 1; i >= 0; i--) {
234 if (ka[i] != kb[i])
235 return false;
236 }
237 //equivalent values for every corresponding key, and
238 //~~~possibly expensive deep test
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// 8. The non-equivalence assertion tests for any deep inequality.
247// assert.notDeepEqual(actual, expected, message_opt);
248
249assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
250 if (_deepEqual(actual, expected)) {
251 fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
252 }
253};
254
255// 9. The strict equality assertion tests strict equality, as determined by ===.
256// assert.strictEqual(actual, expected, message_opt);
257
258assert.strictEqual = function strictEqual(actual, expected, message) {
259 if (actual !== expected) {
260 fail(actual, expected, message, '===', assert.strictEqual);
261 }
262};
263
264// 10. The strict non-equality assertion tests for strict inequality, as
265// determined by !==. assert.notStrictEqual(actual, expected, message_opt);
266
267assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
268 if (actual === expected) {
269 fail(actual, expected, message, '!==', assert.notStrictEqual);
270 }
271};
272
273function 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
289function _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// 11. Expected to throw an error:
321// assert.throws(block, Error_opt, message_opt);
322
323assert.throws = function(block, /*optional*/error, /*optional*/message) {
324 _throws.apply(this, [true].concat(pSlice.call(arguments)));
325};
326
327// EXTENSION! This is annoying to write outside this module.
328assert.doesNotThrow = function(block, /*optional*/message) {
329 _throws.apply(this, [false].concat(pSlice.call(arguments)));
330};
331
332assert.ifError = function(err) { if (err) {throw err;}};