-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexamples.test.ts
43 lines (38 loc) · 1.12 KB
/
examples.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
import {
buildRegExp,
charRange,
choiceOf,
digit,
endOfString,
repeat,
startOfString,
} from '../index';
test('example: IPv4 address validator', () => {
const octet = choiceOf(
[digit],
[charRange('1', '9'), digit],
['1', repeat(digit, { count: 2 })],
['2', charRange('0', '4'), digit],
['25', charRange('0', '5')],
);
const regex = buildRegExp([
startOfString, //
repeat([octet, '.'], { count: 3 }),
octet,
endOfString,
]);
expect(regex).toMatchString('0.0.0.0');
expect(regex).toMatchString('192.168.0.1');
expect(regex).toMatchString('1.99.100.249');
expect(regex).toMatchString('255.255.255.255');
expect(regex).toMatchString('123.45.67.89');
expect(regex).not.toMatchString('0.0.0.');
expect(regex).not.toMatchString('0.0.0.0.');
expect(regex).not.toMatchString('0.-1.0.0');
expect(regex).not.toMatchString('0.1000.0.0');
expect(regex).not.toMatchString('0.0.300.0');
expect(regex).not.toMatchString('255.255.255.256');
expect(regex).toHavePattern(
/^(?:(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/,
);
});