Skip to content

Commit

Permalink
chore: DRYing verifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
crookse committed Apr 24, 2022
1 parent a36d20b commit 6789397
Show file tree
Hide file tree
Showing 11 changed files with 589 additions and 333 deletions.
10 changes: 6 additions & 4 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Callable, Constructor, MethodOf, StubReturnValue } from "./src/types.ts";
import type { Constructor, MethodOf } from "./src/types.ts";
import { MockBuilder } from "./src/mock/mock_builder.ts";
import { FakeBuilder } from "./src/fake/fake_builder.ts";
import { SpyBuilder } from "./src/spy/spy_builder.ts";
Expand Down Expand Up @@ -76,9 +76,11 @@ export function Mock<T>(constructorFn: Constructor<T>): MockBuilder<T> {
////////////////////////////////////////////////////////////////////////////////

export function Spy<ReturnValue>(
fn: (...args: unknown[]) => ReturnValue,
// deno-lint-ignore no-explicit-any
fn: (...args: any[]) => ReturnValue,
returnValue?: ReturnValue
): Interfaces.ISpyStubFunctionExpression & Callable<ReturnValue>;
// deno-lint-ignore no-explicit-any
): Interfaces.ISpyStubFunctionExpression & ((...args: any[]) => ReturnValue);

/**
* Create spy out of a class. Example:
Expand Down Expand Up @@ -203,7 +205,7 @@ export function Stub<OriginalObject, ReturnValue>(
obj: OriginalObject,
dataMember: keyof OriginalObject,
returnValue?: ReturnValue,
): StubReturnValue<OriginalObject, ReturnValue>;
): void;
/**
* Take the given object and stub its given data member to return the given
* return value.
Expand Down
13 changes: 10 additions & 3 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { MethodCalls, MethodOf } from "./types.ts";
import type { MethodOf, MockMethodCalls } from "./types.ts";

export interface IMethodExpectation {
toBeCalled(expectedCalls: number): void;
Expand All @@ -18,7 +18,7 @@ export interface IMethodVerification {
* @returns `this` To allow method chaining.
*/
toBeCalled(expectedCalls?: number): this;
toBeCalledWithArgs(...args: unknown[]): this;
toBeCalledWithArgs(firstArg: unknown, ...restOfArgs: unknown[]): this;
toBeCalledWithoutArgs(): this;
}

Expand Down Expand Up @@ -60,7 +60,14 @@ export interface IFake<OriginalObject> {
}

export interface IMock<OriginalObject> {
calls: MethodCalls<OriginalObject>;
/**
* Property to track method calls.
*/
calls: MockMethodCalls<OriginalObject>;

/**
* Helper property to see that this is a mock object and not the original.
*/
is_mock: boolean;

/**
Expand Down
206 changes: 0 additions & 206 deletions src/method_verifier.ts

This file was deleted.

31 changes: 8 additions & 23 deletions src/mock/mock_mixin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IMock } from "../interfaces.ts";
import type { Constructor, MethodCalls, MethodOf } from "../types.ts";
import type { Constructor, MethodOf, MockMethodCalls } from "../types.ts";

import { MethodVerifier } from "../method_verifier.ts";
import { MethodVerifier } from "../verifiers/method_verifier.ts";
import { MockError } from "../errors.ts";
import { PreProgrammedMethod } from "../pre_programmed_method.ts";

Expand All @@ -12,7 +12,7 @@ class MethodExpectation<OriginalObject> {
/**
* Property to hold the number of expected calls this method should receive.
*/
#expected_calls = 0;
#expected_calls?: number | undefined;

/**
* See `MethodVerifier#method_name`.
Expand Down Expand Up @@ -55,7 +55,7 @@ class MethodExpectation<OriginalObject> {
* to -1 to tell `MethodVerifier` to "just check that the method was called".
*/
public toBeCalled(expectedCalls?: number): void {
this.#expected_calls = expectedCalls ?? -1;
this.#expected_calls = expectedCalls;
}

// public verify(actualCalls: number, actualArgs: unknown[]): void {
Expand All @@ -68,24 +68,9 @@ class MethodExpectation<OriginalObject> {
* @param actualCalls - The number of actual calls.
*/
public verifyCalls(actualCalls: number): void {
// If the expected calls is -1, then we do not show anything in the third
// argument to `this.#verifier.toBeCalled()`. Reason being we want to show
// this in the error stack trace ...
//
// .expects("someMethod").toBeCalled()
//
// ... and not this ...
//
// .expects("someMethod").toBeCalled(-1)
//
const expectedCalls = this.#expected_calls !== -1
? ""
: `${this.#expected_calls}`;

this.#verifier.toBeCalled(
actualCalls,
this.#expected_calls,
`.expects("${this.#method_name}").toBeCalled(${expectedCalls})`,
);
}
}
Expand All @@ -106,14 +91,14 @@ export function createMock<OriginalConstructor, OriginalObject>(
>;
return new class MockExtension extends Original {
/**
* Helper property to see that this is a mock object and not the original.
* See `IMock#is_mock`.
*/
is_mock = true;

/**
* Property to track method calls.
* See `IMock#calls`.
*/
#calls!: MethodCalls<OriginalObject>;
#calls!: MockMethodCalls<OriginalObject>;

/**
* An array of expectations to verify (if any).
Expand All @@ -129,7 +114,7 @@ export function createMock<OriginalConstructor, OriginalObject>(
// FILE MARKER - GETTERS / SETTERS /////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

get calls(): MethodCalls<OriginalObject> {
get calls(): MockMethodCalls<OriginalObject> {
return this.#calls;
}

Expand Down
2 changes: 1 addition & 1 deletion src/spy/spy_mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function createSpy<OriginalConstructor, OriginalObject>(
methodsToTrack.forEach((method: string) => {
const spyMethod = new SpyStubBuilder(this)
.method(method as MethodOf<this>)
.returnValue("stubbed")
.returnValue("spy-stubbed")
.createForObjectMethod();
stubbedMethods[method as MethodOf<OriginalObject>] = spyMethod;
});
Expand Down
Loading

0 comments on commit 6789397

Please sign in to comment.