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.
50 lines
1.8 KiB
TypeScript
50 lines
1.8 KiB
TypeScript
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[];
|
|
}
|