-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #443 from jvalue/feature/runtime-parameter-validat…
…ion-tests runtime-parameter-literal tests
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
libs/interpreter-lib/src/validation-checks/runtime-parameter-literal.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,129 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
import { parseValueToInternalRepresentation } from '@jvalue/jayvee-execution'; | ||
import { | ||
EvaluationContext, | ||
RuntimeParameterLiteral, | ||
RuntimeParameterProvider, | ||
ValidationContext, | ||
createJayveeServices, | ||
useExtension, | ||
} from '@jvalue/jayvee-language-server'; | ||
import { | ||
ParseHelperOptions, | ||
TestLangExtension, | ||
expectNoParserAndLexerErrors, | ||
parseHelper, | ||
readJvTestAssetHelper, | ||
validationAcceptorMockImpl, | ||
} from '@jvalue/jayvee-language-server/test'; | ||
import { AstNode, AstNodeLocator, LangiumDocument } from 'langium'; | ||
import { NodeFileSystem } from 'langium/node'; | ||
|
||
import { validateRuntimeParameterLiteral } from './runtime-parameter-literal'; | ||
|
||
describe('Validation of validateRuntimeParameterLiteral', () => { | ||
let parse: ( | ||
input: string, | ||
options?: ParseHelperOptions, | ||
) => Promise<LangiumDocument<AstNode>>; | ||
|
||
let locator: AstNodeLocator; | ||
|
||
const validationAcceptorMock = jest.fn(validationAcceptorMockImpl); | ||
|
||
const readJvTestAsset = readJvTestAssetHelper( | ||
__dirname, | ||
'../../test/assets/', | ||
); | ||
|
||
async function parseAndValidateRuntimeParameterLiteral( | ||
input: string, | ||
runtimeParameters?: Map<string, string>, | ||
) { | ||
const document = await parse(input); | ||
expectNoParserAndLexerErrors(document); | ||
|
||
const runtimeParameter = locator.getAstNode<RuntimeParameterLiteral>( | ||
document.parseResult.value, | ||
'pipelines@0/blocks@0/body/properties@0/value', | ||
) as RuntimeParameterLiteral; | ||
|
||
const runtimeProvider = new RuntimeParameterProvider(); | ||
runtimeProvider.setValueParser(parseValueToInternalRepresentation); | ||
if (runtimeParameters) { | ||
for (const [key, value] of runtimeParameters.entries()) { | ||
runtimeProvider.setValue(key, value); | ||
} | ||
} | ||
|
||
validateRuntimeParameterLiteral( | ||
runtimeParameter, | ||
new ValidationContext(validationAcceptorMock), | ||
new EvaluationContext(runtimeProvider), | ||
); | ||
} | ||
|
||
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 no error on valid runtime parameter value', async () => { | ||
const text = readJvTestAsset( | ||
'runtime-parameter-literal/valid-text-runtime-property.jv', | ||
); | ||
|
||
await parseAndValidateRuntimeParameterLiteral( | ||
text, | ||
new Map([['TEXT_ENV', 'Value 1']]), | ||
); | ||
|
||
expect(validationAcceptorMock).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('should diagnose error on failed runtime parameter value parsing', async () => { | ||
const text = readJvTestAsset( | ||
'runtime-parameter-literal/valid-integer-runtime-property.jv', | ||
); | ||
|
||
await parseAndValidateRuntimeParameterLiteral( | ||
text, | ||
new Map([['INTEGER_ENV', 'Value 1']]), | ||
); | ||
|
||
expect(validationAcceptorMock).toHaveBeenCalledTimes(1); | ||
expect(validationAcceptorMock).toHaveBeenCalledWith( | ||
'error', | ||
`Unable to parse the value "Value 1" as integer.`, | ||
expect.any(Object), | ||
); | ||
}); | ||
|
||
it('should diagnose error on missing runtime parameter value', async () => { | ||
const text = readJvTestAsset( | ||
'runtime-parameter-literal/valid-text-runtime-property.jv', | ||
); | ||
|
||
await parseAndValidateRuntimeParameterLiteral(text); | ||
|
||
expect(validationAcceptorMock).toHaveBeenCalledTimes(1); | ||
expect(validationAcceptorMock).toHaveBeenCalledWith( | ||
'error', | ||
`A value needs to be provided by adding "-e TEXT_ENV=<value>" to the command.`, | ||
expect.any(Object), | ||
); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
libs/interpreter-lib/test/assets/runtime-parameter-literal/valid-integer-runtime-property.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,9 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pipeline Pipeline { | ||
block Test oftype TestProperty { | ||
integerProperty: requires INTEGER_ENV; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
libs/interpreter-lib/test/assets/runtime-parameter-literal/valid-text-runtime-property.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,9 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pipeline Pipeline { | ||
block Test oftype TestProperty { | ||
textProperty: requires TEXT_ENV; | ||
} | ||
} |