-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathtest-liquid-html.ts
More file actions
176 lines (150 loc) · 5.59 KB
/
Copy pathtest-liquid-html.ts
File metadata and controls
176 lines (150 loc) · 5.59 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
/* global URL */
import test from 'ava';
import fs from 'node:fs';
import * as ohm from 'ohm-js-legacy';
import {performance} from 'perf_hooks';
import {compileAndLoadAll, matchWithInput, unparse, legacyGrammarToWasm} from './_helpers.ts';
const scriptRel = relPath => new URL(relPath, import.meta.url);
const grammarSource = fs.readFileSync(scriptRel('data/liquid-html.ohm'), 'utf8');
const grammars = ohm.grammars(grammarSource);
const startRules = ['LiquidHTML', 'AttrSingleQuoted', 'tagMarkup'];
async function loadWasmLiquidHTML() {
const wg = await compileAndLoadAll(grammarSource, {startRules});
return wg.LiquidHTML;
}
test('basic compilation', async t => {
await compileAndLoadAll(grammarSource, {startRules});
t.pass();
});
test('basic matching (small)', async t => {
const input = `---
layout: default
---
{% assign year = page.started | date: '%Y' %}`;
t.is(grammars.LiquidHTML.match(input).succeeded(), true);
const g = await legacyGrammarToWasm(grammars.LiquidHTML, {startRules});
t.is(matchWithInput(g, input), 1);
t.is(unparse(g), input);
});
test('swatch.liquid', async t => {
const input = `<span
{% if y %}
{% else %}
class="{% if x == 'x' %}x{% endif %}"
{% endif %}
>`;
const g = await loadWasmLiquidHTML();
t.is(matchWithInput(g, input), 1);
});
test('html comment', async t => {
const input = `{% if x %}
<!-- x -->
{% endif %}`;
const g = await loadWasmLiquidHTML();
t.is(matchWithInput(g, input), 1);
});
test('book-review.liquid', async t => {
const input = fs.readFileSync(scriptRel('data/book-review.liquid'), 'utf8');
let start = performance.now();
t.is(grammars.LiquidHTML.match(input).succeeded(), true); // Trigger fillInputBuffer
t.log(`Ohm.js: ${(performance.now() - start).toFixed(2)}ms`);
const g = await legacyGrammarToWasm(grammars.LiquidHTML, {startRules});
start = performance.now();
t.is(matchWithInput(g, input), 1);
t.log(`Wasm: ${(performance.now() - start).toFixed(2)}ms`);
});
test('liquidRawTagImpl', async t => {
// Just verifies the shape of the CST for a specific example in the
// LiquidHTML grammar. This was an example from Shopify's CST tests
// that was failing due to the arity changes in the Wasm implementation.
const sourceCode = `
{% raw -%}
{% if unclosed %}
not a problem
{%- endraw %}
`;
const g = await loadWasmLiquidHTML();
const r = g.match(sourceCode);
t.true(r.succeeded());
const root = r._cst;
t.is(root.ctorName, 'Node');
t.is(root.startIdx, 5);
const [opt, list] = root.children;
t.true(opt.isEmpty());
t.true(list.isList());
const sourceString = list.collect(x => x.sourceString).join('');
t.true(sourceString.startsWith('{% raw -%}'));
const onlyChild = (node, ruleName = undefined) => {
t.assert(node.children.length === 1);
if (ruleName) {
t.assert(node.children[0].ctorName === ruleName);
}
return node.children[0];
};
let child = onlyChild(list, 'liquidNode');
child = onlyChild(child, 'liquidRawTag');
child = onlyChild(child, 'liquidRawTagImpl');
t.is(child.children.length, 19);
});
test('AttrSingleQuoted', async t => {
const sourceCode = 'single=‘single‘';
const g = await loadWasmLiquidHTML();
const r = g.match(sourceCode, 'AttrSingleQuoted');
t.true(r.succeeded());
});
test('tagMarkup', async t => {
const sourceCode = '"example-snippet", id: 2, foo█ ';
const g = await loadWasmLiquidHTML();
const r = g.match(sourceCode, 'tagMarkup');
t.true(r.succeeded());
});
test('Not discards child failures', async t => {
// Grammar where Not's child failures should be completely discarded
const simpleG = ohm.grammar('G { start = (~space any)+ ">" }');
const wg = await legacyGrammarToWasm(simpleG);
// 'abc!' fails because no '>' at end. Inside the star, ~space tries space
// which fails and records "a space" — but Not should discard it.
wg.match('abc!').use(r1 => {
const jsR1 = simpleG.match('abc!');
t.is(r1.getExpectedText(), jsR1.getExpectedText());
});
// Test with alternation inside Not (like the real grammar)
const altG = ohm.grammar('G { start = (~(space | "\'" | "{{") any)+ ">" }');
const wg2 = await legacyGrammarToWasm(altG);
wg2.match('abc!').use(r2 => {
const jsR2 = altG.match('abc!');
t.is(r2.getExpectedText(), jsR2.getExpectedText());
});
});
// Compare sorted descriptions — the content must match, but the wasm compiler's
// grammar transformations may produce a different ordering than the JS interpreter.
const sortDescriptions = text =>
text
.split(/, (?:or )?|(?:^| )or /)
.sort()
.join(', ');
test('failure message', async t => {
const g = await legacyGrammarToWasm(grammars.LiquidHTML, {startRules});
const getExpectedText = input => g.match(input).use(r => r.getExpectedText());
t.is(getExpectedText('{%if cond }}'), '"%}"');
t.is(getExpectedText('{% if cond }}'), '"%}"');
t.is(getExpectedText('< a href = "abc" "></a>'), '"script", "style", or "svg"');
const input = `
<a href="abc" "></a>
`;
t.is(
sortDescriptions(getExpectedText(input)),
sortDescriptions(grammars.LiquidHTML.match(input).getExpectedText())
);
t.is(
getExpectedText('<a href="abc" {%></a>'),
[
'"doc", "comment", "raw", "javascript", "schema", "stylesheet",',
'"style", "end", "case", "capture", "form", "for", "tablerow",',
'"if", "paginate", "unless", "ifchanged", "assign", "break",',
'"continue", "cycle", "content_for", "decrement", "echo", "else",',
'"elsif", "include", "increment", "layout", "liquid", "render",',
'"section", "sections", "when", a letter, or "#"',
].join(' ')
);
});