-
Notifications
You must be signed in to change notification settings - Fork 1
/
unexpectedResemble.js
103 lines (94 loc) · 4.03 KB
/
unexpectedResemble.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
var _ = require('underscore'),
isBrowserResembleJs = false,
resemble;
try {
resemble = require('@alexjeffburke/resemble');
} catch (e) {
isBrowserResembleJs = true;
resemble = require('resemblejs');
}
// The node.js fork of the libary, require('resemble'), does not support data: urls:
function convertToBufferIfNecessary(obj) {
if (!isBrowserResembleJs && typeof obj === 'string') {
var matchDataUrl = obj.match(/^data:[^;]*;base64,(.*)$/);
if (matchDataUrl) {
return new Buffer(matchDataUrl[1], 'base64');
}
}
return obj;
}
module.exports = {
name: 'unexpected-resemble',
installInto: function (expect) {
expect.installPlugin(require('magicpen-media'));
expect.addAssertion('<string|binaryArray> to resemble <any+>', function (expect, subject, otherImage, value) {
subject = convertToBufferIfNecessary(subject);
otherImage = convertToBufferIfNecessary(otherImage);
this.subjectOutput = function () {
this.image(subject, { width: 10, height: 5, link: true });
};
this.argsOutput[0] = function () {
this.image(otherImage, { width: 10, height: 5, link: true });
};
return expect.promise(function (resolve, reject) {
if (typeof value === 'number') {
value = {
isSameDimensions: true,
mismatchPercentage: expect.it('to be less than', value)
};
}
var resemblance = resemble.resemble(subject).compareTo(otherImage);
if (value) {
value = _.extend({}, value);
if (typeof value.misMatchPercentage !== 'undefined' && typeof value.mismatchPercentage === 'undefined') {
value.mismatchPercentage = value.misMatchPercentage;
delete value.misMatchPercentage;
}
} else {
value = {
isSameDimensions: true,
mismatchPercentage: expect.it('to be less than', 1)
};
}
['ignoreColors', 'ignoreAntialiasing'].forEach(function (optionName) {
if (value[optionName]) {
resemblance[optionName]();
delete value[optionName];
}
});
resemblance.onComplete(resolve);
}).then(function (data) {
data.mismatchPercentage = data.misMatchPercentage;
delete data.misMatchPercentage;
if (typeof data.mismatchPercentage === 'string') {
data.mismatchPercentage = parseFloat(data.mismatchPercentage);
}
// remove properties returned from newer the resemble/canvas versions
delete data.rawMisMatchPercentage;
delete data.diffBounds;
var getImageDataUrl = data.getImageDataUrl;
// These are noisy in the 'to satisfy' diff output
delete data.analysisTime;
delete data.getBuffer;
delete data.getImageDataUrl;
delete data.pngStream;
return expect.withError(function () {
return expect(data, 'to satisfy', value);
}, function (err) {
expect.fail({
diff: function (output, diff, inspect, equal) {
output
.append(err.getDiff(output.format).diff)
.nl(2)
.image(getImageDataUrl.call(data), { fallbackToDisc: true });
return {
inline: false,
diff: output
};
}
});
});
});
});
}
};