Skip to content

Commit

Permalink
refactor: use the loadDocuments method where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Oct 21, 2023
1 parent a969226 commit 7f2c924
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 18 deletions.
15 changes: 10 additions & 5 deletions tests/helpers/testResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import path from 'path';
import { globSync } from 'glob';
import { SAFE_DS_FILE_EXTENSIONS } from '../../src/language/helpers/fileExtensions.js';
import { group } from 'radash';
import { LangiumDocument, URI } from 'langium';
import { parseHelper } from 'langium/test';
import { BuildOptions, LangiumDocument, URI } from 'langium';
import { SafeDsServices } from '../../src/language/safe-ds-module.js';
import fs from 'fs';

const TEST_RESOURCES_PATH = path.join(__dirname, '..', 'resources');

Expand Down Expand Up @@ -99,10 +97,17 @@ const isNotSkipped = (pathRelativeToResources: string) => {
/**
* Load the documents at the specified URIs into the workspace managed by the given services.
*
* @param services The language services.
* @param uris The URIs of the documents to load.
* @param options The build options.
* @returns The loaded documents.
*/
export const loadDocuments = async (services: SafeDsServices, uris: URI[]): Promise<LangiumDocument[]> => {
export const loadDocuments = async (
services: SafeDsServices,
uris: URI[],
options: BuildOptions = {},
): Promise<LangiumDocument[]> => {
const documents = uris.map((uri) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(uri));
await services.shared.workspace.DocumentBuilder.build(documents, { validation: true });
await services.shared.workspace.DocumentBuilder.build(documents, options);
return documents;
};
2 changes: 1 addition & 1 deletion tests/language/generation/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const createGenerationTest = async (parentDirectory: URI, inputUris: URI[]): Pro
let runUntil: Location | undefined;

// Load all files, so they get linked
await loadDocuments(services, inputUris);
await loadDocuments(services, inputUris, { validation: true });

for (const uri of inputUris) {
const code = services.shared.workspace.LangiumDocuments.getOrCreateDocument(uri).textDocument.getText();
Expand Down
6 changes: 2 additions & 4 deletions tests/language/generation/testGeneration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createGenerationTests } from './creator.js';
import { SdsModule } from '../../../src/language/generated/ast.js';
import { generatePython } from '../../../src/cli/generator.js';
import fs from 'fs';
import { loadDocuments } from '../../helpers/testResources.js';

const services = createSafeDsServices(NodeFileSystem).SafeDs;
const generationTests = createGenerationTests();
Expand All @@ -27,10 +28,7 @@ describe('generation', async () => {
}

// Load all documents
const documents = test.inputUris.map((uri) =>
services.shared.workspace.LangiumDocuments.getOrCreateDocument(uri),
);
await services.shared.workspace.DocumentBuilder.build(documents);
const documents = await loadDocuments(services, test.inputUris);

// Generate code for all documents
const actualOutputPaths: string[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AssertionError } from 'assert';
import { locationToString } from '../../helpers/location.js';
import { createPartialEvaluationTests } from './creator.js';
import { getNodeByLocation } from '../../helpers/nodeFinder.js';
import { loadDocuments } from '../../helpers/testResources.js';

const services = createSafeDsServices(NodeFileSystem).SafeDs;
const partialEvaluator = services.evaluation.PartialEvaluator;
Expand All @@ -22,8 +23,7 @@ describe('partial evaluation', async () => {
}

// Load all documents
const documents = test.uris.map((uri) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(uri));
await services.shared.workspace.DocumentBuilder.build(documents);
await loadDocuments(services, test.uris);

// Ensure that partially evaluating nodes in the same equivalence class yields the same result
for (const equivalenceClassAssertion of test.equivalenceClassAssertions) {
Expand Down
4 changes: 2 additions & 2 deletions tests/language/scoping/testScoping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AssertionError } from 'assert';
import { isLocationEqual, locationToString } from '../../helpers/location.js';
import { createScopingTests, ExpectedReference } from './creator.js';
import { Location } from 'vscode-languageserver';
import { loadDocuments } from '../../helpers/testResources.js';

const services = createSafeDsServices(NodeFileSystem).SafeDs;

Expand All @@ -27,8 +28,7 @@ describe('scoping', async () => {
}

// Load all documents
const documents = test.uris.map((uri) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(uri));
await services.shared.workspace.DocumentBuilder.build(documents);
await loadDocuments(services, test.uris);

// Ensure all expected references match
for (const expectedReference of test.expectedReferences) {
Expand Down
4 changes: 2 additions & 2 deletions tests/language/typing/type computer/testTyping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AssertionError } from 'assert';
import { locationToString } from '../../../helpers/location.js';
import { createTypingTests } from './creator.js';
import { getNodeByLocation } from '../../../helpers/nodeFinder.js';
import { loadDocuments } from '../../../helpers/testResources.js';

const services = createSafeDsServices(NodeFileSystem).SafeDs;
const typeComputer = services.types.TypeComputer;
Expand All @@ -27,8 +28,7 @@ describe('typing', async () => {
}

// Load all documents
const documents = test.uris.map((uri) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(uri));
await services.shared.workspace.DocumentBuilder.build(documents);
await loadDocuments(services, test.uris)

// Ensure all nodes in the equivalence class have the same type
for (const equivalenceClassAssertion of test.equivalenceClassAssertions) {
Expand Down
4 changes: 2 additions & 2 deletions tests/language/validation/testValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Diagnostic, DiagnosticSeverity } from 'vscode-languageserver';
import { AssertionError } from 'assert';
import { clearDocuments, isRangeEqual } from 'langium/test';
import { locationToString } from '../../helpers/location.js';
import { loadDocuments } from '../../helpers/testResources.js';

const services = createSafeDsServices(NodeFileSystem).SafeDs;

Expand All @@ -26,8 +27,7 @@ describe('validation', async () => {
}

// Load all documents
const documents = test.uris.map((uri) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(uri));
await services.shared.workspace.DocumentBuilder.build(documents, { validation: true });
await loadDocuments(services, test.uris, { validation: true });

// Ensure all expected issues match
for (const expectedIssue of test.expectedIssues) {
Expand Down

0 comments on commit 7f2c924

Please sign in to comment.