-
Notifications
You must be signed in to change notification settings - Fork 214
/
maki.test.js
153 lines (134 loc) · 3.86 KB
/
maki.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const fs = require('fs');
const test = require('tape');
const path = require('path');
const xml2js = require('xml2js');
const maki = require('../');
const makiLayoutAll = require('../layouts/all');
const parseString = xml2js.parseString;
const svgPath = path.join(__dirname, '../icons/');
test('index', function(t) {
t.deepEqual(makiLayoutAll, maki.layouts, 'exports layout');
t.end();
});
test('all.json layout ', function(t) {
fs.readdir(svgPath, function(err, files) {
var svgFiles = files.filter(function(file) {
return (
file
.split('.')
.pop()
.indexOf('svg') !== -1
);
});
var filtered = svgFiles
.filter(function(file) {
return file.indexOf('.svg') > -1;
})
.map(function(file) {
return file.split('.svg')[0];
});
t.deepEqual(filtered, makiLayoutAll, 'includes all icons');
t.end();
});
});
test('valid svgs ', function(t) {
fs.readdir(svgPath, function(err, files) {
var svgFiles = files.filter(function(file) {
return (
file
.split('.')
.pop()
.indexOf('svg') !== -1
);
});
makiLayoutAll.forEach(function(name) {
t.ok(svgFiles.indexOf(`${name}.svg`) >= 0, `${name}.svg exists`);
});
svgFiles.forEach(function(fileName, j) {
fs.readFile('./icons/' + fileName, 'utf8', function(err, file) {
if (err) t.fail(err);
parseString(file, function(err, parsed) {
if (err) t.fail(err);
validateSvg(parsed.svg, fileName);
});
});
if (j + 1 === svgFiles.length) {
t.end();
}
});
});
function validateSvg(svg, fileName) {
var errors = [],
pixelUnitRegex = /^-?([0-9])+( ?px)?$/,
height = parseFloat(svg.$.height),
width = parseFloat(svg.$.width);
function invalidElement(o) {
var keys = Object.keys(o),
invalid = false;
keys.some(function(key) {
invalid =
key.match(
/^(rectangle|circle|ellipse|line|polyline|polygon|style)$/
) && key;
return invalid;
});
return invalid;
}
function checkPathLength(pathArray) {
if (pathArray.length > 1) errors.push('too many paths');
}
function checkPaths(pathArray) {
checkPathLength(pathArray);
pathArray.forEach(function(path) {
if (path.$ && path.$.transform) errors.push('transformed paths');
});
}
function traverseGroups(groupArray) {
groupArray.forEach(function(group) {
if (group.$ && group.$.transform) errors.push('transformed groups');
if (invalidElement(group)) errors.push(' has ' + invalidElement(group));
if (group.path) {
checkPaths(group.path);
}
if (group.g) {
traverseGroups(group.g);
}
});
}
if (
!(height === 15) ||
!(width === 15) ||
height !== width
)
errors.push('invalid size');
if (
parseFloat(svg.$.viewBox.split(' ')[2]) !== width ||
parseFloat(svg.$.viewBox.split(' ')[3]) !== height
)
errors.push('invalid viewBox');
if (svg.$.width && !svg.$.width.toString().match(pixelUnitRegex)) {
errors.push('Width must use pixel units');
}
if (svg.$.height && !svg.$.height.toString().match(pixelUnitRegex)) {
errors.push('Height must use pixel units');
}
if (
svg.$.viewBox &&
svg.$.viewBox.split(' ').some(v => !v.toString().match(pixelUnitRegex))
) {
errors.push('Viewbox must use pixel units');
}
if (invalidElement(svg)) errors.push('has ' + invalidElement(svg));
if (svg.g) traverseGroups(svg.g);
if (svg.path) checkPaths(svg.path);
t.notOk(
errors.length,
fileName +
' has ' +
errors.length +
' errors' +
(errors.length ? ':' : '') +
errors.join(', ')
);
}
});