From 8f8dcf4b516517d444266dc3e33ceda53730240e Mon Sep 17 00:00:00 2001 From: Eric Crooks Date: Thu, 8 Dec 2022 22:53:02 -0500 Subject: [PATCH] chore: cleanup --- src/interfaces.ts | 1 + src/pre_programmed_method.ts | 170 ----------------------------------- 2 files changed, 1 insertion(+), 170 deletions(-) delete mode 100644 src/pre_programmed_method.ts diff --git a/src/interfaces.ts b/src/interfaces.ts index a1e722c..b94b41e 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -100,6 +100,7 @@ export interface IMethodExpectation { //////////////////////////////////////////////////////////////////////////////// export interface IMethodChanger { + // TODO(crookse) Think about introducing this as an alias to willReturn(). // willCall(args: T): void; /** diff --git a/src/pre_programmed_method.ts b/src/pre_programmed_method.ts deleted file mode 100644 index 7d357b9..0000000 --- a/src/pre_programmed_method.ts +++ /dev/null @@ -1,170 +0,0 @@ -import type { IError, IMethodChanger } from "./interfaces.ts"; -import type { MethodOf } from "./types.ts"; - -class PreProgrammedMethodError extends Error {} - -type ReturnValueFunction = (...args: unknown[]) => ReturnValue; - -/** - * Class that allows to be a "stand-in" for a method. For example, when used in - * a mock object, the mock object can replace methods with pre-programmed - * methods (using this class), and have a system under test use the - * pre-programmed methods. - */ -export class PreProgrammedMethod - implements IMethodChanger { - /** - * The original name of the method being pre-programmed. - */ - #method_name: MethodOf; - - /** - * The object containing the pre-programmed setup details. This object is what - * runs under the hood to provide the pre-programmed expectation. - */ - #method_setup?: MethodSetup; - - ////////////////////////////////////////////////////////////////////////////// - // FILE MARKER - CONSTRUCTOR ///////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////////// - - /** - * @param methodName - The name of the method to program. Must be a method of - * the original object in question. - */ - constructor(methodName: MethodOf) { - this.#method_name = methodName; - } - - ////////////////////////////////////////////////////////////////////////////// - // FILE MARKER - METHODS - PUBLIC /////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////////// - - // public willCall(action: (...args: unknown[]) => ReturnValue): void { - // this.#method_setup = new MethodSetupCallsCallback( - // this.#method_name, - // action, - // ); - // } - - // public willReturn(action: (...args: unknown[]) => ReturnValue): void; - - public willReturn( - returnValue: ReturnValue | ReturnValueFunction, - ): void { - if (typeof returnValue === "function") { - this.#method_setup = new MethodSetupCallsCallback( - this.#method_name, - returnValue as ReturnValueFunction, - ); - return; - } - - this.#method_setup = new MethodSetupReturnsStaticValue( - this.#method_name, - returnValue, - ); - } - - public willThrow(error: IError): void { - this.#method_setup = new MethodSetupThrowsError( - this.#method_name, - error, - ); - } - - /** - * Run this method. - * @param args - * @returns - */ - public run(args?: unknown[]): unknown { - if (!this.#method_setup) { - throw new Error( - `Pre-programmed method "${ - String(this.#method_name) - }" does not have a return value.`, - ); - } - return this.#method_setup?.run(args); - } -} - -enum MethodSetupExpectation { - ExecuteCallback, - ReturnStaticValue, - ThrowError, -} - -/** - * Class to hold information about a specific pre-programmed method setup. - */ -abstract class MethodSetup { - readonly id: string; - protected expectation: MethodSetupExpectation; - protected method_name: string; - - constructor( - methodName: MethodOf, - expectation: MethodSetupExpectation, - ) { - this.id = this.run.toString(); - this.method_name = String(methodName); // Make the method name type-safe - this.expectation = expectation; - } - - abstract run(args?: unknown): unknown; -} - -class MethodSetupThrowsError - extends MethodSetup { - #error: IError; - - constructor( - methodName: MethodOf, - returnValue: IError, - ) { - super(methodName, MethodSetupExpectation.ThrowError); - this.#error = returnValue; - } - - run(): void { - throw this.#error; - } -} - -class MethodSetupReturnsStaticValue - extends MethodSetup { - #return_value: ReturnValue; - - constructor( - methodName: MethodOf, - returnValue: ReturnValue, - ) { - super(methodName, MethodSetupExpectation.ReturnStaticValue); - this.#return_value = returnValue; - } - - run(): ReturnValue { - return this.#return_value; - } -} - -class MethodSetupCallsCallback< - OriginalObject, - Callback extends ((...args: unknown[]) => ReturnType), -> extends MethodSetup { - #callback: Callback; - - constructor( - methodName: MethodOf, - callback: Callback, - ) { - super(methodName, MethodSetupExpectation.ReturnStaticValue); - this.#callback = callback; - } - - run(args: unknown[]): ReturnType { - return this.#callback(...args); - } -}