Files
privacy.sexy/tests/unit/shared/Stubs/FunctionDataStub.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

92 lines
2.3 KiB
TypeScript

import type {
ParameterDefinitionData, CodeFunctionData,
FunctionCallsData, CallFunctionData,
} from '@/application/collections/';
import { FunctionCallDataStub } from './FunctionCallDataStub';
export function createFunctionDataWithCode(): FunctionDataStub {
const instance = new FunctionDataStub()
.withCode('stub-code')
.withRevertCode('stub-revert-code');
return instance;
}
export function createFunctionDataWithCall(
call?: FunctionCallsData,
): FunctionDataStub {
let instance = new FunctionDataStub();
if (call) {
instance = instance.withCall(call);
} else {
instance = instance.withMockCall();
}
return instance;
}
export function createFunctionDataWithoutCallOrCode(): FunctionDataStub {
return new FunctionDataStub();
}
interface FunctionDataBuilder<T> {
withName(name: string): T;
withParameters(...parameters: readonly ParameterDefinitionData[]): T;
withParametersObject(parameters: readonly ParameterDefinitionData[]): T;
}
interface CodeFunctionDataBuilder extends FunctionDataBuilder<CodeFunctionDataBuilder> {
withCode(code: string): this;
withRevertCode(revertCode: string): this;
}
interface CallFunctionDataBuilder extends FunctionDataBuilder<CallFunctionDataBuilder> {
withCall(call: FunctionCallsData): this;
withMockCall(): this;
}
class FunctionDataStub
implements CodeFunctionDataBuilder, CallFunctionDataBuilder, CallFunctionData, CodeFunctionData {
public name = 'functionDataStub';
public code: string;
public revertCode?: string;
public call: FunctionCallsData;
public parameters?: readonly ParameterDefinitionData[];
public withName(name: string) {
this.name = name;
return this;
}
public withParameters(...parameters: readonly ParameterDefinitionData[]) {
return this.withParametersObject(parameters);
}
public withParametersObject(parameters: readonly ParameterDefinitionData[]) {
this.parameters = parameters;
return this;
}
public withCode(code: string) {
this.code = code;
return this;
}
public withRevertCode(revertCode: string) {
this.revertCode = revertCode;
return this;
}
public withCall(call: FunctionCallsData) {
this.call = call;
return this;
}
public withMockCall() {
this.call = new FunctionCallDataStub();
return this;
}
}