-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.js
More file actions
277 lines (222 loc) · 8.7 KB
/
tests.js
File metadata and controls
277 lines (222 loc) · 8.7 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*****************
tests
*****************/
(function(){
function createTestContainer(opts){
var container = document.createElement('div');
container.setAttribute('class', 'test');
//if a title was provided...
if (opts.title) {
appendElement('h3', opts.title, container);
}
//if a description was provided...
if (opts.desc) {
appendElement('p', opts.desc, container)
}
return container;
}
function appendElement(elType, text, parent, isError) {
var el = document.createElement(elType);
el.appendChild(document.createTextNode(text));
if (isError) {
el.setAttribute('class', 'error')
}
parent.appendChild(el);
}
function testPathCreation(opts) {
var pathStr = 'Path: ',
container = createTestContainer(opts),
path;
//construct the path string - depends on if it was an array or a string to begin with
if (opts.path instanceof Array) {
pathStr += ('[ ' + opts.path.map(function(item){
return '[' + item.toString() + ']'
}).join(', ') + ' ]');
}
else {
pathStr += ('"' + opts.path + '"');
}
//show the path provided
appendElement('p', pathStr, container)
try {
//initialize the path object
path = new Path(opts.path);
//show the computed svg path string
appendElement('p', 'Computed SVG path string: "' + path.toString() + '"', container);
//render the path and append it to the document
container.appendChild(path.render());
}
catch (e) {
appendElement('p', 'Error: ' + e, container, true)
}
document.body.appendChild(container);
}
function testInstanceOf(){
var container = createTestContainer({title: "instanceof test"})
var path = new Path([[50,50],[150,150]]);
if (path instanceof Path) {
appendElement('h5', "path instanceof Path = true", container)
}
if (path instanceof Array) {
appendElement('h5', "path instanceof Array = true", container)
}
container.appendChild(path.render());
document.body.appendChild(container);
}
function testArrayMethods(opts) {
var container = createTestContainer(opts),
testPath,
modifiedPath;
//create and render original path
testPath = new Path(opts.path)
appendElement('p', 'Original: ' + opts.path.toString(), container);
container.appendChild(testPath.render());
//run the provided function against it, then render that
modifiedPath = testPath[opts.method].apply(testPath, opts.paramsArray);
appendElement('p', "Modified with '" + opts.method + "': " + modifiedPath.toString(), container);
appendElement('p', "modifiedPath instanceof Path = " + (modifiedPath instanceof Path), container);
container.appendChild(modifiedPath.render());
if (opts.callback) {
opts.callback(modifiedPath);
appendElement('p', opts.callbackMsg, container);
container.appendChild(modifiedPath.render());
}
document.body.appendChild(container);
}
//test methods map, filter, and slice
testArrayMethods({
title: "Path.map test",
desc: "Map the given path to a new path with the provided function - should add 50 to all x coordinates",
path: "M 50 50 L 150 50 L 100 150 z",
method: 'map',
paramsArray: [function(pointObj, index, array){
if (pointObj.x) {
pointObj.x = parseInt(pointObj.x) + 50;
}
return pointObj;
}]
});
testArrayMethods({
title: "Path.filter test",
desc: "Should remove any points that have a y value greater than 100, and draw a straight horizontal line",
path: "M 50 50 L 150 50 L 100 150 z",
method: 'filter',
paramsArray: [function(pointObj, index, array){
return pointObj.y <= 100
}]
});
testArrayMethods({
title: "Path.slice test",
desc: "Should give you a slice with elements 1 and 2 in the array, which is an invalid path as it contains no 'M'",
path: "M 50 50 L 150 50 L 100 150 z",
method: 'slice',
paramsArray: [1,3],
callbackMsg: 'Since the above path is invalid, push a new moveto command (25,25) into the front to make it valid',
callback: function(path) {
path.unshift({cmd:'m', x:25, y:25})
}
});
testArrayMethods({
title: "Path.slice test with negative indices",
desc: "This is the same as the above test, except we're slicing with [-3,-1] instead of [1,2].",
path: "M 50 50 L 150 50 L 100 150 z",
method: 'slice',
paramsArray: [-3,-1],
callbackMsg: 'Since the above path is invalid, push a new moveto command (25,25) into the front to make it valid',
callback: function(path) {
path.unshift({cmd:'m', x:25, y:25})
}
});
//test that a path object is both an instance of Path and Array
testInstanceOf();
//test paths provided as an array of points
testPathCreation({
title: 'Diamond with array param',
desc:'Should create a diamond in the top left',
path: [ [0,50],[50,0],[100,50],[50,100],[0,50] ]
});
testPathCreation({
title: 'Triangle with array param',
desc: 'Should create a downward-facing triangle in the middle',
path: [ [50,50],[150,50],[100,150],[50,50] ]
});
//standard lineto commands
testPathCreation({
title: 'Triangle with string param',
desc:'Triangle should be same as the one above, except top left corner is closed via a closepath command',
path: "M 50 50 L 150 50 L 100 150 z"
});
testPathCreation({
title: 'Lineto command, relative points',
desc:'Triangle should be same as the one above.',
path: "M 50 50 l 100 0 l -50 100 z"
});
testPathCreation({
title: 'Lineto command with multiple points',
desc:'This should render a triangle identical to the one above',
path: "M 50 50 L 150 50 100 150 z"
});
testPathCreation({
title: 'Unclosed triangle with string param',
desc:'Should render a triangle with the bottom left edge missing',
path: "M 50 50 L 150 50 L 100 150"
});
//INVALID PATHS
testPathCreation({
title: 'Invalid Path - moveto in the middle',
desc:'Moveto shouldnt appear in the middle of a path string',
path: "M 50 50 m 150 v 100 h -100 z"
});
testPathCreation({
title: 'Invalid Path - closepath in the middle',
desc:'closepath shouldnt appear in the middle of a path string',
path: "M 50 50 z 150 v 100 h -100"
});
testPathCreation({
title: 'Invalid Path - unknown command',
desc:'Cant have unknown commands in the path string',
path: "M 50 50 Q 150 v 100 h -100"
});
testPathCreation({
title: 'Invalid Path - too many spaces',
desc:'cant have more than one space between items in a path string',
path: "M 50 50 150 v 100 h -100"
});
testPathCreation({
title: 'Invalid Path - no moveto',
desc:'Path must contain a moveto at the beginning of the string',
path: "50 50 150 v 100 h -100"
});
testPathCreation({
title: 'Invalid Path - provided as an object',
desc:'Params must be either an svg string or an array of points',
path: {}
});
//horizontal lineto commands
testPathCreation({
title: 'Horizontal lineto, single point',
desc:'This should render a triangle identical to the one above',
path: "M 50 50 H 150 L 100 150 z"
});
testPathCreation({
title: 'Horizontal lineto, multiple points',
desc:'This should render a triangle identical to the one above',
path: "M 50 50 H 100 125 150 L 100 150 z"
});
//vertical lineto commands
testPathCreation({
title: 'Vertical lineto, single point',
desc:'This should render a square in the middle',
path: "M 50 50 H 150 V 150 h -100 z"
});
testPathCreation({
title: 'Vertical lineto, multiple point',
desc:'This should render a square identical to the one above',
path: "M 50 50 H 150 V 75 125 150 h -100 z"
});
testPathCreation({
title: 'Vertical lineto, relative single point',
desc:'This should render a square identical to the one above',
path: "M 50 50 H 150 v 100 h -100 z"
});
})();