Skip to content

Commit

Permalink
Operator tests (#526)
Browse files Browse the repository at this point in the history
* feat: ✅ Added basic test utils and tests for replace operator
  • Loading branch information
rhazn authored Feb 12, 2024
1 parent 07e3adb commit 37a6699
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
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,
);
}

0 comments on commit 37a6699

Please sign in to comment.