Fix compiler bug with nested optional arguments

This commit fixes compiler bug where it fails when optional values are
compiled into absent values in nested calls.

- Throw exception with more context for easier future debugging.
- Add better validation of argument values for nested calls.
- Refactor `FunctionCallCompiler` for better clarity and modularize it
  to make it more maintainable and testable.
- Refactor related interface to not have `I` prefix, and
  function/variable names for better clarity.

Context:

Discovered this issue while attempting to call
`RunInlineCodeAsTrustedInstaller` which in turn invokes `RunPowerShell`
for issue #246. This led to the realization that despite parameters
flagged as optional, the nested argument compilation didn't support
them.
This commit is contained in:
undergroundwires
2023-09-16 16:11:41 +02:00
parent a1f2497381
commit 53222fd83c
49 changed files with 1938 additions and 772 deletions

View File

@@ -0,0 +1,81 @@
import { describe, it, expect } from 'vitest';
import { ParsedFunctionCall } from '@/application/Parser/Script/Compiler/Function/Call/ParsedFunctionCall';
import { IReadOnlyFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/Function/Call/Argument/IFunctionCallArgumentCollection';
import { FunctionCallArgumentCollectionStub } from '@tests/unit/shared/Stubs/FunctionCallArgumentCollectionStub';
import { itEachAbsentObjectValue, itEachAbsentStringValue } from '@tests/unit/shared/TestCases/AbsentTests';
describe('ParsedFunctionCall', () => {
describe('ctor', () => {
describe('args', () => {
describe('throws when args is missing', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing args';
const args = absentValue;
// act
const act = () => new FunctionCallBuilder()
.withArgs(args)
.build();
// assert
expect(act).to.throw(expectedError);
});
});
it('sets args as expected', () => {
// arrange
const expected = new FunctionCallArgumentCollectionStub()
.withArgument('testParameter', 'testValue');
// act
const sut = new FunctionCallBuilder()
.withArgs(expected)
.build();
// assert
expect(sut.args).to.deep.equal(expected);
});
});
describe('functionName', () => {
describe('throws when function name is missing', () => {
itEachAbsentStringValue((absentValue) => {
// arrange
const expectedError = 'missing function name in function call';
const functionName = absentValue;
// act
const act = () => new FunctionCallBuilder()
.withFunctionName(functionName)
.build();
// assert
expect(act).to.throw(expectedError);
});
});
it('sets function name as expected', () => {
// arrange
const expected = 'expectedFunctionName';
// act
const sut = new FunctionCallBuilder()
.withFunctionName(expected)
.build();
// assert
expect(sut.functionName).to.equal(expected);
});
});
});
});
class FunctionCallBuilder {
private functionName = 'functionName';
private args: IReadOnlyFunctionCallArgumentCollection = new FunctionCallArgumentCollectionStub();
public withFunctionName(functionName: string) {
this.functionName = functionName;
return this;
}
public withArgs(args: IReadOnlyFunctionCallArgumentCollection) {
this.args = args;
return this;
}
public build() {
return new ParsedFunctionCall(this.functionName, this.args);
}
}