Files
privacy.sexy/tests/unit/shared/Stubs/ExpressionStub.ts
undergroundwires 68a5d698a2 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.
2022-10-11 20:42:38 +02:00

46 lines
1.8 KiB
TypeScript

import { ExpressionPosition } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition';
import { IExpression } from '@/application/Parser/Script/Compiler/Expressions/Expression/IExpression';
import { IReadOnlyFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection';
import { IExpressionEvaluationContext } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionEvaluationContext';
import { FunctionParameterCollectionStub } from './FunctionParameterCollectionStub';
export class ExpressionStub implements IExpression {
public callHistory = new Array<IExpressionEvaluationContext>();
public position = new ExpressionPosition(0, 5);
public parameters: IReadOnlyFunctionParameterCollection = new FunctionParameterCollectionStub();
private result: string = undefined;
public withParameters(parameters: IReadOnlyFunctionParameterCollection) {
this.parameters = parameters;
return this;
}
public withParameterNames(parameterNames: readonly string[], isOptional = false) {
const collection = new FunctionParameterCollectionStub()
.withParameterNames(parameterNames, isOptional);
return this.withParameters(collection);
}
public withPosition(start: number, end: number) {
this.position = new ExpressionPosition(start, end);
return this;
}
public withEvaluatedResult(result: string) {
this.result = result;
return this;
}
public evaluate(context: IExpressionEvaluationContext): string {
this.callHistory.push(context);
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;
}
}