Skip to content

Commit 66e6f03

Browse files
committed
more robust error handling
1 parent c86c02d commit 66e6f03

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ const defaultOptions = {
1212

1313
function pixelmatch(img1, img2, output, width, height, options) {
1414

15-
if (img1.length !== img2.length) throw new Error('Image sizes do not match.');
16-
17-
if (!ArrayBuffer.isView(img1) || !ArrayBuffer.isView(img2) || (output && !ArrayBuffer.isView(output)))
15+
if (!isPixelData(img1) || !isPixelData(img2) || (output && !isPixelData(output)))
1816
throw new Error('Image data: Uint8Array, Uint8ClampedArray or Buffer expected.');
1917

18+
if (img1.length !== img2.length || (output && output.length !== img1.length))
19+
throw new Error('Image sizes do not match.');
20+
21+
if (img1.length !== width * height * 4) throw new Error('Image data size does not match width/height.');
22+
2023
options = Object.assign({}, defaultOptions, options);
2124

2225
// check if images are identical
@@ -77,6 +80,10 @@ function pixelmatch(img1, img2, output, width, height, options) {
7780
return diff;
7881
}
7982

83+
function isPixelData(arr) {
84+
return arr instanceof Uint8Array || arr instanceof Uint8ClampedArray;
85+
}
86+
8087
// check if a pixel is likely a part of anti-aliasing;
8188
// based on "Anti-aliased Pixel and Intensity Slope Detector" paper by V. Vysniauskas, 2009
8289

test/test.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,22 @@ diffTest('6a', '6b', '6diff', options, 51);
2222
diffTest('6a', '6a', '6empty', {threshold: 0}, 0);
2323

2424
test('throws error if image sizes do not match', (t) => {
25-
t.throws(() => match([1, 2, 3], [1, 2, 3, 4], null, 2, 1), /Image sizes do not match/);
25+
t.throws(() => match(new Uint8Array(8), new Uint8Array(9), null, 2, 1), 'Image sizes do not match');
26+
t.end();
27+
});
28+
29+
test('throws error if image sizes do not match width and height', (t) => {
30+
t.throws(() => match(new Uint8Array(9), new Uint8Array(9), null, 2, 1), 'Image data size does not match width/height');
2631
t.end();
2732
});
2833

2934
test('throws error if provided wrong image data format', (t) => {
30-
const re = /Image data: Uint8Array, Uint8ClampedArray or Buffer expected/;
35+
const err = 'Image data: Uint8Array, Uint8ClampedArray or Buffer expected';
3136
const arr = new Uint8Array(4 * 20 * 20);
3237
const bad = new Array(arr.length).fill(0);
33-
t.throws(() => match(bad, arr, null, 20, 20), re);
34-
t.throws(() => match(arr, bad, null, 20, 20), re);
35-
t.throws(() => match(arr, arr, bad, 20, 20), re);
38+
t.throws(() => match(bad, arr, null, 20, 20), err);
39+
t.throws(() => match(arr, bad, null, 20, 20), err);
40+
t.throws(() => match(arr, arr, bad, 20, 20), err);
3641
t.end();
3742
});
3843

@@ -52,10 +57,10 @@ function diffTest(imgPath1, imgPath2, diffPath, options, expectedMismatch) {
5257
writeImage(diffPath, diff);
5358
} else {
5459
const expectedDiff = readImage(diffPath);
55-
t.same(diff.data, expectedDiff.data, 'diff image');
60+
t.ok(diff.data.equals(expectedDiff.data), 'diff image');
5661
}
57-
t.same(mismatch, expectedMismatch, 'number of mismatched pixels');
58-
t.same(mismatch, mismatch2, 'number of mismatched pixels without diff');
62+
t.equal(mismatch, expectedMismatch, 'number of mismatched pixels');
63+
t.equal(mismatch, mismatch2, 'number of mismatched pixels without diff');
5964

6065
t.end();
6166
});

0 commit comments

Comments
 (0)