Skip to content

Commit

Permalink
Update the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Jan 22, 2024
1 parent 55feeeb commit e40cde2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/langium/src/workspace/document-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export interface DocumentBuilder {
* Wait until the document specified by the {@link uri} has reached the specified state.
*
* @param state The desired state. The promise won't resolve until the document has reached this state
* @param uri The specified URI that points to the document. If the URI does not exist, the promise will never resolve.
* @param uri The specified URI that points to the document. If the URI does not exist, the promise will resolve once the workspace has reached the specified state.
* @param cancelToken Optionally allows to cancel the wait operation, disposing any listeners in the process
* @throws `OperationCancelled` if cancellation has been requested before the state has been reached
*/
Expand Down
15 changes: 6 additions & 9 deletions packages/langium/test/workspace/document-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,21 +248,18 @@ describe('DefaultDocumentBuilder', () => {
documents.addDocument(document);

const actual: string[] = [];
const expected: string[] = [];
function wait(state: DocumentState): void {
expected.push('B' + state);
expected.push('W' + state);
builder.onBuildPhase(state, async () => {
actual.push('B' + state);
await delayNextTick();
});
builder.waitUntil(state).then(() => actual.push('W' + state));
}
for (let i = 2; i <= 6; i++) {
// Register listeners for all possible document states
for (let i = DocumentState.IndexedContent; i <= DocumentState.Validated; i++) {
wait(i);
}
await builder.build([document], { validation: true });
expect(actual).toEqual(expected);
expect(actual).toEqual(['B2', 'W2', 'B3', 'W3', 'B4', 'W4', 'B5', 'W5', 'B6', 'W6']);
});

test('`waitUntil` will correctly wait even though the build process has been cancelled', async () => {
Expand All @@ -278,10 +275,9 @@ describe('DefaultDocumentBuilder', () => {
function wait(state: DocumentState): void {
builder.onBuildPhase(state, async () => {
actual.push('B' + state);
await delayNextTick();
});
}
for (let i = 2; i <= 6; i++) {
for (let i = DocumentState.IndexedContent; i <= DocumentState.Validated; i++) {
wait(i);
}
builder.waitUntil(DocumentState.ComputedScopes).then(() => cancelTokenSource.cancel());
Expand All @@ -291,6 +287,7 @@ describe('DefaultDocumentBuilder', () => {
// Build twice but interrupt the first build after the computing scope phase
try {
await builder.build([document], { validation: true }, cancelTokenSource.token);
fail('The build is supposed to be cancelled');
} catch {
// build has been cancelled, ignore
}
Expand All @@ -311,7 +308,7 @@ describe('DefaultDocumentBuilder', () => {

const cancelTokenSource = new CancellationTokenSource();
builder.waitUntil(DocumentState.IndexedReferences, cancelTokenSource.token).then(() => {
fail('This should have been cancelled');
fail('The test should fail here because the cancellation should reject the promise');
}).catch(err => {
expect(isOperationCancelled(err)).toBeTruthy();
});
Expand Down

0 comments on commit e40cde2

Please sign in to comment.