|
| 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 | +); |
0 commit comments