Skip to content

Commit 7d8a6b0

Browse files
committed
test: add error tests and export tests
1 parent 8e374eb commit 7d8a6b0

3 files changed

Lines changed: 350 additions & 1 deletion

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"regjsgen.js"
2727
],
2828
"scripts": {
29-
"test": "node tests/tests.js",
29+
"test": "node tests/tests.js && node tests/error-tests.js && node tests/export-tests.js",
3030
"coverage": "nyc --reporter=lcov --reporter=text-summary npm run test",
3131
"update-fixtures": "node tests/update-fixtures.js"
3232
},

‎tests/error-tests.js‎

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
var assert = require('assert');
2+
var generate = require('../regjsgen').generate;
3+
4+
// Node factories.
5+
function value(kind, codePoint) {
6+
return { 'type': 'value', 'kind': kind, 'codePoint': codePoint };
7+
}
8+
9+
function symbol(char) {
10+
return value('symbol', char.codePointAt(0));
11+
}
12+
13+
function identifier(name) {
14+
return { 'type': 'identifier', 'value': name };
15+
}
16+
17+
function group(behavior, body) {
18+
return { 'type': 'group', 'behavior': behavior, 'body': body || [symbol('a')] };
19+
}
20+
21+
function quantifier(body, min, max) {
22+
return { 'type': 'quantifier', 'min': min, 'max': max, 'greedy': true, 'body': [body] };
23+
}
24+
25+
function testThrows(description, node, expected) {
26+
assert.throws(function() {
27+
generate(node);
28+
}, expected);
29+
console.log('PASSED TEST: %s', description);
30+
}
31+
32+
function testGenerates(description, node, expected) {
33+
assert.strictEqual(generate(node), expected);
34+
console.log('PASSED TEST: %s', description);
35+
}
36+
37+
/*--------------------------------------------------------------------------*/
38+
39+
// Unknown node types.
40+
testThrows('unknown node type', { 'type': 'unknown' }, /^Error: Invalid node type: unknown$/);
41+
testThrows('inherited property as node type', { 'type': 'toString' }, /^Error: Invalid node type: toString$/);
42+
testThrows('node without a type', {}, /^Error: Invalid node type: undefined$/);
43+
44+
// Type assertions with a single expected type.
45+
testThrows(
46+
'group name that is not an identifier',
47+
{ 'type': 'group', 'behavior': 'normal', 'name': symbol('a'), 'body': [] },
48+
/^Error: Invalid node type: value; expected type: identifier$/
49+
);
50+
testThrows(
51+
'reference name that is not an identifier',
52+
{ 'type': 'reference', 'name': symbol('a') },
53+
/^Error: Invalid node type: value; expected type: identifier$/
54+
);
55+
testThrows(
56+
'class strings containing a non-`classString` node',
57+
{ 'type': 'classStrings', 'strings': [symbol('a')] },
58+
/^Error: Invalid node type: value; expected type: classString$/
59+
);
60+
61+
// Type assertions with multiple expected types.
62+
testThrows(
63+
'quantifier applied to a disjunction',
64+
quantifier({ 'type': 'disjunction', 'body': [] }, 0, null),
65+
/^Error: Invalid node type: disjunction; expected types: /
66+
);
67+
testThrows(
68+
'alternative containing a nested alternative',
69+
{ 'type': 'alternative', 'body': [symbol('a'), { 'type': 'alternative', 'body': [] }] },
70+
/^Error: Invalid node type: alternative; expected types: /
71+
);
72+
testThrows(
73+
'character class containing a group',
74+
{ 'type': 'characterClass', 'negative': false, 'kind': 'union', 'body': [group('normal')] },
75+
/^Error: Invalid node type: group; expected types: /
76+
);
77+
// The compiled type assertion regex is cached; ensure the cached copy still rejects.
78+
testThrows(
79+
'quantifier applied to a quantifier (cached type assertion)',
80+
quantifier(quantifier(symbol('a'), 0, null), 0, null),
81+
/^Error: Invalid node type: quantifier; expected types: /
82+
);
83+
84+
// Anchors.
85+
testThrows('anchor with an invalid kind', { 'type': 'anchor', 'kind': 'middle' }, /^Error: Invalid assertion$/);
86+
87+
// Character class ranges.
88+
testThrows(
89+
'character class range with a nested range as the minimum',
90+
{
91+
'type': 'characterClassRange',
92+
'min': { 'type': 'characterClassRange', 'min': symbol('a'), 'max': symbol('b') },
93+
'max': symbol('z')
94+
},
95+
/^Error: Invalid character class range$/
96+
);
97+
testThrows(
98+
'character class range with a nested range as the maximum',
99+
{
100+
'type': 'characterClassRange',
101+
'min': symbol('a'),
102+
'max': { 'type': 'characterClassRange', 'min': symbol('y'), 'max': symbol('z') }
103+
},
104+
/^Error: Invalid character class range$/
105+
);
106+
107+
// Groups.
108+
testThrows('group with an invalid behavior', group('sideways'), /^Error: Invalid behaviour: sideways$/);
109+
110+
// Backreferences.
111+
testThrows('reference without an index or name', { 'type': 'reference' }, /^Error: Unknown reference type$/);
112+
testThrows('reference with a zero index', { 'type': 'reference', 'matchIndex': 0 }, /^Error: Unknown reference type$/);
113+
114+
// Values.
115+
testThrows('value without a code point', { 'type': 'value', 'kind': 'symbol' }, /^Error: Invalid code point: undefined$/);
116+
testThrows('value with a string code point', value('symbol', '97'), /^Error: Invalid code point: 97$/);
117+
testThrows('value with an unsupported kind', value('emoji', 97), /^Error: Unsupported node kind: emoji$/);
118+
testThrows('single escape with an invalid code point', value('singleEscape', 0x61), /^Error: Invalid code point: 97$/);
119+
120+
// Invalid code points passed to `fromCodePoint`.
121+
testThrows('symbol with a `NaN` code point', value('symbol', NaN), /^RangeError: Invalid code point: NaN$/);
122+
testThrows('symbol with an infinite code point', value('symbol', Infinity), /^RangeError: Invalid code point: Infinity$/);
123+
testThrows('symbol with a negative code point', value('symbol', -1), /^RangeError: Invalid code point: -1$/);
124+
testThrows('symbol with a code point above U+10FFFF', value('symbol', 0x110000), /^RangeError: Invalid code point: 1114112$/);
125+
testThrows('symbol with a non-integer code point', value('symbol', 97.5), /^RangeError: Invalid code point: 97.5$/);
126+
testThrows('identifier escape with a negative code point', value('identifier', -1), /^RangeError: Invalid code point: -1$/);
127+
128+
/*--------------------------------------------------------------------------*/
129+
130+
// Boundary values that should still be generated.
131+
testGenerates('symbol with the maximum code point', value('symbol', 0x10FFFF), '􏿿');
132+
testGenerates('symbol with code point zero', value('symbol', 0), '\0');
133+
testGenerates('unicode code point escape with the maximum code point', value('unicodeCodePointEscape', 0x10FFFF), '\\u{10FFFF}');
134+
testGenerates('quantifier with `min` and `max` of zero', quantifier(symbol('a'), 0, 0), 'a{0}');
135+
testGenerates('quantifier with `max` of `undefined`', quantifier(symbol('a'), 2, undefined), 'a{2,}');
136+
testGenerates(
137+
'lazy quantifier',
138+
{ 'type': 'quantifier', 'min': 0, 'max': 1, 'greedy': false, 'body': [symbol('a')] },
139+
'a??'
140+
);
141+
testGenerates(
142+
'modifier group with only disabled flags',
143+
{ 'type': 'group', 'behavior': 'ignore', 'modifierFlags': { 'enabling': '', 'disabling': 'ims' }, 'body': [symbol('a')] },
144+
'(?-ims:a)'
145+
);
146+
testGenerates(
147+
'modifier group with only enabled flags',
148+
{ 'type': 'group', 'behavior': 'ignore', 'modifierFlags': { 'enabling': 'ims', 'disabling': '' }, 'body': [symbol('a')] },
149+
'(?ims:a)'
150+
);
151+
testGenerates(
152+
'named group and named backreference',
153+
{
154+
'type': 'alternative',
155+
'body': [
156+
{ 'type': 'group', 'behavior': 'normal', 'name': identifier('foo'), 'body': [symbol('a')] },
157+
{ 'type': 'reference', 'name': identifier('foo') }
158+
]
159+
},
160+
'(?<foo>a)\\k<foo>'
161+
);
162+
testGenerates(
163+
'null escape followed by a digit in a character class',
164+
{ 'type': 'characterClass', 'negative': false, 'kind': 'union', 'body': [value('null', 0), symbol('1')] },
165+
'[\\0001]'
166+
);
167+
testGenerates(
168+
'null escape followed by a digit in a class string',
169+
{
170+
'type': 'classStrings',
171+
'strings': [{ 'type': 'classString', 'characters': [value('null', 0), symbol('9')] }]
172+
},
173+
'\\q{\\0009}'
174+
);
175+
testGenerates(
176+
'null escape followed by a non-digit',
177+
{ 'type': 'alternative', 'body': [value('null', 0), symbol('a')] },
178+
'\\0a'
179+
);
180+
testGenerates(
181+
'null escape at the end of a sequence',
182+
{ 'type': 'alternative', 'body': [symbol('1'), value('null', 0)] },
183+
'1\\0'
184+
);

‎tests/export-tests.js‎

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
var assert = require('assert');
2+
var Module = require('module');
3+
var path = require('path');
4+
5+
var filename = path.join(__dirname, '..', 'regjsgen.js');
6+
var realGlobal = global;
7+
8+
// Loads a fresh copy of `regjsgen.js` through the module system (so that
9+
// coverage instrumentation still applies), with the given `exports` object.
10+
function load(exports) {
11+
var mod = new Module(filename, module);
12+
mod.filename = filename;
13+
mod.paths = Module._nodeModulePaths(path.dirname(filename));
14+
if (exports) {
15+
mod.exports = exports;
16+
}
17+
mod.load(filename);
18+
return mod;
19+
}
20+
21+
// Temporarily sets properties on the global object while running `callback`.
22+
function withGlobals(values, callback) {
23+
var names = Object.keys(values),
24+
saved = {};
25+
26+
names.forEach(function(name) {
27+
saved[name] = Object.getOwnPropertyDescriptor(realGlobal, name);
28+
realGlobal[name] = values[name];
29+
});
30+
31+
try {
32+
return callback();
33+
} finally {
34+
names.forEach(function(name) {
35+
if (saved[name]) {
36+
Object.defineProperty(realGlobal, name, saved[name]);
37+
} else {
38+
delete realGlobal[name];
39+
}
40+
});
41+
}
42+
}
43+
44+
function assertIsRegjsgen(regjsgen) {
45+
assert.ok(regjsgen, 'regjsgen should be exported');
46+
assert.strictEqual(typeof regjsgen.generate, 'function');
47+
assert.strictEqual(regjsgen.generate({ 'type': 'dot' }), '.');
48+
}
49+
50+
function test(description, callback) {
51+
callback();
52+
console.log('PASSED TEST: %s', description);
53+
}
54+
55+
// Marking `exports` and `module` as DOM nodes disables CommonJS detection.
56+
function domNodeExports() {
57+
return { 'nodeType': 1 };
58+
}
59+
60+
// Returns a no-op AMD `define`, used to make the module export to its root.
61+
function defineAmd() {
62+
function define() {}
63+
define.amd = {};
64+
return define;
65+
}
66+
67+
/*--------------------------------------------------------------------------*/
68+
69+
test('exports `generate` via CommonJS', function() {
70+
var mod = load();
71+
assertIsRegjsgen(mod.exports);
72+
assert.strictEqual(realGlobal.regjsgen, undefined);
73+
});
74+
75+
test('defines an anonymous AMD module', function() {
76+
var factory;
77+
function define(fn) {
78+
factory = fn;
79+
}
80+
define.amd = {};
81+
82+
withGlobals({ 'define': define }, function() {
83+
var mod = load();
84+
assert.strictEqual(typeof factory, 'function');
85+
assertIsRegjsgen(factory());
86+
// The global object is also assigned to when using AMD.
87+
assert.strictEqual(realGlobal.regjsgen, factory());
88+
// CommonJS exports are skipped when AMD is detected.
89+
assert.strictEqual(mod.exports.generate, undefined);
90+
delete realGlobal.regjsgen;
91+
});
92+
});
93+
94+
test('ignores `define` without `define.amd`', function() {
95+
var called = false;
96+
function define() {
97+
called = true;
98+
}
99+
100+
withGlobals({ 'define': define }, function() {
101+
var mod = load();
102+
assert.strictEqual(called, false);
103+
assertIsRegjsgen(mod.exports);
104+
});
105+
});
106+
107+
test('ignores `define` with a falsy `define.amd`', function() {
108+
var called = false;
109+
function define() {
110+
called = true;
111+
}
112+
define.amd = null;
113+
114+
withGlobals({ 'define': define }, function() {
115+
var mod = load();
116+
assert.strictEqual(called, false);
117+
assertIsRegjsgen(mod.exports);
118+
});
119+
});
120+
121+
test('exports to `this` when CommonJS is unavailable', function() {
122+
var exports = domNodeExports();
123+
load(exports);
124+
assertIsRegjsgen(exports.regjsgen);
125+
assert.strictEqual(exports.generate, undefined);
126+
});
127+
128+
test('exports to `window` when available', function() {
129+
var window = {};
130+
withGlobals({ 'window': window }, function() {
131+
load(domNodeExports());
132+
});
133+
assertIsRegjsgen(window.regjsgen);
134+
});
135+
136+
test('exports to a `global` that references itself via `window`', function() {
137+
var fakeGlobal = {};
138+
fakeGlobal.window = fakeGlobal;
139+
140+
var exports = withGlobals({ 'global': fakeGlobal, 'define': defineAmd() }, function() {
141+
return load().exports;
142+
});
143+
assertIsRegjsgen(fakeGlobal.regjsgen);
144+
assert.strictEqual(exports.regjsgen, undefined);
145+
});
146+
147+
test('exports to a `global` that references itself via `self`', function() {
148+
var fakeGlobal = {};
149+
fakeGlobal.self = fakeGlobal;
150+
151+
withGlobals({ 'global': fakeGlobal, 'define': defineAmd() }, function() {
152+
load();
153+
});
154+
assertIsRegjsgen(fakeGlobal.regjsgen);
155+
});
156+
157+
test('ignores a `global` that does not reference itself', function() {
158+
var fakeGlobal = {};
159+
160+
var exports = withGlobals({ 'global': fakeGlobal, 'define': defineAmd() }, function() {
161+
return load().exports;
162+
});
163+
assert.strictEqual(fakeGlobal.regjsgen, undefined);
164+
assertIsRegjsgen(exports.regjsgen);
165+
});

0 commit comments

Comments
 (0)