Skip to content

Commit

Permalink
feat: ✅ Added basic test utils and tests for replace operator
Browse files Browse the repository at this point in the history
  • Loading branch information
rhazn committed Feb 9, 2024
1 parent 5ca459b commit 48eea66
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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');
});
});
19 changes: 17 additions & 2 deletions libs/language-server/src/lib/ast/expressions/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,34 @@ 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 text;
to outputValue oftype text;
from ${inputValueName} oftype ${inputValueType};
to outputValue oftype ${outputValueType};
outputValue: ${expression};
}
Expand Down

0 comments on commit 48eea66

Please sign in to comment.