Skip to content

Commit

Permalink
WIP operator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rhazn committed Feb 9, 2024
1 parent 64b2934 commit 5ca459b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
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 libs/language-server/src/lib/ast/expressions/test-utils.ts
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,
);
}

0 comments on commit 5ca459b

Please sign in to comment.