|
| 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 | +// UTILITY |
| 26 | +var util = require('util'); |
| 27 | +var pSlice = Array.prototype.slice; |
| 28 | + |
| 29 | +// 1. The assert module provides functions that throw |
| 30 | +// AssertionError's when particular conditions are not met. The |
| 31 | +// assert module must conform to the following interface. |
| 32 | + |
| 33 | +var assert = module.exports = ok; |
| 34 | + |
| 35 | +// 2. The AssertionError is defined in assert. |
| 36 | +// new assert.AssertionError({ message: message, |
| 37 | +// actual: actual, |
| 38 | +// expected: expected }) |
| 39 | + |
| 40 | +assert.AssertionError = function AssertionError(options) { |
| 41 | + this.name = 'AssertionError'; |
| 42 | + this.message = options.message; |
| 43 | + this.actual = options.actual; |
| 44 | + this.expected = options.expected; |
| 45 | + this.operator = options.operator; |
| 46 | + var stackStartFunction = options.stackStartFunction || fail; |
| 47 | + |
| 48 | + if (Error.captureStackTrace) { |
| 49 | + Error.captureStackTrace(this, stackStartFunction); |
| 50 | + } |
| 51 | +}; |
| 52 | +util.inherits(assert.AssertionError, Error); |
| 53 | + |
| 54 | +function replacer(key, value) { |
| 55 | + if (value === undefined) { |
| 56 | + return '' + value; |
| 57 | + } |
| 58 | + if (typeof value === 'number' && (isNaN(value) || !isFinite(value))) { |
| 59 | + return value.toString(); |
| 60 | + } |
| 61 | + if (typeof value === 'function' || value instanceof RegExp) { |
| 62 | + return value.toString(); |
| 63 | + } |
| 64 | + return value; |
| 65 | +} |
| 66 | + |
| 67 | +function truncate(s, n) { |
| 68 | + if (typeof s == 'string') { |
| 69 | + return s.length < n ? s : s.slice(0, n); |
| 70 | + } else { |
| 71 | + return s; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +assert.AssertionError.prototype.toString = function() { |
| 76 | + if (this.message) { |
| 77 | + return [this.name + ':', this.message].join(' '); |
| 78 | + } else { |
| 79 | + return [ |
| 80 | + this.name + ':', |
| 81 | + truncate(JSON.stringify(this.actual, replacer), 128), |
| 82 | + this.operator, |
| 83 | + truncate(JSON.stringify(this.expected, replacer), 128) |
| 84 | + ].join(' '); |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +// assert.AssertionError instanceof Error |
| 89 | + |
| 90 | +assert.AssertionError.__proto__ = Error.prototype; |
| 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 | + |
| 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 | +// EXTENSION! allows for well behaved errors defined elsewhere. |
| 114 | +assert.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 | + |
| 123 | +function ok(value, message) { |
| 124 | + if (!!!value) fail(value, true, message, '==', assert.ok); |
| 125 | +} |
| 126 | +assert.ok = ok; |
| 127 | + |
| 128 | +// 5. The equality assertion tests shallow, coercive equality with |
| 129 | +// ==. |
| 130 | +// assert.equal(actual, expected, message_opt); |
| 131 | + |
| 132 | +assert.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 | + |
| 139 | +assert.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 | + |
| 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 | + // 7.1. All identical values are equivalent, as determined by ===. |
| 156 | + if (actual === expected) { |
| 157 | + return true; |
| 158 | + |
| 159 | + } else if (Buffer.isBuffer(actual) && Buffer.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 (actual instanceof Date && expected instanceof Date) { |
| 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 (actual instanceof RegExp && expected instanceof RegExp) { |
| 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 (typeof actual != 'object' && typeof expected != 'object') { |
| 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 | + |
| 199 | +function isUndefinedOrNull(value) { |
| 200 | + return value === null || value === undefined; |
| 201 | +} |
| 202 | + |
| 203 | +function isArguments(object) { |
| 204 | + return Object.prototype.toString.call(object) == '[object Arguments]'; |
| 205 | +} |
| 206 | + |
| 207 | +function objEquiv(a, b) { |
| 208 | + if (isUndefinedOrNull(a) || isUndefinedOrNull(b)) |
| 209 | + return false; |
| 210 | + // an identical 'prototype' property. |
| 211 | + if (a.prototype !== b.prototype) return false; |
| 212 | + //~~~I've managed to break Object.keys through screwy arguments passing. |
| 213 | + // Converting to array solves the problem. |
| 214 | + if (isArguments(a)) { |
| 215 | + if (!isArguments(b)) { |
| 216 | + return false; |
| 217 | + } |
| 218 | + a = pSlice.call(a); |
| 219 | + b = pSlice.call(b); |
| 220 | + return _deepEqual(a, b); |
| 221 | + } |
| 222 | + try { |
| 223 | + var ka = Object.keys(a), |
| 224 | + kb = Object.keys(b), |
| 225 | + key, i; |
| 226 | + } catch (e) {//happens when one is a string literal and the other isn't |
| 227 | + return false; |
| 228 | + } |
| 229 | + // having the same number of owned properties (keys incorporates |
| 230 | + // hasOwnProperty) |
| 231 | + if (ka.length != kb.length) |
| 232 | + return false; |
| 233 | + //the same set of keys (although not necessarily the same order), |
| 234 | + ka.sort(); |
| 235 | + kb.sort(); |
| 236 | + //~~~cheap key test |
| 237 | + for (i = ka.length - 1; i >= 0; i--) { |
| 238 | + if (ka[i] != kb[i]) |
| 239 | + return false; |
| 240 | + } |
| 241 | + //equivalent values for every corresponding key, and |
| 242 | + //~~~possibly expensive deep test |
| 243 | + for (i = ka.length - 1; i >= 0; i--) { |
| 244 | + key = ka[i]; |
| 245 | + if (!_deepEqual(a[key], b[key])) return false; |
| 246 | + } |
| 247 | + return true; |
| 248 | +} |
| 249 | + |
| 250 | +// 8. The non-equivalence assertion tests for any deep inequality. |
| 251 | +// assert.notDeepEqual(actual, expected, message_opt); |
| 252 | + |
| 253 | +assert.notDeepEqual = function notDeepEqual(actual, expected, message) { |
| 254 | + if (_deepEqual(actual, expected)) { |
| 255 | + fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual); |
| 256 | + } |
| 257 | +}; |
| 258 | + |
| 259 | +// 9. The strict equality assertion tests strict equality, as determined by ===. |
| 260 | +// assert.strictEqual(actual, expected, message_opt); |
| 261 | + |
| 262 | +assert.strictEqual = function strictEqual(actual, expected, message) { |
| 263 | + if (actual !== expected) { |
| 264 | + fail(actual, expected, message, '===', assert.strictEqual); |
| 265 | + } |
| 266 | +}; |
| 267 | + |
| 268 | +// 10. The strict non-equality assertion tests for strict inequality, as |
| 269 | +// determined by !==. assert.notStrictEqual(actual, expected, message_opt); |
| 270 | + |
| 271 | +assert.notStrictEqual = function notStrictEqual(actual, expected, message) { |
| 272 | + if (actual === expected) { |
| 273 | + fail(actual, expected, message, '!==', assert.notStrictEqual); |
| 274 | + } |
| 275 | +}; |
| 276 | + |
| 277 | +function expectedException(actual, expected) { |
| 278 | + if (!actual || !expected) { |
| 279 | + return false; |
| 280 | + } |
| 281 | + |
| 282 | + if (expected instanceof RegExp) { |
| 283 | + return expected.test(actual); |
| 284 | + } else if (actual instanceof expected) { |
| 285 | + return true; |
| 286 | + } else if (expected.call({}, actual) === true) { |
| 287 | + return true; |
| 288 | + } |
| 289 | + |
| 290 | + return false; |
| 291 | +} |
| 292 | + |
| 293 | +function _throws(shouldThrow, block, expected, message) { |
| 294 | + var actual; |
| 295 | + |
| 296 | + if (typeof expected === 'string') { |
| 297 | + message = expected; |
| 298 | + expected = null; |
| 299 | + } |
| 300 | + |
| 301 | + try { |
| 302 | + block(); |
| 303 | + } catch (e) { |
| 304 | + actual = e; |
| 305 | + } |
| 306 | + |
| 307 | + message = (expected && expected.name ? ' (' + expected.name + ').' : '.') + |
| 308 | + (message ? ' ' + message : '.'); |
| 309 | + |
| 310 | + if (shouldThrow && !actual) { |
| 311 | + fail('Missing expected exception' + message); |
| 312 | + } |
| 313 | + |
| 314 | + if (!shouldThrow && expectedException(actual, expected)) { |
| 315 | + fail('Got unwanted exception' + message); |
| 316 | + } |
| 317 | + |
| 318 | + if ((shouldThrow && actual && expected && |
| 319 | + !expectedException(actual, expected)) || (!shouldThrow && actual)) { |
| 320 | + throw actual; |
| 321 | + } |
| 322 | +} |
| 323 | + |
| 324 | +// 11. Expected to throw an error: |
| 325 | +// assert.throws(block, Error_opt, message_opt); |
| 326 | + |
| 327 | +assert.throws = function(block, /*optional*/error, /*optional*/message) { |
| 328 | + _throws.apply(this, [true].concat(pSlice.call(arguments))); |
| 329 | +}; |
| 330 | + |
| 331 | +// EXTENSION! This is annoying to write outside this module. |
| 332 | +assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) { |
| 333 | + _throws.apply(this, [false].concat(pSlice.call(arguments))); |
| 334 | +}; |
| 335 | + |
| 336 | +assert.ifError = function(err) { if (err) {throw err;}}; |
0 commit comments