-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
path.test.js
76 lines (74 loc) · 2.45 KB
/
path.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
'use strict';
const { expect } = require('chai');
const { parsePathData } = require('./path.js');
describe('parse path data', () => {
it('should allow spaces between commands', () => {
expect(parsePathData('M0 10 L \n\r\t20 30')).to.deep.equal([
{ command: 'M', args: [0, 10] },
{ command: 'L', args: [20, 30] },
]);
});
it('should allow spaces and commas between arguments', () => {
expect(parsePathData('M0 , 10 L 20 \n\r\t30,40,50')).to.deep.equal([
{ command: 'M', args: [0, 10] },
{ command: 'L', args: [20, 30] },
{ command: 'L', args: [40, 50] },
]);
});
it('should forbid commas before commands', () => {
expect(parsePathData(', M0 10')).to.deep.equal([]);
});
it('should forbid commas between commands', () => {
expect(parsePathData('M0,10 , L 20,30')).to.deep.equal([
{ command: 'M', args: [0, 10] },
]);
});
it('should forbid commas between command name and argument', () => {
expect(parsePathData('M0,10 L,20,30')).to.deep.equal([
{ command: 'M', args: [0, 10] },
]);
});
it('should forbid multipe commas in a row', () => {
expect(parsePathData('M0 , , 10')).to.deep.equal([]);
});
it('should stop when unknown char appears', () => {
expect(parsePathData('M0 10 , L 20 #40')).to.deep.equal([
{ command: 'M', args: [0, 10] },
]);
});
it('should stop when not enough arguments', () => {
expect(parsePathData('M0 10 L 20 L 30 40')).to.deep.equal([
{ command: 'M', args: [0, 10] },
]);
});
it('should stop if moveto not the first command', () => {
expect(parsePathData('L 10 20')).to.deep.equal([]);
expect(parsePathData('10 20')).to.deep.equal([]);
});
it('should stop on invalid numbers', () => {
expect(parsePathData('M ...')).to.deep.equal([]);
});
it('should handle arcs', () => {
expect(
parsePathData(
`
M600,350
l 50,-25
a25,25 -30 0,1 50,-25
25,50 -30 0,1 50,-25
25,75 -30 0,1 50,-25
a25,100 -30 0,1 50,-25
l 50,-25
`
)
).to.deep.equal([
{ command: 'M', args: [600, 350] },
{ command: 'l', args: [50, -25] },
{ command: 'a', args: [25, 25, -30, 0, 1, 50, -25] },
{ command: 'a', args: [25, 50, -30, 0, 1, 50, -25] },
{ command: 'a', args: [25, 75, -30, 0, 1, 50, -25] },
{ command: 'a', args: [25, 100, -30, 0, 1, 50, -25] },
{ command: 'l', args: [50, -25] },
]);
});
});