diff --git a/lib/index.js b/lib/index.js index ff3e123c1..5269cd786 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,4 +1,4 @@ -/*global console, __dirname, weknowhow*/ +/*global console, __dirname, weknowhow, Uint8Array, Uint16Array*/ var Path = require('path'), fs = require('fs'), vm = require('vm'), @@ -16,7 +16,8 @@ var Path = require('path'), Number: Number, Math: Math, Boolean: Boolean, - Function: Function + Function: Function, + Uint8Array: Uint8Array }); [ diff --git a/lib/unexpected-types.js b/lib/unexpected-types.js index e0261e9c5..599c45022 100644 --- a/lib/unexpected-types.js +++ b/lib/unexpected-types.js @@ -1,4 +1,4 @@ -/*global namespace*/ +/*global namespace, Uint8Array, Uint16Array*/ (function () { var expect = namespace.expect; @@ -55,57 +55,81 @@ } }); - if (typeof Buffer !== 'undefined') { - expect.addType({ - identify: Buffer.isBuffer, - equal: function (a, b) { - if (a.length !== b.length) return false; + function getHexDumpLinesForBufferLikeObject(obj, width) { + var hexDumpLines = []; + if (typeof width !== 'number') { + width = 16; + } else if (width === 0) { + width = obj.length; + } + for (var i = 0 ; i < obj.length ; i += width) { + var hexChars = '', + asciiChars = ' |'; - for (var i = 0; i < a.length; i += 1) { - if (a[i] !== b[i]) return false; + for (var j = 0 ; j < width ; j += 1) { + if (i + j < obj.length) { + var octet = obj[i + j]; + hexChars += (octet < 16 ? '0' : '') + octet.toString(16).toUpperCase() + ' '; + asciiChars += (octet >= 32 && octet < 127) ? String.fromCharCode(octet) : '.'; + } else { + hexChars += ' '; } + } - return true; - }, - getHexDumpLines: function (buffer, width) { - var hexDumpLines = []; - if (typeof width !== 'number') { - width = 16; - } else if (width === 0) { - width = buffer.length; - } - for (var i = 0 ; i < buffer.length ; i += width) { - var hexChars = '', - asciiChars = ' |'; + asciiChars += '|'; + hexDumpLines.push(hexChars + asciiChars); + } + return hexDumpLines; + } - for (var j = 0 ; j < width ; j += 1) { - if (i + j < buffer.length) { - var octet = buffer[i + j]; - hexChars += (octet < 16 ? '0' : '') + octet.toString(16).toUpperCase() + ' '; - asciiChars += (octet >= 32 && octet < 127) ? String.fromCharCode(octet) : '.'; - } else { - hexChars += ' '; - } - } + function inspectBufferLikeObject(buffer) { + var inspectedContents, + maxLength = 20; + if (buffer.length > maxLength) { + inspectedContents = getHexDumpLinesForBufferLikeObject(buffer.slice(0, maxLength), 0) + ' (+' + (buffer.length - maxLength) + ')'; + } else { + inspectedContents = getHexDumpLinesForBufferLikeObject(buffer, 0).join('\n'); + } + return inspectedContents; + } - asciiChars += '|'; - hexDumpLines.push(hexChars + asciiChars); - } - return hexDumpLines; - }, + function bufferLikeObjectsEqual(a, b) { + if (a.length !== b.length) return false; + + for (var i = 0; i < a.length; i += 1) { + if (a[i] !== b[i]) return false; + } + + return true; + } + + if (typeof Buffer !== 'undefined') { + expect.addType({ + identify: Buffer.isBuffer, + equal: bufferLikeObjectsEqual, inspect: function (buffer) { - var inspectedContents, - maxLength = 20; - if (buffer.length > maxLength) { - inspectedContents = this.getHexDumpLines(buffer.slice(0, maxLength), 0) + ' (+' + (buffer.length - maxLength) + ')'; - } else { - inspectedContents = this.getHexDumpLines(buffer, 0).join('\n'); - } - return '[Buffer ' + inspectedContents + ']'; + return '[Buffer ' + inspectBufferLikeObject(buffer) + ']'; }, toJSON: function (buffer) { return { - $buffer: this.getHexDumpLines(buffer) + $buffer: getHexDumpLinesForBufferLikeObject(buffer) + }; + } + }); + } + + if (typeof Uint8Array !== 'undefined') { + expect.addType({ + identify: function (obj) { + return obj && obj instanceof Uint8Array; + }, + equal: bufferLikeObjectsEqual, + inspect: function (uint8Array) { + return '[Uint8Array ' + inspectBufferLikeObject(uint8Array) + ']'; + }, + toJSON: function (uint8Array) { + return { + $uint8Array: getHexDumpLinesForBufferLikeObject(uint8Array) }; } }); diff --git a/test/unexpected.spec.js b/test/unexpected.spec.js index 43c0b950b..9578edbc4 100644 --- a/test/unexpected.spec.js +++ b/test/unexpected.spec.js @@ -1,4 +1,4 @@ -/*global describe, it, expect, beforeEach, setTimeout*/ +/*global describe, it, expect, beforeEach, setTimeout, Uint8Array, Uint16Array*/ // use this instead of Object.create in order to make the tests run in // browsers that are not es5 compatible. @@ -98,6 +98,11 @@ describe('unexpected', function () { expect(buffer, 'to be', buffer); }); + itSkipIf(typeof Uint8Array === 'undefined', 'asserts === equality for Uint8Array', function () { + var uint8Array = new Uint8Array([0x45, 0x59]); + expect(uint8Array, 'to be', uint8Array); + }); + it('throws when the assertion fails', function () { expect(function () { expect('foo', 'to be', 'bar'); @@ -189,6 +194,9 @@ describe('unexpected', function () { expect(new Buffer([0x45, 0x59]), 'to equal', new Buffer([0x45, 0x59])); }); + itSkipIf(typeof Uint8Array === 'undefined', 'asserts equality for Uint8Array', function () { + expect(new Uint8Array([0x45, 0x59]), 'to equal', new Uint8Array([0x45, 0x59])); + }); it('fails gracefully when comparing circular structures', function () { var foo = {}, @@ -289,6 +297,42 @@ describe('unexpected', function () { }); }); }); + + itSkipIf(typeof Uint8Array === 'undefined', 'produces a hex-diff in JSON when Uint8Arrays differ', function () { + expect(function () { + expect( + new Uint8Array([ + 0x00, 0x01, 0x02, 0x48, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, + 0x68, 0x69, 0x6E, 0x67, 0x20, 0x49, 0x20, 0x77, 0x61, 0x73, 0x20, 0x74, 0x61, 0x6C, 0x6B, 0x69, + 0x6E, 0x67, 0x20, 0x61, 0x62, 0x6F, 0x75, 0x74 + ]), + 'to equal', + new Uint8Array([ + 0x00, 0x01, 0x02, 0x48, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, + 0x68, 0x69, 0x6E, 0x67, 0x20, 0x49, 0x20, 0x77, 0x61, 0x73, 0x20, 0x71, 0x75, 0x75, 0x78, 0x69, + 0x6E, 0x67, 0x20, 0x61, 0x62, 0x6F, 0x75, 0x74 + ]) + ); + }, 'to throw', function (err) { + expect(err, 'to have properties', { + showDiff: true, + actual: { + $uint8Array: [ + '00 01 02 48 65 72 65 20 69 73 20 74 68 65 20 74 |...Here is the t|', + '68 69 6E 67 20 49 20 77 61 73 20 74 61 6C 6B 69 |hing I was talki|', + '6E 67 20 61 62 6F 75 74 |ng about|' + ] + }, + expected: { + $uint8Array: [ + '00 01 02 48 65 72 65 20 69 73 20 74 68 65 20 74 |...Here is the t|', + '68 69 6E 67 20 49 20 77 61 73 20 71 75 75 78 69 |hing I was quuxi|', + '6E 67 20 61 62 6F 75 74 |ng about|' + ] + } + }); + }); + }); }); describe('exception assertion', function () {