/
document-builder.test.ts
267 lines (242 loc) · 10.7 KB
/
document-builder.test.ts
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/******************************************************************************
* Copyright 2023 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
import type { AstNode, LangiumDocument, Reference, ValidationChecks } from 'langium';
import { describe, expect, test } from 'vitest';
import { CancellationTokenSource } from 'vscode-languageserver';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { isOperationCancelled, DocumentState, URI } from 'langium';
import { createServicesForGrammarWithLSP, type LangiumServices } from 'langium/lsp';
describe('DefaultDocumentBuilder', () => {
async function createServices() {
const grammar = `
grammar Test
entry Model:
(foos+=Foo | bars+=Bar)*;
Foo:
'foo' value=INT bar=[Bar];
Bar:
'bar' name=ID;
terminal INT returns number: /[0-9]+/;
terminal ID: /[_a-zA-Z][\\w_]*/;
hidden terminal WS: /\\s+/;
`;
const services = await createServicesForGrammarWithLSP({ grammar });
const fastChecks: ValidationChecks<TestAstType> = {
Foo: (node, accept) => {
if (node.value > 10) {
accept('warning', 'Value is too large: ' + node.value, { node });
}
}
};
services.validation.ValidationRegistry.register(fastChecks);
const slowChecks: ValidationChecks<TestAstType> = {
Foo: (node, accept) => {
if (node.bar.ref && node.bar.ref.name.length > 10) {
accept('warning', 'Bar is too long: ' + node.bar.ref.name, { node });
}
}
};
services.validation.ValidationRegistry.register(slowChecks, null, 'slow');
return services;
}
test('emits `onUpdate` on `update` call', async () => {
const services = await createServices();
const documentFactory = services.shared.workspace.LangiumDocumentFactory;
const documents = services.shared.workspace.LangiumDocuments;
const document = documentFactory.fromString('', URI.parse('file:///test1.txt'));
documents.addDocument(document);
const builder = services.shared.workspace.DocumentBuilder;
await builder.build([document], {});
addTextDocument(document, services);
let called = false;
builder.onUpdate(() => {
called = true;
});
await builder.update([document.uri], []);
expect(called).toBe(true);
});
test('emits `onUpdate` on `build` call', async () => {
const services = await createServices();
const documentFactory = services.shared.workspace.LangiumDocumentFactory;
const documents = services.shared.workspace.LangiumDocuments;
const document = documentFactory.fromString('', URI.parse('file:///test1.txt'));
documents.addDocument(document);
const builder = services.shared.workspace.DocumentBuilder;
await builder.build([document], {});
addTextDocument(document, services);
let called = false;
builder.onUpdate(() => {
called = true;
});
await builder.build([document]);
expect(called).toBe(true);
});
test('resumes document build after cancellation', async () => {
const services = await createServices();
const documentFactory = services.shared.workspace.LangiumDocumentFactory;
const documents = services.shared.workspace.LangiumDocuments;
const document1 = documentFactory.fromString(`
foo 1 A
foo 11 B
bar A
bar B
`, URI.parse('file:///test1.txt'));
documents.addDocument(document1);
const document2 = documentFactory.fromString(`
foo 1 C
foo 11 D
bar C
bar D
`, URI.parse('file:///test2.txt'));
documents.addDocument(document2);
const builder = services.shared.workspace.DocumentBuilder;
const tokenSource1 = new CancellationTokenSource();
builder.onBuildPhase(DocumentState.IndexedContent, () => {
tokenSource1.cancel();
});
try {
await builder.build([document1, document2], {}, tokenSource1.token);
} catch (err) {
expect(isOperationCancelled(err)).toBe(true);
}
expect(document1.state).toBe(DocumentState.IndexedContent);
expect(document2.state).toBe(DocumentState.IndexedContent);
addTextDocument(document1, services);
await builder.update([document1.uri], []);
// While the first document is built with validation due to its reported update, the second one
// is resumed with its initial build options, which did not include validation.
expect(document1.state).toBe(DocumentState.Validated);
expect(document1.diagnostics?.map(d => d.message)).toEqual([
'Value is too large: 11'
]);
expect(document2.state).toBe(DocumentState.IndexedReferences);
expect(document2.diagnostics).toBeUndefined();
});
test('includes document with references to updated document', async () => {
const services = await createServices();
const documentFactory = services.shared.workspace.LangiumDocumentFactory;
const documents = services.shared.workspace.LangiumDocuments;
const document1 = documentFactory.fromString(`
foo 1 A
foo 11 B
bar A
bar B
`, URI.parse('file:///test1.txt'));
documents.addDocument(document1);
const document2 = documentFactory.fromString(`
foo 1 C
foo 11 A
bar C
`, URI.parse('file:///test2.txt'));
documents.addDocument(document2);
const builder = services.shared.workspace.DocumentBuilder;
await builder.build([document1, document2], {});
expect(document1.state).toBe(DocumentState.IndexedReferences);
expect(document1.references.filter(r => r.error !== undefined)).toHaveLength(0);
expect(document2.state).toBe(DocumentState.IndexedReferences);
expect(document2.references.filter(r => r.error !== undefined)).toHaveLength(0);
addTextDocument(document1, services);
TextDocument.update(document1.textDocument, [{
// Change `foo 1 A` to `foo 1 D`, breaking the local reference
range: { start: { line: 1, character: 18 }, end: { line: 1, character: 19 } },
text: 'D'
}], 1);
addTextDocument(document2, services);
builder.updateBuildOptions = {
validation: {
// Only the linking error is reported for the first document
stopAfterLinkingErrors: true
}
};
await builder.update([document1.uri], []);
expect(document1.state).toBe(DocumentState.Validated);
expect(document1.diagnostics?.map(d => d.message)).toEqual([
'Could not resolve reference to Bar named \'D\'.'
]);
expect(document2.state).toBe(DocumentState.Validated);
expect(document2.diagnostics?.map(d => d.message)).toEqual([
'Value is too large: 11'
]);
});
test('runs missing validation checks if requested', async () => {
const services = await createServices();
const documentFactory = services.shared.workspace.LangiumDocumentFactory;
const documents = services.shared.workspace.LangiumDocuments;
const document1 = documentFactory.fromString(`
foo 1 AnotherStrangeBar
foo 11 B
bar AnotherStrangeBar
bar B
`, URI.parse('file:///test1.txt'));
documents.addDocument(document1);
const builder = services.shared.workspace.DocumentBuilder;
await builder.build([document1], { validation: { categories: ['built-in', 'fast'] } });
expect(document1.state).toBe(DocumentState.Validated);
expect(document1.diagnostics?.map(d => d.message)).toEqual([
'Value is too large: 11'
]);
await builder.build([document1], { validation: { categories: ['fast', 'slow'] } });
expect(document1.state).toBe(DocumentState.Validated);
expect(document1.diagnostics?.map(d => d.message)).toEqual([
'Value is too large: 11',
'Bar is too long: AnotherStrangeBar'
]);
// Re-running the fast checks should not lead to duplicate diagnostics
await builder.build([document1], { validation: { categories: ['fast'] } });
expect(document1.state).toBe(DocumentState.Validated);
expect(document1.diagnostics?.map(d => d.message)).toEqual([
'Value is too large: 11',
'Bar is too long: AnotherStrangeBar'
]);
});
test('reruns all validation checks if requested', async () => {
const services = await createServices();
const documentFactory = services.shared.workspace.LangiumDocumentFactory;
const documents = services.shared.workspace.LangiumDocuments;
const document1 = documentFactory.fromString(`
foo 1 AnotherStrangeBar
foo 11 B
bar AnotherStrangeBar
bar B
`, URI.parse('file:///test1.txt'));
documents.addDocument(document1);
const builder = services.shared.workspace.DocumentBuilder;
await builder.build([document1], { validation: { categories: ['built-in', 'fast'] } });
expect(document1.state).toBe(DocumentState.Validated);
expect(document1.diagnostics?.map(d => d.message)).toEqual([
'Value is too large: 11'
]);
await builder.build([document1], { validation: true });
expect(document1.state).toBe(DocumentState.Validated);
expect(document1.diagnostics?.map(d => d.message)).toEqual([
'Value is too large: 11',
'Bar is too long: AnotherStrangeBar'
]);
});
});
/**
* Add the given document to the TextDocuments service, simulating it being opened in an editor.
*/
function addTextDocument(doc: LangiumDocument, services: LangiumServices) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const textDocuments = services.shared.workspace.TextDocuments as any;
textDocuments._syncedDocuments.set(doc.uri.toString(), doc.textDocument);
}
type TestAstType = {
Model: Model
Foo: Foo
Bar: Bar
}
interface Model extends AstNode {
foos: Foo[]
}
interface Foo extends AstNode {
value: number
bar: Reference<Bar>
}
interface Bar extends AstNode {
name: string
}