mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 10:30:15 +02:00
Tests: assertSimilar support for Promise and callback
This commit is contained in:
parent
2672de2480
commit
f128ebdbd4
49
test/fixtures/index.js
vendored
49
test/fixtures/index.js
vendored
@ -14,17 +14,15 @@ const getPath = function (filename) {
|
|||||||
|
|
||||||
// Generates a 64-bit-as-binary-string image fingerprint
|
// 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
|
// Based on the dHash gradient method - see http://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html
|
||||||
const fingerprint = function (image, callback) {
|
async function fingerprint (image) {
|
||||||
sharp(image)
|
return sharp(image)
|
||||||
.flatten('gray')
|
.flatten('gray')
|
||||||
.greyscale()
|
.greyscale()
|
||||||
.normalise()
|
.normalise()
|
||||||
.resize(9, 8, { fit: sharp.fit.fill })
|
.resize(9, 8, { fit: sharp.fit.fill })
|
||||||
.raw()
|
.raw()
|
||||||
.toBuffer(function (err, data) {
|
.toBuffer()
|
||||||
if (err) {
|
.then(function (data) {
|
||||||
callback(err);
|
|
||||||
} else {
|
|
||||||
let fingerprint = '';
|
let fingerprint = '';
|
||||||
for (let col = 0; col < 8; col++) {
|
for (let col = 0; col < 8; col++) {
|
||||||
for (let row = 0; row < 8; row++) {
|
for (let row = 0; row < 8; row++) {
|
||||||
@ -33,10 +31,9 @@ const fingerprint = function (image, callback) {
|
|||||||
fingerprint = fingerprint + (left < right ? '1' : '0');
|
fingerprint = fingerprint + (left < right ? '1' : '0');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
callback(null, fingerprint);
|
return fingerprint;
|
||||||
}
|
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
@ -151,46 +148,44 @@ module.exports = {
|
|||||||
// Verify similarity of expected vs actual images via fingerprint
|
// Verify similarity of expected vs actual images via fingerprint
|
||||||
// Specify distance threshold using `options={threshold: 42}`, default
|
// Specify distance threshold using `options={threshold: 42}`, default
|
||||||
// `threshold` is 5;
|
// `threshold` is 5;
|
||||||
assertSimilar: function (expectedImage, actualImage, options, callback) {
|
assertSimilar: async function (expectedImage, actualImage, options, callback) {
|
||||||
if (typeof options === 'function') {
|
if (typeof options === 'function') {
|
||||||
callback = options;
|
callback = options;
|
||||||
options = {};
|
options = {};
|
||||||
}
|
}
|
||||||
|
if (typeof options === 'undefined' || options === null) {
|
||||||
if (typeof options === 'undefined' && options === null) {
|
|
||||||
options = {};
|
options = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.threshold === null || typeof options.threshold === 'undefined') {
|
if (options.threshold === null || typeof options.threshold === 'undefined') {
|
||||||
options.threshold = 5; // ~7% threshold
|
options.threshold = 5; // ~7% threshold
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof options.threshold !== 'number') {
|
if (typeof options.threshold !== 'number') {
|
||||||
throw new TypeError('`options.threshold` must be a number');
|
throw new TypeError('`options.threshold` must be a number');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof callback !== 'function') {
|
try {
|
||||||
throw new TypeError('`callback` must be a function');
|
const [expectedFingerprint, actualFingerprint] = await Promise.all([
|
||||||
}
|
fingerprint(expectedImage),
|
||||||
|
fingerprint(actualImage)
|
||||||
fingerprint(expectedImage, function (err, expectedFingerprint) {
|
]);
|
||||||
if (err) return callback(err);
|
|
||||||
fingerprint(actualImage, function (err, actualFingerprint) {
|
|
||||||
if (err) return callback(err);
|
|
||||||
let distance = 0;
|
let distance = 0;
|
||||||
for (let i = 0; i < 64; i++) {
|
for (let i = 0; i < 64; i++) {
|
||||||
if (expectedFingerprint[i] !== actualFingerprint[i]) {
|
if (expectedFingerprint[i] !== actualFingerprint[i]) {
|
||||||
distance++;
|
distance++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (distance > options.threshold) {
|
if (distance > options.threshold) {
|
||||||
return callback(new Error('Expected maximum similarity distance: ' + options.threshold + '. Actual: ' + distance + '.'));
|
throw new Error(`Expected maximum similarity distance: ${options.threshold}. Actual: ${distance}.`);
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
|
if (callback) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
if (callback) {
|
||||||
callback();
|
callback();
|
||||||
});
|
}
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
assertMaxColourDistance: function (actualImagePath, expectedImagePath, acceptedDistance) {
|
assertMaxColourDistance: function (actualImagePath, expectedImagePath, acceptedDistance) {
|
||||||
|
@ -122,10 +122,7 @@ describe('Blur', function () {
|
|||||||
.toBuffer();
|
.toBuffer();
|
||||||
|
|
||||||
assert.notDeepEqual(approximate, integer);
|
assert.notDeepEqual(approximate, integer);
|
||||||
|
await fixtures.assertSimilar(fixtures.expected('blur-10.jpg'), approximate);
|
||||||
await new Promise(resolve => {
|
|
||||||
fixtures.assertSimilar(fixtures.expected('blur-10.jpg'), approximate, resolve);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('options.sigma is required if options object is passed', function () {
|
it('options.sigma is required if options object is passed', function () {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user