Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/NagRock/ts-mockito
Browse files Browse the repository at this point in the history
  • Loading branch information
NagRock committed Jan 31, 2018
2 parents 808f2af + 67c87ab commit 86b89ef
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/matcher/type/MatchingStringMatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {Matcher} from "./Matcher";

export class MatchingStringMatcher extends Matcher {
constructor(private expectedValue: any) {
super();
}

public match(value: any): boolean {
return value.match(this.expectedValue);
}

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

export class ObjectContainingMatcher extends Matcher {
constructor(private expectedValue: any) {
super();
}

public match(value: Object): boolean {
return _.isMatch(value, this.expectedValue);
}

public toString(): string {
return `objectContaining(${this.expectedValue})`;
}
}
11 changes: 11 additions & 0 deletions src/ts-mockito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import {AnythingMatcher} from "./matcher/type/AnythingMatcher";
import {BetweenMatcher} from "./matcher/type/BetweenMatcher";
import {DeepEqualMatcher} from "./matcher/type/DeepEqualMatcher";
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 {StrictEqualMatcher} from "./matcher/type/StrictEqualMatcher";
import {MethodStubSetter} from "./MethodStubSetter";
import {MethodStubVerificator} from "./MethodStubVerificator";
Expand Down Expand Up @@ -112,5 +114,14 @@ export function strictEqual(expectedValue: any): Matcher {
return new StrictEqualMatcher(expectedValue);
}

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

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

import * as mockito from "./ts-mockito";

export default mockito;
2 changes: 1 addition & 1 deletion test/matcher/type/BetweenMatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("BetweenMatcher", () => {
const testObj: Matcher = between(5, 10);

describe("when given value is lower than min", () => {
it("returns true", () => {
it("returns false", () => {
// when
const result = testObj.match(4);

Expand Down
29 changes: 29 additions & 0 deletions test/matcher/type/MatchingStringMatcher.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {Matcher} from "../../../src/matcher/type/Matcher";
import {match} from "../../../src/ts-mockito";

describe("MatchingStringMatcher", () => {
describe("checking if value matches given regexp", () => {
const testObj: Matcher = match(/\w123/);

describe("when given value matches regexp", () => {
it("returns true", () => {
// when
const result = testObj.match("a123");

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

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

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

});
37 changes: 37 additions & 0 deletions test/matcher/type/ObjectContainingMatcher.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {Matcher} from "../../../src/matcher/type/Matcher";
import {objectContaining} from "../../../src/ts-mockito";

describe("ObjectContainingMatcher", () => {
describe("checking if source object contains given object", () => {
const testObj: Matcher = objectContaining({b: {c: "c", d: {}}});

describe("when given value contains given object", () => {
it("returns true", () => {
// when
const result = testObj.match({a: "a", b: {c: "c", d: {}}});

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

it("returns true", () => {
// when
const result = testObj.match({b: {c: "c", d: {}}});

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

describe("when given value doesn't contain given object", () => {
it("returns false", () => {
// when
const result = testObj.match({b: {c: "c"}});

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

});

0 comments on commit 86b89ef

Please sign in to comment.