Skip to content

Commit

Permalink
Include support for diffing and comparing Uint16Array instances (#35).
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Apr 11, 2014
1 parent f545947 commit 0ae4db8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/index.js
Expand Up @@ -17,7 +17,8 @@ var Path = require('path'),
Math: Math,
Boolean: Boolean,
Function: Function,
Uint8Array: Uint8Array
Uint8Array: Uint8Array,
Uint16Array: Uint16Array
});

[
Expand Down
39 changes: 31 additions & 8 deletions lib/unexpected-types.js
Expand Up @@ -56,7 +56,8 @@
}
});

function getHexDumpLinesForBufferLikeObject(obj, width) {
function getHexDumpLinesForBufferLikeObject(obj, width, digitWidth) {
digitWidth = digitWidth || 2;
var hexDumpLines = [];
if (typeof width !== 'number') {
width = 16;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
};
}
});
}
}());
36 changes: 36 additions & 0 deletions test/unexpected.spec.js
Expand Up @@ -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 () {
Expand Down

0 comments on commit 0ae4db8

Please sign in to comment.