-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathtest-failurePos.ts
More file actions
211 lines (180 loc) · 5.9 KB
/
Copy pathtest-failurePos.ts
File metadata and controls
211 lines (180 loc) · 5.9 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import test from 'ava';
import assert from 'node:assert/strict';
import fc from 'fast-check';
import {readFileSync} from 'node:fs';
import {grammars, grammar} from 'ohm-js-legacy/v18';
import {scriptRel, legacyGrammarToWasm} from './_helpers.ts';
const grammarSource = readFileSync(scriptRel('data/liquid-html.ohm'), 'utf8');
const ns = grammars(grammarSource);
function failurePos(g, input) {
return g.match(input).use(r => {
assert.equal(r.failed(), true, 'expected match failure');
return r.getRightmostFailurePosition();
});
}
/* eslint-disable max-len */
const validInput = `{% comment %}
Renders a swatch component.
Accepts:
- swatch: {Object} a swatch object
- shape: {String} swatch shape. Accepts 'square', defaults to circle.
Usage:
{% render 'swatch',
swatch: value.swatch
shape: 'square'
%}
{% endcomment %}
{%- liquid
assign swatch_value = null
if swatch.image
assign image_url = swatch.image | image_url: width: 50
assign swatch_value = 'url(' | append: image_url | append: ')'
assign swatch_focal_point = swatch.image.presentation.focal_point
elsif swatch.color
assign swatch_value = 'rgb(' | append: swatch.color.rgb | append: ')'
endif
-%}
<span
{% if swatch_value %}
class="swatch{% if shape == 'square' %} swatch--square{% endif %}"
style="--swatch--background: {{ swatch_value }};{% if swatch_focal_point %} --swatch-focal-point: {{ swatch_focal_point }};{% endif %}"
{% else %}
class="swatch swatch--unavailable{% if shape == 'square' %} swatch--square{% endif %}"
{% endif %}
></span>`;
/* eslint-enable max-len */
// Take some valid input, randomly corrupt it, and then check that the
// rightmostFailurePosition is the same as the JS parser reports.
function arbitraryEdit(input) {
return fc
.tuple(
fc.nat({max: input.length - 1}), // Position to edit
fc.integer({min: 1, max: 20}) // Number of characters to delete
)
.map(([pos, numDeleted]) => {
return input.slice(0, pos) + input.slice(pos + numDeleted);
});
}
// A fast-check property that checks that:
// - for some randomly-corrupted input, which fails to parse
// - the rightmostFailurePosition reported by a JS matcher and a Wasm matcher
// is the same.
function sameFailurePos(wasmGrammar) {
return fc.property(arbitraryEdit(validInput), input => {
fc.pre(wasmGrammar.match(input).use(r => r.succeeded()));
assert.equal(
ns.LiquidHTML.match(input).getRightmostFailurePosition(),
wasmGrammar.getRightmostFailurePosition()
);
});
}
test('failure pos (fast-check)', async t => {
const g = await legacyGrammarToWasm(ns.LiquidHTML);
const details = fc.check(sameFailurePos(g), {
includeErrorInReport: true,
interruptAfterTimeLimit: 1000,
});
t.log(`numRuns: ${details.numRuns}`);
t.is(details.failed, false, `${fc.defaultReportMessage(details)}`);
});
test('failure pos: basic 1', async t => {
const g = grammar(`
G {
Start = number+
number = digit+
}`);
const wasmGrammar = await legacyGrammarToWasm(g);
t.is(failurePos(g, 'a'), 0);
t.is(failurePos(wasmGrammar, 'a'), 0);
t.is(failurePos(g, '123a'), 3);
t.is(failurePos(wasmGrammar, '123a'), 3);
t.is(failurePos(g, '1 99a'), 4);
t.is(failurePos(wasmGrammar, '1 99a'), 4);
});
test('failure pos: basic 2', async t => {
const g = grammar(`
G {
Exp = number "+" number ";" -- plus
| number
number = digit+
}`);
const wasmGrammar = await legacyGrammarToWasm(g);
t.is(failurePos(g, '99 + 66'), 7);
t.is(failurePos(wasmGrammar, '99 + 66'), 7);
});
test('failure pos: basic 3', async t => {
const g = grammar(`
G {
Start = letter letter
space := "/*" (~"*/" any)* "*/"
}`);
const wasmGrammar = await legacyGrammarToWasm(g);
t.is(failurePos(wasmGrammar, '99'), 0);
});
test('failure pos: lookahead', async t => {
{
const g = grammar(`
G {
start = ~(anyTwo "!") "a" "b"
anyTwo = any any
}`);
const wasmGrammar = await legacyGrammarToWasm(g);
// Original Ohm behaviour is to ignore failures inside the lookahead, so
// it produces 'Expected "a"' at pos 0.
t.is(failurePos(g, '99'), 0);
t.is(failurePos(wasmGrammar, '99'), 0);
}
});
test('failure pos: memoization', async t => {
{
const g = grammar(`
G {
start = ~anyTwo anyTwo
anyTwo = any any
}`);
const wasmGrammar = await legacyGrammarToWasm(g);
// Original Ohm behaviour is to ignore failures inside the lookahead, so
// it produces 'Expected "a"' at pos 0.
t.is(failurePos(g, '9'), 1);
t.is(failurePos(wasmGrammar, '9'), 1);
}
});
test('failure pos: space skipping', async t => {
const g = grammar(`
G {
Start = digit digit
space += "/*" (~"*/" any)* "*/" -- comment
}`);
const wasmGrammar = await legacyGrammarToWasm(g);
// Failure inside space skipping should be ignored.
t.is(failurePos(g, '9 /* bad'), 2);
t.is(failurePos(wasmGrammar, '9 /* bad'), 2);
});
test('failure pos is always after space skipping', async t => {
const g = grammar(`
G {
Start = "1." "b"
| "2." letter
| "3." twice<"b">
twice<x> = x x
}`);
const wasmGrammar = await legacyGrammarToWasm(g);
// Regular terminal
t.is(failurePos(g, '1. c'), 3);
t.is(failurePos(wasmGrammar, '1. c'), 3);
// Application
t.is(failurePos(g, '2. 3'), 3);
t.is(failurePos(wasmGrammar, '2. 3'), 3);
// Dispatch w/ lifted terminal
t.is(failurePos(g, '3. c'), 3);
t.is(failurePos(wasmGrammar, '3. c'), 3);
// TODO: Is this right? Should be 5, not 4.
// https://github.com/ohmjs/ohm/issues/527
t.is(failurePos(g, '3. b c'), 4);
t.is(failurePos(wasmGrammar, '3. b c'), 4);
});
test('fast-check zoo', async t => {
const wasmGrammar = await legacyGrammarToWasm(ns.LiquidHTML);
const input = '< {% if swatch_value %}';
t.is(failurePos(wasmGrammar, input), failurePos(ns.LiquidHTML, input));
});