-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
90 lines (78 loc) · 2.1 KB
/
index.test.js
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
const crx = require('./index.js');
expect.extend({
toMatchRegex(a, b) {
const sourcesMatch = a.source === b.source;
const flagsMatch = a.flags === b.flags;
const pass = sourcesMatch && flagsMatch;
return {
pass,
message: pass ?
() => `Didn't expect: ${a.toString()}\nReceived: ${b.toString()}` :
() => `Expected: ${a.toString()}\nReceived: ${b.toString()}`
};
}
});
test('handle undefined', () => {
expect(crx())
.toMatchRegex(new RegExp());
});
test('handle null', () => {
expect(crx(null))
.toMatchRegex(new RegExp(null));
});
test('handle simple regex as string', () => {
expect(crx('^yaba(daba)+do+$'))
.toMatchRegex(new RegExp('^yaba(daba)+do+$'));
});
test('handle regex with flags', () => {
expect(crx('/[123]+/gmi'))
.toMatchRegex(new RegExp('[123]+', 'gmi'));
});
test('handle multiline input', () => {
expect(crx`
1
2
3
`)
.toMatchRegex(new RegExp('123'));
});
test('handle comments', () => {
expect(crx`
1 # this is the first part
2 # this is the second
3 # the end
`).toMatchRegex(new RegExp('123'));
});
test('handle multiline with comments and flags', () => {
expect(crx`/
1 # this is the first part
2 # this is the second
3 # the end
/mg`).toMatchRegex(new RegExp('123', 'mg'));
});
test('handle named matching groups', () => {
const regex = crx`^
(?<year>\\d{4})-
(?<month>\\d{2})-
(?<day>\\d{2})
$`;
const input = '2019-01-13';
expect(input.match(regex)).toEqual(
expect.objectContaining({
groups: {
day: '13',
month: '01',
year: '2019'
}
}));
});
test('handle combining regular expressions', () => {
const yearRx = /\d{4}/;
const monthRx = /\d{2}/;
const dayRx = /\d{2}/;
expect(crx`^
${yearRx}-
${monthRx}-
${dayRx}
$`).toMatchRegex(new RegExp('^\\d{4}-\\d{2}-\\d{2}$'));
});