diff --git a/src/MethodStubVerificator.ts b/src/MethodStubVerificator.ts index 6bfceb2..114cfac 100644 --- a/src/MethodStubVerificator.ts +++ b/src/MethodStubVerificator.ts @@ -1,7 +1,7 @@ import {MethodToStub} from "./MethodToStub"; import {MethodCallToStringConverter} from "./utils/MethodCallToStringConverter"; -export class MethodStubVerificator { +export class MethodStubVerificator { private methodCallToStringConverter: MethodCallToStringConverter = new MethodCallToStringConverter(); constructor(private methodToVerify: MethodToStub) { diff --git a/src/matcher/type/EndsWithMatcher.ts b/src/matcher/type/EndsWithMatcher.ts new file mode 100644 index 0000000..bfff5a7 --- /dev/null +++ b/src/matcher/type/EndsWithMatcher.ts @@ -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})`; + } +} diff --git a/src/matcher/type/StartsWithMatcher.ts b/src/matcher/type/StartsWithMatcher.ts new file mode 100644 index 0000000..854b870 --- /dev/null +++ b/src/matcher/type/StartsWithMatcher.ts @@ -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})`; + } +} diff --git a/src/matcher/type/StrictEqualMatcher.ts b/src/matcher/type/StrictEqualMatcher.ts index 593c2de..71d8c16 100644 --- a/src/matcher/type/StrictEqualMatcher.ts +++ b/src/matcher/type/StrictEqualMatcher.ts @@ -5,7 +5,7 @@ export class StrictEqualMatcher extends Matcher { super(); } - public match(value: any): boolean { + public match(value: T): boolean { return this.expectedValue === value; } diff --git a/src/ts-mockito.ts b/src/ts-mockito.ts index 5b37677..7895c4e 100644 --- a/src/ts-mockito.ts +++ b/src/ts-mockito.ts @@ -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"; @@ -38,7 +41,7 @@ export function mock(clazz?: any): T { return new Mocker(clazz).getMock(); } -export function verify(method: T): MethodStubVerificator { +export function verify(method: T): MethodStubVerificator { return new MethodStubVerificator(method as any); } @@ -81,48 +84,56 @@ export function resetCalls(...mockedValues: T[]): void { mockedValues.forEach(mockedValue => (mockedValue as any).__tsmockitoMocker.resetCalls()); } -export function anyOfClass(expectedClass: new (...args: any[]) => T): any { - return new AnyOfClassMatcher(expectedClass) as any; +export function anyOfClass(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(expectedValue: T): T { - return new DeepEqualMatcher(expectedValue) as any; +export function deepEqual(expectedValue: T): DeepEqualMatcher { + return new DeepEqualMatcher(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(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). @@ -145,5 +156,7 @@ export default { notNull, strictEqual, match, + startsWith, + endsWith, objectContaining, }; diff --git a/test/matcher/type/DeepEqualMatcher.spec.ts b/test/matcher/type/DeepEqualMatcher.spec.ts index 9d9c0f9..2d4320f 100644 --- a/test/matcher/type/DeepEqualMatcher.spec.ts +++ b/test/matcher/type/DeepEqualMatcher.spec.ts @@ -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(); }); }); }); diff --git a/test/matcher/type/EndsWithMatcher.spec.ts b/test/matcher/type/EndsWithMatcher.spec.ts new file mode 100644 index 0000000..2e988cc --- /dev/null +++ b/test/matcher/type/EndsWithMatcher.spec.ts @@ -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(); + }); + }); + }); +}); diff --git a/test/matcher/type/StartsWithMatcher.spec.ts b/test/matcher/type/StartsWithMatcher.spec.ts new file mode 100644 index 0000000..7b82843 --- /dev/null +++ b/test/matcher/type/StartsWithMatcher.spec.ts @@ -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(); + }); + }); + }); +});