Add support for pipes in templates #53

The goal is to be able to modify values of variables used in templates.
It enables future functionality such as escaping, inlining etc.

It adds support applying predefined pipes to variables. Pipes
can be applied to variable substitution in with and parameter
substitution expressions. They work in similar way to piping in Unix
where each pipe applied to the compiled result of pipe before.

It adds support for using pipes in `with` and parameter substitution
expressions. It also refactors how their regex is build to reuse more of
the logic by abstracting regex building into a new class.

Finally, it separates and extends documentation for templating.
This commit is contained in:
undergroundwires
2021-09-08 18:58:30 +01:00
parent 862914b06e
commit 4d7ff7edc5
30 changed files with 1112 additions and 207 deletions

View File

@@ -1,13 +1,16 @@
import 'mocha';
import { expect } from 'chai';
import { ExpressionPosition } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition';
import { ExpressionEvaluator } from '@/application/Parser/Script/Compiler/Expressions/Expression/Expression';
import { Expression } from '@/application/Parser/Script/Compiler/Expressions/Expression/Expression';
import { IReadOnlyFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection';
// tslint:disable-next-line:max-line-length
import { ExpressionEvaluator, Expression } from '@/application/Parser/Script/Compiler/Expressions/Expression/Expression';
import { IReadOnlyFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection';
import { FunctionCallArgumentCollectionStub } from '@tests/unit/stubs/FunctionCallArgumentCollectionStub';
import { FunctionParameterCollectionStub } from '@tests/unit/stubs/FunctionParameterCollectionStub';
import { FunctionCallArgumentStub } from '@tests/unit/stubs/FunctionCallArgumentStub';
import { ExpressionEvaluationContextStub } from '@tests/unit/stubs/ExpressionEvaluationContextStub';
import { IPipelineCompiler } from '@/application/Parser/Script/Compiler/Expressions/Pipes/IPipelineCompiler';
import { PipelineCompilerStub } from '@tests/unit/stubs/PipelineCompilerStub';
import { IReadOnlyFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection';
describe('Expression', () => {
describe('ctor', () => {
@@ -79,19 +82,21 @@ describe('Expression', () => {
const testCases = [
{
name: 'throws if arguments is undefined',
args: undefined,
expectedError: 'undefined args, send empty collection instead',
context: undefined,
expectedError: 'undefined context',
},
{
name: 'throws when some of the required args are not provided',
sut: (i: ExpressionBuilder) => i.withParameterNames(['a', 'b', 'c'], false),
args: new FunctionCallArgumentCollectionStub().withArgument('b', 'provided'),
context: new ExpressionEvaluationContextStub()
.withArgs(new FunctionCallArgumentCollectionStub().withArgument('b', 'provided')),
expectedError: 'argument values are provided for required parameters: "a", "c"',
},
{
name: 'throws when none of the required args are not provided',
sut: (i: ExpressionBuilder) => i.withParameterNames(['a', 'b'], false),
args: new FunctionCallArgumentCollectionStub().withArgument('c', 'unrelated'),
context: new ExpressionEvaluationContextStub()
.withArgs(new FunctionCallArgumentCollectionStub().withArgument('c', 'unrelated')),
expectedError: 'argument values are provided for required parameters: "a", "b"',
},
];
@@ -104,7 +109,7 @@ describe('Expression', () => {
}
const sut = sutBuilder.build();
// act
const act = () => sut.evaluate(testCase.args);
const act = () => sut.evaluate(testCase.context);
// assert
expect(act).to.throw(testCase.expectedError);
});
@@ -112,29 +117,50 @@ describe('Expression', () => {
});
it('returns result from evaluator', () => {
// arrange
const evaluatorMock: ExpressionEvaluator = (args) =>
`"${args
const evaluatorMock: ExpressionEvaluator = (c) =>
`"${c
.args
.getAllParameterNames()
.map((name) => args.getArgument(name))
.map((name) => context.args.getArgument(name))
.map((arg) => `${arg.parameterName}': '${arg.argumentValue}'`)
.join('", "')}"`;
const givenArguments = new FunctionCallArgumentCollectionStub()
.withArgument('parameter1', 'value1')
.withArgument('parameter2', 'value2');
const expectedParameterNames = givenArguments.getAllParameterNames();
const expected = evaluatorMock(givenArguments);
const context = new ExpressionEvaluationContextStub()
.withArgs(givenArguments);
const expected = evaluatorMock(context);
const sut = new ExpressionBuilder()
.withEvaluator(evaluatorMock)
.withParameterNames(expectedParameterNames)
.build();
// arrange
const actual = sut.evaluate(givenArguments);
const actual = sut.evaluate(context);
// assert
expect(expected).to.equal(actual,
`\nGiven arguments: ${JSON.stringify(givenArguments)}\n` +
`\nExpected parameter names: ${JSON.stringify(expectedParameterNames)}\n`,
);
});
it('sends pipeline compiler as it is', () => {
// arrange
const expected = new PipelineCompilerStub();
const context = new ExpressionEvaluationContextStub()
.withPipelineCompiler(expected);
let actual: IPipelineCompiler;
const evaluatorMock: ExpressionEvaluator = (c) => {
actual = c.pipelineCompiler;
return '';
};
const sut = new ExpressionBuilder()
.withEvaluator(evaluatorMock)
.build();
// arrange
sut.evaluate(context);
// assert
expect(expected).to.equal(actual);
});
describe('filters unused parameters', () => {
// arrange
const testCases = [
@@ -166,16 +192,18 @@ describe('Expression', () => {
for (const testCase of testCases) {
it(testCase.name, () => {
let actual: IReadOnlyFunctionCallArgumentCollection;
const evaluatorMock: ExpressionEvaluator = (providedArgs) => {
actual = providedArgs;
const evaluatorMock: ExpressionEvaluator = (c) => {
actual = c.args;
return '';
};
const context = new ExpressionEvaluationContextStub()
.withArgs(testCase.arguments);
const sut = new ExpressionBuilder()
.withEvaluator(evaluatorMock)
.withParameters(testCase.expressionParameters)
.build();
// act
sut.evaluate(testCase.arguments);
sut.evaluate(context);
// assert
const actualArguments = actual.getAllParameterNames().map((name) => actual.getArgument(name));
expect(actualArguments).to.deep.equal(testCase.expectedArguments);

View File

@@ -0,0 +1,65 @@
import 'mocha';
import { expect } from 'chai';
import { ExpressionEvaluationContext, IExpressionEvaluationContext } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionEvaluationContext';
import { IReadOnlyFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection';
import { IPipelineCompiler } from '@/application/Parser/Script/Compiler/Expressions/Pipes/IPipelineCompiler';
import { FunctionCallArgumentCollectionStub } from '@tests/unit/stubs/FunctionCallArgumentCollectionStub';
import { PipelineCompilerStub } from '@tests/unit/stubs/PipelineCompilerStub';
describe('ExpressionEvaluationContext', () => {
describe('ctor', () => {
describe('args', () => {
it('throws if args are undefined', () => {
// arrange
const expectedError = 'undefined args';
const builder = new ExpressionEvaluationContextBuilder()
.withArgs(undefined);
// act
const act = () => builder.build();
// assert
expect(act).throw(expectedError);
});
it('sets as expected', () => {
// arrange
const expected = new FunctionCallArgumentCollectionStub()
.withArgument('expectedParameter', 'expectedValue');
const builder = new ExpressionEvaluationContextBuilder()
.withArgs(expected);
// act
const sut = builder.build();
// assert
const actual = sut.args;
expect(actual).to.equal(expected);
});
});
describe('pipelineCompiler', () => {
it('sets as expected', () => {
// arrange
const expected = new PipelineCompilerStub();
const builder = new ExpressionEvaluationContextBuilder()
.withPipelineCompiler(expected);
// act
const sut = builder.build();
// assert
expect(sut.pipelineCompiler).to.equal(expected);
});
});
});
});
class ExpressionEvaluationContextBuilder {
private args: IReadOnlyFunctionCallArgumentCollection = new FunctionCallArgumentCollectionStub();
private pipelineCompiler: IPipelineCompiler = new PipelineCompilerStub();
public withArgs(args: IReadOnlyFunctionCallArgumentCollection) {
this.args = args;
return this;
}
public withPipelineCompiler(pipelineCompiler: IPipelineCompiler) {
this.pipelineCompiler = pipelineCompiler;
return this;
}
public build(): IExpressionEvaluationContext {
return new ExpressionEvaluationContext(this.args, this.pipelineCompiler);
}
}