Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Johanblumenberg matcher types #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/MethodStubVerificator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {MethodToStub} from "./MethodToStub";
import {MethodCallToStringConverter} from "./utils/MethodCallToStringConverter";

export class MethodStubVerificator<T> {
export class MethodStubVerificator {
private methodCallToStringConverter: MethodCallToStringConverter = new MethodCallToStringConverter();

constructor(private methodToVerify: MethodToStub) {
Expand Down
15 changes: 15 additions & 0 deletions src/matcher/type/EndsWithMatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Matcher } from "./Matcher";

export class EndsWithMatcher extends Matcher {
constructor(private expectedValue: string) {
super();
}

public match(value: string): boolean {
return value && (typeof value === "string") && value.endsWith(this.expectedValue);
}

public toString(): string {
return `endsWith(${this.expectedValue})`;
}
}
15 changes: 15 additions & 0 deletions src/matcher/type/StartsWithMatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Matcher } from "./Matcher";

export class StartsWithMatcher extends Matcher {
constructor(private expectedValue: string) {
super();
}

public match(value: string): boolean {
return value && (typeof value === "string") && value.startsWith(this.expectedValue);
}

public toString(): string {
return `startsWith(${this.expectedValue})`;
}
}
2 changes: 1 addition & 1 deletion src/matcher/type/StrictEqualMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class StrictEqualMatcher extends Matcher {
super();
}

public match(value: any): boolean {
public match<T>(value: T): boolean {
return this.expectedValue === value;
}

Expand Down
57 changes: 35 additions & 22 deletions src/ts-mockito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ import {AnyStringMatcher} from "./matcher/type/AnyStringMatcher";
import {AnythingMatcher} from "./matcher/type/AnythingMatcher";
import {BetweenMatcher} from "./matcher/type/BetweenMatcher";
import {DeepEqualMatcher} from "./matcher/type/DeepEqualMatcher";
import {EndsWithMatcher} from "./matcher/type/EndsWithMatcher";
import {Matcher} from "./matcher/type/Matcher";
import {MatchingStringMatcher} from "./matcher/type/MatchingStringMatcher";
import {NotNullMatcher} from "./matcher/type/NotNullMatcher";
import {ObjectContainingMatcher} from "./matcher/type/ObjectContainingMatcher";
import {StartsWithMatcher} from "./matcher/type/StartsWithMatcher";
import {StrictEqualMatcher} from "./matcher/type/StrictEqualMatcher";
import {MethodStubSetter} from "./MethodStubSetter";
import {MethodStubVerificator} from "./MethodStubVerificator";
Expand All @@ -38,7 +41,7 @@ export function mock<T>(clazz?: any): T {
return new Mocker(clazz).getMock();
}

export function verify<T>(method: T): MethodStubVerificator<T> {
export function verify<T>(method: T): MethodStubVerificator {
return new MethodStubVerificator(method as any);
}

Expand Down Expand Up @@ -81,48 +84,56 @@ export function resetCalls<T>(...mockedValues: T[]): void {
mockedValues.forEach(mockedValue => (mockedValue as any).__tsmockitoMocker.resetCalls());
}

export function anyOfClass<T>(expectedClass: new (...args: any[]) => T): any {
return new AnyOfClassMatcher(expectedClass) as any;
export function anyOfClass<T>(expectedClass: new (...args: any[]) => T): Matcher {
return new AnyOfClassMatcher(expectedClass);
}

export function anyFunction(): any {
return new AnyFunctionMatcher() as any;
export function anyFunction(): Matcher {
return new AnyFunctionMatcher();
}

export function anyNumber(): any {
return new AnyNumberMatcher() as any;
export function anyNumber(): Matcher {
return new AnyNumberMatcher();
}

export function anyString(): any {
return new AnyStringMatcher() as any;
export function anyString(): Matcher {
return new AnyStringMatcher();
}

export function anything(): any {
return new AnythingMatcher() as any;
return new AnythingMatcher();
}

export function between(min: number, max: number): any {
return new BetweenMatcher(min, max) as any;
export function between(min: number, max: number): Matcher {
return new BetweenMatcher(min, max);
}

export function deepEqual<T>(expectedValue: T): T {
return new DeepEqualMatcher<T>(expectedValue) as any;
export function deepEqual<T>(expectedValue: T): DeepEqualMatcher<T> {
return new DeepEqualMatcher<T>(expectedValue);
}

export function notNull(): any {
return new NotNullMatcher() as any;
export function notNull(): Matcher {
return new NotNullMatcher();
}

export function strictEqual(expectedValue: any): any {
return new StrictEqualMatcher(expectedValue) as any;
export function strictEqual<T>(expectedValue: T): Matcher {
return new StrictEqualMatcher(expectedValue);
}

export function match(expectedValue: RegExp | string): any {
return new MatchingStringMatcher(expectedValue) as any;
export function match(expectedValue: RegExp | string): Matcher {
return new MatchingStringMatcher(expectedValue);
}

export function objectContaining(expectedValue: Object): any {
return new ObjectContainingMatcher(expectedValue) as any;
export function startsWith(expectedValue: string): Matcher {
return new StartsWithMatcher(expectedValue);
}

export function endsWith(expectedValue: string): Matcher {
return new EndsWithMatcher(expectedValue);
}

export function objectContaining(expectedValue: Object): Matcher {
return new ObjectContainingMatcher(expectedValue);
}

// Export default object with all members (ember-browserify doesn't support named exports).
Expand All @@ -145,5 +156,7 @@ export default {
notNull,
strictEqual,
match,
startsWith,
endsWith,
objectContaining,
};
2 changes: 1 addition & 1 deletion test/matcher/type/DeepEqualMatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe("deepEqual", () => {
}
const foo = mock(Foo);
instance(foo).add("1", 2, {a: "sampleValue"});
verify(foo.add(deepEqual("1"), deepEqual(2), deepEqual({a: "sampleValue"}))).once();
verify(foo.add(deepEqual("1") as any, deepEqual(2) as any, deepEqual({a: "sampleValue"}) as any)).once();
});
});
});
32 changes: 32 additions & 0 deletions test/matcher/type/EndsWithMatcher.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {Matcher} from "../../../src/matcher/type/Matcher";
import {endsWith} from "../../../src/ts-mockito";

describe("EndsWithMatcher", () => {
describe("checking if value starts with given value", () => {
const testObj: Matcher = endsWith("abc");

describe("when given value ends with string", () => {
it("returns true", () => {
// when
const result = testObj.match("123 abc");

// then
expect(result).toBeTruthy();
});

it("describes the matcher", () => {
expect(testObj.toString()).toEqual("endsWith(abc)");
});
});

describe("when given value doesn\'t end with string", () => {
it("returns false", () => {
// when
const result = testObj.match("abc 123");

// then
expect(result).toBeFalsy();
});
});
});
});
32 changes: 32 additions & 0 deletions test/matcher/type/StartsWithMatcher.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {Matcher} from "../../../src/matcher/type/Matcher";
import {startsWith} from "../../../src/ts-mockito";

describe("StartsWithMatcher", () => {
describe("checking if value starts with given value", () => {
const testObj: Matcher = startsWith("abc");

describe("when given value starts with string", () => {
it("returns true", () => {
// when
const result = testObj.match("abc 123");

// then
expect(result).toBeTruthy();
});

it("describes the matcher", () => {
expect(testObj.toString()).toEqual("startsWith(abc)");
});
});

describe("when given value doesn\'t start with string", () => {
it("returns false", () => {
// when
const result = testObj.match("123 abc");

// then
expect(result).toBeFalsy();
});
});
});
});