-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
311 lines (264 loc) · 10.4 KB
/
test.html
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<!DOCTYPE html>
<html>
<head>
<title>Test Path.js</title>
<!-- jQuery needed to run Qunit only -->
<script src='jquery-1.7.2.min.js'></script>
<script src='qunit.js'></script>
<link rel='stylesheet' href='qunit.css' />
<script src='path.js'></script>
<script>
$(document).ready(function() {
module('Initialize');
test('A new empty path should:',function() {
var p = new Path();
ok(p instanceof Path, 'be a Path');
ok(p instanceof Array, 'be an Array');
equal(p.length, 0, 'have a length of 0');
});
test('Path supports the methods:', function() {
var p = new Path(),
supportedMethods = ['clone', 'map', 'filter', 'slice', 'toString'],
i,method;
for (i in supportedMethods) {
method = supportedMethods[i];
ok(method in p && typeof p[method] === 'function', method);
}
});
test('When initialized with an empty array:', function() {
var path = new Path([]);
equal(path.length, 0, 'the length should be 0');
});
test('When initialized with an array of one point:', function() {
var p1 = [0,0],
path = new Path([p1]);
equal(path[0], p1, 'the first element should be the first point');
equal(path.length, 1, 'the length should be 1');
});
test('When initialized with an array of two points:', function() {
var p1 = [0,0],
p2 = [1,1],
path = new Path([p1, p2]);
equal(path[0], p1, 'the first element should be the first point');
equal(path[1], p2, 'the second element should be the second point');
equal(path.length, 2, 'the length should be 2');
});
module('SVG String');
test('When initialized with an empty string:', function() {
var str = '',
path = new Path(str);
equal(path.length, 0, 'the length should be 0');
});
test('When initialized with a moveto command:', function() {
var str = 'M 0 0',
path = new Path(str);
equal(path.length, 1, 'the length should be 1');
deepEqual(path[0], [0,0], 'the first point should be the moved to point');
});
test('When initialized with a moveto command (1,2):', function() {
var str = 'M 1 2',
path = new Path(str);
equal(path.length, 1, 'the length should be 1');
deepEqual(path[0], [1,2], 'the first point should be the moved to point');
});
test('When initialized without a command:', function() {
var str = '0 0';
raises(function() {
var path = new Path(str);
}, 'throws an exception');
});
test('When initialized with partial point:', function() {
var str = 'M 0';
raises(function() {
var path = new Path(str);
}, 'throws an exception');
});
test('When initialized with float point arguments:', function() {
var str = 'M 0 0.5',
path = new Path(str);
deepEqual(path[0], [0,0], 'truncates the arguments');
});
test('When initialized with a non-numeric point argument:', function() {
var str = 'M a b';
raises(function() {
var path = new Path(str);
}, 'throws an exception');
});
test('When initialized with a lineto command:', function() {
var str = 'L 0 0';
raises(function() {
var path = new Path(str);
}, 'throws an exception');
});
test('When initialized with M followed by L', function() {
var str = 'M 0 0 L 100 100',
path = new Path(str);
equal(path.length, 2, 'length should be 2');
deepEqual(path[0], [0,0], 'first point should be the initial point');
deepEqual(path[1], [100,100], 'second point should be the second point');
});
test('When initialized with M followed by M', function() {
var str = 'M 0 0 M 100 100';
raises(function() {
var path = new Path(str);
}, 'raises an exception');
});
test('Extra junk on the end', function() {
var str = 'M 100 100 l -50 50 25';
raises(function() {
var path = new Path(str);
}, 'raises an exception');
});
test('When initialized with a z-terminated string', function() {
var str = 'M 0 0 z',
path = new Path(str);
ok(path.length === 1, 'should have the same length as without');
deepEqual(path[0], [0,0], 'should have the last point preceding the z');
});
test('Relative integer coordinates', function() {
var str = 'M 100 100 l -50 25',
path = new Path(str);
ok(path.length === 2, 'should create an additional point the same as absolute coordinates');
deepEqual(path[1], [50,125], 'should be relative to the prior point');
});
test('Two relative integer coordinates in a row', function() {
var str = 'M 100 100 l -50 25 l 10 -30',
path = new Path(str);
ok(path.length === 3, 'should create an additional point the same as absolute coordinates');
deepEqual(path[2], [60,95], 'should be relative to the prior point');
});
test('Relative followed by absolute', function() {
var str = 'M 100 100 l -50 25 L 10 -30',
path = new Path(str);
deepEqual(path[2], [10,-30], 'should use absolute coordinates');
});
module('push');
test('on an empty Path should', function() {
var path = new Path(),
newPt = [0,10];
path.push(newPt);
equal(path.length, 1, 'increment the length');
deepEqual(path[path.length - 1], newPt, 'append the new point to the path');
});
test('on a Path with points should', function() {
var path = new Path([[0,10],[5,6]]),
newPt = [50,60];
path.push(newPt);
equal(path.length, 3, 'increment the length');
deepEqual(path[path.length - 1], newPt, 'append the new point to the path');
deepEqual(path[0], [0,10], 'not modify the existing points');
deepEqual(path[1], [5,6], 'not modify the existing points');
});
module('toString');
test('on an empty Path', function() {
var path = new Path();
equal(path.toString(), '', 'returns the empty string');
});
test('on a path with one point', function() {
var path = new Path([[0,0]]),
p2 = new Path([[100,200]]);
equal(path.toString(), 'M 0 0', 'returns a moveto that point');
equal(p2.toString(), 'M 100 200', 'returns a moveto that point');
});
test('on a path with two points', function() {
var path = new Path([[0,0], [100,50]]),
p2 = new Path([[100,200], [-50, 25]]);
equal(path.toString(), 'M 0 0 L 100 50', 'returns a moveto that point followed by a lineto the second point');
equal(p2.toString(), 'M 100 200 L -50 25', 'returns a moveto that point followed by a lineto the second point');
});
test('on a closed path', function() {
var path = new Path('M 0 0 L 50 50 L 25 75 z');
equal(path.toString(), 'M 0 0 L 50 50 L 25 75 z', 'should be closed in the result as well');
});
module('map');
test('on an empty path', function() {
var path = new Path(),
f = function(pt) { return pt; };
ok(path.map(f) instanceof Path, 'should return a Path');
});
test('on a Path with elements', function() {
var path = new Path([[0,0]]),
f = function(pt) { return [pt[0] + 50, pt[1] + 25]; },
m = path.map(f);
ok(m instanceof Path, 'should return a path');
equal(m.length, path.length, 'should return a path of the samne length');
deepEqual(m[0], [50,25], 'should apply the function to each element of the path');
});
module('filter');
test('on an empty path', function() {
var path = new Path(),
f = function(pt) { return true; };
ok(path.filter(f) instanceof Path, 'should return a Path');
});
test('on a path with elements', function() {
var path = new Path([[0,0], [100,200]]),
f = function(pt) { return pt[0] === 0; },
filtered = path.filter(f);
ok(filtered instanceof Path, 'should return a Path');
equal(filtered.length, 1, 'should return a path of the correct length');
deepEqual(filtered[0], [0,0], 'should return a path of pts which match the predicate');
});
module('slice');
test('on an empty Path', function() {
var path = new Path();
ok(path.slice(0) instanceof Path, 'should return a Path');
});
test('on a single point', function() {
var path = new Path([[0,0]]),
pathSlice = path.slice(0);
ok(pathSlice instanceof Path, 'should return a Path');
equal(pathSlice.length, 1, 'should have a length of 1');
deepEqual(pathSlice[0], [0,0], 'should return a path with same point');
});
test('on a path with 2 points', function() {
var path = new Path([[0,0], [100,150]]),
pathSlice = path.slice(0,1);
ok(pathSlice instanceof Path, 'should return a Path');
equal(pathSlice.length, 1, 'should have a length of 1');
deepEqual(pathSlice[0], [0,0], 'should return a path with same point');
});
test('on a path with 2 points and a slice size of 2', function() {
var path = new Path([[0,0], [100,150]]),
pathSlice2 = path.slice(0,2);
ok(pathSlice2 instanceof Path, 'should return a Path');
equal(pathSlice2.length, 2, 'should have a length of 2');
deepEqual(pathSlice2[1], [100,150], 'should return a path with same point');
});
module('clone');
test('on an empty Path', function() {
var path = new Path();
deepEqual(path.clone(), path, 'should return an identical Path');
});
test('on a path with elements', function() {
var path = new Path([[0,0],[100,200]]);
deepEqual(path.clone(), path, 'should return an identical Path');
});
test('modifying cloned object should not modify the initial object', function() {
var path = new Path([[0,0]]),
c = path.clone();
c.push([1,1]);
equal(path.length, 1, 'length should be the same');
equal(c.length, 2, 'length should be modified on the clone');
});
test('cloned path with closepath should also have closepath', function() {
var path = new Path('M 0 0 L 1 1 L 2 2 z'),
c = path.clone();
equal(path.toString(), c.toString(), 'should have the same string representation');
});
test('cloned path w/o closepath should not have closepath', function() {
var path = new Path('M 0 0 L 1 1 L 2 2'),
c = path.clone();
equal(path.toString(), c.toString(), 'should have the same string representation');
});
});
</script>
</head>
<body>
<h1 id='qunit-header'>QUnit example</h1>
<h2 id='qunit-banner'></h2>
<div id='qunit-testrunner-toolbar'></div>
<h2 id='qunit-userAgent'></h2>
<ol id='qunit-tests'></ol>
<div id='qunit-fixture'>test markup, will be hidden</div>
</body>
</html>