Skip to content

Commit

Permalink
Make matchers check type
Browse files Browse the repository at this point in the history
  • Loading branch information
johanblumenberg committed May 25, 2019
1 parent 4477bb1 commit e59b0c8
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 37 deletions.
14 changes: 7 additions & 7 deletions src/ts-mockito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,35 +89,35 @@ 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;
}

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<T>(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<T>(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;
}

Expand Down
4 changes: 2 additions & 2 deletions test/MethodAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions test/MethodStub.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
Expand Down
10 changes: 5 additions & 5 deletions test/matcher/type/AnyNumberMatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -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({});
Expand Down
8 changes: 4 additions & 4 deletions test/matcher/type/AnyStringMatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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({});
Expand All @@ -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("");
Expand All @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions test/matcher/type/BetweenMatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions test/matcher/type/DeepEqualMatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/matcher/type/MatchingStringMatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
8 changes: 4 additions & 4 deletions test/matcher/type/StrictEqualMatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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");
Expand Down

0 comments on commit e59b0c8

Please sign in to comment.