Skip to content

Commit cf48339

Browse files
Copilotnikku
andcommitted
Add tests for parsePath utility and performance verification
Co-authored-by: nikku <58601+nikku@users.noreply.github.com>
1 parent 1673662 commit cf48339

1 file changed

Lines changed: 57 additions & 7 deletions

File tree

test/intersect.spec.js

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import intersect from 'path-intersection';
1+
import intersect, { parsePath } from 'path-intersection';
22
import { expect } from 'chai';
33

44
import domify from 'domify';
@@ -110,12 +110,62 @@ describe('path-intersection', function() {
110110

111111
it('should export parsePath utility', function() {
112112

113-
// This test would use:
114-
// import { parsePath } from 'path-intersection';
115-
116-
// For now, we test that the main export works
117-
// The actual named export test requires ES module import which karma handles
118-
expect(intersect).to.be.a('function');
113+
// given
114+
var pathString = 'M0,0L100,100';
115+
116+
// when
117+
var parsed = parsePath(pathString);
118+
119+
// then
120+
expect(parsed).to.be.an('array');
121+
expect(parsed).to.have.length(2);
122+
expect(parsed[0][0]).to.eql('M');
123+
expect(parsed[1][0]).to.eql('C'); // Lines are converted to curves
124+
});
125+
126+
127+
it('should use parsePath with intersect', function() {
128+
129+
// given
130+
var parsed1 = parsePath('M0,0L100,100');
131+
var parsed2 = parsePath('M0,100L100,0');
132+
133+
// when
134+
var intersections = intersect(parsed1, parsed2);
135+
136+
// then
137+
expect(intersections).to.have.length(1);
138+
expect(intersections[0].x).to.eql(50);
139+
expect(intersections[0].y).to.eql(50);
140+
});
141+
142+
143+
it('should provide performance improvement with parsePath', function() {
144+
145+
// given
146+
var path1Str = 'M0,0L100,100';
147+
var path2Str = 'M0,100L100,0';
148+
var parsed1 = parsePath(path1Str);
149+
var parsed2 = parsePath(path2Str);
150+
var iterations = 100;
151+
152+
// when - measure string path performance
153+
var stringStart = Date.now();
154+
for (var i = 0; i < iterations; i++) {
155+
intersect(path1Str, path2Str);
156+
}
157+
var stringTime = Date.now() - stringStart;
158+
159+
// when - measure pre-parsed path performance
160+
var parsedStart = Date.now();
161+
for (var j = 0; j < iterations; j++) {
162+
intersect(parsed1, parsed2);
163+
}
164+
var parsedTime = Date.now() - parsedStart;
165+
166+
// then - pre-parsed should be at least 2x faster
167+
var speedup = stringTime / parsedTime;
168+
expect(speedup).to.be.at.least(2);
119169
});
120170

121171
});

0 commit comments

Comments
 (0)