-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
49 lines (43 loc) · 1.54 KB
/
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
'use strict';
const test = require('ava');
const m = require('./index.js');
function macro(t, regex, flags, expected) {
if (!expected) {
expected = flags;
flags = '';
}
t.is(m(regex, flags), expected);
}
macro.title = (providedTitle, regex, flags, expected) => {
if (!expected) {
expected = flags;
flags = '';
}
return `/${regex}/${flags} => /${expected}/${flags}`;
};
test(macro, '[0-9]', '\\d');
test(macro, '[0-9]', 'ig', '\\d');
test(macro, '[^0-9]', '\\D');
test(macro, '[a-zA-Z0-9_]', '\\w');
test(macro, '[a-zA-Z0-9_]', 'i', '\\w');
test(macro, '[a-zA-Z\\d_]', '\\w');
test(macro, '[A-Za-z0-9_]', '\\w');
test(macro, '[A-Za-z\\d_]', '\\w');
test(macro, '[a-z0-9_]', 'i', '\\w');
test(macro, '[a-z0-9_]', 'ig', '\\w');
test(macro, '[a-z\\d_]', 'i', '\\w');
test(macro, '[^a-zA-Z0-9_]', '\\W');
test(macro, '[^A-Za-z0-9_]', '\\W');
test(macro, '[^a-zA-Z\\d_]', '\\W');
test(macro, '[^A-Za-z\\d_]', '\\W');
test(macro, '[^a-z0-9_]', 'i', '\\W');
test(macro, '[^a-z0-9_]', 'g', '[^a-z0-9_]');
test(macro, '[^a-z\\d_]', 'i', '\\W');
test(macro, '[a-zA-Z0-9]', '[a-zA-Z0-9]');
test(macro, '[0-9]+\\.[^0-9]?\\.[a-zA-Z0-9_]+', '\\d+\\.\\D?\\.\\w+');
test(macro, '[0-9]+\\.[^0-9]?\\.[a-zA-Z0-9_]+\\.[^a-z0-9_]', 'i', '\\d+\\.\\D?\\.\\w+\\.\\W');
test(macro, '[0-9]+\\.[^0-9]?\\.[a-zA-Z0-9_]+\\.[^a-z0-9_]', 'g', '\\d+\\.\\D?\\.\\w+\\.[^a-z0-9_]');
test('error', t => {
t.throws(() => m(98), {message: 'Expected regexp to be of type `string`, got `number`'});
t.throws(() => m('[0-9]', {}), {message: 'Expected flags to be of type `string`, got `object`'});
});