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:
47
tests/unit/shared/Stubs/SingleCallCompilerStub.ts
Normal file
47
tests/unit/shared/Stubs/SingleCallCompilerStub.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { CompiledCode } from '@/application/Parser/Script/Compiler/Function/Call/Compiler/CompiledCode';
|
||||
import { FunctionCallCompilationContext } from '@/application/Parser/Script/Compiler/Function/Call/Compiler/FunctionCallCompilationContext';
|
||||
import { SingleCallCompiler } from '@/application/Parser/Script/Compiler/Function/Call/Compiler/SingleCall/SingleCallCompiler';
|
||||
import { FunctionCall } from '@/application/Parser/Script/Compiler/Function/Call/FunctionCall';
|
||||
import { StubWithObservableMethodCalls } from './StubWithObservableMethodCalls';
|
||||
import { CompiledCodeStub } from './CompiledCodeStub';
|
||||
|
||||
interface CallCompilationScenario {
|
||||
readonly givenCall: FunctionCall;
|
||||
readonly result: CompiledCode[];
|
||||
}
|
||||
|
||||
export class SingleCallCompilerStub
|
||||
extends StubWithObservableMethodCalls<SingleCallCompiler>
|
||||
implements SingleCallCompiler {
|
||||
private readonly callCompilationScenarios = new Array<CallCompilationScenario>();
|
||||
|
||||
public withCallCompilationScenarios(scenarios: Map<FunctionCall, CompiledCode[]>): this {
|
||||
for (const [call, result] of scenarios) {
|
||||
this.withCallCompilationScenario({
|
||||
givenCall: call,
|
||||
result,
|
||||
});
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public withCallCompilationScenario(scenario: CallCompilationScenario): this {
|
||||
this.callCompilationScenarios.push(scenario);
|
||||
return this;
|
||||
}
|
||||
|
||||
public compileSingleCall(
|
||||
call: FunctionCall,
|
||||
context: FunctionCallCompilationContext,
|
||||
): CompiledCode[] {
|
||||
this.registerMethodCall({
|
||||
methodName: 'compileSingleCall',
|
||||
args: [call, context],
|
||||
});
|
||||
const callCompilation = this.callCompilationScenarios.find((s) => s.givenCall === call);
|
||||
if (callCompilation) {
|
||||
return callCompilation.result;
|
||||
}
|
||||
return [new CompiledCodeStub()];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user