Skip to content

Commit

Permalink
Added support for diffing and comparing Uint8Array instances (#35).
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Apr 11, 2014
1 parent 2594ebd commit 96a250f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 46 deletions.
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global console, __dirname, weknowhow*/
/*global console, __dirname, weknowhow, Uint8Array, Uint16Array*/
var Path = require('path'),
fs = require('fs'),
vm = require('vm'),
Expand All @@ -16,7 +16,8 @@ var Path = require('path'),
Number: Number,
Math: Math,
Boolean: Boolean,
Function: Function
Function: Function,
Uint8Array: Uint8Array
});

[
Expand Down
110 changes: 67 additions & 43 deletions lib/unexpected-types.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global namespace*/
/*global namespace, Uint8Array, Uint16Array*/
(function () {
var expect = namespace.expect;

Expand Down Expand Up @@ -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)
};
}
});
Expand Down
46 changes: 45 additions & 1 deletion test/unexpected.spec.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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 = {},
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 96a250f

Please sign in to comment.