Skip to content

Commit

Permalink
Added RangeConstraintExecutor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x98 committed Aug 28, 2023
1 parent 5a09036 commit 95f81a7
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg
//
// SPDX-License-Identifier: AGPL-3.0-only

import {
BlockDefinition,
InternalValueRepresentation,
TypedConstraintDefinition,
createJayveeServices,
useExtension,
} from '@jvalue/jayvee-language-server';
import {
ParseHelperOptions,
TestLangExtension,
expectNoParserAndLexerErrors,
parseHelper,
readJvTestAssetHelper,
} from '@jvalue/jayvee-language-server/test';
import { AstNode, AstNodeLocator, LangiumDocument } from 'langium';
import { NodeFileSystem } from 'langium/node';

import { getTestExecutionContext } from '../../../../test/utils';

import { RangeConstraintExecutor } from './range-constraint-executor';

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

let locator: AstNodeLocator;

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

async function parseAndValidateConstraint(
input: string,
value: InternalValueRepresentation,
): Promise<boolean> {
const document = await parse(input, { validationChecks: 'all' });
expectNoParserAndLexerErrors(document);

const usageBlock = locator.getAstNode<BlockDefinition>(
document.parseResult.value,
'pipelines@0/blocks@2',
) as BlockDefinition;
const constraint = locator.getAstNode<TypedConstraintDefinition>(
document.parseResult.value,
'constraints@0',
) as TypedConstraintDefinition;

return new RangeConstraintExecutor().isValid(
value,
// Execution context with initial stack containing usage block of constraint and constraint itself
getTestExecutionContext(locator, document, [usageBlock, constraint]),
);
}

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);
});

it('should diagnose no error on valid value', async () => {
const text = readJvTestAsset(
'range-constraint-executor/valid-constraint.jv',
);

const valid = await parseAndValidateConstraint(text, 2);

expect(valid).toBe(true);
});

it('should diagnose error on invalid value', async () => {
const text = readJvTestAsset(
'range-constraint-executor/valid-constraint.jv',
);

const valid = await parseAndValidateConstraint(text, 11);

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

constraint TestConstraint oftype RangeConstraint {
lowerBound: 1;
upperBound: 10;
}

valuetype ConstraintType oftype integer {
constraints: [
TestConstraint,
];
}

pipeline TestPipeline {

block TestExtractor oftype TestFileExtractor {
}

block TestLoader oftype TestTableLoader {
}

block TestProperty oftype TestProperty {
valuetypeAssignmentProperty: "test" oftype ConstraintType;
}

TestExtractor -> TestProperty -> TestLoader;
}

0 comments on commit 95f81a7

Please sign in to comment.