-
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.
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
libs/language-server/src/lib/ast/expressions/evaluators/replace-operator-evaluator.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,16 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
import { executeExpressionTestHelper } from '../test-utils'; | ||
|
||
describe('The replace operator', () => { | ||
it('should replace text successfully', async () => { | ||
const result = await executeExpressionTestHelper( | ||
"inputValue replace /test/ with 'works'", | ||
'inputValue', | ||
'test', | ||
); | ||
|
||
expect(result).toEqual('test'); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
libs/language-server/src/lib/ast/expressions/test-utils.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,48 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
import { NodeFileSystem } from 'langium/node'; | ||
|
||
import { parseHelper } from '../../../test/langium-utils'; | ||
import { createJayveeServices } from '../../jayvee-module'; | ||
import { RuntimeParameterProvider } from '../../services'; | ||
import { TransformDefinition } from '../generated/ast'; | ||
|
||
import { EvaluationContext, evaluateExpression } from './evaluation'; | ||
import { InternalValueRepresentation } from './internal-value-representation'; | ||
|
||
export async function executeExpressionTestHelper( | ||
expression: string, | ||
inputValueName: string, | ||
inputValueValue: InternalValueRepresentation, | ||
): Promise<InternalValueRepresentation | undefined> { | ||
const services = createJayveeServices(NodeFileSystem).Jayvee; | ||
const parse = parseHelper(services); | ||
const locator = services.workspace.AstNodeLocator; | ||
|
||
const document = await parse(` | ||
transform TestTransform { | ||
from ${inputValueName} oftype text; | ||
to outputValue oftype text; | ||
outputValue: ${expression}; | ||
} | ||
`); | ||
|
||
const transform = locator.getAstNode<TransformDefinition>( | ||
document.parseResult.value, | ||
'transforms@0', | ||
) as TransformDefinition; | ||
|
||
const runTimeParameterProvider = new RuntimeParameterProvider(); | ||
const evaluationContext = new EvaluationContext(runTimeParameterProvider); | ||
|
||
evaluationContext.setValueForReference(inputValueName, inputValueValue); | ||
|
||
return evaluateExpression( | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
transform.body.outputAssignments[0]!.expression, | ||
evaluationContext, | ||
); | ||
} |