diff --git a/lib/index.js b/lib/index.js index 5269cd786..26d317b0b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -17,7 +17,8 @@ var Path = require('path'), Math: Math, Boolean: Boolean, Function: Function, - Uint8Array: Uint8Array + Uint8Array: Uint8Array, + Uint16Array: Uint16Array }); [ diff --git a/lib/unexpected-types.js b/lib/unexpected-types.js index 7ec764a32..f72376881 100644 --- a/lib/unexpected-types.js +++ b/lib/unexpected-types.js @@ -56,7 +56,8 @@ } }); - function getHexDumpLinesForBufferLikeObject(obj, width) { + function getHexDumpLinesForBufferLikeObject(obj, width, digitWidth) { + digitWidth = digitWidth || 2; var hexDumpLines = []; if (typeof width !== 'number') { width = 16; @@ -70,26 +71,30 @@ for (var j = 0 ; j < width ; j += 1) { if (i + j < obj.length) { var octet = obj[i + j]; - hexChars += leftPad(octet.toString(16).toUpperCase(), 2, '0') + ' '; + hexChars += leftPad(octet.toString(16).toUpperCase(), digitWidth, '0') + ' '; asciiChars += (octet >= 32 && octet < 127) ? String.fromCharCode(octet) : '.'; - } else { + } else if (digitWidth === 2) { hexChars += ' '; } } - asciiChars += '|'; - hexDumpLines.push(hexChars + asciiChars); + if (digitWidth === 2) { + asciiChars += '|'; + hexDumpLines.push(hexChars + asciiChars); + } else { + hexDumpLines.push(hexChars.replace(/\s+$/, '')); + } } return hexDumpLines; } - function inspectBufferLikeObject(buffer) { + function inspectBufferLikeObject(buffer, digitWidth) { var inspectedContents, maxLength = 20; if (buffer.length > maxLength) { - inspectedContents = getHexDumpLinesForBufferLikeObject(buffer.slice(0, maxLength), 0) + ' (+' + (buffer.length - maxLength) + ')'; + inspectedContents = getHexDumpLinesForBufferLikeObject(buffer.slice(0, maxLength), 0, digitWidth) + ' (+' + (buffer.length - maxLength) + ')'; } else { - inspectedContents = getHexDumpLinesForBufferLikeObject(buffer, 0).join('\n'); + inspectedContents = getHexDumpLinesForBufferLikeObject(buffer, 0, digitWidth).join('\n'); } return inspectedContents; } @@ -135,4 +140,22 @@ } }); } + + + if (typeof Uint16Array !== 'undefined') { + expect.addType({ + identify: function (obj) { + return obj && obj instanceof Uint16Array; + }, + equal: bufferLikeObjectsEqual, + inspect: function (uint16Array) { + return '[Uint16Array ' + inspectBufferLikeObject(uint16Array, 8, 4, false) + ']'; + }, + toJSON: function (uint16Array) { + return { + $Uint16Array: getHexDumpLinesForBufferLikeObject(uint16Array, 8, 4, false) + }; + } + }); + } }()); diff --git a/test/unexpected.spec.js b/test/unexpected.spec.js index 929f3ae55..013a22905 100644 --- a/test/unexpected.spec.js +++ b/test/unexpected.spec.js @@ -333,6 +333,42 @@ describe('unexpected', function () { }); }); }); + + itSkipIf(typeof Uint16Array === 'undefined', 'produces a hex-diff in JSON when Uint16Arrays differ', function () { + expect(function () { + expect( + new Uint16Array([ + 0x0001, 0x0248, 0x6572, 0x6520, 0x6973, 0x2074, 0x6865, 0x2074, + 0x6869, 0x6E67, 0x2049, 0x2077, 0x6173, 0x2074, 0x616C, 0x6B69, + 0x6E67, 0x2061, 0x626F, 0x7574 + ]), + 'to equal', + new Uint16Array([ + 0x0001, 0x0248, 0x6572, 0x6520, 0x6973, 0x2074, 0x6865, 0x2074, + 0x6869, 0x6E67, 0x2049, 0x2077, 0x6173, 0x2071, 0x7575, 0x7869, + 0x6E67, 0x2061, 0x626F, 0x7574 + ]) + ); + }, 'to throw', function (err) { + expect(err, 'to have properties', { + showDiff: true, + actual: { + $Uint16Array: [ + '0001 0248 6572 6520 6973 2074 6865 2074', + '6869 6E67 2049 2077 6173 2074 616C 6B69', + '6E67 2061 626F 7574' + ] + }, + expected: { + $Uint16Array: [ + '0001 0248 6572 6520 6973 2074 6865 2074', + '6869 6E67 2049 2077 6173 2071 7575 7869', + '6E67 2061 626F 7574' + ] + } + }); + }); + }); }); describe('exception assertion', function () {