-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchar-class.test.ts
127 lines (110 loc) · 4.33 KB
/
char-class.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import {
anyOf,
buildRegExp,
charClass,
charRange,
digit,
negated,
nonDigit,
nonWhitespace,
nonWord,
oneOrMore,
optional,
whitespace,
word,
zeroOrMore,
} from '../..';
test('`charClass` base cases', () => {
expect(charClass(charRange('a', 'z'))).toEqualRegex(/[a-z]/);
expect(charClass(charRange('a', 'z'), charRange('A', 'Z'))).toEqualRegex(/[a-zA-Z]/);
expect(charClass(charRange('a', 'z'), anyOf('05'))).toEqualRegex(/[a-z05]/);
expect(charClass(charRange('a', 'z'), whitespace, anyOf('05'))).toEqualRegex(/[a-z\s05]/);
});
test('`charClass` joins character escapes', () => {
expect(charClass(word)).toEqualRegex(/[\w]/);
expect(charClass(digit)).toEqualRegex(/[\d]/);
expect(charClass(whitespace)).toEqualRegex(/[\s]/);
expect(charClass(nonWord)).toEqualRegex(/[\W]/);
expect(charClass(nonDigit)).toEqualRegex(/[\D]/);
expect(charClass(nonWhitespace)).toEqualRegex(/[\S]/);
expect(charClass(whitespace, nonWhitespace)).toEqualRegex(/[\s\S]/);
expect(charClass(word, whitespace)).toEqualRegex(/[\w\s]/);
expect(charClass(word, digit, whitespace)).toEqualRegex(/[\w\d\s]/);
expect(charClass(word, nonDigit)).toEqualRegex(/[\w\D]/);
});
test('`charClass` throws on empty text', () => {
expect(() => charClass()).toThrowErrorMatchingInlineSnapshot(
`"\`charClass\` should receive at least one element"`,
);
});
test('`charRange` pattern', () => {
expect(charRange('a', 'z')).toEqualRegex(/[a-z]/);
expect(['x', charRange('0', '9')]).toEqualRegex(/x[0-9]/);
expect([charRange('A', 'F'), 'x']).toEqualRegex(/[A-F]x/);
});
test('`charRange` throws on incorrect arguments', () => {
expect(() => charRange('z', 'a')).toThrowErrorMatchingInlineSnapshot(
`"\`start\` should be before or equal to \`end\`"`,
);
expect(() => charRange('aa', 'z')).toThrowErrorMatchingInlineSnapshot(
`"\`charRange\` should receive only single character \`start\` string"`,
);
expect(() => charRange('a', 'zz')).toThrowErrorMatchingInlineSnapshot(
`"\`charRange\` should receive only single character \`end\` string"`,
);
});
test('`anyOf` pattern', () => {
expect(anyOf('a')).toEqualRegex(/[a]/);
expect(['x', anyOf('a'), 'x']).toEqualRegex(/x[a]x/);
expect(anyOf('ab')).toEqualRegex(/[ab]/);
expect(['x', anyOf('ab')]).toEqualRegex(/x[ab]/);
expect(['x', anyOf('ab'), 'x']).toEqualRegex(/x[ab]x/);
});
test('`anyOf` pattern with quantifiers', () => {
expect(['x', oneOrMore(anyOf('abc')), 'x']).toEqualRegex(/x[abc]+x/);
expect(['x', optional(anyOf('abc')), 'x']).toEqualRegex(/x[abc]?x/);
expect(['x', zeroOrMore(anyOf('abc')), 'x']).toEqualRegex(/x[abc]*x/);
});
test('`anyOf` pattern escapes special characters', () => {
expect(anyOf('abc-+.]\\')).toEqualRegex(/[abc+.\]\\-]/);
});
test('`anyOf` pattern moves hyphen to the last position', () => {
expect(anyOf('a-bc')).toEqualRegex(/[abc-]/);
});
test('`anyOf` pattern edge cases', () => {
expect(anyOf('^-')).toEqualRegex(/[\^-]/);
expect(anyOf('-^')).toEqualRegex(/[\^-]/);
expect(anyOf('-^a')).toEqualRegex(/[a^-]/);
expect(anyOf('.')).toEqualRegex(/[.]/);
expect(anyOf('*')).toEqualRegex(/[*]/);
expect(anyOf('+')).toEqualRegex(/[+]/);
expect(anyOf('?')).toEqualRegex(/[?]/);
expect(anyOf('^')).toEqualRegex(/[^]/);
expect(anyOf('$')).toEqualRegex(/[$]/);
expect(anyOf('{')).toEqualRegex(/[{]/);
expect(anyOf('}')).toEqualRegex(/[}]/);
expect(anyOf('(')).toEqualRegex(/[(]/);
expect(anyOf(')')).toEqualRegex(/[)]/);
expect(anyOf('|')).toEqualRegex(/[|]/);
expect(anyOf('[')).toEqualRegex(/[[]/);
expect(anyOf(']')).toEqualRegex(/[\]]/);
expect(anyOf('\\')).toEqualRegex(/[\\]/);
});
test('`anyOf` throws on empty text', () => {
expect(() => anyOf('')).toThrowErrorMatchingInlineSnapshot(
`"\`anyOf\` should received at least one character"`,
);
});
test('`negated` character class pattern', () => {
expect(negated(anyOf('a'))).toEqualRegex(/[^a]/);
expect(negated(anyOf('abc'))).toEqualRegex(/[^abc]/);
});
test('`negated` character class matching', () => {
expect(negated(anyOf('a'))).not.toMatchString('aa');
expect(negated(anyOf('a'))).toMatchGroups('aba', ['b']);
});
test('`encodeCharacterClass` throws on empty text', () => {
expect(() => buildRegExp(negated({ chars: [], ranges: [] }))).toThrowErrorMatchingInlineSnapshot(
`"Character class should contain at least one character or character range"`,
);
});