Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added validation tests for composite blocktypes #440

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg
//
// SPDX-License-Identifier: AGPL-3.0-only

import { AstNode, AstNodeLocator, LangiumDocument } from 'langium';
import { NodeFileSystem } from 'langium/node';

import {
CompositeBlocktypeDefinition,
EvaluationContext,
RuntimeParameterProvider,
ValidationContext,
createJayveeServices,
useExtension,
} from '../..';
import {
ParseHelperOptions,
expectNoParserAndLexerErrors,
parseHelper,
readJvTestAssetHelper,
validationAcceptorMockImpl,
} from '../../../test';
import { TestLangExtension } from '../../../test/extension';

import { validateCompositeBlockTypeDefinition } from './composite-blocktype-definition';

describe('Validation of CompositeBlocktypeDefinition', () => {
let parse: (
input: string,
options?: ParseHelperOptions,
) => Promise<LangiumDocument<AstNode>>;

const validationAcceptorMock = jest.fn(validationAcceptorMockImpl);

let locator: AstNodeLocator;

const readJvTestAsset = readJvTestAssetHelper(
__dirname,
'../../../test/assets/',
);

async function parseAndValidateBlocktype(input: string) {
const document = await parse(input);
expectNoParserAndLexerErrors(document);

const blocktype = locator.getAstNode<CompositeBlocktypeDefinition>(
document.parseResult.value,
'blocktypes@0',
) as CompositeBlocktypeDefinition;

validateCompositeBlockTypeDefinition(
blocktype,
new ValidationContext(validationAcceptorMock),
new EvaluationContext(new RuntimeParameterProvider()),
);
}

beforeAll(() => {
// Register test extension
useExtension(new TestLangExtension());
// Create language services
const services = createJayveeServices(NodeFileSystem).Jayvee;
locator = services.workspace.AstNodeLocator;
// Parse function for Jayvee (without validation)
parse = parseHelper(services);
});

afterEach(() => {
// Reset mock
validationAcceptorMock.mockReset();
});

it('should diagnose error on missing pipeline in composite blocktype', async () => {
const text = readJvTestAsset(
'composite-blocktype-definition/invalid-composite-blocktype-no-pipeline.jv',
);

await parseAndValidateBlocktype(text);

expect(validationAcceptorMock).toHaveBeenCalledTimes(1);
expect(validationAcceptorMock).toHaveBeenCalledWith(
'error',
`Composite blocktypes must define one pipeline 'TestBlock'`,
expect.any(Object),
);
});

it('should diagnose error on multiple pipelines in composite blocktype', async () => {
const text = readJvTestAsset(
'composite-blocktype-definition/invalid-composite-blocktype-multiple-pipelines.jv',
);

await parseAndValidateBlocktype(text);

expect(validationAcceptorMock).toHaveBeenCalledTimes(2);
expect(validationAcceptorMock).toHaveBeenCalledWith(
'error',
`Found more than one pipeline definition in composite blocktype 'TestBlock'`,
expect.any(Object),
);
});

it('should have no error on valid extractor blocktype definition', async () => {
const text = readJvTestAsset(
'composite-blocktype-definition/valid-composite-blocktype-extractor.jv',
);

await parseAndValidateBlocktype(text);

expect(validationAcceptorMock).toHaveBeenCalledTimes(0);
});

it('should have no error on valid extractor blocktype definition using recursion', async () => {
const text = readJvTestAsset(
'composite-blocktype-definition/valid-composite-blocktype-recursive.jv',
);

await parseAndValidateBlocktype(text);

expect(validationAcceptorMock).toHaveBeenCalledTimes(0);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg
//
// SPDX-License-Identifier: AGPL-3.0-only

composite blocktype TestBlock {

input inputName oftype None;
output outputName oftype Sheet;

block FileExtractor oftype HttpExtractor { url: 'url'; }
block FileTextInterpreter oftype TextFileInterpreter {}

block FileCSVInterpreter oftype CSVInterpreter {
}

inputName
->FileExtractor
->FileTextInterpreter
->FileCSVInterpreter
->outputName;

inputName
->FileExtractor
->FileTextInterpreter
->FileCSVInterpreter
->outputName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg
//
// SPDX-License-Identifier: AGPL-3.0-only

composite blocktype TestBlock {
property test oftype text;

input inputName oftype None;
output outputName oftype Sheet;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg
//
// SPDX-License-Identifier: AGPL-3.0-only

composite blocktype CSVExtractor {
property url oftype text;
property delimiter oftype text: ',';
property enclosing oftype text: '';
property enclosingEscape oftype text: '';

input inputName oftype None;
output outputName oftype Sheet;

block FileExtractor oftype HttpExtractor { url: url; }
block FileTextInterpreter oftype TextFileInterpreter {}

block FileCSVInterpreter oftype CSVInterpreter {
delimiter: delimiter;
enclosing: enclosing;
enclosingEscape: enclosingEscape;
}

inputName
->FileExtractor
->FileTextInterpreter
->FileCSVInterpreter
->outputName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg
//
// SPDX-License-Identifier: AGPL-3.0-only

composite blocktype TextFileExtractor {
property url oftype text;

input inputName oftype None;
output outputName oftype File;

block FileExtractor oftype HttpExtractor { url: url; }

block FileTextInterpreter oftype TextFileInterpreter {}

inputName
->FileExtractor
->FileTextInterpreter
->outputName;
}

composite blocktype CSVExtractor {
property url oftype text;
property delimiter oftype text: ',';
property enclosing oftype text: '';
property enclosingEscape oftype text: '';

input inputName oftype None;
output outputName oftype Sheet;

block TextFileExtractor oftype TextFileExtractor { url: url; }

block FileCSVInterpreter oftype CSVInterpreter {
delimiter: delimiter;
enclosing: enclosing;
enclosingEscape: enclosingEscape;
}

inputName
->TextFileExtractor
->FileCSVInterpreter
->outputName;
}
Loading