-
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.
* feat: ✅ Added basic test utils and tests for replace operator
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
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,24 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
import { executeDefaultTextToTextExpression } from '../test-utils'; | ||
|
||
describe('The replace operator', () => { | ||
it('should replace text successfully', async () => { | ||
const result = await executeDefaultTextToTextExpression( | ||
"inputValue replace /Test/ with 'World'", | ||
'Hello Test', | ||
); | ||
|
||
expect(result).toEqual('Hello World'); | ||
}); | ||
|
||
it('should be able to replace text with nothing', async () => { | ||
const result = await executeDefaultTextToTextExpression( | ||
"inputValue replace / Test/ with ''", | ||
'Hello Test', | ||
); | ||
|
||
expect(result).toEqual('Hello'); | ||
}); | ||
}); |
63 changes: 63 additions & 0 deletions
63
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,63 @@ | ||
// 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 executeDefaultTextToTextExpression( | ||
expression: string, | ||
input: InternalValueRepresentation, | ||
) { | ||
return executeExpressionTestHelper( | ||
expression, | ||
'inputValue', | ||
'text', | ||
input, | ||
'text', | ||
); | ||
} | ||
|
||
export async function executeExpressionTestHelper( | ||
expression: string, | ||
inputValueName: string, | ||
inputValueType: 'text', | ||
inputValueValue: InternalValueRepresentation, | ||
outputValueType: 'text', | ||
): 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 ${inputValueType}; | ||
to outputValue oftype ${outputValueType}; | ||
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, | ||
); | ||
} |