Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operator tests #526

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 libs/language-server/src/lib/ast/expressions/test-utils.ts
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,
);
}
Loading