-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
libs/execution/src/lib/constraints/executors/range-constraint-executor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
libs/execution/test/assets/range-constraint-executor/valid-constraint.jv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |