-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuilder.test.ts
55 lines (45 loc) · 2.18 KB
/
builder.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { buildRegExp, char, unicodeProperty } from '..';
test('`regexBuilder` flags', () => {
expect(buildRegExp('a').flags).toBe('');
expect(buildRegExp('a', {}).flags).toBe('');
expect(buildRegExp('a', { global: true }).flags).toBe('g');
expect(buildRegExp('a', { global: false }).flags).toBe('');
expect(buildRegExp('a', { ignoreCase: true }).flags).toBe('i');
expect(buildRegExp('a', { ignoreCase: false }).flags).toBe('');
expect(buildRegExp('a', { multiline: true }).flags).toBe('m');
expect(buildRegExp('a', { multiline: false }).flags).toBe('');
expect(buildRegExp('a', { hasIndices: true }).flags).toBe('d');
expect(buildRegExp('a', { hasIndices: false }).flags).toBe('');
expect(buildRegExp('a', { dotAll: true }).flags).toBe('s');
expect(buildRegExp('a', { dotAll: false }).flags).toBe('');
expect(buildRegExp('a', { sticky: true }).flags).toBe('y');
expect(buildRegExp('a', { sticky: false }).flags).toBe('');
expect(
buildRegExp('a', {
global: true, //
ignoreCase: true,
multiline: false,
dotAll: true,
sticky: true,
}).flags,
).toBe('gisy');
});
test('`regexBuilder` throws when using unicode-aware features without `unicode` flag', () => {
expect(() => buildRegExp(char(0x1234))).not.toThrow();
expect(() => buildRegExp(char(0x12345), { unicode: true })).not.toThrow();
expect(() => buildRegExp(unicodeProperty('Emoji_Presentation'), { unicode: true })).not.toThrow();
expect(() => buildRegExp(char(0x123456))).toThrowErrorMatchingInlineSnapshot(
`"Expected a valid unicode code point but received 1193046"`,
);
expect(() => buildRegExp(char(0x12345))).toThrowErrorMatchingInlineSnapshot(
`"The pattern "\\u{12345}" requires Unicode-aware mode. Please ensure the "unicode" flag is set."`,
);
expect(() =>
buildRegExp(unicodeProperty('Emoji_Presentation')),
).toThrowErrorMatchingInlineSnapshot(
`"The pattern "\\p{Emoji_Presentation}" requires Unicode-aware mode. Please ensure the "unicode" flag is set."`,
);
expect(() => buildRegExp(/\P{Letter}/u)).toThrowErrorMatchingInlineSnapshot(
`"The pattern "\\P{Letter}" requires Unicode-aware mode. Please ensure the "unicode" flag is set."`,
);
});