|
| 1 | +import { describe, test, expect } from '@jest/globals'; |
| 2 | +import { SVGPathData } from '../index.js'; |
| 3 | + |
| 4 | +export function testReversePath(input: string, preserveSubpathOrder?: boolean) { |
| 5 | + return new SVGPathData(input).reverse(preserveSubpathOrder).encode(); |
| 6 | +} |
| 7 | + |
| 8 | +describe('Reverse paths', () => { |
| 9 | + describe('Valid', () => { |
| 10 | + test('empty path', () => { |
| 11 | + const input = ''; |
| 12 | + const expected = ''; |
| 13 | + expect(testReversePath(input)).toEqual(expected); |
| 14 | + }); |
| 15 | + |
| 16 | + test('single point path', () => { |
| 17 | + const input = 'M10,10'; |
| 18 | + // A single point path results in just a move command |
| 19 | + const expected = 'M10 10'; |
| 20 | + expect(testReversePath(input)).toEqual(expected); |
| 21 | + }); |
| 22 | + |
| 23 | + test('simple line path', () => { |
| 24 | + const input = 'M10,10 L20,20 L30,10'; |
| 25 | + const expected = 'M30 10L20 20L10 10'; |
| 26 | + expect(testReversePath(input)).toEqual(expected); |
| 27 | + }); |
| 28 | + |
| 29 | + test('horizontal and vertical lines', () => { |
| 30 | + const input = 'M10,10 H30 V30 H10'; |
| 31 | + const expected = 'M10 30H30V10H10'; |
| 32 | + expect(testReversePath(input)).toEqual(expected); |
| 33 | + }); |
| 34 | + |
| 35 | + test('closed path (with Z command)', () => { |
| 36 | + const input = 'M10,10 L20,20 L30,10 Z'; |
| 37 | + // The Z command is preserved in the reversed path |
| 38 | + const expected = 'M30 10L20 20L10 10z'; |
| 39 | + expect(testReversePath(input)).toEqual(expected); |
| 40 | + }); |
| 41 | + |
| 42 | + test('path with cubic bezier curves', () => { |
| 43 | + const input = 'M10,10 C20,20 40,20 50,10'; |
| 44 | + // Reversed path with flipped control points |
| 45 | + const expected = 'M50 10C40 20 20 20 10 10'; |
| 46 | + expect(testReversePath(input)).toEqual(expected); |
| 47 | + }); |
| 48 | + |
| 49 | + test('path with cubic bezier curve as second command', () => { |
| 50 | + const input = 'M10,10 C20,20 30,30 40,10'; |
| 51 | + const expected = 'M40 10C30 30 20 20 10 10'; |
| 52 | + expect(testReversePath(input)).toEqual(expected); |
| 53 | + }); |
| 54 | + |
| 55 | + test('path closed both explicitly and implicitly', () => { |
| 56 | + const input = 'M10,10 L20,20 L30,10 L10,10 Z'; // Note: Last point (10,10) matches first point + Z |
| 57 | + // Should still reverse correctly and maintain Z |
| 58 | + const expected = 'M10 10L30 10L20 20L10 10z'; |
| 59 | + expect(testReversePath(input)).toEqual(expected); |
| 60 | + }); |
| 61 | + |
| 62 | + test('path closed only implicitly (without Z command)', () => { |
| 63 | + const input = 'M10,10 L20,20 L30,10 L10,10'; // Note: Last point matches first point, but no Z |
| 64 | + // Should still reverse correctly and maintain implicit closure |
| 65 | + const expected = 'M10 10L30 10L20 20L10 10'; |
| 66 | + expect(testReversePath(input)).toEqual(expected); |
| 67 | + }); |
| 68 | + |
| 69 | + test('complex mixed path with multiple command types', () => { |
| 70 | + const input = 'M10,10 H30 V30 L40,40 C50,50 60,40 70,30 H80 V20 Z'; |
| 71 | + const expected = 'M80 20V30H70C60 40 50 50 40 40L30 30V10H10z'; |
| 72 | + expect(testReversePath(input)).toEqual(expected); |
| 73 | + }); |
| 74 | + |
| 75 | + test('bezier curve with high precision coordinates (C)', () => { |
| 76 | + const input = |
| 77 | + 'M10.123456789,10.987654321 C20.111222333,20.444555666 40.777888999,20.111222333 50.555666777,10.333222111'; |
| 78 | + const expected = |
| 79 | + 'M50.555666777 10.333222111C40.777888999 20.111222333 20.111222333 20.444555666 10.123456789 10.987654321'; |
| 80 | + expect(testReversePath(input)).toEqual(expected); |
| 81 | + }); |
| 82 | + |
| 83 | + test('path with multiple subpaths (multiple M and Z commands)', () => { |
| 84 | + const input = 'M10,10 L20,20 Z M30,30 L40,40 Z'; |
| 85 | + const expected = 'M20 20L10 10zM40 40L30 30z'; |
| 86 | + expect(testReversePath(input)).toEqual(expected); |
| 87 | + }); |
| 88 | + |
| 89 | + test('path with multiple open subpaths (multiple M commands without Z)', () => { |
| 90 | + const input = 'M10,10 L20,20 M30,30 L40,40'; |
| 91 | + const expected = 'M20 20L10 10M40 40L30 30'; |
| 92 | + expect(testReversePath(input)).toEqual(expected); |
| 93 | + }); |
| 94 | + |
| 95 | + test('path with multiple subpaths and reversed subpath order', () => { |
| 96 | + const input = 'M10,10 L20,20 Z M30,30 L40,40 Z'; |
| 97 | + const expected = 'M40 40L30 30zM20 20L10 10z'; |
| 98 | + expect(testReversePath(input, false)).toEqual(expected); |
| 99 | + }); |
| 100 | + |
| 101 | + test('path with multiple open subpaths and reversed subpath order', () => { |
| 102 | + const input = 'M10,10 L20,20 M30,30 L40,40 M50,50 L60,60'; |
| 103 | + const expected = 'M60 60L50 50M40 40L30 30M20 20L10 10'; |
| 104 | + expect(testReversePath(input, false)).toEqual(expected); |
| 105 | + }); |
| 106 | + |
| 107 | + // New tests for combined HVL commands |
| 108 | + test('mixed H, V, and L commands', () => { |
| 109 | + const input = 'M10,10 H20 V20 L30,30'; |
| 110 | + const expected = 'M30 30L20 20V10H10'; |
| 111 | + expect(testReversePath(input)).toEqual(expected); |
| 112 | + }); |
| 113 | + |
| 114 | + test('alternating H and V commands', () => { |
| 115 | + const input = 'M10,10 H20 V20 H10 V30 H30'; |
| 116 | + const expected = 'M30 30H10V20H20V10H10'; |
| 117 | + expect(testReversePath(input)).toEqual(expected); |
| 118 | + }); |
| 119 | + |
| 120 | + test('H, V, and L commands with varying coordinates', () => { |
| 121 | + const input = 'M10,10 H40 V30 L20,40 H10 V10'; |
| 122 | + const expected = 'M10 10V40H20L40 30V10H10'; |
| 123 | + expect(testReversePath(input)).toEqual(expected); |
| 124 | + }); |
| 125 | + |
| 126 | + test('H and L commands in sequence', () => { |
| 127 | + const input = 'M10,10 H30 L40,20 H50'; |
| 128 | + const expected = 'M50 20H40L30 10H10'; |
| 129 | + expect(testReversePath(input)).toEqual(expected); |
| 130 | + }); |
| 131 | + |
| 132 | + test('V and L commands in sequence', () => { |
| 133 | + const input = 'M10,10 V30 L20,40 V50'; |
| 134 | + const expected = 'M20 50V40L10 30V10'; |
| 135 | + expect(testReversePath(input)).toEqual(expected); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + describe('Invalid', () => { |
| 140 | + test('throw on relative commands', () => { |
| 141 | + const input = 'm10,10 l10,10 l10,-10'; |
| 142 | + expect(() => testReversePath(input)).toThrow( |
| 143 | + 'Relative command are not supported convert first with `toAbs()`', |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + test('throw on quadratic bezier curve (Q)', () => { |
| 148 | + const input = 'M10,10 Q25,25 40,10'; |
| 149 | + expect(() => testReversePath(input)).toThrow( |
| 150 | + 'Unsupported command: Q (quadratic bezier)', |
| 151 | + ); |
| 152 | + }); |
| 153 | + |
| 154 | + test('throw on smooth cubic bezier curve (S)', () => { |
| 155 | + const input = 'M10,10 S25,25 40,10'; |
| 156 | + expect(() => testReversePath(input)).toThrow( |
| 157 | + 'Unsupported command: S (smooth cubic bezier)', |
| 158 | + ); |
| 159 | + }); |
| 160 | + |
| 161 | + test('throw on smooth quadratic bezier curve (T)', () => { |
| 162 | + const input = 'M10,10 T40,10'; |
| 163 | + expect(() => testReversePath(input)).toThrow( |
| 164 | + 'Unsupported command: T (smooth quadratic bezier)', |
| 165 | + ); |
| 166 | + }); |
| 167 | + |
| 168 | + test('throw on arc commands (A)', () => { |
| 169 | + const input = 'M10,10 A5,5 0 0 1 20,20'; |
| 170 | + expect(() => testReversePath(input)).toThrow( |
| 171 | + 'Unsupported command: A (arc)', |
| 172 | + ); |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments