Skip to content

Commit

Permalink
refactor: shorten method to load documents
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Oct 21, 2023
1 parent b30d649 commit a969226
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
19 changes: 5 additions & 14 deletions tests/helpers/testResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,12 @@ const isNotSkipped = (pathRelativeToResources: string) => {
};

/**
* For a list of given URIs, load all files into the current workspace.
* Load the documents at the specified URIs into the workspace managed by the given services.
*
* @returns List of loaded documents
* @returns The loaded documents.
*/
export const loadAllDocuments = async (services: SafeDsServices, uris: URI[]): Promise<LangiumDocument[]> => {
const documents = await Promise.all(uris.map(async (uri) => loadDocumentWithDiagnostics(services, uri)));
await services.shared.workspace.DocumentBuilder.build(documents);
export const loadDocuments = async (services: SafeDsServices, uris: URI[]): Promise<LangiumDocument[]> => {
const documents = uris.map((uri) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(uri));
await services.shared.workspace.DocumentBuilder.build(documents, { validation: true });
return documents;
};

const loadDocumentWithDiagnostics = async function (services: SafeDsServices, uri: URI) {
const parse = parseHelper(services);
const code = fs.readFileSync(uri.fsPath).toString();
return parse(code, {
documentUri: uri.toString(),
validation: true,
});
};
7 changes: 4 additions & 3 deletions tests/language/generation/creator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
listTestPythonFiles,
listTestSafeDsFilesGroupedByParentDirectory,
loadAllDocuments,
loadDocuments,
uriToShortenedTestResourceName,
} from '../../helpers/testResources.js';
import path from 'path';
Expand Down Expand Up @@ -31,8 +31,9 @@ const createGenerationTest = async (parentDirectory: URI, inputUris: URI[]): Pro
const actualOutputRoot = URI.file(path.join(parentDirectory.fsPath, 'generated'));
const expectedOutputFiles = readExpectedOutputFiles(expectedOutputRoot, actualOutputRoot);
let runUntil: Location | undefined;
// First read all files; Then check diagnostics
await loadAllDocuments(services, inputUris);

// Load all files, so they get linked
await loadDocuments(services, inputUris);

for (const uri of inputUris) {
const code = services.shared.workspace.LangiumDocuments.getOrCreateDocument(uri).textDocument.getText();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
listTestSafeDsFilesGroupedByParentDirectory,
uriToShortenedTestResourceName,
} from '../../helpers/testResources.js';
} from '../../../helpers/testResources.js';
import fs from 'fs';
import { findTestChecks } from '../../helpers/testChecks.js';
import { findTestChecks } from '../../../helpers/testChecks.js';
import { Location } from 'vscode-languageserver';
import { getSyntaxErrors, SyntaxErrorsInCodeError } from '../../helpers/diagnostics.js';
import { getSyntaxErrors, SyntaxErrorsInCodeError } from '../../../helpers/diagnostics.js';
import { EmptyFileSystem, URI } from 'langium';
import { createSafeDsServices } from '../../../src/language/safe-ds-module.js';
import { TestDescription, TestDescriptionError } from '../../helpers/testDescription.js';
import { createSafeDsServices } from '../../../../src/language/safe-ds-module.js';
import { TestDescription, TestDescriptionError } from '../../../helpers/testDescription.js';

const services = createSafeDsServices(EmptyFileSystem).SafeDs;
const rootResourceName = 'typing';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { afterEach, beforeEach, describe, it } from 'vitest';
import { createSafeDsServices } from '../../../src/language/safe-ds-module.js';
import { createSafeDsServices } from '../../../../src/language/safe-ds-module.js';
import { NodeFileSystem } from 'langium/node';
import { clearDocuments } from 'langium/test';
import { AssertionError } from 'assert';
import { locationToString } from '../../helpers/location.js';
import { locationToString } from '../../../helpers/location.js';
import { createTypingTests } from './creator.js';
import { getNodeByLocation } from '../../helpers/nodeFinder.js';
import { getNodeByLocation } from '../../../helpers/nodeFinder.js';

const services = createSafeDsServices(NodeFileSystem).SafeDs;
const typeComputer = services.types.TypeComputer;
Expand Down

0 comments on commit a969226

Please sign in to comment.