Add support for nested templates
Add support for expressions inside expressions.
Add support for templating where the output of one expression results in
another template part with expressions.
E.g., this did not work before, but compilation will now evaluate both
with expression with `$condition` and parameter substitution with
`$text`:
```
{{ with $condition }}
echo '{{ $text }}'
{{ end }}
```
Add also more sanity checks (validation logic) when compiling
expressions to reveal problems quickly.
This commit is contained in:
@@ -31,4 +31,184 @@ describe('ExpressionPosition', () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe('isInInsideOf', () => {
|
||||
// arrange
|
||||
const testCases: readonly {
|
||||
name: string,
|
||||
sut: ExpressionPosition,
|
||||
potentialParent: ExpressionPosition,
|
||||
expectedResult: boolean,
|
||||
}[] = [
|
||||
{
|
||||
name: 'true; when other contains sut inside boundaries',
|
||||
sut: new ExpressionPosition(4, 8),
|
||||
potentialParent: new ExpressionPosition(0, 10),
|
||||
expectedResult: true,
|
||||
},
|
||||
{
|
||||
name: 'true; when other contains sut with same upper boundary',
|
||||
sut: new ExpressionPosition(4, 10),
|
||||
potentialParent: new ExpressionPosition(0, 10),
|
||||
expectedResult: true,
|
||||
},
|
||||
{
|
||||
name: 'true; when other contains sut with same lower boundary',
|
||||
sut: new ExpressionPosition(0, 8),
|
||||
potentialParent: new ExpressionPosition(0, 10),
|
||||
expectedResult: true,
|
||||
},
|
||||
{
|
||||
name: 'false; when other is same as sut',
|
||||
sut: new ExpressionPosition(0, 10),
|
||||
potentialParent: new ExpressionPosition(0, 10),
|
||||
expectedResult: false,
|
||||
},
|
||||
{
|
||||
name: 'false; when sut contains other',
|
||||
sut: new ExpressionPosition(0, 10),
|
||||
potentialParent: new ExpressionPosition(4, 8),
|
||||
expectedResult: false,
|
||||
},
|
||||
{
|
||||
name: 'false; when sut starts and ends before other',
|
||||
sut: new ExpressionPosition(0, 10),
|
||||
potentialParent: new ExpressionPosition(15, 25),
|
||||
expectedResult: false,
|
||||
},
|
||||
{
|
||||
name: 'false; when sut starts before other but ends inside other',
|
||||
sut: new ExpressionPosition(0, 10),
|
||||
potentialParent: new ExpressionPosition(5, 10),
|
||||
expectedResult: false,
|
||||
},
|
||||
{
|
||||
name: 'false; when sut starts inside other but ends after other',
|
||||
sut: new ExpressionPosition(5, 11),
|
||||
potentialParent: new ExpressionPosition(0, 10),
|
||||
expectedResult: false,
|
||||
},
|
||||
{
|
||||
name: 'false; when sut starts at same position but end after other',
|
||||
sut: new ExpressionPosition(0, 11),
|
||||
potentialParent: new ExpressionPosition(0, 10),
|
||||
expectedResult: false,
|
||||
},
|
||||
{
|
||||
name: 'false; when sut ends at same positions but start before other',
|
||||
sut: new ExpressionPosition(0, 10),
|
||||
potentialParent: new ExpressionPosition(1, 10),
|
||||
expectedResult: false,
|
||||
},
|
||||
];
|
||||
for (const testCase of testCases) {
|
||||
it(testCase.name, () => {
|
||||
// act
|
||||
const actual = testCase.sut.isInInsideOf(testCase.potentialParent);
|
||||
// assert
|
||||
expect(actual).to.equal(testCase.expectedResult);
|
||||
});
|
||||
}
|
||||
});
|
||||
describe('isSame', () => {
|
||||
// arrange
|
||||
const testCases: readonly {
|
||||
name: string,
|
||||
sut: ExpressionPosition,
|
||||
other: ExpressionPosition,
|
||||
expectedResult: boolean,
|
||||
}[] = [
|
||||
{
|
||||
name: 'true; when positions are same',
|
||||
sut: new ExpressionPosition(0, 10),
|
||||
other: new ExpressionPosition(0, 10),
|
||||
expectedResult: true,
|
||||
},
|
||||
{
|
||||
name: 'false; when start position is different',
|
||||
sut: new ExpressionPosition(0, 10),
|
||||
other: new ExpressionPosition(1, 10),
|
||||
expectedResult: false,
|
||||
},
|
||||
{
|
||||
name: 'false; when end position is different',
|
||||
sut: new ExpressionPosition(0, 10),
|
||||
other: new ExpressionPosition(0, 11),
|
||||
expectedResult: false,
|
||||
},
|
||||
{
|
||||
name: 'false; when both start and end positions are different',
|
||||
sut: new ExpressionPosition(0, 10),
|
||||
other: new ExpressionPosition(20, 30),
|
||||
expectedResult: false,
|
||||
},
|
||||
];
|
||||
for (const testCase of testCases) {
|
||||
it(testCase.name, () => {
|
||||
// act
|
||||
const actual = testCase.sut.isSame(testCase.other);
|
||||
// assert
|
||||
expect(actual).to.equal(testCase.expectedResult);
|
||||
});
|
||||
}
|
||||
});
|
||||
describe('isIntersecting', () => {
|
||||
// arrange
|
||||
const testCases: readonly {
|
||||
name: string,
|
||||
first: ExpressionPosition,
|
||||
second: ExpressionPosition,
|
||||
expectedResult: boolean,
|
||||
}[] = [
|
||||
{
|
||||
name: 'true; when one contains other inside boundaries',
|
||||
first: new ExpressionPosition(4, 8),
|
||||
second: new ExpressionPosition(0, 10),
|
||||
expectedResult: true,
|
||||
},
|
||||
{
|
||||
name: 'true; when one starts inside other\'s ending boundary without being contained',
|
||||
first: new ExpressionPosition(0, 10),
|
||||
second: new ExpressionPosition(9, 15),
|
||||
expectedResult: true,
|
||||
},
|
||||
{
|
||||
name: 'true; when positions are the same',
|
||||
first: new ExpressionPosition(0, 5),
|
||||
second: new ExpressionPosition(0, 5),
|
||||
expectedResult: true,
|
||||
},
|
||||
{
|
||||
name: 'true; when one starts inside other\'s starting boundary without being contained',
|
||||
first: new ExpressionPosition(5, 10),
|
||||
second: new ExpressionPosition(5, 11),
|
||||
expectedResult: true,
|
||||
},
|
||||
{
|
||||
name: 'false; when one starts directly after other',
|
||||
first: new ExpressionPosition(0, 10),
|
||||
second: new ExpressionPosition(10, 20),
|
||||
expectedResult: false,
|
||||
},
|
||||
{
|
||||
name: 'false; when one starts after other with margin',
|
||||
first: new ExpressionPosition(0, 10),
|
||||
second: new ExpressionPosition(100, 200),
|
||||
expectedResult: false,
|
||||
},
|
||||
];
|
||||
for (const testCase of testCases) {
|
||||
it(testCase.name, () => {
|
||||
// act
|
||||
const actual = testCase.first.isIntersecting(testCase.second);
|
||||
// assert
|
||||
expect(actual).to.equal(testCase.expectedResult);
|
||||
});
|
||||
it(`reversed: ${testCase.name}`, () => {
|
||||
// act
|
||||
const actual = testCase.second.isIntersecting(testCase.first);
|
||||
// assert
|
||||
expect(actual).to.equal(testCase.expectedResult);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,10 +6,11 @@ import { ExpressionStub } from '@tests/unit/shared/Stubs/ExpressionStub';
|
||||
import { ExpressionParserStub } from '@tests/unit/shared/Stubs/ExpressionParserStub';
|
||||
import { FunctionCallArgumentCollectionStub } from '@tests/unit/shared/Stubs/FunctionCallArgumentCollectionStub';
|
||||
import { itEachAbsentObjectValue, itEachAbsentStringValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||
import { IExpression } from '@/application/Parser/Script/Compiler/Expressions/Expression/IExpression';
|
||||
|
||||
describe('ExpressionsCompiler', () => {
|
||||
describe('compileExpressions', () => {
|
||||
describe('returns code when it is absent', () => {
|
||||
describe('returns code when code is absent', () => {
|
||||
itEachAbsentStringValue((absentValue) => {
|
||||
// arrange
|
||||
const expected = absentValue;
|
||||
@@ -21,6 +22,99 @@ describe('ExpressionsCompiler', () => {
|
||||
expect(value).to.equal(expected);
|
||||
});
|
||||
});
|
||||
describe('can compile nested expressions', () => {
|
||||
it('when one expression is evaluated to a text that contains another expression', () => {
|
||||
// arrange
|
||||
const expectedResult = 'hello world!';
|
||||
const rawCode = 'hello {{ firstExpression }}!';
|
||||
const outerExpressionResult = '{{ secondExpression }}';
|
||||
const expectedCodeAfterFirstCompilationRound = 'hello {{ secondExpression }}!';
|
||||
const innerExpressionResult = 'world';
|
||||
const expressionParserMock = new ExpressionParserStub()
|
||||
.withResult(rawCode, [
|
||||
new ExpressionStub()
|
||||
// {{ firstExpression }
|
||||
.withPosition(6, 27)
|
||||
// Parser would hit the outer expression
|
||||
.withEvaluatedResult(outerExpressionResult),
|
||||
])
|
||||
.withResult(expectedCodeAfterFirstCompilationRound, [
|
||||
new ExpressionStub()
|
||||
// {{ secondExpression }}
|
||||
.withPosition(6, 28)
|
||||
// once the outer expression parser, compiler now parses its evaluated result
|
||||
.withEvaluatedResult(innerExpressionResult),
|
||||
]);
|
||||
const sut = new SystemUnderTest(expressionParserMock);
|
||||
const args = new FunctionCallArgumentCollectionStub();
|
||||
// act
|
||||
const actual = sut.compileExpressions(rawCode, args);
|
||||
// assert
|
||||
expect(actual).to.equal(expectedResult);
|
||||
});
|
||||
describe('when one expression contains another hardcoded expression', () => {
|
||||
it('when hardcoded expression is does not contain the hardcoded expression', () => {
|
||||
// arrange
|
||||
const expectedResult = 'hi !';
|
||||
const rawCode = 'hi {{ outerExpressionStart }}delete {{ innerExpression }} me{{ outerExpressionEnd }}!';
|
||||
const outerExpressionResult = '';
|
||||
const innerExpressionResult = 'should not be there';
|
||||
const expressionParserMock = new ExpressionParserStub()
|
||||
.withResult(rawCode, [
|
||||
new ExpressionStub()
|
||||
// {{ outerExpressionStart }}delete {{ innerExpression }} me{{ outerExpressionEnd }}
|
||||
.withPosition(3, 84)
|
||||
.withEvaluatedResult(outerExpressionResult),
|
||||
new ExpressionStub()
|
||||
// {{ innerExpression }}
|
||||
.withPosition(36, 57)
|
||||
// Parser would hit both expressions as one is hardcoded in other
|
||||
.withEvaluatedResult(innerExpressionResult),
|
||||
])
|
||||
.withResult(expectedResult, []);
|
||||
const sut = new SystemUnderTest(expressionParserMock);
|
||||
const args = new FunctionCallArgumentCollectionStub();
|
||||
// act
|
||||
const actual = sut.compileExpressions(rawCode, args);
|
||||
// assert
|
||||
expect(actual).to.equal(expectedResult);
|
||||
});
|
||||
it('when hardcoded expression contains the hardcoded expression', () => {
|
||||
// arrange
|
||||
const expectedResult = 'hi game of thrones!';
|
||||
const rawCode = 'hi {{ outerExpressionStart }} game {{ innerExpression }} {{ outerExpressionEnd }}!';
|
||||
const expectedCodeAfterFirstCompilationRound = 'hi game {{ innerExpression }}!'; // outer is compiled first
|
||||
const outerExpressionResult = 'game {{ innerExpression }}';
|
||||
const innerExpressionResult = 'of thrones';
|
||||
const expressionParserMock = new ExpressionParserStub()
|
||||
.withResult(rawCode, [
|
||||
new ExpressionStub()
|
||||
// {{ outerExpressionStart }} game {{ innerExpression }} {{ outerExpressionEnd }}
|
||||
.withPosition(3, 81)
|
||||
// Parser would hit the outer expression
|
||||
.withEvaluatedResult(outerExpressionResult),
|
||||
new ExpressionStub()
|
||||
// {{ innerExpression }}
|
||||
.withPosition(35, 57)
|
||||
// Parser would hit both expressions as one is hardcoded in other
|
||||
.withEvaluatedResult(innerExpressionResult),
|
||||
])
|
||||
.withResult(expectedCodeAfterFirstCompilationRound, [
|
||||
new ExpressionStub()
|
||||
// {{ innerExpression }}
|
||||
.withPosition(8, 29)
|
||||
// once the outer expression parser, compiler now parses its evaluated result
|
||||
.withEvaluatedResult(innerExpressionResult),
|
||||
]);
|
||||
const sut = new SystemUnderTest(expressionParserMock);
|
||||
const args = new FunctionCallArgumentCollectionStub();
|
||||
// act
|
||||
const actual = sut.compileExpressions(rawCode, args);
|
||||
// assert
|
||||
expect(actual).to.equal(expectedResult);
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('combines expressions as expected', () => {
|
||||
// arrange
|
||||
const code = 'part1 {{ a }} part2 {{ b }} part3';
|
||||
@@ -60,7 +154,7 @@ describe('ExpressionsCompiler', () => {
|
||||
for (const testCase of testCases) {
|
||||
it(testCase.name, () => {
|
||||
const expressionParserMock = new ExpressionParserStub()
|
||||
.withResult(testCase.expressions);
|
||||
.withResult(code, testCase.expressions);
|
||||
const args = new FunctionCallArgumentCollectionStub();
|
||||
const sut = new SystemUnderTest(expressionParserMock);
|
||||
// act
|
||||
@@ -75,21 +169,26 @@ describe('ExpressionsCompiler', () => {
|
||||
// arrange
|
||||
const expected = new FunctionCallArgumentCollectionStub()
|
||||
.withArgument('test-arg', 'test-value');
|
||||
const code = 'non-important';
|
||||
const code = 'longer than 6 characters';
|
||||
const expressions = [
|
||||
new ExpressionStub(),
|
||||
new ExpressionStub(),
|
||||
new ExpressionStub().withPosition(0, 3),
|
||||
new ExpressionStub().withPosition(3, 6),
|
||||
];
|
||||
const expressionParserMock = new ExpressionParserStub()
|
||||
.withResult(expressions);
|
||||
.withResult(code, expressions);
|
||||
const sut = new SystemUnderTest(expressionParserMock);
|
||||
// act
|
||||
sut.compileExpressions(code, expected);
|
||||
// assert
|
||||
expect(expressions[0].callHistory).to.have.lengthOf(1);
|
||||
expect(expressions[0].callHistory[0].args).to.equal(expected);
|
||||
expect(expressions[1].callHistory).to.have.lengthOf(1);
|
||||
expect(expressions[1].callHistory[0].args).to.equal(expected);
|
||||
const actualArgs = expressions
|
||||
.flatMap((expression) => expression.callHistory)
|
||||
.map((context) => context.args);
|
||||
expect(
|
||||
actualArgs.every((arg) => arg === expected),
|
||||
`Expected: ${JSON.stringify(expected)}\n`
|
||||
+ `Actual: ${JSON.stringify(actualArgs)}\n`
|
||||
+ `Not equal: ${actualArgs.filter((arg) => arg !== expected)}`,
|
||||
);
|
||||
});
|
||||
describe('throws if arguments is missing', () => {
|
||||
itEachAbsentObjectValue((absentValue) => {
|
||||
@@ -105,66 +204,122 @@ describe('ExpressionsCompiler', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('throws when expected argument is not provided but used in code', () => {
|
||||
// arrange
|
||||
const testCases = [
|
||||
{
|
||||
name: 'empty parameters',
|
||||
expressions: [
|
||||
new ExpressionStub().withParameterNames(['parameter'], false),
|
||||
],
|
||||
args: new FunctionCallArgumentCollectionStub(),
|
||||
expectedError: 'parameter value(s) not provided for: "parameter" but used in code',
|
||||
},
|
||||
{
|
||||
name: 'unnecessary parameter is provided',
|
||||
expressions: [
|
||||
new ExpressionStub().withParameterNames(['parameter'], false),
|
||||
],
|
||||
args: new FunctionCallArgumentCollectionStub()
|
||||
.withArgument('unnecessaryParameter', 'unnecessaryValue'),
|
||||
expectedError: 'parameter value(s) not provided for: "parameter" but used in code',
|
||||
},
|
||||
{
|
||||
name: 'multiple values are not provided',
|
||||
expressions: [
|
||||
new ExpressionStub().withParameterNames(['parameter1'], false),
|
||||
new ExpressionStub().withParameterNames(['parameter2', 'parameter3'], false),
|
||||
],
|
||||
args: new FunctionCallArgumentCollectionStub(),
|
||||
expectedError: 'parameter value(s) not provided for: "parameter1", "parameter2", "parameter3" but used in code',
|
||||
},
|
||||
{
|
||||
name: 'some values are provided',
|
||||
expressions: [
|
||||
new ExpressionStub().withParameterNames(['parameter1'], false),
|
||||
new ExpressionStub().withParameterNames(['parameter2', 'parameter3'], false),
|
||||
],
|
||||
args: new FunctionCallArgumentCollectionStub()
|
||||
.withArgument('parameter2', 'value'),
|
||||
expectedError: 'parameter value(s) not provided for: "parameter1", "parameter3" but used in code',
|
||||
},
|
||||
{
|
||||
name: 'parameter names are not repeated in error message',
|
||||
expressions: [
|
||||
new ExpressionStub().withParameterNames(['parameter1', 'parameter1', 'parameter2', 'parameter2'], false),
|
||||
],
|
||||
args: new FunctionCallArgumentCollectionStub(),
|
||||
expectedError: 'parameter value(s) not provided for: "parameter1", "parameter2" but used in code',
|
||||
},
|
||||
];
|
||||
for (const testCase of testCases) {
|
||||
it(testCase.name, () => {
|
||||
const code = 'non-important-code';
|
||||
const expressionParserMock = new ExpressionParserStub()
|
||||
.withResult(testCase.expressions);
|
||||
const sut = new SystemUnderTest(expressionParserMock);
|
||||
// act
|
||||
const act = () => sut.compileExpressions(code, testCase.args);
|
||||
// assert
|
||||
expect(act).to.throw(testCase.expectedError);
|
||||
});
|
||||
}
|
||||
describe('throws when expressions are invalid', () => {
|
||||
describe('throws when expected argument is not provided but used in code', () => {
|
||||
// arrange
|
||||
const testCases = [
|
||||
{
|
||||
name: 'empty parameters',
|
||||
expressions: [
|
||||
new ExpressionStub().withParameterNames(['parameter'], false),
|
||||
],
|
||||
args: new FunctionCallArgumentCollectionStub(),
|
||||
expectedError: 'parameter value(s) not provided for: "parameter" but used in code',
|
||||
},
|
||||
{
|
||||
name: 'unnecessary parameter is provided',
|
||||
expressions: [
|
||||
new ExpressionStub().withParameterNames(['parameter'], false),
|
||||
],
|
||||
args: new FunctionCallArgumentCollectionStub()
|
||||
.withArgument('unnecessaryParameter', 'unnecessaryValue'),
|
||||
expectedError: 'parameter value(s) not provided for: "parameter" but used in code',
|
||||
},
|
||||
{
|
||||
name: 'multiple values are not provided',
|
||||
expressions: [
|
||||
new ExpressionStub().withParameterNames(['parameter1'], false),
|
||||
new ExpressionStub().withParameterNames(['parameter2', 'parameter3'], false),
|
||||
],
|
||||
args: new FunctionCallArgumentCollectionStub(),
|
||||
expectedError: 'parameter value(s) not provided for: "parameter1", "parameter2", "parameter3" but used in code',
|
||||
},
|
||||
{
|
||||
name: 'some values are provided',
|
||||
expressions: [
|
||||
new ExpressionStub().withParameterNames(['parameter1'], false),
|
||||
new ExpressionStub().withParameterNames(['parameter2', 'parameter3'], false),
|
||||
],
|
||||
args: new FunctionCallArgumentCollectionStub()
|
||||
.withArgument('parameter2', 'value'),
|
||||
expectedError: 'parameter value(s) not provided for: "parameter1", "parameter3" but used in code',
|
||||
},
|
||||
{
|
||||
name: 'parameter names are not repeated in error message',
|
||||
expressions: [
|
||||
new ExpressionStub().withParameterNames(['parameter1', 'parameter1', 'parameter2', 'parameter2'], false),
|
||||
],
|
||||
args: new FunctionCallArgumentCollectionStub(),
|
||||
expectedError: 'parameter value(s) not provided for: "parameter1", "parameter2" but used in code',
|
||||
},
|
||||
];
|
||||
for (const testCase of testCases) {
|
||||
it(testCase.name, () => {
|
||||
const code = 'non-important-code';
|
||||
const expressionParserMock = new ExpressionParserStub()
|
||||
.withResult(code, testCase.expressions);
|
||||
const sut = new SystemUnderTest(expressionParserMock);
|
||||
// act
|
||||
const act = () => sut.compileExpressions(code, testCase.args);
|
||||
// assert
|
||||
expect(act).to.throw(testCase.expectedError);
|
||||
});
|
||||
}
|
||||
});
|
||||
describe('throws when expression positions are unexpected', () => {
|
||||
// arrange
|
||||
const code = 'c'.repeat(30);
|
||||
const testCases: readonly {
|
||||
name: string,
|
||||
expressions: readonly IExpression[],
|
||||
expectedError: string,
|
||||
expectedResult: boolean,
|
||||
}[] = [
|
||||
(() => {
|
||||
const badExpression = new ExpressionStub().withPosition(0, code.length + 5);
|
||||
const goodExpression = new ExpressionStub().withPosition(0, code.length - 1);
|
||||
return {
|
||||
name: 'an expression has out-of-range position',
|
||||
expressions: [badExpression, goodExpression],
|
||||
expectedError: `Expressions out of range:\n${JSON.stringify([badExpression])}`,
|
||||
expectedResult: true,
|
||||
};
|
||||
})(),
|
||||
(() => {
|
||||
const duplicatedExpression = new ExpressionStub().withPosition(0, code.length - 1);
|
||||
const uniqueExpression = new ExpressionStub().withPosition(0, code.length - 2);
|
||||
return {
|
||||
name: 'two expressions at the same position',
|
||||
expressions: [duplicatedExpression, duplicatedExpression, uniqueExpression],
|
||||
expectedError: `Instructions at same position:\n${JSON.stringify([duplicatedExpression, duplicatedExpression])}`,
|
||||
expectedResult: true,
|
||||
};
|
||||
})(),
|
||||
(() => {
|
||||
const goodExpression = new ExpressionStub().withPosition(0, 5);
|
||||
const intersectingExpression = new ExpressionStub().withPosition(5, 10);
|
||||
const intersectingExpressionOther = new ExpressionStub().withPosition(7, 12);
|
||||
return {
|
||||
name: 'intersecting expressions',
|
||||
expressions: [goodExpression, intersectingExpression, intersectingExpressionOther],
|
||||
expectedError: `Instructions intersecting unexpectedly:\n${JSON.stringify([intersectingExpression, intersectingExpressionOther])}`,
|
||||
expectedResult: true,
|
||||
};
|
||||
})(),
|
||||
];
|
||||
for (const testCase of testCases) {
|
||||
it(testCase.name, () => {
|
||||
const expressionParserMock = new ExpressionParserStub()
|
||||
.withResult(code, testCase.expressions);
|
||||
const sut = new SystemUnderTest(expressionParserMock);
|
||||
const args = new FunctionCallArgumentCollectionStub();
|
||||
// act
|
||||
const act = () => sut.compileExpressions(code, args);
|
||||
// assert
|
||||
expect(act).to.throw(testCase.expectedError);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
it('calls parser with expected code', () => {
|
||||
// arrange
|
||||
|
||||
@@ -4,15 +4,25 @@ import { IExpressionParser } from '@/application/Parser/Script/Compiler/Expressi
|
||||
export class ExpressionParserStub implements IExpressionParser {
|
||||
public callHistory = new Array<string>();
|
||||
|
||||
private result: IExpression[] = [];
|
||||
private results = new Map<string, readonly IExpression[]>();
|
||||
|
||||
public withResult(result: IExpression[]) {
|
||||
this.result = result;
|
||||
public withResult(code: string, result: readonly IExpression[]) {
|
||||
if (this.results.has(code)) {
|
||||
throw new Error(
|
||||
'Result for code is already registered.'
|
||||
+ `\nCode: ${code}`
|
||||
+ `\nResult: ${JSON.stringify(result)}`,
|
||||
);
|
||||
}
|
||||
this.results.set(code, result);
|
||||
return this;
|
||||
}
|
||||
|
||||
public findExpressions(code: string): IExpression[] {
|
||||
this.callHistory.push(code);
|
||||
return this.result;
|
||||
if (this.results.has(code)) {
|
||||
return [...this.results.get(code)];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ export class ExpressionStub implements IExpression {
|
||||
|
||||
public parameters: IReadOnlyFunctionParameterCollection = new FunctionParameterCollectionStub();
|
||||
|
||||
private result: string;
|
||||
private result: string = undefined;
|
||||
|
||||
public withParameters(parameters: IReadOnlyFunctionParameterCollection) {
|
||||
this.parameters = parameters;
|
||||
@@ -35,9 +35,11 @@ export class ExpressionStub implements IExpression {
|
||||
}
|
||||
|
||||
public evaluate(context: IExpressionEvaluationContext): string {
|
||||
const { args } = context;
|
||||
this.callHistory.push(context);
|
||||
const result = this.result || `[expression-stub] args: ${args ? Object.keys(args).map((key) => `${key}: ${args[key]}`).join('", "') : 'none'}`;
|
||||
return result;
|
||||
if (this.result === undefined /* not empty string */) {
|
||||
const { args } = context;
|
||||
return `[expression-stub] args: ${args ? Object.keys(args).map((key) => `${key}: ${args[key]}`).join('", "') : 'none'}`;
|
||||
}
|
||||
return this.result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user