Name: js-handler/node_modules/nodeunit/lib/assert.js
| 1: | /** |
| 2: | * This file is based on the node.js assert module, but with some small |
| 3: | * changes for browser-compatibility |
| 4: | * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS! |
| 5: | */ |
| 6: | |
| 7: | |
| 8: | /** |
| 9: | * Added for browser compatibility |
| 10: | */ |
| 11: | |
| 12: | var _keys = function(obj){ |
| 13: | if(Object.keys) return Object.keys(obj); |
| 14: | if (typeof obj != 'object' && typeof obj != 'function') { |
| 15: | throw new TypeError('-'); |
| 16: | } |
| 17: | var keys = []; |
| 18: | for(var k in obj){ |
| 19: | if(obj.hasOwnProperty(k)) keys.push(k); |
| 20: | } |
| 21: | return keys; |
| 22: | }; |
| 23: | |
| 24: | |
| 25: | |
| 26: | // http://wiki.commonjs.org/wiki/Unit_Testing/1.0 |
| 27: | // |
| 28: | // THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8! |
| 29: | // |
| 30: | // Originally from narwhal.js (http://narwhaljs.org) |
| 31: | // Copyright (c) 2009 Thomas Robinson <280north.com> |
| 32: | // |
| 33: | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 34: | // of this software and associated documentation files (the 'Software'), to |
| 35: | // deal in the Software without restriction, including without limitation the |
| 36: | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 37: | // sell copies of the Software, and to permit persons to whom the Software is |
| 38: | // furnished to do so, subject to the following conditions: |
| 39: | // |
| 40: | // The above copyright notice and this permission notice shall be included in |
| 41: | // all copies or substantial portions of the Software. |
| 42: | // |
| 43: | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 44: | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 45: | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 46: | // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 47: | // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 48: | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 49: | |
| 50: | |
| 51: | var pSlice = Array.prototype.slice; |
| 52: | |
| 53: | // 1. The assert module provides functions that throw |
| 54: | // AssertionError's when particular conditions are not met. The |
| 55: | // assert module must conform to the following interface. |
| 56: | |
| 57: | var assert = exports; |
| 58: | |
| 59: | // 2. The AssertionError is defined in assert. |
| 60: | // new assert.AssertionError({message: message, actual: actual, expected: expected}) |
| 61: | |
| 62: | assert.AssertionError = function AssertionError (options) { |
| 63: | this.name = "AssertionError"; |
| 64: | this.message = options.message; |
| 65: | this.actual = options.actual; |
| 66: | this.expected = options.expected; |
| 67: | this.operator = options.operator; |
| 68: | var stackStartFunction = options.stackStartFunction || fail; |
| 69: | |
| 70: | if (Error.captureStackTrace) { |
| 71: | Error.captureStackTrace(this, stackStartFunction); |
| 72: | } |
| 73: | }; |
| 74: | // code from util.inherits in node |
| 75: | assert.AssertionError.super_ = Error; |
| 76: | |
| 77: | |
| 78: | // EDITED FOR BROWSER COMPATIBILITY: replaced Object.create call |
| 79: | // TODO: test what effect this may have |
| 80: | var ctor = function () { this.constructor = assert.AssertionError; }; |
| 81: | ctor.prototype = Error.prototype; |
| 82: | assert.AssertionError.prototype = new ctor(); |
| 83: | |
| 84: | |
| 85: | assert.AssertionError.prototype.toString = function() { |
| 86: | if (this.message) { |
| 87: | return [this.name+":", this.message].join(' '); |
| 88: | } else { |
| 89: | return [ this.name+":" |
| 90: | , JSON.stringify(this.expected ) |
| 91: | , this.operator |
| 92: | , JSON.stringify(this.actual) |
| 93: | ].join(" "); |
| 94: | } |
| 95: | }; |
| 96: | |
| 97: | // assert.AssertionError instanceof Error |
| 98: | |
| 99: | assert.AssertionError.__proto__ = Error.prototype; |
| 100: | |
| 101: | // At present only the three keys mentioned above are used and |
| 102: | // understood by the spec. Implementations or sub modules can pass |
| 103: | // other keys to the AssertionError's constructor - they will be |
| 104: | // ignored. |
| 105: | |
| 106: | // 3. All of the following functions must throw an AssertionError |
| 107: | // when a corresponding condition is not met, with a message that |
| 108: | // may be undefined if not provided. All assertion methods provide |
| 109: | // both the actual and expected values to the assertion error for |
| 110: | // display purposes. |
| 111: | |
| 112: | function fail(actual, expected, message, operator, stackStartFunction) { |
| 113: | throw new assert.AssertionError({ |
| 114: | message: message, |
| 115: | actual: actual, |
| 116: | expected: expected, |
| 117: | operator: operator, |
| 118: | stackStartFunction: stackStartFunction |
| 119: | }); |
| 120: | } |
| 121: | |
| 122: | // EXTENSION! allows for well behaved errors defined elsewhere. |
| 123: | assert.fail = fail; |
| 124: | |
| 125: | // 4. Pure assertion tests whether a value is truthy, as determined |
| 126: | // by !!guard. |
| 127: | // assert.ok(guard, message_opt); |
| 128: | // This statement is equivalent to assert.equal(true, guard, |
| 129: | // message_opt);. To test strictly for the value true, use |
| 130: | // assert.strictEqual(true, guard, message_opt);. |
| 131: | |
| 132: | assert.ok = function ok(value, message) { |
| 133: | if (!!!value) fail(value, true, message, "==", assert.ok); |
| 134: | }; |
| 135: | |
| 136: | // 5. The equality assertion tests shallow, coercive equality with |
| 137: | // ==. |
| 138: | // assert.equal(actual, expected, message_opt); |
| 139: | |
| 140: | assert.equal = function equal(actual, expected, message) { |
| 141: | if (actual != expected) fail(actual, expected, message, "==", assert.equal); |
| 142: | }; |
| 143: | |
| 144: | // 6. The non-equality assertion tests for whether two objects are not equal |
| 145: | // with != assert.notEqual(actual, expected, message_opt); |
| 146: | |
| 147: | assert.notEqual = function notEqual(actual, expected, message) { |
| 148: | if (actual == expected) { |
| 149: | fail(actual, expected, message, "!=", assert.notEqual); |
| 150: | } |
| 151: | }; |
| 152: | |
| 153: | // 7. The equivalence assertion tests a deep equality relation. |
| 154: | // assert.deepEqual(actual, expected, message_opt); |
| 155: | |
| 156: | assert.deepEqual = function deepEqual(actual, expected, message) { |
| 157: | if (!_deepEqual(actual, expected)) { |
| 158: | fail(actual, expected, message, "deepEqual", assert.deepEqual); |
| 159: | } |
| 160: | }; |
| 161: | |
| 162: | var Buffer = null; |
| 163: | if (typeof require !== 'undefined' && typeof process !== 'undefined') { |
| 164: | try { |
| 165: | Buffer = require('buffer').Buffer; |
| 166: | } |
| 167: | catch (e) { |
| 168: | // May be a CommonJS environment other than Node.js |
| 169: | Buffer = null; |
| 170: | } |
| 171: | } |
| 172: | |
| 173: | function _deepEqual(actual, expected) { |
| 174: | // 7.1. All identical values are equivalent, as determined by ===. |
| 175: | if (actual === expected) { |
| 176: | return true; |
| 177: | // 7.2. If the expected value is a Date object, the actual value is |
| 178: | // equivalent if it is also a Date object that refers to the same time. |
| 179: | } else if (actual instanceof Date && expected instanceof Date) { |
| 180: | return actual.getTime() === expected.getTime(); |
| 181: | |
| 182: | // 7.2.1 If the expcted value is a RegExp object, the actual value is |
| 183: | // equivalent if it is also a RegExp object that refers to the same source and options |
| 184: | } else if (actual instanceof RegExp && expected instanceof RegExp) { |
| 185: | return actual.source === expected.source && |
| 186: | actual.global === expected.global && |
| 187: | actual.ignoreCase === expected.ignoreCase && |
| 188: | actual.multiline === expected.multiline; |
| 189: | |
| 190: | } else if (Buffer && actual instanceof Buffer && expected instanceof Buffer) { |
| 191: | return (function() { |
| 192: | var i, len; |
| 193: | |
| 194: | for (i = 0, len = expected.length; i < len; i++) { |
| 195: | if (actual[i] !== expected[i]) { |
| 196: | return false; |
| 197: | } |
| 198: | } |
| 199: | return actual.length === expected.length; |
| 200: | })(); |
| 201: | // 7.3. Other pairs that do not both pass typeof value == "object", |
| 202: | // equivalence is determined by ==. |
| 203: | } else if (typeof actual != 'object' && typeof expected != 'object') { |
| 204: | return actual == expected; |
| 205: | |
| 206: | // 7.4. For all other Object pairs, including Array objects, equivalence is |
| 207: | // determined by having the same number of owned properties (as verified |
| 208: | // with Object.prototype.hasOwnProperty.call), the same set of keys |
| 209: | // (although not necessarily the same order), equivalent values for every |
| 210: | // corresponding key, and an identical "prototype" property. Note: this |
| 211: | // accounts for both named and indexed properties on Arrays. |
| 212: | } else { |
| 213: | return objEquiv(actual, expected); |
| 214: | } |
| 215: | } |
| 216: | |
| 217: | function isUndefinedOrNull (value) { |
| 218: | return value === null || value === undefined; |
| 219: | } |
| 220: | |
| 221: | function isArguments (object) { |
| 222: | return Object.prototype.toString.call(object) == '[object Arguments]'; |
| 223: | } |
| 224: | |
| 225: | function objEquiv (a, b) { |
| 226: | if (isUndefinedOrNull(a) || isUndefinedOrNull(b)) |
| 227: | return false; |
| 228: | // an identical "prototype" property. |
| 229: | if (a.prototype !== b.prototype) return false; |
| 230: | //~~~I've managed to break Object.keys through screwy arguments passing. |
| 231: | // Converting to array solves the problem. |
| 232: | if (isArguments(a)) { |
| 233: | if (!isArguments(b)) { |
| 234: | return false; |
| 235: | } |
| 236: | a = pSlice.call(a); |
| 237: | b = pSlice.call(b); |
| 238: | return _deepEqual(a, b); |
| 239: | } |
| 240: | try{ |
| 241: | var ka = _keys(a), |
| 242: | kb = _keys(b), |
| 243: | key, i; |
| 244: | } catch (e) {//happens when one is a string literal and the other isn't |
| 245: | return false; |
| 246: | } |
| 247: | // having the same number of owned properties (keys incorporates hasOwnProperty) |
| 248: | if (ka.length != kb.length) |
| 249: | return false; |
| 250: | //the same set of keys (although not necessarily the same order), |
| 251: | ka.sort(); |
| 252: | kb.sort(); |
| 253: | //~~~cheap key test |
| 254: | for (i = ka.length - 1; i >= 0; i--) { |
| 255: | if (ka[i] != kb[i]) |
| 256: | return false; |
| 257: | } |
| 258: | //equivalent values for every corresponding key, and |
| 259: | //~~~possibly expensive deep test |
| 260: | for (i = ka.length - 1; i >= 0; i--) { |
| 261: | key = ka[i]; |
| 262: | if (!_deepEqual(a[key], b[key] )) |
| 263: | return false; |
| 264: | } |
| 265: | return true; |
| 266: | } |
| 267: | |
| 268: | // 8. The non-equivalence assertion tests for any deep inequality. |
| 269: | // assert.notDeepEqual(actual, expected, message_opt); |
| 270: | |
| 271: | assert.notDeepEqual = function notDeepEqual(actual, expected, message) { |
| 272: | if (_deepEqual(actual, expected)) { |
| 273: | fail(actual, expected, message, "notDeepEqual", assert.notDeepEqual); |
| 274: | } |
| 275: | }; |
| 276: | |
| 277: | // 9. The strict equality assertion tests strict equality, as determined by ===. |
| 278: | // assert.strictEqual(actual, expected, message_opt); |
| 279: | |
| 280: | assert.strictEqual = function strictEqual(actual, expected, message) { |
| 281: | if (actual !== expected) { |
| 282: | fail(actual, expected, message, "===", assert.strictEqual); |
| 283: | } |
| 284: | }; |
| 285: | |
| 286: | // 10. The strict non-equality assertion tests for strict inequality, as determined by !==. |
| 287: | // assert.notStrictEqual(actual, expected, message_opt); |
| 288: | |
| 289: | assert.notStrictEqual = function notStrictEqual(actual, expected, message) { |
| 290: | if (actual === expected) { |
| 291: | fail(actual, expected, message, "!==", assert.notStrictEqual); |
| 292: | } |
| 293: | }; |
| 294: | |
| 295: | function expectedException(actual, expected) { |
| 296: | if (!actual || !expected) { |
| 297: | return false; |
| 298: | } |
| 299: | |
| 300: | if (expected instanceof RegExp) { |
| 301: | return expected.test(actual.message || actual); |
| 302: | } else if (actual instanceof expected) { |
| 303: | return true; |
| 304: | } else if (expected.call({}, actual) === true) { |
| 305: | return true; |
| 306: | } |
| 307: | |
| 308: | return false; |
| 309: | } |
| 310: | |
| 311: | function _throws(shouldThrow, block, expected, message) { |
| 312: | var actual; |
| 313: | |
| 314: | if (typeof expected === 'string') { |
| 315: | message = expected; |
| 316: | expected = null; |
| 317: | } |
| 318: | |
| 319: | try { |
| 320: | block(); |
| 321: | } catch (e) { |
| 322: | actual = e; |
| 323: | } |
| 324: | |
| 325: | message = (expected && expected.name ? ' (' + expected.name + ').' : '.') + |
| 326: | (message ? ' ' + message : '.'); |
| 327: | |
| 328: | if (shouldThrow && !actual) { |
| 329: | fail('Missing expected exception' + message); |
| 330: | } |
| 331: | |
| 332: | if (!shouldThrow && expectedException(actual, expected)) { |
| 333: | fail('Got unwanted exception' + message); |
| 334: | } |
| 335: | |
| 336: | if ((shouldThrow && actual && expected && |
| 337: | !expectedException(actual, expected)) || (!shouldThrow && actual)) { |
| 338: | throw actual; |
| 339: | } |
| 340: | } |
| 341: | |
| 342: | // 11. Expected to throw an error: |
| 343: | // assert.throws(block, Error_opt, message_opt); |
| 344: | |
| 345: | assert.throws = function(block, /*optional*/error, /*optional*/message) { |
| 346: | _throws.apply(this, [true].concat(pSlice.call(arguments))); |
| 347: | }; |
| 348: | |
| 349: | // EXTENSION! This is annoying to write outside this module. |
| 350: | assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) { |
| 351: | _throws.apply(this, [false].concat(pSlice.call(arguments))); |
| 352: | }; |
| 353: | |
| 354: | assert.ifError = function (err) { if (err) {throw err;}}; |
