Files
privacy.sexy/tests/unit/shared/Stubs/ExpressionsCompilerStub.ts
undergroundwires 949fac1a7c Refactor to enforce strictNullChecks
This commit applies `strictNullChecks` to the entire codebase to improve
maintainability and type safety. Key changes include:

- Remove some explicit null-checks where unnecessary.
- Add necessary null-checks.
- Refactor static factory functions for a more functional approach.
- Improve some test names and contexts for better debugging.
- Add unit tests for any additional logic introduced.
- Refactor `createPositionFromRegexFullMatch` to its own function as the
  logic is reused.
- Prefer `find` prefix on functions that may return `undefined` and
  `get` prefix for those that always return a value.
2023-11-12 22:54:00 +01:00

82 lines
2.9 KiB
TypeScript

import { IExpressionsCompiler } from '@/application/Parser/Script/Compiler/Expressions/IExpressionsCompiler';
import { IReadOnlyFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/Function/Call/Argument/IFunctionCallArgumentCollection';
import { scrambledEqual } from '@/application/Common/Array';
import { FunctionBodyType, ISharedFunction } from '@/application/Parser/Script/Compiler/Function/ISharedFunction';
import { FunctionCallArgumentCollectionStub } from '@tests/unit/shared/Stubs/FunctionCallArgumentCollectionStub';
import { StubWithObservableMethodCalls } from './StubWithObservableMethodCalls';
export class ExpressionsCompilerStub
extends StubWithObservableMethodCalls<IExpressionsCompiler>
implements IExpressionsCompiler {
private readonly scenarios = new Array<ExpressionCompilationScenario>();
public setup(scenario: ExpressionCompilationScenario): this {
this.scenarios.push(scenario);
return this;
}
public setupToReturnFunctionCode(
func: ISharedFunction,
givenArgs: FunctionCallArgumentCollectionStub,
): this {
if (func.body.type !== FunctionBodyType.Code) {
throw new Error('not a code body');
}
if (func.body.code.revert) {
this.setup({
givenCode: func.body.code.revert,
givenArgs,
result: func.body.code.revert,
});
}
return this.setup({
givenCode: func.body.code.execute,
givenArgs,
result: func.body.code.execute,
});
}
public compileExpressions(
code: string,
parameters: IReadOnlyFunctionCallArgumentCollection,
): string {
this.registerMethodCall({
methodName: 'compileExpressions',
args: [code, parameters],
});
const scenario = this.scenarios.find(
(s) => s.givenCode === code && deepEqual(s.givenArgs, parameters),
);
if (scenario) {
return scenario.result;
}
const parametersAndValues = parameters
.getAllParameterNames()
.map((name) => `${name}=${parameters.getArgument(name).argumentValue}`)
.join('\n\t');
return `[${ExpressionsCompilerStub.name}]\ncode: "${code}"\nparameters: ${parametersAndValues}`;
}
}
interface ExpressionCompilationScenario {
readonly givenCode: string;
readonly givenArgs: 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;
}
return expectedParameterNames.every((parameterName) => {
const expectedValue = expected.getArgument(parameterName).argumentValue;
const actualValue = actual.getArgument(parameterName).argumentValue;
return expectedValue === actualValue;
});
}