Use image fingerprints in functional tests #122

This commit is contained in:
Lovell Fuller 2015-04-30 16:59:12 +01:00
parent 75d72cfded
commit b50fb53f27
28 changed files with 142 additions and 101 deletions

View File

@ -669,6 +669,10 @@ A [guide for contributors](https://github.com/lovell/sharp/blob/master/CONTRIBUT
### Functional tests
Where possible, the functional tests use gradient-based perceptual hashes
based on [dHash](http://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html)
to compare expected vs actual images.
#### Coverage
[![Test Coverage](https://coveralls.io/repos/lovell/sharp/badge.png?branch=master)](https://coveralls.io/r/lovell/sharp?branch=master)

BIN
test/fixtures/expected/exif-5.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
test/fixtures/expected/exif-8.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 506 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

BIN
test/fixtures/expected/extract.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 348 B

BIN
test/fixtures/expected/extract.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

BIN
test/fixtures/expected/extract.tiff vendored Normal file

Binary file not shown.

BIN
test/fixtures/expected/extract.webp vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

BIN
test/fixtures/expected/flatten-black.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 996 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

BIN
test/fixtures/expected/flip-and-flop.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
test/fixtures/expected/flip.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
test/fixtures/expected/flop.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

BIN
test/fixtures/expected/gravity-east.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

BIN
test/fixtures/expected/gravity-north.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

BIN
test/fixtures/expected/gravity-south.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

BIN
test/fixtures/expected/gravity-west.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@ -1,11 +1,42 @@
'use strict';
var path = require('path');
var assert = require('assert');
var sharp = require('../../index');
var getPath = function(filename) {
return path.join(__dirname, filename);
};
// Generates a 64-bit-as-binary-string image fingerprint
// Based on the dHash gradient method - see http://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html
var fingerprint = function(image, done) {
sharp(image)
.greyscale()
.normalise()
.resize(9, 8)
.ignoreAspectRatio()
.interpolateWith(sharp.interpolator.vertexSplitQuadraticBasisSpline)
.raw()
.toBuffer(function(err, data) {
if (err) {
done(err);
} else {
var fingerprint = '';
for (var col = 0; col < 8; col++) {
var gradient = 0;
for (var row = 0; row < 8; row++) {
var left = data[row * 8 + col];
var right = data[row * 8 + col + 1];
fingerprint = fingerprint + (left < right ? '1' : '0');
}
}
done(null, fingerprint);
}
});
};
module.exports = {
inputJpg: getPath('2569067123_aca715a2ee_o.jpg'), // http://www.flickr.com/photos/grizdave/2569067123/
@ -35,6 +66,30 @@ module.exports = {
outputWebP: getPath('output.webp'),
outputZoinks: getPath('output.zoinks'), // an 'unknown' file extension
path: getPath // allows tests to write files to fixtures directory (for testing with human eyes)
// Path for tests requiring human inspection
path: getPath,
// Path for expected output images
expected: function(filename) {
return getPath(path.join('expected', filename));
},
// Verify similarity of expected vs actual images via fingerprint
assertSimilar: function(expectedImage, actualImage, done) {
fingerprint(expectedImage, function(err, expectedFingerprint) {
if (err) throw err;
fingerprint(actualImage, function(err, actualFingerprint) {
if (err) throw err;
var distance = 0;
for (var i = 0; i < 64; i++) {
if (expectedFingerprint[i] !== actualFingerprint[i]) {
distance++;
}
}
assert.strictEqual(true, distance <= 5); // ~7% threshold
done();
});
});
}
};

View File

@ -13,7 +13,12 @@ describe('Alpha transparency', function() {
sharp(fixtures.inputPngWithTransparency)
.flatten()
.resize(400, 300)
.toFile(fixtures.path('output.flatten-black.jpg'), done);
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(400, info.width);
assert.strictEqual(300, info.height);
fixtures.assertSimilar(fixtures.expected('flatten-black.jpg'), data, done);
});
});
it('Flatten to RGB orange', function(done) {
@ -21,7 +26,12 @@ describe('Alpha transparency', function() {
.flatten()
.background({r: 255, g: 102, b: 0})
.resize(400, 300)
.toFile(fixtures.path('output.flatten-rgb-orange.jpg'), done);
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(400, info.width);
assert.strictEqual(300, info.height);
fixtures.assertSimilar(fixtures.expected('flatten-orange.jpg'), data, done);
});
});
it('Flatten to CSS/hex orange', function(done) {
@ -29,7 +39,12 @@ describe('Alpha transparency', function() {
.flatten()
.background('#ff6600')
.resize(400, 300)
.toFile(fixtures.path('output.flatten-hex-orange.jpg'), done);
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(400, info.width);
assert.strictEqual(300, info.height);
fixtures.assertSimilar(fixtures.expected('flatten-orange.jpg'), data, done);
});
});
it('Do not flatten', function(done) {

View File

@ -13,11 +13,11 @@ describe('Crop gravities', function() {
sharp(fixtures.inputJpg)
.resize(320, 80)
.crop(sharp.gravity.north)
.toFile(fixtures.path('output.gravity-north.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(80, info.height);
done();
fixtures.assertSimilar(fixtures.expected('gravity-north.jpg'), data, done);
});
});
@ -25,11 +25,11 @@ describe('Crop gravities', function() {
sharp(fixtures.inputJpg)
.resize(80, 320)
.crop(sharp.gravity.east)
.toFile(fixtures.path('output.gravity-east.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(80, info.width);
assert.strictEqual(320, info.height);
done();
fixtures.assertSimilar(fixtures.expected('gravity-east.jpg'), data, done);
});
});
@ -37,11 +37,11 @@ describe('Crop gravities', function() {
sharp(fixtures.inputJpg)
.resize(320, 80)
.crop(sharp.gravity.south)
.toFile(fixtures.path('output.gravity-south.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(80, info.height);
done();
fixtures.assertSimilar(fixtures.expected('gravity-south.jpg'), data, done);
});
});
@ -49,11 +49,11 @@ describe('Crop gravities', function() {
sharp(fixtures.inputJpg)
.resize(80, 320)
.crop(sharp.gravity.west)
.toFile(fixtures.path('output.gravity-west.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(80, info.width);
assert.strictEqual(320, info.height);
done();
fixtures.assertSimilar(fixtures.expected('gravity-west.jpg'), data, done);
});
});
@ -61,11 +61,11 @@ describe('Crop gravities', function() {
sharp(fixtures.inputJpg)
.resize(320, 80)
.crop(sharp.gravity.center)
.toFile(fixtures.path('output.gravity-center.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(80, info.height);
done();
fixtures.assertSimilar(fixtures.expected('gravity-center.jpg'), data, done);
});
});
@ -73,23 +73,18 @@ describe('Crop gravities', function() {
sharp(fixtures.inputJpg)
.resize(80, 320)
.crop(sharp.gravity.centre)
.toFile(fixtures.path('output.gravity-centre.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(80, info.width);
assert.strictEqual(320, info.height);
done();
fixtures.assertSimilar(fixtures.expected('gravity-centre.jpg'), data, done);
});
});
it('Invalid', function(done) {
var isValid = true;
try {
it('Invalid', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).crop(5);
} catch (err) {
isValid = false;
}
assert.strictEqual(false, isValid);
done();
});
});
});

View File

@ -12,22 +12,22 @@ describe('Partial image extraction', function() {
it('JPEG', function(done) {
sharp(fixtures.inputJpg)
.extract(2, 2, 20, 20)
.toFile(fixtures.path('output.extract.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(20, info.width);
assert.strictEqual(20, info.height);
done();
fixtures.assertSimilar(fixtures.expected('extract.jpg'), data, done);
});
});
it('PNG', function(done) {
sharp(fixtures.inputPng)
.extract(300, 200, 400, 200)
.toFile(fixtures.path('output.extract.png'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(400, info.width);
assert.strictEqual(200, info.height);
done();
fixtures.assertSimilar(fixtures.expected('extract.png'), data, done);
});
});
@ -35,11 +35,11 @@ describe('Partial image extraction', function() {
it('WebP', function(done) {
sharp(fixtures.inputWebP)
.extract(50, 100, 125, 200)
.toFile(fixtures.path('output.extract.webp'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(125, info.width);
assert.strictEqual(200, info.height);
done();
fixtures.assertSimilar(fixtures.expected('extract.webp'), data, done);
});
});
}
@ -47,11 +47,12 @@ describe('Partial image extraction', function() {
it('TIFF', function(done) {
sharp(fixtures.inputTiff)
.extract(63, 34, 341, 529)
.toFile(fixtures.path('output.extract.tiff'), function(err, info) {
.jpeg()
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(341, info.width);
assert.strictEqual(529, info.height);
done();
fixtures.assertSimilar(fixtures.expected('extract.tiff'), data, done);
});
});
@ -59,11 +60,11 @@ describe('Partial image extraction', function() {
sharp(fixtures.inputJpg)
.extract(10, 10, 10, 500, 500)
.resize(100, 100)
.toFile(fixtures.path('output.extract.resize.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(100, info.width);
assert.strictEqual(100, info.height);
done();
fixtures.assertSimilar(fixtures.expected('extract-resize.jpg'), data, done);
});
});
@ -72,11 +73,11 @@ describe('Partial image extraction', function() {
.resize(500, 500)
.crop(sharp.gravity.north)
.extract(10, 10, 100, 100)
.toFile(fixtures.path('output.resize.crop.extract.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(100, info.width);
assert.strictEqual(100, info.height);
done();
fixtures.assertSimilar(fixtures.expected('resize-crop-extract.jpg'), data, done);
});
});
@ -86,11 +87,11 @@ describe('Partial image extraction', function() {
.resize(500, 500)
.crop(sharp.gravity.north)
.extract(10, 10, 100, 100)
.toFile(fixtures.path('output.extract.resize.crop.extract.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(100, info.width);
assert.strictEqual(100, info.height);
done();
fixtures.assertSimilar(fixtures.expected('extract-resize-crop-extract.jpg'), data, done);
});
});
@ -98,11 +99,11 @@ describe('Partial image extraction', function() {
sharp(fixtures.inputJpg)
.extract(10, 10, 100, 100)
.rotate(90)
.toFile(fixtures.path('output.extract.extract-then-rotate.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(100, info.width);
assert.strictEqual(100, info.height);
done();
fixtures.assertSimilar(fixtures.expected('extract-rotate.jpg'), data, done);
});
});
@ -110,69 +111,44 @@ describe('Partial image extraction', function() {
sharp(fixtures.inputJpg)
.rotate(90)
.extract(10, 10, 100, 100)
.toFile(fixtures.path('output.extract.rotate-then-extract.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(100, info.width);
assert.strictEqual(100, info.height);
done();
fixtures.assertSimilar(fixtures.expected('rotate-extract.jpg'), data, done);
});
});
describe('Invalid parameters', function() {
it('Undefined', function(done) {
var isValid = true;
try {
it('Undefined', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).extract();
} catch (err) {
isValid = false;
}
assert.strictEqual(false, isValid);
done();
});
});
it('String top', function(done) {
var isValid = true;
try {
it('String top', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).extract('spoons', 10, 10, 10);
} catch (err) {
isValid = false;
}
assert.strictEqual(false, isValid);
done();
});
});
it('Non-integral left', function(done) {
var isValid = true;
try {
it('Non-integral left', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).extract(10, 10.2, 10, 10);
} catch (err) {
isValid = false;
}
assert.strictEqual(false, isValid);
done();
});
});
it('Negative width - negative', function(done) {
var isValid = true;
try {
it('Negative width - negative', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).extract(10, 10, -10, 10);
} catch (err) {
isValid = false;
}
assert.strictEqual(false, isValid);
done();
});
});
it('Null height', function(done) {
var isValid = true;
try {
it('Null height', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).extract(10, 10, 10, null);
} catch (err) {
isValid = false;
}
assert.strictEqual(false, isValid);
done();
});
});
});

View File

@ -37,12 +37,12 @@ describe('Rotation', function() {
sharp(fixtures.inputJpgWithExif)
.rotate()
.resize(320)
.toFile(fixtures.path('output.exif.8.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
done();
fixtures.assertSimilar(fixtures.expected('exif-8.jpg'), data, done);
});
});
@ -50,12 +50,12 @@ describe('Rotation', function() {
sharp(fixtures.inputJpgWithExifMirroring)
.rotate()
.resize(320)
.toFile(fixtures.path('output.exif.5.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
done();
fixtures.assertSimilar(fixtures.expected('exif-5.jpg'), data, done);
});
});
@ -85,26 +85,22 @@ describe('Rotation', function() {
});
});
it('Rotate to an invalid angle, should fail', function(done) {
var fail = false;
try {
it('Rotate to an invalid angle, should fail', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).rotate(1);
fail = true;
} catch (e) {}
assert(!fail);
done();
});
});
it('Flip - vertical', function(done) {
sharp(fixtures.inputJpg)
.resize(320)
.flip()
.toFile(fixtures.path('output.flip.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(261, info.height);
done();
fixtures.assertSimilar(fixtures.expected('flip.jpg'), data, done);
});
});
@ -112,12 +108,12 @@ describe('Rotation', function() {
sharp(fixtures.inputJpg)
.resize(320)
.flop()
.toFile(fixtures.path('output.flop.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(261, info.height);
done();
fixtures.assertSimilar(fixtures.expected('flop.jpg'), data, done);
});
});
@ -125,12 +121,12 @@ describe('Rotation', function() {
sharp(fixtures.inputJpg)
.resize(320)
.flop()
.toFile(fixtures.path('output.flip.flop.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(261, info.height);
done();
fixtures.assertSimilar(fixtures.expected('flip-and-flop.jpg'), data, done);
});
});
@ -139,12 +135,12 @@ describe('Rotation', function() {
.resize(320)
.flip(false)
.flop(false)
.toFile(fixtures.path('output.flip.flop.nope.jpg'), function(err, info) {
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(261, info.height);
done();
fixtures.assertSimilar(fixtures.inputJpg, data, done);
});
});