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

Matcher types #139

Open
wants to merge 2 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
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: any) {
super();
}

public match(value: Object): 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: any) {
super();
}

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

public toString(): string {
return `startsWith(${this.expectedValue})`;
}
}
26 changes: 19 additions & 7 deletions src/ts-mockito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -87,38 +89,46 @@ 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;
}

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;
}
Expand All @@ -143,5 +153,7 @@ export default {
notNull,
strictEqual,
match,
startsWith,
endsWith,
objectContaining,
};
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
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") 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();
});
});
});
});
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
Loading