From 4477bb1b771788d6795d16bab630beaa22449151 Mon Sep 17 00:00:00 2001 From: johanblumenberg Date: Mon, 6 May 2019 12:05:37 +0200 Subject: [PATCH 1/3] Add startsWith() and endsWith() string matchers --- src/matcher/type/EndsWithMatcher.ts | 15 ++++++++++ src/matcher/type/StartsWithMatcher.ts | 15 ++++++++++ src/ts-mockito.ts | 12 ++++++++ test/matcher/type/EndsWithMatcher.spec.ts | 32 +++++++++++++++++++++ test/matcher/type/StartsWithMatcher.spec.ts | 32 +++++++++++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 src/matcher/type/EndsWithMatcher.ts create mode 100644 src/matcher/type/StartsWithMatcher.ts create mode 100644 test/matcher/type/EndsWithMatcher.spec.ts create mode 100644 test/matcher/type/StartsWithMatcher.spec.ts diff --git a/src/matcher/type/EndsWithMatcher.ts b/src/matcher/type/EndsWithMatcher.ts new file mode 100644 index 0000000..c3b12ca --- /dev/null +++ b/src/matcher/type/EndsWithMatcher.ts @@ -0,0 +1,15 @@ +import { Matcher } from "./Matcher"; + +export class EndsWithMatcher extends Matcher { + constructor(private expectedValue: any) { + super(); + } + + public match(value: Object): 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..eb0a60b --- /dev/null +++ b/src/matcher/type/StartsWithMatcher.ts @@ -0,0 +1,15 @@ +import { Matcher } from "./Matcher"; + +export class StartsWithMatcher extends Matcher { + constructor(private expectedValue: any) { + super(); + } + + public match(value: Object): boolean { + return value && (typeof value === "string") && value.startsWith(this.expectedValue); + } + + public toString(): string { + return `startsWith(${this.expectedValue})`; + } +} diff --git a/src/ts-mockito.ts b/src/ts-mockito.ts index 7f2b162..67db4b7 100644 --- a/src/ts-mockito.ts +++ b/src/ts-mockito.ts @@ -18,9 +18,11 @@ 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 {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"; @@ -119,6 +121,14 @@ export function match(expectedValue: RegExp | string): any { return new MatchingStringMatcher(expectedValue) as any; } +export function startsWith(expectedValue: string): string { + return new StartsWithMatcher(expectedValue) as any; +} + +export function endsWith(expectedValue: string): string { + return new EndsWithMatcher(expectedValue) as any; +} + export function objectContaining(expectedValue: Object): any { return new ObjectContainingMatcher(expectedValue) as any; } @@ -143,5 +153,7 @@ export default { notNull, strictEqual, match, + startsWith, + endsWith, objectContaining, }; diff --git a/test/matcher/type/EndsWithMatcher.spec.ts b/test/matcher/type/EndsWithMatcher.spec.ts new file mode 100644 index 0000000..07e05f6 --- /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") as any; + + 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..672fb40 --- /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") as any; + + 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(); + }); + }); + }); +}); From e59b0c852deb484d5d3293f1fc676134de76437a Mon Sep 17 00:00:00 2001 From: johanblumenberg Date: Mon, 6 May 2019 12:07:16 +0200 Subject: [PATCH 2/3] Make matchers check type --- src/ts-mockito.ts | 14 +++++++------- test/MethodAction.spec.ts | 4 ++-- test/MethodStub.spec.ts | 12 ++++++------ test/matcher/type/AnyNumberMatcher.spec.ts | 10 +++++----- test/matcher/type/AnyStringMatcher.spec.ts | 8 ++++---- test/matcher/type/BetweenMatcher.spec.ts | 4 ++-- test/matcher/type/DeepEqualMatcher.spec.ts | 12 ++++++------ test/matcher/type/MatchingStringMatcher.spec.ts | 2 +- test/matcher/type/StrictEqualMatcher.spec.ts | 8 ++++---- 9 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/ts-mockito.ts b/src/ts-mockito.ts index 67db4b7..b109d01 100644 --- a/src/ts-mockito.ts +++ b/src/ts-mockito.ts @@ -89,11 +89,11 @@ export function anyFunction(): any { return new AnyFunctionMatcher() as any; } -export function anyNumber(): any { +export function anyNumber(): number { return new AnyNumberMatcher() as any; } -export function anyString(): any { +export function anyString(): string { return new AnyStringMatcher() as any; } @@ -101,23 +101,23 @@ export function anything(): any { return new AnythingMatcher() as any; } -export function between(min: number, max: number): any { +export function between(min: number, max: number): number { return new BetweenMatcher(min, max) as any; } -export function deepEqual(expectedValue: any): any { - return new DeepEqualMatcher(expectedValue); +export function deepEqual(expectedValue: T): T { + return new DeepEqualMatcher(expectedValue) as any; } export function notNull(): any { return new NotNullMatcher() as any; } -export function strictEqual(expectedValue: any): any { +export function strictEqual(expectedValue: T): T { return new StrictEqualMatcher(expectedValue) as any; } -export function match(expectedValue: RegExp | string): any { +export function match(expectedValue: RegExp | string): string { return new MatchingStringMatcher(expectedValue) as any; } diff --git a/test/MethodAction.spec.ts b/test/MethodAction.spec.ts index 618ace2..d080409 100644 --- a/test/MethodAction.spec.ts +++ b/test/MethodAction.spec.ts @@ -12,7 +12,7 @@ describe("MethodAction", () => { const testObj: MethodAction = new MethodAction(methodName, [firstArg, secondArg]); // when - const result = testObj.isApplicable(methodName, [strictEqual(firstArg), strictEqual(secondArg)]); + const result = testObj.isApplicable(methodName, [strictEqual(firstArg) as any, strictEqual(secondArg) as any]); // then expect(result).toBeTruthy(); @@ -28,7 +28,7 @@ describe("MethodAction", () => { const testObj: MethodAction = new MethodAction(methodName, [firstArg, notMatchingArg]); // when - const result = testObj.isApplicable(methodName, [strictEqual(firstArg), strictEqual(secondArg)]); + const result = testObj.isApplicable(methodName, [strictEqual(firstArg) as any, strictEqual(secondArg) as any]); // then expect(result).toBeFalsy(); diff --git a/test/MethodStub.spec.ts b/test/MethodStub.spec.ts index f5e71c8..37805d2 100644 --- a/test/MethodStub.spec.ts +++ b/test/MethodStub.spec.ts @@ -5,7 +5,7 @@ describe("ReturnValueMethodStub", () => { describe("checking if given arg is applicable", () => { it("returns true when arg match", () => { // given - const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(10)], 50); + const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(10) as any], 50); // when const result = testObj.isApplicable([10]); @@ -16,7 +16,7 @@ describe("ReturnValueMethodStub", () => { it("returns false when arg doesn't match", () => { // given - const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(10)], 50); + const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(10) as any], 50); // when const result = testObj.isApplicable([999]); @@ -31,7 +31,7 @@ describe("ReturnValueMethodStub", () => { // given const firstValue = 10; const secondValue = 20; - const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(firstValue), strictEqual(secondValue)], 50); + const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(firstValue) as any, strictEqual(secondValue) as any], 50); // when const result = testObj.isApplicable([firstValue, secondValue]); @@ -44,7 +44,7 @@ describe("ReturnValueMethodStub", () => { // given const firstValue = 10; const secondValue = 20; - const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(firstValue), strictEqual(secondValue)], 50); + const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(firstValue) as any, strictEqual(secondValue) as any], 50); // when const result = testObj.isApplicable([30, secondValue]); @@ -57,7 +57,7 @@ describe("ReturnValueMethodStub", () => { // given const firstValue = 10; const secondValue = 20; - const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(firstValue), strictEqual(secondValue)], 50); + const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(firstValue) as any, strictEqual(secondValue) as any], 50); // when const result = testObj.isApplicable([firstValue, 30]); @@ -70,7 +70,7 @@ describe("ReturnValueMethodStub", () => { // given const firstValue = 10; const secondValue = 20; - const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(firstValue), strictEqual(secondValue)], 50); + const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(firstValue) as any, strictEqual(secondValue) as any], 50); // when const result = testObj.isApplicable([30, 40]); diff --git a/test/matcher/type/AnyNumberMatcher.spec.ts b/test/matcher/type/AnyNumberMatcher.spec.ts index 0f214a9..cb89511 100644 --- a/test/matcher/type/AnyNumberMatcher.spec.ts +++ b/test/matcher/type/AnyNumberMatcher.spec.ts @@ -5,7 +5,7 @@ describe("AnyNumberMatcher", () => { describe("checking if positive number is matching", () => { it("returns true", () => { // given - const testObj: Matcher = anyNumber(); + const testObj: Matcher = anyNumber() as any; // when const result = testObj.match(3); @@ -18,7 +18,7 @@ describe("AnyNumberMatcher", () => { describe("checking if negative number is matching", () => { it("returns true", () => { // given - const testObj: Matcher = anyNumber(); + const testObj: Matcher = anyNumber() as any; // when const result = testObj.match(-3); @@ -31,7 +31,7 @@ describe("AnyNumberMatcher", () => { describe("checking if zero is matching", () => { it("returns true", () => { // given - const testObj: Matcher = anyNumber(); + const testObj: Matcher = anyNumber() as any; // when const result = testObj.match(0); @@ -44,7 +44,7 @@ describe("AnyNumberMatcher", () => { describe("checking if string representation of number is matching", () => { it("returns false", () => { // given - const testObj: Matcher = anyNumber(); + const testObj: Matcher = anyNumber() as any; // when const result = testObj.match("5"); @@ -57,7 +57,7 @@ describe("AnyNumberMatcher", () => { describe("checking if object is matching", () => { it("returns false", () => { // given - const testObj: Matcher = anyNumber(); + const testObj: Matcher = anyNumber() as any; // when const result = testObj.match({}); diff --git a/test/matcher/type/AnyStringMatcher.spec.ts b/test/matcher/type/AnyStringMatcher.spec.ts index 8904fc7..915b311 100644 --- a/test/matcher/type/AnyStringMatcher.spec.ts +++ b/test/matcher/type/AnyStringMatcher.spec.ts @@ -5,7 +5,7 @@ describe("AnyStringMatcher", () => { describe("checking if number matches", () => { it("returns false", () => { // given - const testObj: Matcher = anyString(); + const testObj: Matcher = anyString() as any; // when const result = testObj.match(3); @@ -18,7 +18,7 @@ describe("AnyStringMatcher", () => { describe("checking if object matches", () => { it("returns false", () => { // given - const testObj: Matcher = anyString(); + const testObj: Matcher = anyString() as any; // when const result = testObj.match({}); @@ -31,7 +31,7 @@ describe("AnyStringMatcher", () => { describe("checking if empty string matches", () => { it("returns true", () => { // given - const testObj: Matcher = anyString(); + const testObj: Matcher = anyString() as any; // when const result = testObj.match(""); @@ -44,7 +44,7 @@ describe("AnyStringMatcher", () => { describe("checking if sample string matches", () => { it("returns true", () => { // given - const testObj: Matcher = anyString(); + const testObj: Matcher = anyString() as any; // when const result = testObj.match("sampleString"); diff --git a/test/matcher/type/BetweenMatcher.spec.ts b/test/matcher/type/BetweenMatcher.spec.ts index 10c0708..194a309 100644 --- a/test/matcher/type/BetweenMatcher.spec.ts +++ b/test/matcher/type/BetweenMatcher.spec.ts @@ -3,7 +3,7 @@ import {between} from "../../../src/ts-mockito"; describe("BetweenMatcher", () => { describe("checking if value matches given min and max", () => { - const testObj: Matcher = between(5, 10); + const testObj: Matcher = between(5, 10) as any; describe("when given value is lower than min", () => { it("returns false", () => { @@ -61,7 +61,7 @@ describe("BetweenMatcher", () => { // when let error = null; try { - const testObj: Matcher = between(10, 9); + const testObj: Matcher = between(10, 9) as any; } catch (e) { error = e; } diff --git a/test/matcher/type/DeepEqualMatcher.spec.ts b/test/matcher/type/DeepEqualMatcher.spec.ts index 69a43af..8d0b1e2 100644 --- a/test/matcher/type/DeepEqualMatcher.spec.ts +++ b/test/matcher/type/DeepEqualMatcher.spec.ts @@ -7,7 +7,7 @@ describe("DeepEqualMatcher", () => { // given const firstValue = 3; const secondValue = 3; - const testObj: Matcher = deepEqual(firstValue); + const testObj: Matcher = deepEqual(firstValue) as any; // when const result = testObj.match(secondValue); @@ -22,7 +22,7 @@ describe("DeepEqualMatcher", () => { // given const firstValue = "sampleString"; const secondValue = "sampleString"; - const testObj: Matcher = deepEqual(firstValue); + const testObj: Matcher = deepEqual(firstValue) as any; // when const result = testObj.match(secondValue); @@ -37,7 +37,7 @@ describe("DeepEqualMatcher", () => { // given const firstValue = {a: 1, b: {c: 2}}; const secondValue = {a: 1, b: {c: 2}}; - const testObj: Matcher = deepEqual(firstValue); + const testObj: Matcher = deepEqual(firstValue) as any; // when const result = testObj.match(secondValue); @@ -52,7 +52,7 @@ describe("DeepEqualMatcher", () => { // given const firstValue = {a: 1, b: {c: 2}}; const secondValue = {a: 1, b: {c: 99999}}; - const testObj: Matcher = deepEqual(firstValue); + const testObj: Matcher = deepEqual(firstValue) as any; // when const result = testObj.match(secondValue); @@ -67,7 +67,7 @@ describe("DeepEqualMatcher", () => { // given const firstValue = {a: 1, b: anyString()}; const secondValue = {a: 1, b: "2"}; - const testObj: Matcher = deepEqual(firstValue); + const testObj: Matcher = deepEqual(firstValue) as any; // when const result = testObj.match(secondValue); @@ -80,7 +80,7 @@ describe("DeepEqualMatcher", () => { // given const firstValue = {a: 1, b: anyString()}; const secondValue = {a: 1, b: 2}; - const testObj: Matcher = deepEqual(firstValue); + const testObj: Matcher = deepEqual(firstValue) as any; // when const result = testObj.match(secondValue); diff --git a/test/matcher/type/MatchingStringMatcher.spec.ts b/test/matcher/type/MatchingStringMatcher.spec.ts index e4288d0..ec4bfd0 100644 --- a/test/matcher/type/MatchingStringMatcher.spec.ts +++ b/test/matcher/type/MatchingStringMatcher.spec.ts @@ -3,7 +3,7 @@ import {match} from "../../../src/ts-mockito"; describe("MatchingStringMatcher", () => { describe("checking if value matches given regexp", () => { - const testObj: Matcher = match(/\w123/); + const testObj: Matcher = match(/\w123/) as any; describe("when given value matches regexp", () => { it("returns true", () => { diff --git a/test/matcher/type/StrictEqualMatcher.spec.ts b/test/matcher/type/StrictEqualMatcher.spec.ts index dc56efe..9756881 100644 --- a/test/matcher/type/StrictEqualMatcher.spec.ts +++ b/test/matcher/type/StrictEqualMatcher.spec.ts @@ -5,7 +5,7 @@ describe("StrictEqualMatcher", () => { describe("checking if string representation of number matches with number", () => { it("returns false", () => { // given - const testObj: Matcher = strictEqual("5"); + const testObj: Matcher = strictEqual("5") as any; // when const result = testObj.match(5); @@ -18,7 +18,7 @@ describe("StrictEqualMatcher", () => { describe("checking if false matches with zero", () => { it("returns false", () => { // given - const testObj: Matcher = strictEqual(false); + const testObj: Matcher = strictEqual(false) as any; // when const result = testObj.match(0); @@ -31,7 +31,7 @@ describe("StrictEqualMatcher", () => { describe("checking if true matches with one", () => { it("returns false", () => { // given - const testObj: Matcher = strictEqual(true); + const testObj: Matcher = strictEqual(true) as any; // when const result = testObj.match(1); @@ -44,7 +44,7 @@ describe("StrictEqualMatcher", () => { describe("checking if same strings matches", () => { it("returns true", () => { // given - const testObj: Matcher = strictEqual("5"); + const testObj: Matcher = strictEqual("5") as any; // when const result = testObj.match("5"); From b4cd9f3c8fe41d97c126aabc448eece9f3132c1d Mon Sep 17 00:00:00 2001 From: lironh Date: Tue, 28 Sep 2021 13:15:17 +0300 Subject: [PATCH 3/3] fix(matchers): fixed some typing --- src/MethodStubVerificator.ts | 2 +- src/matcher/type/EndsWithMatcher.ts | 4 +- src/matcher/type/StartsWithMatcher.ts | 4 +- src/matcher/type/StrictEqualMatcher.ts | 2 +- src/ts-mockito.ts | 53 ++++++++++--------- test/MethodAction.spec.ts | 4 +- test/MethodStub.spec.ts | 12 ++--- test/matcher/type/AnyNumberMatcher.spec.ts | 10 ++-- test/matcher/type/AnyStringMatcher.spec.ts | 8 +-- test/matcher/type/BetweenMatcher.spec.ts | 4 +- test/matcher/type/DeepEqualMatcher.spec.ts | 14 ++--- test/matcher/type/EndsWithMatcher.spec.ts | 2 +- .../type/MatchingStringMatcher.spec.ts | 2 +- test/matcher/type/StartsWithMatcher.spec.ts | 2 +- test/matcher/type/StrictEqualMatcher.spec.ts | 8 +-- 15 files changed, 66 insertions(+), 65 deletions(-) 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 index c3b12ca..bfff5a7 100644 --- a/src/matcher/type/EndsWithMatcher.ts +++ b/src/matcher/type/EndsWithMatcher.ts @@ -1,11 +1,11 @@ import { Matcher } from "./Matcher"; export class EndsWithMatcher extends Matcher { - constructor(private expectedValue: any) { + constructor(private expectedValue: string) { super(); } - public match(value: Object): boolean { + public match(value: string): boolean { return value && (typeof value === "string") && value.endsWith(this.expectedValue); } diff --git a/src/matcher/type/StartsWithMatcher.ts b/src/matcher/type/StartsWithMatcher.ts index eb0a60b..854b870 100644 --- a/src/matcher/type/StartsWithMatcher.ts +++ b/src/matcher/type/StartsWithMatcher.ts @@ -1,11 +1,11 @@ import { Matcher } from "./Matcher"; export class StartsWithMatcher extends Matcher { - constructor(private expectedValue: any) { + constructor(private expectedValue: string) { super(); } - public match(value: Object): boolean { + public match(value: string): boolean { return value && (typeof value === "string") && value.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 47e9433..7895c4e 100644 --- a/src/ts-mockito.ts +++ b/src/ts-mockito.ts @@ -19,6 +19,7 @@ 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"; @@ -40,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); } @@ -83,56 +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(): number { - return new AnyNumberMatcher() as any; +export function anyNumber(): Matcher { + return new AnyNumberMatcher(); } -export function anyString(): string { - 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): number { - 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: T): T { - return new StrictEqualMatcher(expectedValue) as any; +export function strictEqual(expectedValue: T): Matcher { + return new StrictEqualMatcher(expectedValue); } -export function match(expectedValue: RegExp | string): string { - return new MatchingStringMatcher(expectedValue) as any; +export function match(expectedValue: RegExp | string): Matcher { + return new MatchingStringMatcher(expectedValue); } -export function startsWith(expectedValue: string): string { - return new StartsWithMatcher(expectedValue) as any; +export function startsWith(expectedValue: string): Matcher { + return new StartsWithMatcher(expectedValue); } -export function endsWith(expectedValue: string): string { - return new EndsWithMatcher(expectedValue) as any; +export function endsWith(expectedValue: string): Matcher { + return new EndsWithMatcher(expectedValue); } -export function objectContaining(expectedValue: Object): any { - return new ObjectContainingMatcher(expectedValue) as any; +export function objectContaining(expectedValue: Object): Matcher { + return new ObjectContainingMatcher(expectedValue); } // Export default object with all members (ember-browserify doesn't support named exports). diff --git a/test/MethodAction.spec.ts b/test/MethodAction.spec.ts index d080409..618ace2 100644 --- a/test/MethodAction.spec.ts +++ b/test/MethodAction.spec.ts @@ -12,7 +12,7 @@ describe("MethodAction", () => { const testObj: MethodAction = new MethodAction(methodName, [firstArg, secondArg]); // when - const result = testObj.isApplicable(methodName, [strictEqual(firstArg) as any, strictEqual(secondArg) as any]); + const result = testObj.isApplicable(methodName, [strictEqual(firstArg), strictEqual(secondArg)]); // then expect(result).toBeTruthy(); @@ -28,7 +28,7 @@ describe("MethodAction", () => { const testObj: MethodAction = new MethodAction(methodName, [firstArg, notMatchingArg]); // when - const result = testObj.isApplicable(methodName, [strictEqual(firstArg) as any, strictEqual(secondArg) as any]); + const result = testObj.isApplicable(methodName, [strictEqual(firstArg), strictEqual(secondArg)]); // then expect(result).toBeFalsy(); diff --git a/test/MethodStub.spec.ts b/test/MethodStub.spec.ts index 37805d2..f5e71c8 100644 --- a/test/MethodStub.spec.ts +++ b/test/MethodStub.spec.ts @@ -5,7 +5,7 @@ describe("ReturnValueMethodStub", () => { describe("checking if given arg is applicable", () => { it("returns true when arg match", () => { // given - const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(10) as any], 50); + const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(10)], 50); // when const result = testObj.isApplicable([10]); @@ -16,7 +16,7 @@ describe("ReturnValueMethodStub", () => { it("returns false when arg doesn't match", () => { // given - const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(10) as any], 50); + const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(10)], 50); // when const result = testObj.isApplicable([999]); @@ -31,7 +31,7 @@ describe("ReturnValueMethodStub", () => { // given const firstValue = 10; const secondValue = 20; - const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(firstValue) as any, strictEqual(secondValue) as any], 50); + const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(firstValue), strictEqual(secondValue)], 50); // when const result = testObj.isApplicable([firstValue, secondValue]); @@ -44,7 +44,7 @@ describe("ReturnValueMethodStub", () => { // given const firstValue = 10; const secondValue = 20; - const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(firstValue) as any, strictEqual(secondValue) as any], 50); + const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(firstValue), strictEqual(secondValue)], 50); // when const result = testObj.isApplicable([30, secondValue]); @@ -57,7 +57,7 @@ describe("ReturnValueMethodStub", () => { // given const firstValue = 10; const secondValue = 20; - const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(firstValue) as any, strictEqual(secondValue) as any], 50); + const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(firstValue), strictEqual(secondValue)], 50); // when const result = testObj.isApplicable([firstValue, 30]); @@ -70,7 +70,7 @@ describe("ReturnValueMethodStub", () => { // given const firstValue = 10; const secondValue = 20; - const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(firstValue) as any, strictEqual(secondValue) as any], 50); + const testObj: ReturnValueMethodStub = new ReturnValueMethodStub(0, [strictEqual(firstValue), strictEqual(secondValue)], 50); // when const result = testObj.isApplicable([30, 40]); diff --git a/test/matcher/type/AnyNumberMatcher.spec.ts b/test/matcher/type/AnyNumberMatcher.spec.ts index cb89511..0f214a9 100644 --- a/test/matcher/type/AnyNumberMatcher.spec.ts +++ b/test/matcher/type/AnyNumberMatcher.spec.ts @@ -5,7 +5,7 @@ describe("AnyNumberMatcher", () => { describe("checking if positive number is matching", () => { it("returns true", () => { // given - const testObj: Matcher = anyNumber() as any; + const testObj: Matcher = anyNumber(); // when const result = testObj.match(3); @@ -18,7 +18,7 @@ describe("AnyNumberMatcher", () => { describe("checking if negative number is matching", () => { it("returns true", () => { // given - const testObj: Matcher = anyNumber() as any; + const testObj: Matcher = anyNumber(); // when const result = testObj.match(-3); @@ -31,7 +31,7 @@ describe("AnyNumberMatcher", () => { describe("checking if zero is matching", () => { it("returns true", () => { // given - const testObj: Matcher = anyNumber() as any; + const testObj: Matcher = anyNumber(); // when const result = testObj.match(0); @@ -44,7 +44,7 @@ describe("AnyNumberMatcher", () => { describe("checking if string representation of number is matching", () => { it("returns false", () => { // given - const testObj: Matcher = anyNumber() as any; + const testObj: Matcher = anyNumber(); // when const result = testObj.match("5"); @@ -57,7 +57,7 @@ describe("AnyNumberMatcher", () => { describe("checking if object is matching", () => { it("returns false", () => { // given - const testObj: Matcher = anyNumber() as any; + const testObj: Matcher = anyNumber(); // when const result = testObj.match({}); diff --git a/test/matcher/type/AnyStringMatcher.spec.ts b/test/matcher/type/AnyStringMatcher.spec.ts index 915b311..8904fc7 100644 --- a/test/matcher/type/AnyStringMatcher.spec.ts +++ b/test/matcher/type/AnyStringMatcher.spec.ts @@ -5,7 +5,7 @@ describe("AnyStringMatcher", () => { describe("checking if number matches", () => { it("returns false", () => { // given - const testObj: Matcher = anyString() as any; + const testObj: Matcher = anyString(); // when const result = testObj.match(3); @@ -18,7 +18,7 @@ describe("AnyStringMatcher", () => { describe("checking if object matches", () => { it("returns false", () => { // given - const testObj: Matcher = anyString() as any; + const testObj: Matcher = anyString(); // when const result = testObj.match({}); @@ -31,7 +31,7 @@ describe("AnyStringMatcher", () => { describe("checking if empty string matches", () => { it("returns true", () => { // given - const testObj: Matcher = anyString() as any; + const testObj: Matcher = anyString(); // when const result = testObj.match(""); @@ -44,7 +44,7 @@ describe("AnyStringMatcher", () => { describe("checking if sample string matches", () => { it("returns true", () => { // given - const testObj: Matcher = anyString() as any; + const testObj: Matcher = anyString(); // when const result = testObj.match("sampleString"); diff --git a/test/matcher/type/BetweenMatcher.spec.ts b/test/matcher/type/BetweenMatcher.spec.ts index 194a309..10c0708 100644 --- a/test/matcher/type/BetweenMatcher.spec.ts +++ b/test/matcher/type/BetweenMatcher.spec.ts @@ -3,7 +3,7 @@ import {between} from "../../../src/ts-mockito"; describe("BetweenMatcher", () => { describe("checking if value matches given min and max", () => { - const testObj: Matcher = between(5, 10) as any; + const testObj: Matcher = between(5, 10); describe("when given value is lower than min", () => { it("returns false", () => { @@ -61,7 +61,7 @@ describe("BetweenMatcher", () => { // when let error = null; try { - const testObj: Matcher = between(10, 9) as any; + const testObj: Matcher = between(10, 9); } catch (e) { error = e; } diff --git a/test/matcher/type/DeepEqualMatcher.spec.ts b/test/matcher/type/DeepEqualMatcher.spec.ts index f141a8d..2d4320f 100644 --- a/test/matcher/type/DeepEqualMatcher.spec.ts +++ b/test/matcher/type/DeepEqualMatcher.spec.ts @@ -8,7 +8,7 @@ describe("DeepEqualMatcher", () => { // given const firstValue = 3; const secondValue = 3; - const testObj: Matcher = new DeepEqualMatcher(firstValue) as any; + const testObj: Matcher = new DeepEqualMatcher(firstValue); // when const result = testObj.match(secondValue); @@ -23,7 +23,7 @@ describe("DeepEqualMatcher", () => { // given const firstValue = "sampleString"; const secondValue = "sampleString"; - const testObj: Matcher = new DeepEqualMatcher(firstValue) as any; + const testObj: Matcher = new DeepEqualMatcher(firstValue); // when const result = testObj.match(secondValue); @@ -38,7 +38,7 @@ describe("DeepEqualMatcher", () => { // given const firstValue = {a: 1, b: {c: 2}}; const secondValue = {a: 1, b: {c: 2}}; - const testObj: Matcher = new DeepEqualMatcher(firstValue) as any; + const testObj: Matcher = new DeepEqualMatcher(firstValue); // when const result = testObj.match(secondValue); @@ -53,7 +53,7 @@ describe("DeepEqualMatcher", () => { // given const firstValue = {a: 1, b: {c: 2}}; const secondValue = {a: 1, b: {c: 99999}}; - const testObj: Matcher = new DeepEqualMatcher(firstValue) as any; + const testObj: Matcher = new DeepEqualMatcher(firstValue); // when const result = testObj.match(secondValue); @@ -68,7 +68,7 @@ describe("DeepEqualMatcher", () => { // given const firstValue = {a: 1, b: anyString()}; const secondValue = {a: 1, b: "2"}; - const testObj: Matcher = new DeepEqualMatcher(firstValue) as any; + const testObj: Matcher = new DeepEqualMatcher(firstValue); // when const result = testObj.match(secondValue); @@ -81,7 +81,7 @@ describe("DeepEqualMatcher", () => { // given const firstValue = {a: 1, b: anyString()}; const secondValue = {a: 1, b: 2}; - const testObj: Matcher = new DeepEqualMatcher(firstValue) as any; + const testObj: Matcher = new DeepEqualMatcher(firstValue); // when const result = testObj.match(secondValue); @@ -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 index 07e05f6..2e988cc 100644 --- a/test/matcher/type/EndsWithMatcher.spec.ts +++ b/test/matcher/type/EndsWithMatcher.spec.ts @@ -3,7 +3,7 @@ import {endsWith} from "../../../src/ts-mockito"; describe("EndsWithMatcher", () => { describe("checking if value starts with given value", () => { - const testObj: Matcher = endsWith("abc") as any; + const testObj: Matcher = endsWith("abc"); describe("when given value ends with string", () => { it("returns true", () => { diff --git a/test/matcher/type/MatchingStringMatcher.spec.ts b/test/matcher/type/MatchingStringMatcher.spec.ts index ec4bfd0..e4288d0 100644 --- a/test/matcher/type/MatchingStringMatcher.spec.ts +++ b/test/matcher/type/MatchingStringMatcher.spec.ts @@ -3,7 +3,7 @@ import {match} from "../../../src/ts-mockito"; describe("MatchingStringMatcher", () => { describe("checking if value matches given regexp", () => { - const testObj: Matcher = match(/\w123/) as any; + const testObj: Matcher = match(/\w123/); describe("when given value matches regexp", () => { it("returns true", () => { diff --git a/test/matcher/type/StartsWithMatcher.spec.ts b/test/matcher/type/StartsWithMatcher.spec.ts index 672fb40..7b82843 100644 --- a/test/matcher/type/StartsWithMatcher.spec.ts +++ b/test/matcher/type/StartsWithMatcher.spec.ts @@ -3,7 +3,7 @@ import {startsWith} from "../../../src/ts-mockito"; describe("StartsWithMatcher", () => { describe("checking if value starts with given value", () => { - const testObj: Matcher = startsWith("abc") as any; + const testObj: Matcher = startsWith("abc"); describe("when given value starts with string", () => { it("returns true", () => { diff --git a/test/matcher/type/StrictEqualMatcher.spec.ts b/test/matcher/type/StrictEqualMatcher.spec.ts index 9756881..dc56efe 100644 --- a/test/matcher/type/StrictEqualMatcher.spec.ts +++ b/test/matcher/type/StrictEqualMatcher.spec.ts @@ -5,7 +5,7 @@ describe("StrictEqualMatcher", () => { describe("checking if string representation of number matches with number", () => { it("returns false", () => { // given - const testObj: Matcher = strictEqual("5") as any; + const testObj: Matcher = strictEqual("5"); // when const result = testObj.match(5); @@ -18,7 +18,7 @@ describe("StrictEqualMatcher", () => { describe("checking if false matches with zero", () => { it("returns false", () => { // given - const testObj: Matcher = strictEqual(false) as any; + const testObj: Matcher = strictEqual(false); // when const result = testObj.match(0); @@ -31,7 +31,7 @@ describe("StrictEqualMatcher", () => { describe("checking if true matches with one", () => { it("returns false", () => { // given - const testObj: Matcher = strictEqual(true) as any; + const testObj: Matcher = strictEqual(true); // when const result = testObj.match(1); @@ -44,7 +44,7 @@ describe("StrictEqualMatcher", () => { describe("checking if same strings matches", () => { it("returns true", () => { // given - const testObj: Matcher = strictEqual("5") as any; + const testObj: Matcher = strictEqual("5"); // when const result = testObj.match("5");