-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
59 lines (48 loc) · 1.57 KB
/
test.js
File metadata and controls
59 lines (48 loc) · 1.57 KB
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
// regexlab — smoke tests. run via `node test.js`.
import { explain } from './src/index.js';
import assert from 'node:assert';
function it(name, fn) {
try { fn(); console.log(` ok ${name}`); }
catch (e) { console.error(` FAIL ${name}: ${e.message}`); process.exitCode = 1; }
}
console.log('regexlab smoke tests');
it('plain literal', () => {
const r = explain('foo');
assert.equal(r.tokens.length, 3);
assert.match(r.tokens[0].explanation, /literal character "f"/);
});
it('digits with quantifier', () => {
const r = explain('\\d{3}');
assert.equal(r.tokens.length, 1);
assert.match(r.tokens[0].explanation, /digit/);
assert.match(r.tokens[0].explanation, /exactly 3 times/);
});
it('character class with range', () => {
const r = explain('[a-z]+');
assert.match(r.tokens[0].explanation, /one of/);
assert.match(r.tokens[0].explanation, /one or more/);
});
it('negated character class', () => {
const r = explain('[^0-9]');
assert.match(r.tokens[0].explanation, /except/);
});
it('alternation', () => {
const r = explain('foo|bar');
const pipeTok = r.tokens.find(t => t.token === '|');
assert.ok(pipeTok);
assert.match(pipeTok.explanation, /OR/);
});
it('capturing group', () => {
const r = explain('(abc)');
assert.match(r.tokens[0].explanation, /capturing group/);
});
it('flags parsed', () => {
const r = explain('/foo/gi');
assert.equal(r.flags, 'gi');
assert.equal(r.flagsExplained.length, 2);
});
it('lookahead', () => {
const r = explain('(?=\\d)');
assert.match(r.tokens[0].explanation, /positive lookahead/);
});
console.log('done');