Files
privacy.sexy/tests/unit/stubs/ExpressionsCompilerStub.ts
undergroundwires 6a89c6224b Add optionality for parameters
This commit allows for parameters that does not require any arguments to
be provided in function calls. It changes collection syntax where
parameters are list of objects instead of primitive strings. A
parameter has now 'name' and 'optional' properties. 'name' is required
and used in same way as older strings as parameter definitions.
'Optional' property is optional, 'false' is the default behavior if
undefined. It also adds additional validation to restrict parameter
names to alphanumeric strings to have a clear syntax in expressions.
2021-09-02 18:59:25 +01:00

57 lines
2.2 KiB
TypeScript

import { IExpressionsCompiler } from '@/application/Parser/Script/Compiler/Expressions/IExpressionsCompiler';
import { IReadOnlyFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection';
import { scrambledEqual } from '@/application/Common/Array';
export class ExpressionsCompilerStub implements IExpressionsCompiler {
public readonly callHistory = new Array<{code: string, parameters: IReadOnlyFunctionCallArgumentCollection}>();
private readonly scenarios = new Array<ITestScenario>();
public setup(
code: string,
parameters: IReadOnlyFunctionCallArgumentCollection,
result: string): ExpressionsCompilerStub {
this.scenarios.push({ code, parameters, result });
return this;
}
public compileExpressions(
code: string,
parameters: IReadOnlyFunctionCallArgumentCollection): string {
this.callHistory.push({ code, parameters});
const scenario = this.scenarios.find((s) => s.code === code && deepEqual(s.parameters, parameters));
if (scenario) {
return scenario.result;
}
const parametersAndValues = parameters
.getAllParameterNames()
.map((name) => `${name}=${parameters.getArgument(name).argumentValue}`)
.join('", "');
return `[ExpressionsCompilerStub] code: "${code}" | parameters: "${parametersAndValues}"`;
}
}
interface ITestScenario {
readonly code: string;
readonly parameters: IReadOnlyFunctionCallArgumentCollection;
readonly result: string;
}
function deepEqual(
expected: IReadOnlyFunctionCallArgumentCollection,
actual: IReadOnlyFunctionCallArgumentCollection): boolean {
const expectedParameterNames = expected.getAllParameterNames();
const actualParameterNames = actual.getAllParameterNames();
if (!scrambledEqual(expectedParameterNames, actualParameterNames)) {
return false;
}
for (const parameterName of expectedParameterNames) {
const expectedValue = expected.getArgument(parameterName);
const actualValue = expected.getArgument(parameterName);
if (expectedValue !== actualValue) {
return false;
}
}
return true;
}