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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1208 545 545 545 545 545 1 544 544 544 556 544 556 556 7 35 104 104 9 95 189 189 2 187 189 189 189 189 188 189 1 188 1 187 1 189 95 95 168 168 168 95 189 189 94 95 5 5 31 5 90 90 158 158 158 68 90 95 25 25 25 1 746 16 35 40 5 35 1 34 121 34 34 98 98 98 34 121 121 87 34 4 4 35 4 30 30 86 86 86 56 30 34 5 1 757 7 8 8 1 758 3 2 1 777 4 12 12 3 12 12 2 1 5 2 1 780 1 12 12 12 8 12 4 8 6 12 24 24 360 320 320 320 40 32 24 16 16 8 12 1 6 6 6 4 2 2 2 2 6 1 5 5 5 74 2 1 1 2 2 2 2 1 1 794 2 2 2 2 1 1 798 2 2 2 2 1 1532 271 1 1596 1 1877 1 1876 1 1889 | /*global Uint8Array, Uint16Array*/ var utils = require('./utils'); var isRegExp = utils.isRegExp; var leftPad = utils.leftPad; var shim = require('./shim'); var json = shim.JSON; var every = shim.every; var some = shim.some; var forEach = shim.forEach; var map = shim.map; var getKeys = shim.getKeys; var reduce = shim.reduce; var extend = utils.extend; module.exports = function (expect) { expect.addType({ name: 'object', identify: function (obj) { return obj && typeof obj === 'object'; }, equal: function (a, b, equal) { Iif (a === b) { return true; } // an identical "prototype" property. Iif (a.prototype !== b.prototype) { return false; } //~~~I've managed to break Object.keys through screwy arguments passing. // Converting to array solves the problem. Iif (utils.isArguments(a)) { if (!utils.isArguments(b)) { return false; } return equal(Array.prototype.slice.call(a), Array.prototype.slice.call(b)); } var actualKeys = utils.getKeysOfDefinedProperties(a), expectedKeys = utils.getKeysOfDefinedProperties(b), key, i; // having the same number of owned properties (keys incorporates hasOwnProperty) if (actualKeys.length !== expectedKeys.length) { return false; } //the same set of keys (although not necessarily the same order), actualKeys.sort(); expectedKeys.sort(); //~~~cheap key test for (i = actualKeys.length - 1; i >= 0; i -= 1) { Iif (actualKeys[i] !== expectedKeys[i]) { return false; } } //equivalent values for every corresponding key, and //~~~possibly expensive deep test for (i = actualKeys.length - 1; i >= 0; i -= 1) { key = actualKeys[i]; if (!equal(a[key], b[key])) { return false; } } return true; }, inspect: function (output, obj, inspect) { var keys = getKeys(obj); if (keys.length === 0) { return output.text('{}'); } var inspectedItems = map(keys, function (key) { var propertyOutput = output.clone(); if (key.match(/["' ]/)) { propertyOutput.append(expect.inspect(key)); } else { propertyOutput.key(key); } propertyOutput.text(':'); var hasGetter = obj.__lookupGetter__ && obj.__lookupGetter__(key); var hasSetter = obj.__lookupGetter__ && obj.__lookupSetter__(key); if (hasGetter || !hasSetter) { propertyOutput.sp().append(inspect(propertyOutput.clone(), obj[key])); } if (hasGetter && hasSetter) { propertyOutput.sp().text('[Getter/Setter]'); } else if (hasGetter) { propertyOutput.sp().text('[Getter]'); } else if (hasSetter) { propertyOutput.sp().text('[Setter]'); } return propertyOutput; }); var width = 0; var multipleLines = some(inspectedItems, function (o) { var size = o.size(); width += size.width; return width > 50 || size.height > 1; }); forEach(inspectedItems, function (inspectedItem, index) { var lastIndex = index === inspectedItems.length - 1; if (!lastIndex) { inspectedItem.text(','); } }); if (multipleLines) { output.text('{').nl().indentLines(); forEach(inspectedItems, function (inspectedItem, index) { output.i().block(inspectedItem).nl(); }); output.outdentLines().text('}'); } else { output.text('{ '); forEach(inspectedItems, function (inspectedItem, index) { output.append(inspectedItem); var lastIndex = index === inspectedItems.length - 1; if (!lastIndex) { output.sp(); } }); output.text(' }'); } return output; }, toJSON: function (obj, toJSON) { return reduce(getKeys(obj), function (result, key) { result[key] = toJSON(obj[key]); return result; }, {}); } }); expect.addType({ name: 'array', identify: function (arr) { return utils.isArray(arr) || utils.isArguments(arr); }, equal: function (a, b, equal) { return a === b || (a.length === b.length && every(a, function (v, index) { return equal(v, b[index]); })); }, inspect: function (output, arr, inspect, depth) { if (arr.length === 0) { return output.text('[]'); } if (depth === 1) { return output.text('[...]'); } var inspectedItems = map(arr, function (v) { return inspect(output.clone(), v); }); var width = 0; var multipleLines = some(inspectedItems, function (o) { var size = o.size(); width += size.width; return width > 50 || o.height > 1; }); forEach(inspectedItems, function (inspectedItem, index) { var lastIndex = index === inspectedItems.length - 1; if (!lastIndex) { inspectedItem.text(','); } }); if (multipleLines) { output.text('[').nl().indentLines(); forEach(inspectedItems, function (inspectedItem, index) { output.i().block(inspectedItem).nl(); }); output.outdentLines().text(']'); } else { output.text('[ '); forEach(inspectedItems, function (inspectedItem, index) { output.append(inspectedItem); var lastIndex = index === inspectedItems.length - 1; if (!lastIndex) { output.sp(); } }); output.text(' ]'); } return output; }, toJSON: function (arr, toJSON) { return map(arr, toJSON); } }); expect.addType({ base: 'object', name: 'Error', identify: function (value) { return utils.isError(value); }, equal: function (a, b, equal) { return a === b || (equal(a.message, b.message) && this.baseType.equal(a, b, equal)); }, inspect: function (output, value, inspect) { var errorObject = extend({ message: value.message }, value); return output.text('[Error: ').append(inspect(output.clone(), errorObject)).text(']'); } }); expect.addType({ name: 'date', identify: function (obj) { return Object.prototype.toString.call(obj) === '[object Date]'; }, equal: function (a, b) { return a.getTime() === b.getTime(); }, inspect: function (output, date) { return output.text('[Date ').text(date.toUTCString()).text(']'); }, toJSON: function (date) { return { $Date: expect.inspect(date) }; } }); expect.addType({ name: 'function', identify: function (f) { return typeof f === 'function'; }, equal: function (a, b) { return a === b || a.toString() === b.toString(); }, inspect: function (output, f) { output.text('[Function'); if (f.name) { output.text(': ').text(f.name); } output.text(']'); return output; }, toJSON: function (f) { return { $Function: expect.inspect(f) }; } }); expect.addType({ name: 'regexp', identify: isRegExp, equal: function (a, b) { return a === b || ( a.source === b.source && a.global === b.global && a.ignoreCase === b.ignoreCase && a.multiline === b.multiline ); }, inspect: function (output, regExp) { return output.text(regExp); }, toJSON: function (regExp) { return { $RegExp: expect.inspect(regExp) }; } }); expect.addType({ name: 'DomElement', identify: function (value) { return utils.isDOMElement(value); }, inspect: function (output, value) { return output.code(utils.getOuterHTML(value), 'html'); } }); function getHexDumpLinesForBufferLikeObject(obj, width, digitWidth, maxLength) { digitWidth = digitWidth || 2; var hexDumpLines = []; if (typeof maxLength === 'undefined') { maxLength = obj.length; } if (typeof width !== 'number') { width = 16; } else if (width === 0) { width = maxLength; } for (var i = 0 ; i < maxLength ; i += width) { var hexChars = '', asciiChars = ' |'; for (var j = 0 ; j < width ; j += 1) { if (i + j < maxLength) { var octet = obj[i + j]; hexChars += leftPad(octet.toString(16).toUpperCase(), digitWidth, '0') + ' '; asciiChars += (octet >= 32 && octet < 127) ? String.fromCharCode(octet) : '.'; } else if (digitWidth === 2) { hexChars += ' '; } } if (digitWidth === 2) { asciiChars += '|'; hexDumpLines.push(hexChars + asciiChars); } else { hexDumpLines.push(hexChars.replace(/\s+$/, '')); } } return hexDumpLines; } function inspectBufferLikeObject(output, buffer, digitWidth, maxLength) { Eif (typeof maxLength !== 'number') { maxLength = 20; } if (buffer.length > maxLength) { output.text(getHexDumpLinesForBufferLikeObject(buffer, 0, digitWidth, maxLength)) .text(' (+').text(buffer.length - maxLength).text(')'); } else { var hexDumpLines = getHexDumpLinesForBufferLikeObject(buffer, 0, digitWidth); forEach(hexDumpLines, function (line, i) { Iif (i > 0) { output.nl(); } output.text(line); }); } return output; } function bufferLikeObjectsEqual(a, b) { Iif (a === b) { return true; } Iif (a.length !== b.length) return false; for (var i = 0; i < a.length; i += 1) { if (a[i] !== b[i]) return false; } return true; } Eif (typeof Buffer !== 'undefined') { expect.addType({ name: 'Buffer', identify: Buffer.isBuffer, equal: bufferLikeObjectsEqual, inspect: function (output, buffer) { output.text('[Buffer '); inspectBufferLikeObject(output, buffer); return output.text(']'); }, toJSON: function (buffer) { return { $Buffer: getHexDumpLinesForBufferLikeObject(buffer) }; } }); } Eif (typeof Uint8Array !== 'undefined') { expect.addType({ name: 'Uint8Array', identify: function (obj) { return obj && obj instanceof Uint8Array; }, equal: bufferLikeObjectsEqual, inspect: function (output, uint8Array) { output.text('[Uint8Array '); inspectBufferLikeObject(output, uint8Array); return output.text(']'); }, toJSON: function (uint8Array) { return { $Uint8Array: getHexDumpLinesForBufferLikeObject(uint8Array) }; } }); } Eif (typeof Uint16Array !== 'undefined') { expect.addType({ name: 'Uint16Array', identify: function (obj) { return obj && obj instanceof Uint16Array; }, equal: bufferLikeObjectsEqual, inspect: function (output, uint16Array) { output.text('[Uint16Array '); inspectBufferLikeObject(output, uint16Array, 4); return output.text(']'); }, toJSON: function (uint16Array) { return { $Uint16Array: getHexDumpLinesForBufferLikeObject(uint16Array, 8, 4) }; } }); } expect.addType({ name: 'string', identify: function (value) { return typeof value === 'string'; }, inspect: function (output, value) { return output.strings('\'') .strings(json.stringify(value).replace(/^"|"$/g, '') .replace(/'/g, "\\'") .replace(/\\"/g, '"')) .strings('\''); } }); expect.addType({ name: 'number', identify: function (value) { return typeof value === 'number'; } }); expect.addType({ name: 'boolean', identify: function (value) { return typeof value === 'boolean'; } }); expect.addType({ name: 'undefined', identify: function (value) { return typeof value === 'undefined'; } }); expect.addType({ name: 'null', identify: function (value) { return value === null; } }); }; |