|
| 1 | +import { describe, test, expect } from '@jest/globals'; |
| 2 | + |
| 3 | +import { SVGPathData } from '../SVGPathData.js'; |
| 4 | + |
| 5 | +function testRemoveCollinear(path: string): string { |
| 6 | + const pathData = new SVGPathData(path).removeCollinear(); |
| 7 | + return pathData.encode(); |
| 8 | +} |
| 9 | + |
| 10 | +describe('Remove collinear points', () => { |
| 11 | + test('Horizontal line with collinear point', () => { |
| 12 | + expect(testRemoveCollinear('M10,10 L20,10 L30,10')).toEqual('M10 10L30 10'); |
| 13 | + }); |
| 14 | + |
| 15 | + test('Diagonal line with collinear point', () => { |
| 16 | + expect(testRemoveCollinear('M10,10 L20,20 L30,30')).toEqual('M10 10L30 30'); |
| 17 | + }); |
| 18 | + |
| 19 | + test('Vertical line with collinear point', () => { |
| 20 | + expect(testRemoveCollinear('M10,10 L10,20 L10,30')).toEqual('M10 10L10 30'); |
| 21 | + }); |
| 22 | + |
| 23 | + test('Multiple collinear sections', () => { |
| 24 | + expect( |
| 25 | + testRemoveCollinear( |
| 26 | + 'M10,10 L20,10 L30,10 L40,20 L50,30 L60,40 L60,50 L60,60', |
| 27 | + ), |
| 28 | + ).toEqual('M10 10L30 10L60 40L60 60'); |
| 29 | + }); |
| 30 | + |
| 31 | + test('Preserves curves', () => { |
| 32 | + expect( |
| 33 | + testRemoveCollinear('M10,10 C20,20 30,20 40,10 L50,10 L60,10'), |
| 34 | + ).toEqual('M10 10C20 20 30 20 40 10L60 10'); |
| 35 | + }); |
| 36 | + |
| 37 | + test('Preserves closed paths', () => { |
| 38 | + expect(testRemoveCollinear('M10,10 L20,10 L30,10 L30,20 L10,20 Z')).toEqual( |
| 39 | + 'M10 10L30 10L30 20L10 20z', |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + test('Handles multiple subpaths', () => { |
| 44 | + expect( |
| 45 | + testRemoveCollinear('M10,10 L20,10 L30,10 M40,40 L50,40 L60,40'), |
| 46 | + ).toEqual('M10 10L30 10M40 40L60 40'); |
| 47 | + }); |
| 48 | + |
| 49 | + // Tests for relative paths |
| 50 | + test('Relative horizontal line with collinear point', () => { |
| 51 | + expect(testRemoveCollinear('m10,10 l10,0 l10,0')).toEqual('m10 10l20 0'); |
| 52 | + }); |
| 53 | + |
| 54 | + test('Relative diagonal line with collinear point', () => { |
| 55 | + expect(testRemoveCollinear('m10,10 l10,10 l10,10')).toEqual('m10 10l20 20'); |
| 56 | + }); |
| 57 | + |
| 58 | + test('Relative vertical line with collinear point', () => { |
| 59 | + expect(testRemoveCollinear('m10,10 l0,10 l0,10')).toEqual('m10 10l0 20'); |
| 60 | + }); |
| 61 | + |
| 62 | + test('Mixed relative and absolute commands', () => { |
| 63 | + expect(testRemoveCollinear('M10,10 L20,10 l10,0 l10,0')).toEqual( |
| 64 | + 'M10 10l30 0', |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + test('Relative multiple collinear sections', () => { |
| 69 | + expect( |
| 70 | + testRemoveCollinear( |
| 71 | + 'm10,10 l10,0 l10,0 l10,10 l10,10 l10,10 l0,10 l0,10', |
| 72 | + ), |
| 73 | + ).toEqual('m10 10l20 0l30 30l0 20'); |
| 74 | + }); |
| 75 | +}); |
0 commit comments