Add "with" expression for templating #53

Allows optionally rendering content if an argument is given. The
expression is designed to be used with `optional` parameters.

Goal is to allow using `RunPowerShell` function on every function that
consists of PowerShell code. Before this commit, they were all required
to provide revertCode, or none of them could be able to have it. It
would not work because some scripts can be reverted, meanwhile some are
one-way scripts that cannot be reverted (such as cleaning scripts). In
this case a way to optionally render revertCode was required. `with`
expression give each callee script ability to turn off `revertCode` if
not needed, therefore enables using `RunPowerShell` everywhere.

This commit also improves error message for script code for better
debugging and refactors parser tests for more code reuse. It also adds
more tests to parameter substitution, and renames some tests of both
expressions for consistency.
This commit is contained in:
undergroundwires
2021-09-06 21:02:41 +01:00
parent 6c3c2e6709
commit 862914b06e
9 changed files with 321 additions and 67 deletions

View File

@@ -0,0 +1,49 @@
import 'mocha';
import { expect } from 'chai';
import { ExpressionPosition } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition';
import { IExpressionParser } from '@/application/Parser/Script/Compiler/Expressions/Parser/IExpressionParser';
import { FunctionCallArgumentCollectionStub } from '@tests/unit/stubs/FunctionCallArgumentCollectionStub';
export class SyntaxParserTestsRunner {
constructor(private readonly sut: IExpressionParser) {
}
public expectPosition(...testCases: IExpectPositionTestCase[]) {
for (const testCase of testCases) {
it(testCase.name, () => {
// act
const expressions = this.sut.findExpressions(testCase.code);
// assert
const actual = expressions.map((e) => e.position);
expect(actual).to.deep.equal(testCase.expected);
});
}
return this;
}
public expectResults(...testCases: IExpectResultTestCase[]) {
for (const testCase of testCases) {
it(testCase.name, () => {
// arrange
const args = testCase.args(new FunctionCallArgumentCollectionStub());
// act
const expressions = this.sut.findExpressions(testCase.code);
// assert
const actual = expressions.map((e) => e.evaluate(args));
expect(actual).to.deep.equal(testCase.expected);
});
}
return this;
}
}
interface IExpectResultTestCase {
name: string;
code: string;
args: (builder: FunctionCallArgumentCollectionStub) => FunctionCallArgumentCollectionStub;
expected: readonly string[];
}
interface IExpectPositionTestCase {
name: string;
code: string;
expected: readonly ExpressionPosition[];
}