|
| 1 | +import { |
| 2 | + buildRegExp, |
| 3 | + char, |
| 4 | + charClass, |
| 5 | + endOfString, |
| 6 | + type RegexSequence, |
| 7 | + startOfString, |
| 8 | + unicodeProperty, |
| 9 | +} from '../..'; |
| 10 | + |
| 11 | +function u(sequence: RegexSequence) { |
| 12 | + return buildRegExp(sequence, { unicode: true }); |
| 13 | +} |
| 14 | + |
| 15 | +test('`char` pattern', () => { |
| 16 | + // eslint-disable-next-line no-control-regex |
| 17 | + expect(char(0)).toEqualRegex(/\u0000/); |
| 18 | + // eslint-disable-next-line no-control-regex |
| 19 | + expect(char(0x1)).toEqualRegex(/\u0001/); |
| 20 | + // eslint-disable-next-line no-control-regex |
| 21 | + expect(char(0x12)).toEqualRegex(/\u0012/); |
| 22 | + expect(char(0x123)).toEqualRegex(/\u0123/); |
| 23 | + expect(char(0x1234)).toEqualRegex(/\u1234/); |
| 24 | + |
| 25 | + // eslint-disable-next-line no-control-regex |
| 26 | + expect(u(char(0))).toEqualRegex(new RegExp('\\u0000', 'u')); |
| 27 | + // eslint-disable-next-line no-control-regex |
| 28 | + expect(u(char(0x1))).toEqualRegex(new RegExp('\\u0001', 'u')); |
| 29 | + expect(u(char(0x12))).toEqualRegex( |
| 30 | + // eslint-disable-next-line no-control-regex |
| 31 | + new RegExp('\\u0012', 'u'), |
| 32 | + ); |
| 33 | + expect(char(0x0123)).toEqualRegex(/\u0123/); |
| 34 | + expect(char(0x1234)).toEqualRegex(/\u1234/); |
| 35 | + |
| 36 | + expect(u(char(0x0123))).toEqualRegex(/\u0123/u); |
| 37 | + expect(u(char(0x1234))).toEqualRegex(/\u1234/u); |
| 38 | + expect(u(char(0x12345))).toEqualRegex(new RegExp('\\u{12345}', 'u')); |
| 39 | + expect(u(char(0x103456))).toEqualRegex(new RegExp('\\u{103456}', 'u')); |
| 40 | +}); |
| 41 | + |
| 42 | +test('`char` matching', () => { |
| 43 | + expect(char(0)).toMatchString('\u{0}'); |
| 44 | + expect(char(0x1)).toMatchString('\u{1}'); |
| 45 | + expect(char(0x12)).toMatchString('\u{12}}'); |
| 46 | + expect(char(0x123)).toMatchString('\u{123}'); |
| 47 | + expect(char(0x1234)).toMatchString('\u{1234}}'); |
| 48 | + |
| 49 | + expect(char('a'.codePointAt(0)!)).toMatchString('a'); |
| 50 | + expect(char('ą'.codePointAt(0)!)).toMatchString('ą'); |
| 51 | + expect(char('©'.codePointAt(0)!)).toMatchString('©'); |
| 52 | + |
| 53 | + expect(u(char(0))).toMatchString('\u{0}'); |
| 54 | + expect(u(char(0))).not.toMatchString('a'); |
| 55 | + expect(u(char(0x1))).toMatchString('\u{1}'); |
| 56 | + expect(u(char(0x12))).toMatchString('\u{12}'); |
| 57 | + expect(u(char(0x123))).toMatchString('\u{123}'); |
| 58 | + expect(u(char(0x1234))).toMatchString('\u{1234}'); |
| 59 | + expect(u(char(0x12345))).toMatchString('\u{12345}'); |
| 60 | + expect(u(char(0x103456))).toMatchString('\u{103456}'); |
| 61 | + |
| 62 | + expect(u(char('a'.codePointAt(0)!))).toMatchString('a'); |
| 63 | + expect(u(char('ą'.codePointAt(0)!))).toMatchString('ą'); |
| 64 | + expect(u(char('©'.codePointAt(0)!))).toMatchString('©'); |
| 65 | + expect(u(char('😎'.codePointAt(0)!))).toMatchString('😎'); |
| 66 | + expect(u(char('😎'.codePointAt(0)!))).toMatchString('\u{1f60e}'); |
| 67 | +}); |
| 68 | + |
| 69 | +test('`char` nesting matching', () => { |
| 70 | + expect(u(charClass(char('a'.codePointAt(0)!), char('ą'.codePointAt(0)!)))).toMatchString('a'); |
| 71 | + expect(u(charClass(char('a'.codePointAt(0)!), char('ą'.codePointAt(0)!)))).toMatchString('ą'); |
| 72 | + expect(u(charClass(char('a'.codePointAt(0)!), char('ą'.codePointAt(0)!)))).not.toMatchString('b'); |
| 73 | +}); |
| 74 | + |
| 75 | +test('`char` edge cases handling', () => { |
| 76 | + expect(() => u(char(NaN))).toThrowErrorMatchingInlineSnapshot( |
| 77 | + `"Expected a valid unicode code point but received NaN"`, |
| 78 | + ); |
| 79 | + expect(() => u(char(1.5))).toThrowErrorMatchingInlineSnapshot( |
| 80 | + `"Expected a valid unicode code point but received 1.5"`, |
| 81 | + ); |
| 82 | + expect(() => u(char(-1))).toThrowErrorMatchingInlineSnapshot( |
| 83 | + `"Expected a valid unicode code point but received -1"`, |
| 84 | + ); |
| 85 | + expect(() => u(char(0x110000))).toThrowErrorMatchingInlineSnapshot( |
| 86 | + `"Expected a valid unicode code point but received 1114112"`, |
| 87 | + ); |
| 88 | + |
| 89 | + expect(u(char(0x10ffff))).toEqualRegex(/\u{10ffff}/u); |
| 90 | +}); |
| 91 | + |
| 92 | +test('`unicodeProperty` pattern', () => { |
| 93 | + expect(u(unicodeProperty('General_Category', 'Letter'))).toEqualRegex( |
| 94 | + /\p{General_Category=Letter}/u, |
| 95 | + ); |
| 96 | + expect(u(unicodeProperty('Letter'))).toEqualRegex(/\p{Letter}/u); |
| 97 | + expect(u(unicodeProperty('L'))).toEqualRegex(/\p{L}/u); |
| 98 | + expect(u(unicodeProperty('Lu'))).toEqualRegex(/\p{Lu}/u); |
| 99 | + expect(u(unicodeProperty('Ll'))).toEqualRegex(/\p{Ll}/u); |
| 100 | + expect(u(unicodeProperty('Lt'))).toEqualRegex(/\p{Lt}/u); |
| 101 | + expect(u(unicodeProperty('Lm'))).toEqualRegex(/\p{Lm}/u); |
| 102 | + expect(u(unicodeProperty('Lo'))).toEqualRegex(/\p{Lo}/u); |
| 103 | + |
| 104 | + expect(u(unicodeProperty('Script', 'Latin'))).toEqualRegex('\\p{Script=Latin}'); |
| 105 | + expect(u(unicodeProperty('Script', 'Grek'))).toEqualRegex('\\p{Script=Grek}'); |
| 106 | + expect(u(unicodeProperty('sc', 'Cyrillic'))).toEqualRegex('\\p{sc=Cyrillic}'); |
| 107 | + |
| 108 | + expect(u(unicodeProperty('Script', 'Thaana'))).toEqualRegex('\\p{Script=Thaana}'); |
| 109 | + expect(u(unicodeProperty('Script_Extensions', 'Thaana'))).toEqualRegex( |
| 110 | + '\\p{Script_Extensions=Thaana}', |
| 111 | + ); |
| 112 | + expect(u(unicodeProperty('scx', 'Thaana'))).toEqualRegex('\\p{scx=Thaana}'); |
| 113 | + |
| 114 | + expect(u(unicodeProperty('Emoji'))).toEqualRegex('\\p{Emoji}'); |
| 115 | +}); |
| 116 | + |
| 117 | +test('`unicodeProperty` matching', () => { |
| 118 | + expect(u(unicodeProperty('General_Category', 'Letter'))).toMatchString('A'); |
| 119 | + expect(u(unicodeProperty('Letter'))).toMatchString('A'); |
| 120 | + expect(u(unicodeProperty('L'))).toMatchString('A'); |
| 121 | + |
| 122 | + expect(u(unicodeProperty('Uppercase'))).toMatchString('A'); |
| 123 | + expect(u(unicodeProperty('Uppercase'))).not.toMatchString('a'); |
| 124 | + expect(u(unicodeProperty('Lu'))).toMatchString('A'); |
| 125 | + |
| 126 | + expect(u(unicodeProperty('Lowercase'))).toMatchString('a'); |
| 127 | + expect(u(unicodeProperty('Lowercase'))).not.toMatchString('A'); |
| 128 | + expect(u(unicodeProperty('Ll'))).toMatchString('a'); |
| 129 | + |
| 130 | + expect(u(unicodeProperty('Script', 'Latin'))).toMatchString('A'); |
| 131 | + expect(u(unicodeProperty('Script', 'Latin'))).not.toMatchString('α'); |
| 132 | + expect(u(unicodeProperty('Script', 'Grek'))).toMatchString('α'); |
| 133 | + expect(u(unicodeProperty('Script', 'Grek'))).not.toMatchString('A'); |
| 134 | + |
| 135 | + // Basic emoji |
| 136 | + expect(u([startOfString, unicodeProperty('Emoji'), endOfString])).toMatchString('😎'); |
| 137 | + expect(u([startOfString, unicodeProperty('Emoji'), endOfString])).toMatchString('🐌'); |
| 138 | + |
| 139 | + // Complex emoji with skin tone modifier |
| 140 | + expect(u(unicodeProperty('Emoji'))).toMatchString('☝🏼'); |
| 141 | + expect(u([startOfString, unicodeProperty('Emoji'), endOfString])).not.toMatchString('☝🏼'); |
| 142 | +}); |
| 143 | + |
| 144 | +test('`unicodeProperty` nesting matching', () => { |
| 145 | + expect(u(charClass(unicodeProperty('Lowercase'), unicodeProperty('White_Space')))).toMatchString( |
| 146 | + 'a', |
| 147 | + ); |
| 148 | + expect(u(charClass(unicodeProperty('Lowercase'), unicodeProperty('White_Space')))).toMatchString( |
| 149 | + ' ', |
| 150 | + ); |
| 151 | + expect( |
| 152 | + u(charClass(unicodeProperty('Lowercase'), unicodeProperty('White_Space'))), |
| 153 | + ).not.toMatchString('A'); |
| 154 | +}); |
0 commit comments