forked from cncf/svg-autocrop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
80 lines (75 loc) · 3.39 KB
/
test.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
const _ = require('lodash');
const compareBoxes = require('./compareBoxes');
const autoCropSvg = require('./index');
const captureMode = !!process.env['CAPTURE'];
const match = process.env['MATCH'];
async function main() {
const files = require('fs').readdirSync('fixtures');
const inputFiles = files.filter( (x) => x.indexOf('input') !== -1).filter( match ? ((x) => x.indexOf(match) !== -1) : (x) => true) ;
console.info(inputFiles);
for (var file of inputFiles) {
const inputFile = `./fixtures/${file}`;
const outputFile = `./fixtures/${file.replace('input','output')}`;
const errorFile = `./fixtures/${file.replace('input','error').replace('.svg', '.txt')}`;
const inputContent = require('fs').readFileSync(inputFile, 'utf-8');
let convertedSvg;
let errorMessage;
console.info(`Processing: ${inputFile}`);
try {
convertedSvg = await autoCropSvg(inputContent, {title: `${file} logo`});
} catch (ex) {
console.info(ex);
const message = ex.message || ex;
errorMessage = message;
}
if (captureMode) {
if (convertedSvg) {
require('fs').writeFileSync(outputFile, convertedSvg);
} else {
require('fs').writeFileSync(errorFile, errorMessage);
}
} else {
if (convertedSvg) {
const outputContent = require('fs').readFileSync(outputFile, 'utf-8');
const separateContentAndViewbox = function(svg) {
const viewBox = svg.match(/viewBox="(.*?)"/)[1];
const otherContent = svg.replace(/viewBox=".*?"/, "");
return {
x: +viewBox.split(' ')[0],
y: +viewBox.split(' ')[1],
width: +viewBox.split(' ')[2],
height: +viewBox.split(' ')[3],
remaining: otherContent
}
}
const realParts = separateContentAndViewbox(convertedSvg);
const expectedParts = separateContentAndViewbox(outputContent);
const matchingLevel = compareBoxes(realParts, expectedParts);
console.info(matchingLevel);
if (realParts.remaining !== expectedParts.remaining || matchingLevel < 0.995) {
const beautify = require('xml-beautifier');
console.info(`Fixture do not match: ${inputFile}, ${outputFile}`);
const pd = require('prettydiff');
const options = pd.defaults;
options.source = beautify(realParts.remaining);
options.diff = beautify(expectedParts.remaining);
console.info(pd.mode(options));
process.exit(1);
} else {
console.info('Match');
}
} else {
const expectedMessage = require('fs').readFileSync(errorFile, 'utf-8');
console.info('Match');
if (expectedMessage !== errorMessage) {
console.info(`Fixture do not match: on invalid ${inputFile}, error is ${errorMessage}, but we need ${expectedMessage}`);
process.exit(1);
}
}
}
}
}
main().catch(function(x) {
console.info(x);
process.exit(1)
});