From 57439a19b65318b71a6797848154e41c40471ac5 Mon Sep 17 00:00:00 2001 From: Philip Heltweg Date: Fri, 9 Feb 2024 12:52:42 +0100 Subject: [PATCH 1/2] WIP operator tests --- .../replace-operator-evaluator.spec.ts | 16 +++++++ .../src/lib/ast/expressions/test-utils.ts | 48 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 libs/language-server/src/lib/ast/expressions/evaluators/replace-operator-evaluator.spec.ts create mode 100644 libs/language-server/src/lib/ast/expressions/test-utils.ts diff --git a/libs/language-server/src/lib/ast/expressions/evaluators/replace-operator-evaluator.spec.ts b/libs/language-server/src/lib/ast/expressions/evaluators/replace-operator-evaluator.spec.ts new file mode 100644 index 000000000..9101a83bb --- /dev/null +++ b/libs/language-server/src/lib/ast/expressions/evaluators/replace-operator-evaluator.spec.ts @@ -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'); + }); +}); diff --git a/libs/language-server/src/lib/ast/expressions/test-utils.ts b/libs/language-server/src/lib/ast/expressions/test-utils.ts new file mode 100644 index 000000000..0132801c0 --- /dev/null +++ b/libs/language-server/src/lib/ast/expressions/test-utils.ts @@ -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 { + 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( + 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, + ); +} From 2a8b60593bc94225b7e9627f1135a28df7f10bf5 Mon Sep 17 00:00:00 2001 From: Philip Heltweg Date: Fri, 9 Feb 2024 13:38:33 +0100 Subject: [PATCH 2/2] feat: :white_check_mark: Added basic test utils and tests for replace operator --- .../replace-operator-evaluator.spec.ts | 20 +++++++++++++------ .../src/lib/ast/expressions/test-utils.ts | 19 ++++++++++++++++-- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/libs/language-server/src/lib/ast/expressions/evaluators/replace-operator-evaluator.spec.ts b/libs/language-server/src/lib/ast/expressions/evaluators/replace-operator-evaluator.spec.ts index 9101a83bb..e27482d7b 100644 --- a/libs/language-server/src/lib/ast/expressions/evaluators/replace-operator-evaluator.spec.ts +++ b/libs/language-server/src/lib/ast/expressions/evaluators/replace-operator-evaluator.spec.ts @@ -1,16 +1,24 @@ // SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg // // SPDX-License-Identifier: AGPL-3.0-only -import { executeExpressionTestHelper } from '../test-utils'; +import { executeDefaultTextToTextExpression } from '../test-utils'; describe('The replace operator', () => { it('should replace text successfully', async () => { - const result = await executeExpressionTestHelper( - "inputValue replace /test/ with 'works'", - 'inputValue', - 'test', + const result = await executeDefaultTextToTextExpression( + "inputValue replace /Test/ with 'World'", + 'Hello Test', ); - expect(result).toEqual('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'); }); }); diff --git a/libs/language-server/src/lib/ast/expressions/test-utils.ts b/libs/language-server/src/lib/ast/expressions/test-utils.ts index 0132801c0..8b98b80f0 100644 --- a/libs/language-server/src/lib/ast/expressions/test-utils.ts +++ b/libs/language-server/src/lib/ast/expressions/test-utils.ts @@ -12,10 +12,25 @@ 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 { const services = createJayveeServices(NodeFileSystem).Jayvee; const parse = parseHelper(services); @@ -23,8 +38,8 @@ export async function executeExpressionTestHelper( const document = await parse(` transform TestTransform { - from ${inputValueName} oftype text; - to outputValue oftype text; + from ${inputValueName} oftype ${inputValueType}; + to outputValue oftype ${outputValueType}; outputValue: ${expression}; }