|
1 | 1 | /* eslint-disable no-regex-spaces,no-control-regex */ |
2 | 2 | import { describe, it, expect } from 'vitest'; |
3 | | -import Regex, {Match} from '../src/Regex'; |
| 3 | +import Regex, {Match, Group} from '../src/Regex'; |
4 | 4 |
|
5 | 5 | const str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; |
6 | 6 | const regex = new Regex('(?<' + 'first>[A-E]+)', ['i']); |
@@ -89,4 +89,64 @@ describe('Regex', () => { |
89 | 89 | i) => i)).equal('0FGHIJKLMNOPQRSTUVWXYZ1fghijklmnopqrstuvwxyz'); |
90 | 90 | }); |
91 | 91 | }); |
| 92 | + |
| 93 | + describe('static methods', () => { |
| 94 | + it('should escape special regex characters', () => { |
| 95 | + // Test the Regex.escape static method (lines 148-150) |
| 96 | + expect(Regex.escape('hello')).toBe('hello'); |
| 97 | + expect(Regex.escape('hello.world')).toBe('hello\\.world'); |
| 98 | + expect(Regex.escape('test[123]')).toBe('test\\[123\\]'); |
| 99 | + expect(Regex.escape('path/to/file')).toBe('path\\/to\\/file'); |
| 100 | + expect(Regex.escape('braces{1,2}')).toBe('braces\\{1,2\\}'); |
| 101 | + expect(Regex.escape('parentheses(group)')).toBe('parentheses\\(group\\)'); |
| 102 | + expect(Regex.escape('asterisk*plus+')).toBe('asterisk\\*plus\\+'); |
| 103 | + expect(Regex.escape('question?')).toBe('question\\?'); |
| 104 | + expect(Regex.escape('backslash\\n')).toBe('backslash\\\\n'); |
| 105 | + expect(Regex.escape('caret^dollar$')).toBe('caret\\^dollar\\$'); |
| 106 | + expect(Regex.escape('pipe|or')).toBe('pipe\\|or'); |
| 107 | + |
| 108 | + // Test all special characters together |
| 109 | + const specialChars = '-[]/{}()*+?.\\^$|'; |
| 110 | + const escaped = Regex.escape(specialChars); |
| 111 | + expect(escaped).toBe('\\-\\[\\]\\/\\{\\}\\(\\)\\*\\+\\?\\.\\\\\\^\\$\\|'); |
| 112 | + |
| 113 | + // Test that escaped strings work in actual regex |
| 114 | + const testStr = 'special.chars[here]'; |
| 115 | + const escapedPattern = Regex.escape(testStr); |
| 116 | + const regex = new Regex(escapedPattern); |
| 117 | + expect(regex.isMatch(testStr)).toBe(true); |
| 118 | + expect(regex.isMatch('special-chars-here-')).toBe(false); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('Group class', () => { |
| 123 | + it('should provide Empty static property', () => { |
| 124 | + // Test the Group.Empty static getter (lines 280-282) |
| 125 | + const emptyGroup = Group.Empty; |
| 126 | + expect(emptyGroup).toBeDefined(); |
| 127 | + expect(emptyGroup.value).toBe(''); |
| 128 | + expect(emptyGroup.index).toBe(-1); |
| 129 | + expect(emptyGroup.success).toBe(false); |
| 130 | + |
| 131 | + // Verify it's always the same instance |
| 132 | + const emptyGroup2 = Group.Empty; |
| 133 | + expect(emptyGroup).toBe(emptyGroup2); |
| 134 | + |
| 135 | + // Test that it's frozen (immutable) |
| 136 | + expect(() => { |
| 137 | + (emptyGroup as any).value = 'test'; |
| 138 | + }).toThrow(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should have success property based on index', () => { |
| 142 | + const successfulGroup = new Group('match', 5); |
| 143 | + expect(successfulGroup.success).toBe(true); |
| 144 | + |
| 145 | + const failedGroup = new Group('', -1); |
| 146 | + expect(failedGroup.success).toBe(false); |
| 147 | + |
| 148 | + const zeroIndexGroup = new Group('match', 0); |
| 149 | + expect(zeroIndexGroup.success).toBe(true); |
| 150 | + }); |
| 151 | + }); |
92 | 152 | }); |
0 commit comments