-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathcustom_matchers.js
242 lines (227 loc) · 8.06 KB
/
custom_matchers.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
function parseTranslate (actual) {
var parts = /translate\((-?[\d\.]*)(?:[, ](.*))?\)/.exec(actual);
if (!parts) {
return null;
}
if (parts[2] === undefined) {
parts[2] = 0;
}
expect(parts.length).toEqual(3);
return parts;
}
function parseTranslateRotate (actual) {
var parts = /translate\((-?[\d\.]*)(?:[, ](.*))?\)[, ]rotate\((-?[\d\.]*)\)/.exec(actual);
if (!parts) {
return null;
}
if (parts[2] === undefined) {
parts[2] = 0;
}
expect(parts.length).toEqual(4);
return parts;
}
function parsePath (path) {
// an svg path is a string of any number of letters
// each followed by zero or more numbers separated by spaces or commas
var instrexp = /([a-z])[^a-z]*/gi,
argexp = /(-?\d+(?:\.\d*)?)[, ]*/gi;
var match, result = [], die = 99;
while ((match = instrexp.exec(path))) {
var instr = match[0];
var cmd = {op: match[1], args: []};
argexp.lastIndex = 0;
while ((match = argexp.exec(instr))) {
cmd.args.push(match[1]);
}
result.push(cmd);
if (!--die) {
throw 'give up';
}
}
return result;
}
// there doesn't seem to be any way to access jasmine custom matchers
function compareWithinDelta (actual, expected, delta) {
if (delta === undefined) {
delta = 1e-6;
}
var result = {};
result.pass = actual >= (+expected - delta) && actual <= (+expected + delta);
var pre = 'Expected ' + actual + ' to ',
post = 'be within [' + (+expected - delta) + '/' + (+expected + delta) + ']';
if (result.pass) {
result.message = pre + 'not ' + post;
} else {
result.message = pre + post;
}
return result;
}
// note: to make these reusable as boolean predicates, they only returns the first
// failure instead of using expect
function compareSubPath (got, wanted, i, j, delta) {
for (var k = 0; k !== wanted.length; ++k) {
var commandNum = 'path command #' + i + k;
if (got[i + k].op.toUpperCase() !== wanted[j + k].op.toUpperCase()) {
return {
pass: false,
message: commandNum + ' actual \'' + got[i + k].op.toUpperCase() +
'\' != expected \'' + wanted[j + k].op.toUpperCase() + '\''
};
}
if (got[i + k].args.length !== wanted[j + k].args.length) {
return {
pass: false,
message: commandNum + ' number of arguments ' +
got[i + k].args.length + ' != expected ' + wanted[j + k].args.length
};
}
for (var h = 0; h < got[i + k].args.length; ++h) {
var result = compareWithinDelta(got[i + k].args[h], wanted[j + k].args[h], delta);
if (!result.pass) {
result.message = commandNum + ', element ' + h + ': ' + result.message;
return result;
}
}
}
return {pass: true};
}
function comparePaths (actual, expected, delta) {
delta = delta || 1; // default delta of 1px
var got = parsePath(actual),
wanted = parsePath(expected);
if (got.length !== wanted.length) {
return {
pass: false,
message: 'actual number of path cmds ' + actual.length +
' did not match expected number ' + expected.length
};
}
return compareSubPath(got, wanted, 0, 0, delta);
}
function findSubPath (actual, expected, delta) {
delta = delta || 1; // default delta of 1px
var got = parsePath(actual),
wanted = parsePath(expected),
end = got.length - wanted.length;
for (var i = 0; i < end; ++i) {
var result = compareSubPath(got, wanted, i, 0, delta);
if (result.pass) {
return result;
}
}
return {
pass: false,
message: 'did not find expected subpath \'' + expected + '\' in actual path \'' + actual + '\''
};
}
// actually number list, but presumably you'd want closeness if you need that
function compareIntList (actual, expected) {
var aparts = actual.split(/, */),
eparts = expected.split(/, */);
if (aparts.length !== eparts.length) {
return {
pass: false,
message: 'actual number of list items ' + aparts.length +
' did not match expected number ' + eparts.length
};
}
for (var i = 0; i < eparts.length; ++i) {
if (+aparts[i] !== +eparts[i]) {
return {
pass: false,
message: 'list item[' + i + '] value ' + aparts[i] + ' did not equal expected value ' + eparts[i]
};
}
}
return {pass: true};
}
beforeEach(function () {
jasmine.addMatchers({
toBeWithinDelta: function (_) {
return {
compare: compareWithinDelta
};
},
// note: all of these custom matchers ignore the possibility of .not.toMatch
// (can't imagine how that would be useful here)
toMatchTranslate: function () {
return {
compare: function (actual, x, y, prec) {
var parts = parseTranslate(actual);
if (!parts) {
return {pass: false, message: '\'' + actual + '\' did not match translate(x[,y]) regexp'};
}
expect(+parts[1]).toBeCloseTo(x, prec);
expect(+parts[2]).toBeCloseTo(y, prec);
return {pass: true};
}
};
},
toMatchTransRot: function () {
return {
compare: function (actual, x, y, r, prec) {
var parts = parseTranslateRotate(actual);
if (!parts) {
return {pass: false, message: '\'' + actual + '\' did not match translate(x[,y]),rotate(r) regexp'};
}
expect(+parts[1]).toBeCloseTo(x, prec);
expect(+parts[2]).toBeCloseTo(y, prec);
expect(+parts[3]).toBeCloseTo(r, prec);
return {pass: true};
}
};
},
toMatchUrl: function () {
return {
compare: function (actual, url) {
/*
URL can be like:
url(http://localhost:8888/spec/?random=true#composite-chart-clip)
url("http://localhost:8888/spec/?random=true#composite-chart-clip")
http://localhost:8888/spec/?random=true#composite-chart-clip
http://localhost:8888/spec/##composite-chart-clip
*/
var cleanURL = function (u) {
var matches = u.match(/url\((.*)\)/);
if (matches) {
u = matches[1].replace(/"/g, '');
}
return u.replace(/\#+/, '#');
};
expect(cleanURL(actual)).toEqual(cleanURL(url));
return {pass: true};
}
};
},
toMatchPath: function () {
return {
compare: comparePaths
};
},
toContainPath: function () {
return {
compare: findSubPath
};
},
toEqualIntList: function () {
return {
compare: compareIntList
};
},
toMatchColors: function () {
return {
compare: function (actual, expected) {
// Colors can be rgb(0, 0, 0), #000000 or black
var normalizeColor = function (c) {
// will convert to rgb(0, 0, 0)
return d3.color(c).toString();
};
actual = actual.map(normalizeColor);
expected = expected.map(normalizeColor);
expect(actual).toEqual(expected);
return {pass: true};
}
};
}
});
});