From b76f0e12e5bd202a68f3504feb91fe1e74e680e0 Mon Sep 17 00:00:00 2001 From: cakeinpanic Date: Fri, 19 Jan 2018 19:36:41 +0300 Subject: [PATCH 01/12] fix typo --- test/matcher/type/BetweenMatcher.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/matcher/type/BetweenMatcher.spec.ts b/test/matcher/type/BetweenMatcher.spec.ts index bd77383..10c0708 100644 --- a/test/matcher/type/BetweenMatcher.spec.ts +++ b/test/matcher/type/BetweenMatcher.spec.ts @@ -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); From 150a73189761fc982a2678aef295dc7442cce107 Mon Sep 17 00:00:00 2001 From: cakeinpanic Date: Fri, 19 Jan 2018 19:36:58 +0300 Subject: [PATCH 02/12] add matching string matcher --- src/matcher/type/MatchingStringMatcher.ts | 15 ++++++++++ src/ts-mockito.ts | 8 ++++- .../type/MatchingStringMatcher.spec.ts | 29 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/matcher/type/MatchingStringMatcher.ts create mode 100644 test/matcher/type/MatchingStringMatcher.spec.ts diff --git a/src/matcher/type/MatchingStringMatcher.ts b/src/matcher/type/MatchingStringMatcher.ts new file mode 100644 index 0000000..2806500 --- /dev/null +++ b/src/matcher/type/MatchingStringMatcher.ts @@ -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})`; + } +} diff --git a/src/ts-mockito.ts b/src/ts-mockito.ts index 12cd3e1..41d61bf 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 {Matcher} from "./matcher/type/Matcher"; +import {MatchingStringMatcher} from "./matcher/type/MatchingStringMatcher"; import {NotNullMatcher} from "./matcher/type/NotNullMatcher"; import {StrictEqualMatcher} from "./matcher/type/StrictEqualMatcher"; import {MethodStubSetter} from "./MethodStubSetter"; @@ -31,7 +32,7 @@ export function spy(instanceToSpy: T): T { return new Spy(instanceToSpy).getMock(); } -export function mock(clazz: { new(...args: any[]): T; } | (Function & { prototype: T }) ): T { +export function mock(clazz: { new(...args: any[]): T; } | (Function & { prototype: T })): T { return new Mocker(clazz).getMock(); } @@ -117,5 +118,10 @@ export function strictEqual(expectedValue: any): Matcher { return new StrictEqualMatcher(expectedValue); } +export function match(expectedValue: RegExp | string): Matcher { + return new MatchingStringMatcher(expectedValue); +} + import * as mockito from "./ts-mockito"; + export default mockito; diff --git a/test/matcher/type/MatchingStringMatcher.spec.ts b/test/matcher/type/MatchingStringMatcher.spec.ts new file mode 100644 index 0000000..e4288d0 --- /dev/null +++ b/test/matcher/type/MatchingStringMatcher.spec.ts @@ -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(); + }); + }); + }); + +}); From 869810c0516ffc5aa42ed83c2669a3e040b6d9d3 Mon Sep 17 00:00:00 2001 From: cakeinpanic Date: Mon, 22 Jan 2018 14:54:16 +0300 Subject: [PATCH 03/12] add objectContaining matcher --- src/matcher/type/ObjectContainingMatcher.ts | 16 ++++++++ src/ts-mockito.ts | 7 +++- .../type/ObjectContainingMatcher.spec.ts | 37 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/matcher/type/ObjectContainingMatcher.ts create mode 100644 test/matcher/type/ObjectContainingMatcher.spec.ts diff --git a/src/matcher/type/ObjectContainingMatcher.ts b/src/matcher/type/ObjectContainingMatcher.ts new file mode 100644 index 0000000..285ac51 --- /dev/null +++ b/src/matcher/type/ObjectContainingMatcher.ts @@ -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})`; + } +} diff --git a/src/ts-mockito.ts b/src/ts-mockito.ts index 41d61bf..911828e 100644 --- a/src/ts-mockito.ts +++ b/src/ts-mockito.ts @@ -21,6 +21,7 @@ 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"; @@ -32,7 +33,7 @@ export function spy(instanceToSpy: T): T { return new Spy(instanceToSpy).getMock(); } -export function mock(clazz: { new(...args: any[]): T; } | (Function & { prototype: T })): T { +export function mock(clazz: { new(...args: any[]): T; } | (Function & { prototype: T }) ): T { return new Mocker(clazz).getMock(); } @@ -122,6 +123,10 @@ 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; diff --git a/test/matcher/type/ObjectContainingMatcher.spec.ts b/test/matcher/type/ObjectContainingMatcher.spec.ts new file mode 100644 index 0000000..d8c5376 --- /dev/null +++ b/test/matcher/type/ObjectContainingMatcher.spec.ts @@ -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(); + }); + }); + }); + +}); From 16c44f443ea60a40f05ecbea07429ee4219a5299 Mon Sep 17 00:00:00 2001 From: cakeinpanic Date: Tue, 23 Jan 2018 18:38:29 +0300 Subject: [PATCH 04/12] fix lint --- src/matcher/type/ObjectContainingMatcher.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/matcher/type/ObjectContainingMatcher.ts b/src/matcher/type/ObjectContainingMatcher.ts index 285ac51..7ed3814 100644 --- a/src/matcher/type/ObjectContainingMatcher.ts +++ b/src/matcher/type/ObjectContainingMatcher.ts @@ -1,4 +1,4 @@ -import * as _ from 'lodash'; +import * as _ from "lodash"; import {Matcher} from "./Matcher"; export class ObjectContainingMatcher extends Matcher { From fc16cc11943a1c755c2a4f90b9ed7fc515cd80c4 Mon Sep 17 00:00:00 2001 From: cakeinpanic Date: Tue, 23 Jan 2018 23:21:17 +0300 Subject: [PATCH 05/12] create not matcher --- src/matcher/type/AnyFunctionMatcher.ts | 4 +-- src/matcher/type/AnyNumberMatcher.ts | 4 +-- src/matcher/type/AnyOfClassMatcher.ts | 4 +-- src/matcher/type/AnyStringMatcher.ts | 4 +-- src/matcher/type/AnythingMatcher.ts | 4 +-- src/matcher/type/BetweenMatcher.ts | 4 +-- src/matcher/type/DeepEqualMatcher.ts | 7 +++-- src/matcher/type/Matcher.ts | 24 +++++++++++++-- src/matcher/type/NotNullMatcher.ts | 4 +-- src/matcher/type/StrictEqualMatcher.ts | 6 ++-- test/matcher/type/AnyFunctionMatcher.spec.ts | 30 +++++++++--------- test/matcher/type/AnyNumberMatcher.spec.ts | 31 ++++++++++--------- test/matcher/type/AnyOfClassMatcher.spec.ts | 23 ++++++++------ test/matcher/type/AnyStringMatcher.spec.ts | 25 ++++++++------- test/matcher/type/AnythingMatcher.spec.ts | 31 ++++++++++--------- test/matcher/type/BetweenMatcher.spec.ts | 16 +++++++++- test/matcher/type/DeepEqualMatcher.spec.ts | 12 ++++++++ test/matcher/type/Matcher.spec.ts | 29 ++++++++++++++++++ test/matcher/type/NotNullMatcher.spec.ts | 32 +++++++++++--------- test/matcher/type/StrictEqualMatcher.spec.ts | 8 +++++ 20 files changed, 197 insertions(+), 105 deletions(-) create mode 100644 test/matcher/type/Matcher.spec.ts diff --git a/src/matcher/type/AnyFunctionMatcher.ts b/src/matcher/type/AnyFunctionMatcher.ts index bf9a1b9..bf1592f 100644 --- a/src/matcher/type/AnyFunctionMatcher.ts +++ b/src/matcher/type/AnyFunctionMatcher.ts @@ -7,10 +7,10 @@ export class AnyFunctionMatcher extends Matcher { } public match(value: any): boolean { - return _.isFunction(value); + return this.reverseResult(_.isFunction(value)); } public toString(): string { - return "anyFunction()"; + return `${this.prefix}anyFunction()`; } } diff --git a/src/matcher/type/AnyNumberMatcher.ts b/src/matcher/type/AnyNumberMatcher.ts index 9a69149..ecad65e 100644 --- a/src/matcher/type/AnyNumberMatcher.ts +++ b/src/matcher/type/AnyNumberMatcher.ts @@ -7,10 +7,10 @@ export class AnyNumberMatcher extends Matcher { } public match(value: any): boolean { - return _.isNumber(value); + return this.reverseResult(_.isNumber(value)); } public toString(): string { - return "anyNumber()"; + return `${this.prefix}anyNumber()`; } } diff --git a/src/matcher/type/AnyOfClassMatcher.ts b/src/matcher/type/AnyOfClassMatcher.ts index a4098de..6515368 100644 --- a/src/matcher/type/AnyOfClassMatcher.ts +++ b/src/matcher/type/AnyOfClassMatcher.ts @@ -9,10 +9,10 @@ export class AnyOfClassMatcher extends Matcher { } public match(value: any): boolean { - return value instanceof this.expectedClass; + return this.reverseResult(value instanceof this.expectedClass); } public toString() { - return `anyOfClass(${this.expectedClass["name"]})`; + return `${this.prefix}anyOfClass(${this.expectedClass["name"]})`; } } diff --git a/src/matcher/type/AnyStringMatcher.ts b/src/matcher/type/AnyStringMatcher.ts index efbab75..2437a27 100644 --- a/src/matcher/type/AnyStringMatcher.ts +++ b/src/matcher/type/AnyStringMatcher.ts @@ -7,10 +7,10 @@ export class AnyStringMatcher extends Matcher { } public match(value: any): boolean { - return _.isString(value); + return this.reverseResult(_.isString(value)); } public toString(): string { - return "anyString()"; + return `${this.prefix}anyString()`; } } diff --git a/src/matcher/type/AnythingMatcher.ts b/src/matcher/type/AnythingMatcher.ts index b273ced..b6de219 100644 --- a/src/matcher/type/AnythingMatcher.ts +++ b/src/matcher/type/AnythingMatcher.ts @@ -6,10 +6,10 @@ export class AnythingMatcher extends Matcher { } public match(value: any): boolean { - return true; + return this.reverseResult(true); } public toString(): string { - return "anything()"; + return `${this.prefix}anything()`; } } diff --git a/src/matcher/type/BetweenMatcher.ts b/src/matcher/type/BetweenMatcher.ts index 5a8bc56..65c2d0c 100644 --- a/src/matcher/type/BetweenMatcher.ts +++ b/src/matcher/type/BetweenMatcher.ts @@ -10,10 +10,10 @@ export class BetweenMatcher extends Matcher { } public match(value: any): boolean { - return value >= this.min && value <= this.max; + return this.reverseResult(value >= this.min && value <= this.max); } public toString(): string { - return `between(${this.min}, ${this.max})`; + return `${this.prefix}between(${this.min}, ${this.max})`; } } diff --git a/src/matcher/type/DeepEqualMatcher.ts b/src/matcher/type/DeepEqualMatcher.ts index 544cb67..b8a9c93 100644 --- a/src/matcher/type/DeepEqualMatcher.ts +++ b/src/matcher/type/DeepEqualMatcher.ts @@ -7,7 +7,7 @@ export class DeepEqualMatcher extends Matcher { } public match(value: any): boolean { - return _.isEqualWith(this.expectedValue, value, + const result = _.isEqualWith(this.expectedValue, value, (expected: any, actual: any): boolean => { if (expected instanceof Matcher) { return expected.match(actual); @@ -15,13 +15,14 @@ export class DeepEqualMatcher extends Matcher { return undefined; }); + return this.reverseResult(result); } public toString(): string { if (this.expectedValue instanceof Array) { - return `deepEqual([${this.expectedValue}])`; + return `${this.prefix}deepEqual([${this.expectedValue}])`; } else { - return `deepEqual(${this.expectedValue})`; + return `${this.prefix}deepEqual(${this.expectedValue})`; } } } diff --git a/src/matcher/type/Matcher.ts b/src/matcher/type/Matcher.ts index 87bbc83..1297c62 100644 --- a/src/matcher/type/Matcher.ts +++ b/src/matcher/type/Matcher.ts @@ -1,9 +1,27 @@ +import * as _ from "lodash"; + export class Matcher { - public match(value: any): boolean { - return false; + public isNot: boolean = false; + + public match(...values: any[]): boolean { + return this.reverseResult(false); } public toString(): string { - return ""; + return `${this.prefix}`; + } + + public not() { + const newMatcher = _.cloneDeep(this); + newMatcher.isNot = true; + return newMatcher; + } + + protected get prefix(): string { + return this.isNot ? "not()." : ""; + } + + protected reverseResult(result: boolean): boolean { + return this.isNot ? !result : result; } } diff --git a/src/matcher/type/NotNullMatcher.ts b/src/matcher/type/NotNullMatcher.ts index 1337a63..dd528b4 100644 --- a/src/matcher/type/NotNullMatcher.ts +++ b/src/matcher/type/NotNullMatcher.ts @@ -3,10 +3,10 @@ import {Matcher} from "./Matcher"; export class NotNullMatcher extends Matcher { public match(value: any): boolean { - return !_.isNull(value); + return this.reverseResult(!_.isNull(value)); } public toString(): string { - return "notNull()"; + return `${this.prefix}notNull()`; } } diff --git a/src/matcher/type/StrictEqualMatcher.ts b/src/matcher/type/StrictEqualMatcher.ts index 593c2de..c25be4f 100644 --- a/src/matcher/type/StrictEqualMatcher.ts +++ b/src/matcher/type/StrictEqualMatcher.ts @@ -6,14 +6,14 @@ export class StrictEqualMatcher extends Matcher { } public match(value: any): boolean { - return this.expectedValue === value; + return this.reverseResult(this.expectedValue === value); } public toString(): string { if (this.expectedValue instanceof Array) { - return `strictEqual([${this.expectedValue}])`; + return `${this.prefix}strictEqual([${this.expectedValue}])`; } else { - return `strictEqual(${this.expectedValue})`; + return `${this.prefix}strictEqual(${this.expectedValue})`; } } } diff --git a/test/matcher/type/AnyFunctionMatcher.spec.ts b/test/matcher/type/AnyFunctionMatcher.spec.ts index a9ef677..f76de92 100644 --- a/test/matcher/type/AnyFunctionMatcher.spec.ts +++ b/test/matcher/type/AnyFunctionMatcher.spec.ts @@ -2,69 +2,69 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; import {anyFunction} from "../../../src/ts-mockito"; describe("AnyFunctionMatcher", () => { + let testObj: Matcher; + + beforeEach(() => { + testObj = anyFunction(); + }); describe("checking if function is function", () => { it("returns true", () => { - // given - const testObj: Matcher = anyFunction(); - // when const result = testObj.match(() => "arbitrary return value"); + const notResult = testObj.not().match(() => "arbitrary return value"); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); describe("checking if string is function", () => { it("returns false", () => { - // given - const testObj: Matcher = anyFunction(); - // when const result = testObj.match("some string"); + const notResult = testObj.not().match("some string"); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); describe("checking if number is function", () => { it("returns false", () => { - // given - const testObj: Matcher = anyFunction(); - // when const result = testObj.match(5); + const notResult = testObj.not().match(5); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); describe("checking if object is function", () => { it("returns false", () => { - // given - const testObj: Matcher = anyFunction(); - // when const result = testObj.match({prop1: "prop1Value"}); + const notResult = testObj.not().match({prop1: "prop1Value"}); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); describe("checking if toString works", () => { it("returns 'anyFunction()'", () => { - // given - const testObj: Matcher = anyFunction(); - // when const result = testObj.toString(); + const notResult = testObj.not().toString(); // then expect(result).toEqual("anyFunction()"); + expect(notResult).toEqual("not().anyFunction()"); }); }); diff --git a/test/matcher/type/AnyNumberMatcher.spec.ts b/test/matcher/type/AnyNumberMatcher.spec.ts index 0f214a9..0f0aa77 100644 --- a/test/matcher/type/AnyNumberMatcher.spec.ts +++ b/test/matcher/type/AnyNumberMatcher.spec.ts @@ -2,68 +2,69 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; import {anyNumber} from "../../../src/ts-mockito"; describe("AnyNumberMatcher", () => { + let testObj: Matcher; + + beforeEach(() => { + testObj = anyNumber(); + }); + describe("checking if positive number is matching", () => { it("returns true", () => { - // given - const testObj: Matcher = anyNumber(); - // when const result = testObj.match(3); + const notResult = testObj.not().match(3); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); describe("checking if negative number is matching", () => { it("returns true", () => { - // given - const testObj: Matcher = anyNumber(); - // when const result = testObj.match(-3); + const notResult = testObj.not().match(-3); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); describe("checking if zero is matching", () => { it("returns true", () => { - // given - const testObj: Matcher = anyNumber(); - // when const result = testObj.match(0); + const notResult = testObj.not().match(0); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); describe("checking if string representation of number is matching", () => { it("returns false", () => { - // given - const testObj: Matcher = anyNumber(); - // when const result = testObj.match("5"); + const notResult = testObj.not().match("5"); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); describe("checking if object is matching", () => { it("returns false", () => { - // given - const testObj: Matcher = anyNumber(); - // when const result = testObj.match({}); + const notResult = testObj.not().match({}); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); }); diff --git a/test/matcher/type/AnyOfClassMatcher.spec.ts b/test/matcher/type/AnyOfClassMatcher.spec.ts index d7755a6..3f39e58 100644 --- a/test/matcher/type/AnyOfClassMatcher.spec.ts +++ b/test/matcher/type/AnyOfClassMatcher.spec.ts @@ -2,37 +2,42 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; import {anyOfClass} from "../../../src/ts-mockito"; describe("AnyOfClassMatcher", () => { + let testObj: Matcher; class Car { constructor() { } } + beforeEach(() => { + testObj = anyOfClass(Car); + }); + describe("checking if class matches", () => { it("returns true", () => { - // given - const testObj: Matcher = anyOfClass(Car); const value = new Car(); // when const result = testObj.match(value); + const notResult = testObj.not().match(value); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); describe("checking if null matches", () => { it("returns false", () => { - // given - const testObj: Matcher = anyOfClass(Car); const value = null; // when const result = testObj.match(value); + const notResult = testObj.not().match(value); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); @@ -49,28 +54,26 @@ describe("AnyOfClassMatcher", () => { describe("checking if different classes match", () => { it("returns false", () => { - // given - const testObj: Matcher = anyOfClass(Car); const value = "a string"; // when const result = testObj.match(value); + const notResult = testObj.not().match(value); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); describe("checking if toString works", () => { it("returns 'anyOfClass(Car)'", () => { - // given - const testObj: Matcher = anyOfClass(Car); - - // when const result = testObj.toString(); + const notResult = testObj.not().toString(); // then expect(result).toEqual("anyOfClass(Car)"); + expect(notResult).toEqual("not().anyOfClass(Car)"); }); }); }); diff --git a/test/matcher/type/AnyStringMatcher.spec.ts b/test/matcher/type/AnyStringMatcher.spec.ts index 8904fc7..baa7911 100644 --- a/test/matcher/type/AnyStringMatcher.spec.ts +++ b/test/matcher/type/AnyStringMatcher.spec.ts @@ -2,55 +2,58 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; import {anyString} from "../../../src/ts-mockito"; describe("AnyStringMatcher", () => { + let testObj: Matcher; + + beforeEach(() => { + testObj = anyString(); + }); + describe("checking if number matches", () => { it("returns false", () => { - // given - const testObj: Matcher = anyString(); - // when const result = testObj.match(3); + const notResult = testObj.not().match(3); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); describe("checking if object matches", () => { it("returns false", () => { - // given - const testObj: Matcher = anyString(); // when const result = testObj.match({}); + const notResult = testObj.not().match({}); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); describe("checking if empty string matches", () => { it("returns true", () => { - // given - const testObj: Matcher = anyString(); - // when const result = testObj.match(""); + const notResult = testObj.not().match(""); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); describe("checking if sample string matches", () => { it("returns true", () => { - // given - const testObj: Matcher = anyString(); - // when const result = testObj.match("sampleString"); + const notResult = testObj.not().match("sampleString"); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); }); diff --git a/test/matcher/type/AnythingMatcher.spec.ts b/test/matcher/type/AnythingMatcher.spec.ts index 42512ef..834c290 100644 --- a/test/matcher/type/AnythingMatcher.spec.ts +++ b/test/matcher/type/AnythingMatcher.spec.ts @@ -2,68 +2,69 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; import {anything} from "../../../src/ts-mockito"; describe("AnythingMatcher", () => { + let testObj: Matcher; + + beforeEach(() => { + testObj = anything(); + }); + describe("checking if number matches", () => { it("returns true", () => { - // given - const testObj: Matcher = anything(); - // when const result = testObj.match(3); + const notResult = testObj.not().match(3); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); describe("checking if object matches", () => { it("returns true", () => { - // given - const testObj: Matcher = anything(); - // when const result = testObj.match({}); + const notResult = testObj.not().match({}); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); describe("checking if empty string matches", () => { it("returns true", () => { - // given - const testObj: Matcher = anything(); - // when const result = testObj.match(""); + const notResult = testObj.not().match(""); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); describe("checking if sample string matches", () => { it("returns true", () => { - // given - const testObj: Matcher = anything(); - // when const result = testObj.match("sampleString"); + const notResult = testObj.not().match("sampleString"); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); describe("checking if null matches", () => { it("returns true", () => { - // given - const testObj: Matcher = anything(); - // when const result = testObj.match(null); + const notResult = testObj.not().match(null); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); }); diff --git a/test/matcher/type/BetweenMatcher.spec.ts b/test/matcher/type/BetweenMatcher.spec.ts index bd77383..6b19c6f 100644 --- a/test/matcher/type/BetweenMatcher.spec.ts +++ b/test/matcher/type/BetweenMatcher.spec.ts @@ -3,15 +3,21 @@ import {between} from "../../../src/ts-mockito"; describe("BetweenMatcher", () => { describe("checking if value matches given min and max", () => { - const testObj: Matcher = between(5, 10); + let testObj: Matcher; + + beforeEach(() => { + testObj = between(5, 10); + }); describe("when given value is lower than min", () => { it("returns true", () => { // when const result = testObj.match(4); + const notResult = testObj.not().match(4); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); @@ -19,9 +25,11 @@ describe("BetweenMatcher", () => { it("returns true", () => { // when const result = testObj.match(5); + // const notResult = testObj.not().match(5); // then expect(result).toBeTruthy(); + // expect(notResult).toBeFalsy(); }); }); @@ -29,9 +37,11 @@ describe("BetweenMatcher", () => { it("returns true", () => { // when const result = testObj.match(7); + const notResult = testObj.not().match(7); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); @@ -39,9 +49,11 @@ describe("BetweenMatcher", () => { it("returns true", () => { // when const result = testObj.match(10); + const notResult = testObj.not().match(10); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); @@ -49,9 +61,11 @@ describe("BetweenMatcher", () => { it("returns true", () => { // when const result = testObj.match(11); + const notResult = testObj.not().match(11); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); }); diff --git a/test/matcher/type/DeepEqualMatcher.spec.ts b/test/matcher/type/DeepEqualMatcher.spec.ts index 69a43af..6a5f030 100644 --- a/test/matcher/type/DeepEqualMatcher.spec.ts +++ b/test/matcher/type/DeepEqualMatcher.spec.ts @@ -11,9 +11,11 @@ describe("DeepEqualMatcher", () => { // when const result = testObj.match(secondValue); + const notResult = testObj.not().match(secondValue); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); @@ -26,9 +28,11 @@ describe("DeepEqualMatcher", () => { // when const result = testObj.match(secondValue); + const notResult = testObj.not().match(secondValue); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); @@ -41,9 +45,11 @@ describe("DeepEqualMatcher", () => { // when const result = testObj.match(secondValue); + const notResult = testObj.not().match(secondValue); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); @@ -56,9 +62,11 @@ describe("DeepEqualMatcher", () => { // when const result = testObj.match(secondValue); + const notResult = testObj.not().match(secondValue); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); @@ -71,9 +79,11 @@ describe("DeepEqualMatcher", () => { // when const result = testObj.match(secondValue); + const notResult = testObj.not().match(secondValue); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); it("returns false if matcher returns false", () => { @@ -84,9 +94,11 @@ describe("DeepEqualMatcher", () => { // when const result = testObj.match(secondValue); + const notResult = testObj.not().match(secondValue); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); }); diff --git a/test/matcher/type/Matcher.spec.ts b/test/matcher/type/Matcher.spec.ts new file mode 100644 index 0000000..f711e29 --- /dev/null +++ b/test/matcher/type/Matcher.spec.ts @@ -0,0 +1,29 @@ +import {Matcher} from "../../../src/matcher/type/Matcher"; + +export function xor(a: boolean, b: boolean): boolean { + return a && !b || !b && a; +} + +describe("Matcher", () => { + const testObj: Matcher = new Matcher(); + + describe("not", () => { + it("returns opposite value", () => { + // when + const result = testObj.match(null); + const notResult = testObj.not().match(null); + + // then + expect(xor(result, notResult)).toBeFalsy(); + }); + + it("returns toString with 'not' prepended", () => { + // when + const result = testObj.toString(); + const notResult = testObj.not().toString(); + + // then + expect(`not().${result}`).toEqual(notResult); + }); + }); +}); diff --git a/test/matcher/type/NotNullMatcher.spec.ts b/test/matcher/type/NotNullMatcher.spec.ts index 7e9fd09..3f9aef3 100644 --- a/test/matcher/type/NotNullMatcher.spec.ts +++ b/test/matcher/type/NotNullMatcher.spec.ts @@ -2,68 +2,70 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; import {notNull} from "../../../src/ts-mockito"; describe("NotNullMatcher", () => { + let testObj: Matcher; + + beforeEach(() => { + testObj = notNull(); + + }); + describe("checking if null matches", () => { it("returns false", () => { - // given - const testObj: Matcher = notNull(); - // when const result = testObj.match(null); + const notResult = testObj.not().match(null); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); describe("checking if false matches", () => { it("returns true", () => { - // given - const testObj: Matcher = notNull(); - // when const result = testObj.match(false); + const notResult = testObj.not().match(false); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); describe("checking if zero matches", () => { it("returns true", () => { - // given - const testObj: Matcher = notNull(); - // when const result = testObj.match(0); + const notResult = testObj.not().match(0); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); describe("checking if sample object matches", () => { it("returns true", () => { - // given - const testObj: Matcher = notNull(); - // when const result = testObj.match({}); + const notResult = testObj.not().match({}); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); describe("checking if sample string matches", () => { it("returns true", () => { - // given - const testObj: Matcher = notNull(); - // when const result = testObj.match("sampleString"); + const notResult = testObj.not().match("sampleString"); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); }); diff --git a/test/matcher/type/StrictEqualMatcher.spec.ts b/test/matcher/type/StrictEqualMatcher.spec.ts index dc56efe..bc19afe 100644 --- a/test/matcher/type/StrictEqualMatcher.spec.ts +++ b/test/matcher/type/StrictEqualMatcher.spec.ts @@ -9,9 +9,11 @@ describe("StrictEqualMatcher", () => { // when const result = testObj.match(5); + const notResult = testObj.not().match(5); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); @@ -22,9 +24,11 @@ describe("StrictEqualMatcher", () => { // when const result = testObj.match(0); + const notResult = testObj.not().match(0); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); @@ -35,9 +39,11 @@ describe("StrictEqualMatcher", () => { // when const result = testObj.match(1); + const notResult = testObj.not().match(1); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); @@ -48,9 +54,11 @@ describe("StrictEqualMatcher", () => { // when const result = testObj.match("5"); + const notResult = testObj.not().match("5"); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); }); From b060b28553e80523cb26b9aa7cea6129c80b6fec Mon Sep 17 00:00:00 2001 From: cakeinpanic Date: Wed, 24 Jan 2018 00:33:37 +0300 Subject: [PATCH 06/12] update tests for new matchers --- src/matcher/type/MatchingStringMatcher.ts | 4 ++-- src/matcher/type/ObjectContainingMatcher.ts | 4 ++-- test/matcher/type/MatchingStringMatcher.spec.ts | 4 ++++ test/matcher/type/ObjectContainingMatcher.spec.ts | 6 ++++++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/matcher/type/MatchingStringMatcher.ts b/src/matcher/type/MatchingStringMatcher.ts index 2806500..4d0f881 100644 --- a/src/matcher/type/MatchingStringMatcher.ts +++ b/src/matcher/type/MatchingStringMatcher.ts @@ -6,10 +6,10 @@ export class MatchingStringMatcher extends Matcher { } public match(value: any): boolean { - return value.match(this.expectedValue); + return this.reverseResult(value.match(this.expectedValue)); } public toString(): string { - return `match(${this.expectedValue})`; + return `${this.prefix}match(${this.expectedValue})`; } } diff --git a/src/matcher/type/ObjectContainingMatcher.ts b/src/matcher/type/ObjectContainingMatcher.ts index 7ed3814..c662324 100644 --- a/src/matcher/type/ObjectContainingMatcher.ts +++ b/src/matcher/type/ObjectContainingMatcher.ts @@ -7,10 +7,10 @@ export class ObjectContainingMatcher extends Matcher { } public match(value: Object): boolean { - return _.isMatch(value, this.expectedValue); + return this.reverseResult(_.isMatch(value, this.expectedValue)); } public toString(): string { - return `objectContaining(${this.expectedValue})`; + return `${this.prefix}objectContaining(${this.expectedValue})`; } } diff --git a/test/matcher/type/MatchingStringMatcher.spec.ts b/test/matcher/type/MatchingStringMatcher.spec.ts index e4288d0..c1b7673 100644 --- a/test/matcher/type/MatchingStringMatcher.spec.ts +++ b/test/matcher/type/MatchingStringMatcher.spec.ts @@ -9,9 +9,11 @@ describe("MatchingStringMatcher", () => { it("returns true", () => { // when const result = testObj.match("a123"); + const notResult = testObj.not().match("a123"); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); @@ -19,9 +21,11 @@ describe("MatchingStringMatcher", () => { it("returns false", () => { // when const result = testObj.match("123"); + const notResult = testObj.not().match("123"); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); }); diff --git a/test/matcher/type/ObjectContainingMatcher.spec.ts b/test/matcher/type/ObjectContainingMatcher.spec.ts index d8c5376..a544452 100644 --- a/test/matcher/type/ObjectContainingMatcher.spec.ts +++ b/test/matcher/type/ObjectContainingMatcher.spec.ts @@ -9,17 +9,21 @@ describe("ObjectContainingMatcher", () => { it("returns true", () => { // when const result = testObj.match({a: "a", b: {c: "c", d: {}}}); + const notResult = testObj.not().match({a: "a", b: {c: "c", d: {}}}); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); it("returns true", () => { // when const result = testObj.match({b: {c: "c", d: {}}}); + const notResult = testObj.not().match({b: {c: "c", d: {}}}); // then expect(result).toBeTruthy(); + expect(notResult).toBeFalsy(); }); }); @@ -27,9 +31,11 @@ describe("ObjectContainingMatcher", () => { it("returns false", () => { // when const result = testObj.match({b: {c: "c"}}); + const notResult = testObj.not().match({b: {c: "c"}}); // then expect(result).toBeFalsy(); + expect(notResult).toBeTruthy(); }); }); }); From 4f4ed84f85055c351707e09b95da859324995514 Mon Sep 17 00:00:00 2001 From: cakeinpanic Date: Wed, 24 Jan 2018 00:36:52 +0300 Subject: [PATCH 07/12] update readme --- README.md | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a2d531b..bd4c9b2 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Mocking library for TypeScript inspired by http://mockito.org/ * `thenThrow` - throw an error * `thenCall` - call custom method * Checking if methods were called with given arguments (`verify`) - * `anything`, `notNull`, `anyString`, `anyOfClass` etc. - for more flexible comparision + * `anything`, `notNull`, `anyString`, `anyOfClass`, `not` etc. - for more flexible comparision * `once`, `twice`, `times`, `atLeast` etc. - allows call count verification * `calledBefore`, `calledAfter` - allows call order verification * Resetting mock (`reset`, `resetCalls`) @@ -105,13 +105,15 @@ foo.getBar(2); foo.getBar(3); // Call count verification -verify(mockedFoo.getBar(1)).once(); // was called with arg === 1 only once -verify(mockedFoo.getBar(2)).twice(); // was called with arg === 2 exactly two times -verify(mockedFoo.getBar(between(2, 3))).thrice(); // was called with arg beween 2-3 exactly three times -verify(mockedFoo.getBar(anyNumber()).times(4); // was called with any number arg exactly four times -verify(mockedFoo.getBar(2)).atLeast(2); // was called with arg === 2 min two times -verify(mockedFoo.getBar(1)).atMost(1); // was called with arg === 1 max one time -verify(mockedFoo.getBar(4)).never(); // was never called with arg === 4 +verify(mockedFoo.getBar(1)).once(); // was called with arg === 1 only once +verify(mockedFoo.getBar(2)).twice(); // was called with arg === 2 exactly two times +verify(mockedFoo.getBar(strictEqual(2).not())).once(); // was called with anything except 2 once +verify(mockedFoo.getBar(between(2, 3))).thrice(); // was called with arg beween 2-3 exactly three times +verify(mockedFoo.getBar(anyNumber()).times(4); // was called with any number arg exactly four times +verify(mockedFoo.getBar(anyNumber.not()).times(4); // was called with anything but not a number exactly four times +verify(mockedFoo.getBar(2)).atLeast(2); // was called with arg === 2 min two times +verify(mockedFoo.getBar(1)).atMost(1); // was called with arg === 1 max one time +verify(mockedFoo.getBar(4)).never(); // was never called with arg === 4 ``` ### Call order verification @@ -159,7 +161,7 @@ let mockedFoo:Foo = mock(Foo); let foo:Foo = instance(mockedFoo); when(mockedFoo.sumTwoNumbers(anyNumber(), anyNumber())).thenCall((arg1:number, arg2:number) => { - return arg1 * arg2; + return arg1 * arg2; }); // prints '50' because we've changed sum method implementation to multiply! @@ -335,13 +337,13 @@ const spiedFoo = spy(foo); foo.bar(); -console.log(capture(spiedFoo.bar).last()); // [42] +console.log(capture(spiedFoo.bar).last()); // [42] ``` ### Thanks -* Szczepan Faber (https://www.linkedin.com/in/szczepiq) -* Sebastian Konkol (https://www.linkedin.com/in/sebastiankonkol) +* Szczepan Faber (https://www.linkedin.com/in/szczepiq) +* Sebastian Konkol (https://www.linkedin.com/in/sebastiankonkol) * Clickmeeting (http://clickmeeting.com) * Michał Stocki (https://github.com/michalstocki) * Łukasz Bendykowski (https://github.com/viman) From 32bd8d845ee2ffe19946bf1514ae504c9e3cf3f7 Mon Sep 17 00:00:00 2001 From: cakeinpanic Date: Wed, 24 Jan 2018 00:59:54 +0300 Subject: [PATCH 08/12] add not() matcher --- README.md | 5 +- ...StringMatcher.ts => MatchStringMatcher.ts} | 2 +- src/matcher/type/Matcher.ts | 11 ++-- src/matcher/type/Not.ts | 60 +++++++++++++++++++ src/ts-mockito.ts | 15 +++-- src/utils/MockableFunctionsFinder.ts | 2 +- test/matcher/type/AnyFunctionMatcher.spec.ts | 14 +++-- test/matcher/type/AnyNumberMatcher.spec.ts | 14 +++-- test/matcher/type/AnyOfClassMatcher.spec.ts | 14 +++-- test/matcher/type/AnyStringMatcher.spec.ts | 12 ++-- test/matcher/type/AnythingMatcher.spec.ts | 14 +++-- test/matcher/type/BetweenMatcher.spec.ts | 14 +++-- test/matcher/type/DeepEqualMatcher.spec.ts | 20 ++++--- test/matcher/type/Matcher.spec.ts | 7 ++- .../type/MatchingStringMatcher.spec.ts | 9 +-- test/matcher/type/NotNullMatcher.spec.ts | 15 ++--- .../type/ObjectContainingMatcher.spec.ts | 9 +-- test/matcher/type/StrictEqualMatcher.spec.ts | 14 +++-- 18 files changed, 170 insertions(+), 81 deletions(-) rename src/matcher/type/{MatchingStringMatcher.ts => MatchStringMatcher.ts} (85%) create mode 100644 src/matcher/type/Not.ts diff --git a/README.md b/README.md index bd4c9b2..9b6951d 100644 --- a/README.md +++ b/README.md @@ -107,10 +107,11 @@ foo.getBar(3); // Call count verification verify(mockedFoo.getBar(1)).once(); // was called with arg === 1 only once verify(mockedFoo.getBar(2)).twice(); // was called with arg === 2 exactly two times -verify(mockedFoo.getBar(strictEqual(2).not())).once(); // was called with anything except 2 once +verify(mockedFoo.getBar(not().strictEqual(2))).once(); // was called with anything except 2 once verify(mockedFoo.getBar(between(2, 3))).thrice(); // was called with arg beween 2-3 exactly three times verify(mockedFoo.getBar(anyNumber()).times(4); // was called with any number arg exactly four times -verify(mockedFoo.getBar(anyNumber.not()).times(4); // was called with anything but not a number exactly four times +verify(mockedFoo.getBar(not().anyNumber).times(4); // was called with anything but not a number exactly four times +verify(mockedFoo.matchString(/param\d+/)).once(); // was once called with arg matching regexp verify(mockedFoo.getBar(2)).atLeast(2); // was called with arg === 2 min two times verify(mockedFoo.getBar(1)).atMost(1); // was called with arg === 1 max one time verify(mockedFoo.getBar(4)).never(); // was never called with arg === 4 diff --git a/src/matcher/type/MatchingStringMatcher.ts b/src/matcher/type/MatchStringMatcher.ts similarity index 85% rename from src/matcher/type/MatchingStringMatcher.ts rename to src/matcher/type/MatchStringMatcher.ts index 4d0f881..d42a434 100644 --- a/src/matcher/type/MatchingStringMatcher.ts +++ b/src/matcher/type/MatchStringMatcher.ts @@ -1,6 +1,6 @@ import {Matcher} from "./Matcher"; -export class MatchingStringMatcher extends Matcher { +export class MatchStringMatcher extends Matcher { constructor(private expectedValue: any) { super(); } diff --git a/src/matcher/type/Matcher.ts b/src/matcher/type/Matcher.ts index 1297c62..7ffe582 100644 --- a/src/matcher/type/Matcher.ts +++ b/src/matcher/type/Matcher.ts @@ -1,7 +1,5 @@ -import * as _ from "lodash"; - export class Matcher { - public isNot: boolean = false; + private isNot: boolean = false; public match(...values: any[]): boolean { return this.reverseResult(false); @@ -11,10 +9,9 @@ export class Matcher { return `${this.prefix}`; } - public not() { - const newMatcher = _.cloneDeep(this); - newMatcher.isNot = true; - return newMatcher; + public reverse() { + this.isNot = true; + return this; } protected get prefix(): string { diff --git a/src/matcher/type/Not.ts b/src/matcher/type/Not.ts new file mode 100644 index 0000000..fb77dd2 --- /dev/null +++ b/src/matcher/type/Not.ts @@ -0,0 +1,60 @@ +import {AnyFunctionMatcher} from "./AnyFunctionMatcher"; +import {AnyNumberMatcher} from "./AnyNumberMatcher"; +import {AnyOfClassMatcher} from "./AnyOfClassMatcher"; +import {AnyStringMatcher} from "./AnyStringMatcher"; +import {AnythingMatcher} from "./AnythingMatcher"; +import {BetweenMatcher} from "./BetweenMatcher"; +import {DeepEqualMatcher} from "./DeepEqualMatcher"; +import {Matcher} from "./Matcher"; +import {MatchStringMatcher} from "./MatchStringMatcher"; +import {NotNullMatcher} from "./NotNullMatcher"; +import {ObjectContainingMatcher} from "./ObjectContainingMatcher"; +import {StrictEqualMatcher} from "./StrictEqualMatcher"; + +export class Not { + + public anyOfClass(expectedClass: {new (...args: any[]): T}): Matcher { + return (new AnyOfClassMatcher(expectedClass)).reverse(); + } + + public anyFunction(): Matcher { + return (new AnyFunctionMatcher()).reverse(); + } + + public anyNumber(): Matcher { + return (new AnyNumberMatcher()).reverse(); + } + + public anyString(): Matcher { + return (new AnyStringMatcher()).reverse(); + } + + public anything(): Matcher { + return (new AnythingMatcher()).reverse(); + } + + public between(min: number, max: number): Matcher { + return (new BetweenMatcher(min, max)).reverse(); + } + + public deepEqual(expectedValue: any): Matcher { + return (new DeepEqualMatcher(expectedValue)).reverse(); + } + + public notNull(): Matcher { + return (new NotNullMatcher()).reverse(); + } + + public strictEqual(expectedValue: any): Matcher { + return (new StrictEqualMatcher(expectedValue)).reverse(); + } + + public matchString(expectedValue: string | RegExp): Matcher { + return (new MatchStringMatcher(expectedValue)).reverse(); + } + + public objectContaining(expectedValue: Object): Matcher { + return (new ObjectContainingMatcher(expectedValue)).reverse(); + } + +} diff --git a/src/ts-mockito.ts b/src/ts-mockito.ts index 9c51f33..e6863f5 100644 --- a/src/ts-mockito.ts +++ b/src/ts-mockito.ts @@ -19,7 +19,8 @@ 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 {MatchStringMatcher} from "./matcher/type/MatchStringMatcher"; +import {Not} from "./matcher/type/Not"; import {NotNullMatcher} from "./matcher/type/NotNullMatcher"; import {ObjectContainingMatcher} from "./matcher/type/ObjectContainingMatcher"; import {StrictEqualMatcher} from "./matcher/type/StrictEqualMatcher"; @@ -33,7 +34,7 @@ export function spy(instanceToSpy: T): T { return new Spy(instanceToSpy).getMock(); } -export function mock(clazz: { new(...args: any[]): T; } | (Function & { prototype: T }) ): T { +export function mock(clazz: {new(...args: any[]): T; } | (Function & {prototype: T})): T { return new Mocker(clazz).getMock(); } @@ -78,7 +79,7 @@ export function resetCalls(mockedValue: T): void { (mockedValue as any).__tsmockitoMocker.resetCalls(); } -export function anyOfClass(expectedClass: { new (...args: any[]): T }): any { +export function anyOfClass(expectedClass: {new (...args: any[]): T}): any { return new AnyOfClassMatcher(expectedClass) as any; } @@ -114,14 +115,18 @@ export function strictEqual(expectedValue: any): Matcher { return new StrictEqualMatcher(expectedValue); } -export function match(expectedValue: RegExp | string): Matcher { - return new MatchingStringMatcher(expectedValue); +export function matchString(expectedValue: RegExp | string): Matcher { + return new MatchStringMatcher(expectedValue); } export function objectContaining(expectedValue: Object): Matcher { return new ObjectContainingMatcher(expectedValue); } +export function not(): Not { + return new Not(); +} + import * as mockito from "./ts-mockito"; export default mockito; diff --git a/src/utils/MockableFunctionsFinder.ts b/src/utils/MockableFunctionsFinder.ts index 600b1cd..c73e6a4 100644 --- a/src/utils/MockableFunctionsFinder.ts +++ b/src/utils/MockableFunctionsFinder.ts @@ -1,6 +1,6 @@ /** * Looking for all function calls and declarations and provides an array of their names. The mechanism is greedy - * and tries to match as many function names as it can find and not only those of inspecting class. + * and tries to matchString as many function names as it can find and not only those of inspecting class. * * Matching occurrences are: * - [.]functionName( diff --git a/test/matcher/type/AnyFunctionMatcher.spec.ts b/test/matcher/type/AnyFunctionMatcher.spec.ts index f76de92..f32cf42 100644 --- a/test/matcher/type/AnyFunctionMatcher.spec.ts +++ b/test/matcher/type/AnyFunctionMatcher.spec.ts @@ -1,18 +1,20 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; -import {anyFunction} from "../../../src/ts-mockito"; +import {anyFunction, not} from "../../../src/ts-mockito"; describe("AnyFunctionMatcher", () => { let testObj: Matcher; + let notTestObj: Matcher; beforeEach(() => { testObj = anyFunction(); + notTestObj = not().anyFunction(); }); describe("checking if function is function", () => { it("returns true", () => { // when const result = testObj.match(() => "arbitrary return value"); - const notResult = testObj.not().match(() => "arbitrary return value"); + const notResult = notTestObj.match(() => "arbitrary return value"); // then expect(result).toBeTruthy(); @@ -24,7 +26,7 @@ describe("AnyFunctionMatcher", () => { it("returns false", () => { // when const result = testObj.match("some string"); - const notResult = testObj.not().match("some string"); + const notResult = notTestObj.match("some string"); // then expect(result).toBeFalsy(); @@ -36,7 +38,7 @@ describe("AnyFunctionMatcher", () => { it("returns false", () => { // when const result = testObj.match(5); - const notResult = testObj.not().match(5); + const notResult = notTestObj.match(5); // then expect(result).toBeFalsy(); @@ -48,7 +50,7 @@ describe("AnyFunctionMatcher", () => { it("returns false", () => { // when const result = testObj.match({prop1: "prop1Value"}); - const notResult = testObj.not().match({prop1: "prop1Value"}); + const notResult = notTestObj.match({prop1: "prop1Value"}); // then expect(result).toBeFalsy(); @@ -60,7 +62,7 @@ describe("AnyFunctionMatcher", () => { it("returns 'anyFunction()'", () => { // when const result = testObj.toString(); - const notResult = testObj.not().toString(); + const notResult = notTestObj.toString(); // then expect(result).toEqual("anyFunction()"); diff --git a/test/matcher/type/AnyNumberMatcher.spec.ts b/test/matcher/type/AnyNumberMatcher.spec.ts index 0f0aa77..92b3320 100644 --- a/test/matcher/type/AnyNumberMatcher.spec.ts +++ b/test/matcher/type/AnyNumberMatcher.spec.ts @@ -1,18 +1,20 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; -import {anyNumber} from "../../../src/ts-mockito"; +import {anyNumber, not} from "../../../src/ts-mockito"; describe("AnyNumberMatcher", () => { let testObj: Matcher; + let notTestObj: Matcher; beforeEach(() => { testObj = anyNumber(); + notTestObj = not().anyNumber(); }); describe("checking if positive number is matching", () => { it("returns true", () => { // when const result = testObj.match(3); - const notResult = testObj.not().match(3); + const notResult = notTestObj.match(3); // then expect(result).toBeTruthy(); @@ -24,7 +26,7 @@ describe("AnyNumberMatcher", () => { it("returns true", () => { // when const result = testObj.match(-3); - const notResult = testObj.not().match(-3); + const notResult = notTestObj.match(-3); // then expect(result).toBeTruthy(); @@ -36,7 +38,7 @@ describe("AnyNumberMatcher", () => { it("returns true", () => { // when const result = testObj.match(0); - const notResult = testObj.not().match(0); + const notResult = notTestObj.match(0); // then expect(result).toBeTruthy(); @@ -48,7 +50,7 @@ describe("AnyNumberMatcher", () => { it("returns false", () => { // when const result = testObj.match("5"); - const notResult = testObj.not().match("5"); + const notResult = notTestObj.match("5"); // then expect(result).toBeFalsy(); @@ -60,7 +62,7 @@ describe("AnyNumberMatcher", () => { it("returns false", () => { // when const result = testObj.match({}); - const notResult = testObj.not().match({}); + const notResult = notTestObj.match({}); // then expect(result).toBeFalsy(); diff --git a/test/matcher/type/AnyOfClassMatcher.spec.ts b/test/matcher/type/AnyOfClassMatcher.spec.ts index 3f39e58..12c226c 100644 --- a/test/matcher/type/AnyOfClassMatcher.spec.ts +++ b/test/matcher/type/AnyOfClassMatcher.spec.ts @@ -1,8 +1,9 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; -import {anyOfClass} from "../../../src/ts-mockito"; +import {anyOfClass, not} from "../../../src/ts-mockito"; describe("AnyOfClassMatcher", () => { let testObj: Matcher; + let notTestObj: Matcher; class Car { constructor() { @@ -11,6 +12,7 @@ describe("AnyOfClassMatcher", () => { beforeEach(() => { testObj = anyOfClass(Car); + notTestObj = not().anyOfClass(Car); }); describe("checking if class matches", () => { @@ -19,7 +21,7 @@ describe("AnyOfClassMatcher", () => { // when const result = testObj.match(value); - const notResult = testObj.not().match(value); + const notResult = notTestObj.match(value); // then expect(result).toBeTruthy(); @@ -33,7 +35,7 @@ describe("AnyOfClassMatcher", () => { // when const result = testObj.match(value); - const notResult = testObj.not().match(value); + const notResult = notTestObj.match(value); // then expect(result).toBeFalsy(); @@ -52,13 +54,13 @@ describe("AnyOfClassMatcher", () => { }); }); - describe("checking if different classes match", () => { + describe("checking if different classes matchString", () => { it("returns false", () => { const value = "a string"; // when const result = testObj.match(value); - const notResult = testObj.not().match(value); + const notResult = notTestObj.match(value); // then expect(result).toBeFalsy(); @@ -69,7 +71,7 @@ describe("AnyOfClassMatcher", () => { describe("checking if toString works", () => { it("returns 'anyOfClass(Car)'", () => { const result = testObj.toString(); - const notResult = testObj.not().toString(); + const notResult = notTestObj.toString(); // then expect(result).toEqual("anyOfClass(Car)"); diff --git a/test/matcher/type/AnyStringMatcher.spec.ts b/test/matcher/type/AnyStringMatcher.spec.ts index baa7911..4f8e5b0 100644 --- a/test/matcher/type/AnyStringMatcher.spec.ts +++ b/test/matcher/type/AnyStringMatcher.spec.ts @@ -1,18 +1,20 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; -import {anyString} from "../../../src/ts-mockito"; +import {anyString, not} from "../../../src/ts-mockito"; describe("AnyStringMatcher", () => { let testObj: Matcher; + let notTestObj: Matcher; beforeEach(() => { testObj = anyString(); + notTestObj = not().anyString(); }); describe("checking if number matches", () => { it("returns false", () => { // when const result = testObj.match(3); - const notResult = testObj.not().match(3); + const notResult = notTestObj.match(3); // then expect(result).toBeFalsy(); @@ -25,7 +27,7 @@ describe("AnyStringMatcher", () => { // when const result = testObj.match({}); - const notResult = testObj.not().match({}); + const notResult = notTestObj.match({}); // then expect(result).toBeFalsy(); @@ -37,7 +39,7 @@ describe("AnyStringMatcher", () => { it("returns true", () => { // when const result = testObj.match(""); - const notResult = testObj.not().match(""); + const notResult = notTestObj.match(""); // then expect(result).toBeTruthy(); @@ -49,7 +51,7 @@ describe("AnyStringMatcher", () => { it("returns true", () => { // when const result = testObj.match("sampleString"); - const notResult = testObj.not().match("sampleString"); + const notResult = notTestObj.match("sampleString"); // then expect(result).toBeTruthy(); diff --git a/test/matcher/type/AnythingMatcher.spec.ts b/test/matcher/type/AnythingMatcher.spec.ts index 834c290..8d5c6ec 100644 --- a/test/matcher/type/AnythingMatcher.spec.ts +++ b/test/matcher/type/AnythingMatcher.spec.ts @@ -1,18 +1,20 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; -import {anything} from "../../../src/ts-mockito"; +import {anything, not} from "../../../src/ts-mockito"; describe("AnythingMatcher", () => { let testObj: Matcher; + let notTestObj: Matcher; beforeEach(() => { testObj = anything(); + notTestObj = not().anything(); }); describe("checking if number matches", () => { it("returns true", () => { // when const result = testObj.match(3); - const notResult = testObj.not().match(3); + const notResult = notTestObj.match(3); // then expect(result).toBeTruthy(); @@ -24,7 +26,7 @@ describe("AnythingMatcher", () => { it("returns true", () => { // when const result = testObj.match({}); - const notResult = testObj.not().match({}); + const notResult = notTestObj.match({}); // then expect(result).toBeTruthy(); @@ -36,7 +38,7 @@ describe("AnythingMatcher", () => { it("returns true", () => { // when const result = testObj.match(""); - const notResult = testObj.not().match(""); + const notResult = notTestObj.match(""); // then expect(result).toBeTruthy(); @@ -48,7 +50,7 @@ describe("AnythingMatcher", () => { it("returns true", () => { // when const result = testObj.match("sampleString"); - const notResult = testObj.not().match("sampleString"); + const notResult = notTestObj.match("sampleString"); // then expect(result).toBeTruthy(); @@ -60,7 +62,7 @@ describe("AnythingMatcher", () => { it("returns true", () => { // when const result = testObj.match(null); - const notResult = testObj.not().match(null); + const notResult = notTestObj.match(null); // then expect(result).toBeTruthy(); diff --git a/test/matcher/type/BetweenMatcher.spec.ts b/test/matcher/type/BetweenMatcher.spec.ts index af98afc..b44b4aa 100644 --- a/test/matcher/type/BetweenMatcher.spec.ts +++ b/test/matcher/type/BetweenMatcher.spec.ts @@ -1,19 +1,21 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; -import {between} from "../../../src/ts-mockito"; +import {between, not} from "../../../src/ts-mockito"; describe("BetweenMatcher", () => { describe("checking if value matches given min and max", () => { let testObj: Matcher; + let notTestObj: Matcher; beforeEach(() => { testObj = between(5, 10); + notTestObj = not().between(5, 10); }); describe("when given value is lower than min", () => { it("returns false", () => { // when const result = testObj.match(4); - const notResult = testObj.not().match(4); + const notResult = notTestObj.match(4); // then expect(result).toBeFalsy(); @@ -25,7 +27,7 @@ describe("BetweenMatcher", () => { it("returns true", () => { // when const result = testObj.match(5); - // const notResult = testObj.not().match(5); + // const notResult = notTestObj.matchString(5); // then expect(result).toBeTruthy(); @@ -37,7 +39,7 @@ describe("BetweenMatcher", () => { it("returns true", () => { // when const result = testObj.match(7); - const notResult = testObj.not().match(7); + const notResult = notTestObj.match(7); // then expect(result).toBeTruthy(); @@ -49,7 +51,7 @@ describe("BetweenMatcher", () => { it("returns true", () => { // when const result = testObj.match(10); - const notResult = testObj.not().match(10); + const notResult = notTestObj.match(10); // then expect(result).toBeTruthy(); @@ -61,7 +63,7 @@ describe("BetweenMatcher", () => { it("returns true", () => { // when const result = testObj.match(11); - const notResult = testObj.not().match(11); + const notResult = notTestObj.match(11); // then expect(result).toBeFalsy(); diff --git a/test/matcher/type/DeepEqualMatcher.spec.ts b/test/matcher/type/DeepEqualMatcher.spec.ts index 6a5f030..97a7234 100644 --- a/test/matcher/type/DeepEqualMatcher.spec.ts +++ b/test/matcher/type/DeepEqualMatcher.spec.ts @@ -1,5 +1,5 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; -import {anyString, deepEqual} from "../../../src/ts-mockito"; +import {anyString, deepEqual, not} from "../../../src/ts-mockito"; describe("DeepEqualMatcher", () => { describe("checking if two different instances of same number matches", () => { @@ -8,10 +8,11 @@ describe("DeepEqualMatcher", () => { const firstValue = 3; const secondValue = 3; const testObj: Matcher = deepEqual(firstValue); + const notTestObj: Matcher = not().deepEqual(firstValue); // when const result = testObj.match(secondValue); - const notResult = testObj.not().match(secondValue); + const notResult = notTestObj.match(secondValue); // then expect(result).toBeTruthy(); @@ -25,10 +26,11 @@ describe("DeepEqualMatcher", () => { const firstValue = "sampleString"; const secondValue = "sampleString"; const testObj: Matcher = deepEqual(firstValue); + const notTestObj: Matcher = not().deepEqual(firstValue); // when const result = testObj.match(secondValue); - const notResult = testObj.not().match(secondValue); + const notResult = notTestObj.match(secondValue); // then expect(result).toBeTruthy(); @@ -42,10 +44,11 @@ describe("DeepEqualMatcher", () => { const firstValue = {a: 1, b: {c: 2}}; const secondValue = {a: 1, b: {c: 2}}; const testObj: Matcher = deepEqual(firstValue); + const notTestObj: Matcher = not().deepEqual(firstValue); // when const result = testObj.match(secondValue); - const notResult = testObj.not().match(secondValue); + const notResult = notTestObj.match(secondValue); // then expect(result).toBeTruthy(); @@ -59,10 +62,11 @@ describe("DeepEqualMatcher", () => { const firstValue = {a: 1, b: {c: 2}}; const secondValue = {a: 1, b: {c: 99999}}; const testObj: Matcher = deepEqual(firstValue); + const notTestObj: Matcher = not().deepEqual(firstValue); // when const result = testObj.match(secondValue); - const notResult = testObj.not().match(secondValue); + const notResult = notTestObj.match(secondValue); // then expect(result).toBeFalsy(); @@ -76,10 +80,11 @@ describe("DeepEqualMatcher", () => { const firstValue = {a: 1, b: anyString()}; const secondValue = {a: 1, b: "2"}; const testObj: Matcher = deepEqual(firstValue); + const notTestObj: Matcher = not().deepEqual(firstValue); // when const result = testObj.match(secondValue); - const notResult = testObj.not().match(secondValue); + const notResult = notTestObj.match(secondValue); // then expect(result).toBeTruthy(); @@ -91,10 +96,11 @@ describe("DeepEqualMatcher", () => { const firstValue = {a: 1, b: anyString()}; const secondValue = {a: 1, b: 2}; const testObj: Matcher = deepEqual(firstValue); + const notTestObj: Matcher = not().deepEqual(firstValue); // when const result = testObj.match(secondValue); - const notResult = testObj.not().match(secondValue); + const notResult = notTestObj.match(secondValue); // then expect(result).toBeFalsy(); diff --git a/test/matcher/type/Matcher.spec.ts b/test/matcher/type/Matcher.spec.ts index f711e29..a879789 100644 --- a/test/matcher/type/Matcher.spec.ts +++ b/test/matcher/type/Matcher.spec.ts @@ -6,12 +6,13 @@ export function xor(a: boolean, b: boolean): boolean { describe("Matcher", () => { const testObj: Matcher = new Matcher(); + const notTestObj: Matcher = (new Matcher()).reverse(); - describe("not", () => { + describe("reverse", () => { it("returns opposite value", () => { // when const result = testObj.match(null); - const notResult = testObj.not().match(null); + const notResult = notTestObj.match(null); // then expect(xor(result, notResult)).toBeFalsy(); @@ -20,7 +21,7 @@ describe("Matcher", () => { it("returns toString with 'not' prepended", () => { // when const result = testObj.toString(); - const notResult = testObj.not().toString(); + const notResult = notTestObj.toString(); // then expect(`not().${result}`).toEqual(notResult); diff --git a/test/matcher/type/MatchingStringMatcher.spec.ts b/test/matcher/type/MatchingStringMatcher.spec.ts index c1b7673..2464f41 100644 --- a/test/matcher/type/MatchingStringMatcher.spec.ts +++ b/test/matcher/type/MatchingStringMatcher.spec.ts @@ -1,15 +1,16 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; -import {match} from "../../../src/ts-mockito"; +import {matchString, not} from "../../../src/ts-mockito"; describe("MatchingStringMatcher", () => { describe("checking if value matches given regexp", () => { - const testObj: Matcher = match(/\w123/); + const testObj: Matcher = matchString(/\w123/); + const notTestObj: Matcher = not().matchString(/\w123/); describe("when given value matches regexp", () => { it("returns true", () => { // when const result = testObj.match("a123"); - const notResult = testObj.not().match("a123"); + const notResult = notTestObj.match("a123"); // then expect(result).toBeTruthy(); @@ -21,7 +22,7 @@ describe("MatchingStringMatcher", () => { it("returns false", () => { // when const result = testObj.match("123"); - const notResult = testObj.not().match("123"); + const notResult = notTestObj.match("123"); // then expect(result).toBeFalsy(); diff --git a/test/matcher/type/NotNullMatcher.spec.ts b/test/matcher/type/NotNullMatcher.spec.ts index 3f9aef3..6182acd 100644 --- a/test/matcher/type/NotNullMatcher.spec.ts +++ b/test/matcher/type/NotNullMatcher.spec.ts @@ -1,19 +1,20 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; -import {notNull} from "../../../src/ts-mockito"; +import {not, notNull} from "../../../src/ts-mockito"; describe("NotNullMatcher", () => { let testObj: Matcher; + let notTestObj: Matcher; beforeEach(() => { testObj = notNull(); - + notTestObj = not().notNull(); }); describe("checking if null matches", () => { it("returns false", () => { // when const result = testObj.match(null); - const notResult = testObj.not().match(null); + const notResult = notTestObj.match(null); // then expect(result).toBeFalsy(); @@ -25,7 +26,7 @@ describe("NotNullMatcher", () => { it("returns true", () => { // when const result = testObj.match(false); - const notResult = testObj.not().match(false); + const notResult = notTestObj.match(false); // then expect(result).toBeTruthy(); @@ -37,7 +38,7 @@ describe("NotNullMatcher", () => { it("returns true", () => { // when const result = testObj.match(0); - const notResult = testObj.not().match(0); + const notResult = notTestObj.match(0); // then expect(result).toBeTruthy(); @@ -49,7 +50,7 @@ describe("NotNullMatcher", () => { it("returns true", () => { // when const result = testObj.match({}); - const notResult = testObj.not().match({}); + const notResult = notTestObj.match({}); // then expect(result).toBeTruthy(); @@ -61,7 +62,7 @@ describe("NotNullMatcher", () => { it("returns true", () => { // when const result = testObj.match("sampleString"); - const notResult = testObj.not().match("sampleString"); + const notResult = notTestObj.match("sampleString"); // then expect(result).toBeTruthy(); diff --git a/test/matcher/type/ObjectContainingMatcher.spec.ts b/test/matcher/type/ObjectContainingMatcher.spec.ts index a544452..f510276 100644 --- a/test/matcher/type/ObjectContainingMatcher.spec.ts +++ b/test/matcher/type/ObjectContainingMatcher.spec.ts @@ -1,15 +1,16 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; -import {objectContaining} from "../../../src/ts-mockito"; +import {not, objectContaining} from "../../../src/ts-mockito"; describe("ObjectContainingMatcher", () => { describe("checking if source object contains given object", () => { const testObj: Matcher = objectContaining({b: {c: "c", d: {}}}); + const notTestObj: Matcher = not().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: {}}}); - const notResult = testObj.not().match({a: "a", b: {c: "c", d: {}}}); + const notResult = notTestObj.match({a: "a", b: {c: "c", d: {}}}); // then expect(result).toBeTruthy(); @@ -19,7 +20,7 @@ describe("ObjectContainingMatcher", () => { it("returns true", () => { // when const result = testObj.match({b: {c: "c", d: {}}}); - const notResult = testObj.not().match({b: {c: "c", d: {}}}); + const notResult = notTestObj.match({b: {c: "c", d: {}}}); // then expect(result).toBeTruthy(); @@ -31,7 +32,7 @@ describe("ObjectContainingMatcher", () => { it("returns false", () => { // when const result = testObj.match({b: {c: "c"}}); - const notResult = testObj.not().match({b: {c: "c"}}); + const notResult = notTestObj.match({b: {c: "c"}}); // then expect(result).toBeFalsy(); diff --git a/test/matcher/type/StrictEqualMatcher.spec.ts b/test/matcher/type/StrictEqualMatcher.spec.ts index bc19afe..becd21a 100644 --- a/test/matcher/type/StrictEqualMatcher.spec.ts +++ b/test/matcher/type/StrictEqualMatcher.spec.ts @@ -1,15 +1,16 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; -import {strictEqual} from "../../../src/ts-mockito"; +import {not, strictEqual} from "../../../src/ts-mockito"; describe("StrictEqualMatcher", () => { describe("checking if string representation of number matches with number", () => { it("returns false", () => { // given const testObj: Matcher = strictEqual("5"); + const notTestObj: Matcher = not().strictEqual("5"); // when const result = testObj.match(5); - const notResult = testObj.not().match(5); + const notResult = notTestObj.match(5); // then expect(result).toBeFalsy(); @@ -21,10 +22,11 @@ describe("StrictEqualMatcher", () => { it("returns false", () => { // given const testObj: Matcher = strictEqual(false); + const notTestObj: Matcher = not().strictEqual(false); // when const result = testObj.match(0); - const notResult = testObj.not().match(0); + const notResult = notTestObj.match(0); // then expect(result).toBeFalsy(); @@ -36,10 +38,11 @@ describe("StrictEqualMatcher", () => { it("returns false", () => { // given const testObj: Matcher = strictEqual(true); + const notTestObj: Matcher = not().strictEqual(true); // when const result = testObj.match(1); - const notResult = testObj.not().match(1); + const notResult = notTestObj.match(1); // then expect(result).toBeFalsy(); @@ -51,10 +54,11 @@ describe("StrictEqualMatcher", () => { it("returns true", () => { // given const testObj: Matcher = strictEqual("5"); + const notTestObj: Matcher = not().strictEqual("5"); // when const result = testObj.match("5"); - const notResult = testObj.not().match("5"); + const notResult = notTestObj.match("5"); // then expect(result).toBeTruthy(); From 02c16af8fa50b8bf1215d103a9076a38976541d2 Mon Sep 17 00:00:00 2001 From: cakeinpanic Date: Tue, 6 Feb 2018 11:28:03 +0300 Subject: [PATCH 09/12] fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9b6951d..965fd5b 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ verify(mockedFoo.getBar(2)).twice(); // was called with arg = verify(mockedFoo.getBar(not().strictEqual(2))).once(); // was called with anything except 2 once verify(mockedFoo.getBar(between(2, 3))).thrice(); // was called with arg beween 2-3 exactly three times verify(mockedFoo.getBar(anyNumber()).times(4); // was called with any number arg exactly four times -verify(mockedFoo.getBar(not().anyNumber).times(4); // was called with anything but not a number exactly four times +verify(mockedFoo.getBar(not().anyNumber()).times(4); // was called with anything but not a number exactly four times verify(mockedFoo.matchString(/param\d+/)).once(); // was once called with arg matching regexp verify(mockedFoo.getBar(2)).atLeast(2); // was called with arg === 2 min two times verify(mockedFoo.getBar(1)).atMost(1); // was called with arg === 1 max one time From 7f4947fde5b487df5a47edd9be1388d9ffe24fb1 Mon Sep 17 00:00:00 2001 From: cakeinpanic Date: Tue, 6 Feb 2018 11:30:25 +0300 Subject: [PATCH 10/12] rename matchString to match --- README.md | 2 +- .../type/{MatchStringMatcher.ts => MatchMatcher.ts} | 0 src/matcher/type/Not.ts | 4 ++-- src/ts-mockito.ts | 4 ++-- test/matcher/type/AnyOfClassMatcher.spec.ts | 2 +- test/matcher/type/BetweenMatcher.spec.ts | 4 ++-- ...MatchingStringMatcher.spec.ts => MatchMatcher.spec.ts} | 8 ++++---- 7 files changed, 12 insertions(+), 12 deletions(-) rename src/matcher/type/{MatchStringMatcher.ts => MatchMatcher.ts} (100%) rename test/matcher/type/{MatchingStringMatcher.spec.ts => MatchMatcher.spec.ts} (80%) diff --git a/README.md b/README.md index 965fd5b..137fb4f 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ verify(mockedFoo.getBar(not().strictEqual(2))).once(); // was called with anyth verify(mockedFoo.getBar(between(2, 3))).thrice(); // was called with arg beween 2-3 exactly three times verify(mockedFoo.getBar(anyNumber()).times(4); // was called with any number arg exactly four times verify(mockedFoo.getBar(not().anyNumber()).times(4); // was called with anything but not a number exactly four times -verify(mockedFoo.matchString(/param\d+/)).once(); // was once called with arg matching regexp +verify(mockedFoo.match(/param\d+/)).once(); // was once called with arg matching regexp verify(mockedFoo.getBar(2)).atLeast(2); // was called with arg === 2 min two times verify(mockedFoo.getBar(1)).atMost(1); // was called with arg === 1 max one time verify(mockedFoo.getBar(4)).never(); // was never called with arg === 4 diff --git a/src/matcher/type/MatchStringMatcher.ts b/src/matcher/type/MatchMatcher.ts similarity index 100% rename from src/matcher/type/MatchStringMatcher.ts rename to src/matcher/type/MatchMatcher.ts diff --git a/src/matcher/type/Not.ts b/src/matcher/type/Not.ts index fb77dd2..4731a64 100644 --- a/src/matcher/type/Not.ts +++ b/src/matcher/type/Not.ts @@ -6,7 +6,7 @@ import {AnythingMatcher} from "./AnythingMatcher"; import {BetweenMatcher} from "./BetweenMatcher"; import {DeepEqualMatcher} from "./DeepEqualMatcher"; import {Matcher} from "./Matcher"; -import {MatchStringMatcher} from "./MatchStringMatcher"; +import {MatchStringMatcher} from "./MatchMatcher"; import {NotNullMatcher} from "./NotNullMatcher"; import {ObjectContainingMatcher} from "./ObjectContainingMatcher"; import {StrictEqualMatcher} from "./StrictEqualMatcher"; @@ -49,7 +49,7 @@ export class Not { return (new StrictEqualMatcher(expectedValue)).reverse(); } - public matchString(expectedValue: string | RegExp): Matcher { + public match(expectedValue: string | RegExp): Matcher { return (new MatchStringMatcher(expectedValue)).reverse(); } diff --git a/src/ts-mockito.ts b/src/ts-mockito.ts index e6863f5..41df6c8 100644 --- a/src/ts-mockito.ts +++ b/src/ts-mockito.ts @@ -19,7 +19,7 @@ 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 {MatchStringMatcher} from "./matcher/type/MatchStringMatcher"; +import {MatchStringMatcher} from "./matcher/type/MatchMatcher"; import {Not} from "./matcher/type/Not"; import {NotNullMatcher} from "./matcher/type/NotNullMatcher"; import {ObjectContainingMatcher} from "./matcher/type/ObjectContainingMatcher"; @@ -115,7 +115,7 @@ export function strictEqual(expectedValue: any): Matcher { return new StrictEqualMatcher(expectedValue); } -export function matchString(expectedValue: RegExp | string): Matcher { +export function match(expectedValue: RegExp | string): Matcher { return new MatchStringMatcher(expectedValue); } diff --git a/test/matcher/type/AnyOfClassMatcher.spec.ts b/test/matcher/type/AnyOfClassMatcher.spec.ts index 12c226c..d208e48 100644 --- a/test/matcher/type/AnyOfClassMatcher.spec.ts +++ b/test/matcher/type/AnyOfClassMatcher.spec.ts @@ -54,7 +54,7 @@ describe("AnyOfClassMatcher", () => { }); }); - describe("checking if different classes matchString", () => { + describe("checking if different classes match", () => { it("returns false", () => { const value = "a string"; diff --git a/test/matcher/type/BetweenMatcher.spec.ts b/test/matcher/type/BetweenMatcher.spec.ts index b44b4aa..4c4e1fb 100644 --- a/test/matcher/type/BetweenMatcher.spec.ts +++ b/test/matcher/type/BetweenMatcher.spec.ts @@ -27,11 +27,11 @@ describe("BetweenMatcher", () => { it("returns true", () => { // when const result = testObj.match(5); - // const notResult = notTestObj.matchString(5); + const notResult = notTestObj.match(5); // then expect(result).toBeTruthy(); - // expect(notResult).toBeFalsy(); + expect(notResult).toBeFalsy(); }); }); diff --git a/test/matcher/type/MatchingStringMatcher.spec.ts b/test/matcher/type/MatchMatcher.spec.ts similarity index 80% rename from test/matcher/type/MatchingStringMatcher.spec.ts rename to test/matcher/type/MatchMatcher.spec.ts index 2464f41..2c73a1a 100644 --- a/test/matcher/type/MatchingStringMatcher.spec.ts +++ b/test/matcher/type/MatchMatcher.spec.ts @@ -1,10 +1,10 @@ import {Matcher} from "../../../src/matcher/type/Matcher"; -import {matchString, not} from "../../../src/ts-mockito"; +import {match, not} from "../../../src/ts-mockito"; -describe("MatchingStringMatcher", () => { +describe("MatchingMatcher", () => { describe("checking if value matches given regexp", () => { - const testObj: Matcher = matchString(/\w123/); - const notTestObj: Matcher = not().matchString(/\w123/); + const testObj: Matcher = match(/\w123/); + const notTestObj: Matcher = not().match(/\w123/); describe("when given value matches regexp", () => { it("returns true", () => { From 5bab8367b3a20e17d50c534f9dc758f48d652e51 Mon Sep 17 00:00:00 2001 From: cakeinpanic Date: Tue, 6 Feb 2018 14:28:09 +0300 Subject: [PATCH 11/12] update unit tests cases names to correspond testing code --- test/MethodAction.spec.ts | 4 ++-- test/matcher/type/AnyFunctionMatcher.spec.ts | 16 +++++++++++----- test/matcher/type/AnyNumberMatcher.spec.ts | 10 +++++----- test/matcher/type/AnyOfClassMatcher.spec.ts | 13 +++++++++---- test/matcher/type/AnyStringMatcher.spec.ts | 8 ++++---- test/matcher/type/AnythingMatcher.spec.ts | 10 +++++----- test/matcher/type/BetweenMatcher.spec.ts | 10 +++++----- test/matcher/type/DeepEqualMatcher.spec.ts | 12 ++++++------ test/matcher/type/MatchMatcher.spec.ts | 4 ++-- test/matcher/type/NotNullMatcher.spec.ts | 10 +++++----- .../matcher/type/ObjectContainingMatcher.spec.ts | 6 +++--- test/matcher/type/StrictEqualMatcher.spec.ts | 8 ++++---- 12 files changed, 61 insertions(+), 50 deletions(-) diff --git a/test/MethodAction.spec.ts b/test/MethodAction.spec.ts index 618ace2..0483246 100644 --- a/test/MethodAction.spec.ts +++ b/test/MethodAction.spec.ts @@ -4,7 +4,7 @@ import {strictEqual} from "../src/ts-mockito"; describe("MethodAction", () => { describe("checking if method with matchers is applicable", () => { describe("when all matchers match", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // given const methodName = "sampleMethodName"; const firstArg = 5; @@ -19,7 +19,7 @@ describe("MethodAction", () => { }); }); describe("when one matcher doesn`t match", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { // given const methodName = "sampleMethodName"; const firstArg = 5; diff --git a/test/matcher/type/AnyFunctionMatcher.spec.ts b/test/matcher/type/AnyFunctionMatcher.spec.ts index f32cf42..2017bfa 100644 --- a/test/matcher/type/AnyFunctionMatcher.spec.ts +++ b/test/matcher/type/AnyFunctionMatcher.spec.ts @@ -11,7 +11,7 @@ describe("AnyFunctionMatcher", () => { }); describe("checking if function is function", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match(() => "arbitrary return value"); const notResult = notTestObj.match(() => "arbitrary return value"); @@ -23,7 +23,7 @@ describe("AnyFunctionMatcher", () => { }); describe("checking if string is function", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { // when const result = testObj.match("some string"); const notResult = notTestObj.match("some string"); @@ -35,7 +35,7 @@ describe("AnyFunctionMatcher", () => { }); describe("checking if number is function", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { // when const result = testObj.match(5); const notResult = notTestObj.match(5); @@ -47,7 +47,7 @@ describe("AnyFunctionMatcher", () => { }); describe("checking if object is function", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { // when const result = testObj.match({prop1: "prop1Value"}); const notResult = notTestObj.match({prop1: "prop1Value"}); @@ -62,10 +62,16 @@ describe("AnyFunctionMatcher", () => { it("returns 'anyFunction()'", () => { // when const result = testObj.toString(); - const notResult = notTestObj.toString(); // then expect(result).toEqual("anyFunction()"); + }); + + it("returns 'not().anyFunction()' for .not() matcher", () => { + // when + const notResult = notTestObj.toString(); + + // then expect(notResult).toEqual("not().anyFunction()"); }); }); diff --git a/test/matcher/type/AnyNumberMatcher.spec.ts b/test/matcher/type/AnyNumberMatcher.spec.ts index 92b3320..8bd2164 100644 --- a/test/matcher/type/AnyNumberMatcher.spec.ts +++ b/test/matcher/type/AnyNumberMatcher.spec.ts @@ -11,7 +11,7 @@ describe("AnyNumberMatcher", () => { }); describe("checking if positive number is matching", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match(3); const notResult = notTestObj.match(3); @@ -23,7 +23,7 @@ describe("AnyNumberMatcher", () => { }); describe("checking if negative number is matching", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match(-3); const notResult = notTestObj.match(-3); @@ -35,7 +35,7 @@ describe("AnyNumberMatcher", () => { }); describe("checking if zero is matching", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match(0); const notResult = notTestObj.match(0); @@ -47,7 +47,7 @@ describe("AnyNumberMatcher", () => { }); describe("checking if string representation of number is matching", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { // when const result = testObj.match("5"); const notResult = notTestObj.match("5"); @@ -59,7 +59,7 @@ describe("AnyNumberMatcher", () => { }); describe("checking if object is matching", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { // when const result = testObj.match({}); const notResult = notTestObj.match({}); diff --git a/test/matcher/type/AnyOfClassMatcher.spec.ts b/test/matcher/type/AnyOfClassMatcher.spec.ts index d208e48..415ae84 100644 --- a/test/matcher/type/AnyOfClassMatcher.spec.ts +++ b/test/matcher/type/AnyOfClassMatcher.spec.ts @@ -16,7 +16,7 @@ describe("AnyOfClassMatcher", () => { }); describe("checking if class matches", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { const value = new Car(); // when @@ -30,7 +30,7 @@ describe("AnyOfClassMatcher", () => { }); describe("checking if null matches", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { const value = null; // when @@ -55,7 +55,7 @@ describe("AnyOfClassMatcher", () => { }); describe("checking if different classes match", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { const value = "a string"; // when @@ -71,10 +71,15 @@ describe("AnyOfClassMatcher", () => { describe("checking if toString works", () => { it("returns 'anyOfClass(Car)'", () => { const result = testObj.toString(); - const notResult = notTestObj.toString(); // then expect(result).toEqual("anyOfClass(Car)"); + }); + + it("returns 'not().anyOfClass(Car)' for .not() matcher", () => { + const notResult = notTestObj.toString(); + + // then expect(notResult).toEqual("not().anyOfClass(Car)"); }); }); diff --git a/test/matcher/type/AnyStringMatcher.spec.ts b/test/matcher/type/AnyStringMatcher.spec.ts index 4f8e5b0..18384a9 100644 --- a/test/matcher/type/AnyStringMatcher.spec.ts +++ b/test/matcher/type/AnyStringMatcher.spec.ts @@ -11,7 +11,7 @@ describe("AnyStringMatcher", () => { }); describe("checking if number matches", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { // when const result = testObj.match(3); const notResult = notTestObj.match(3); @@ -23,7 +23,7 @@ describe("AnyStringMatcher", () => { }); describe("checking if object matches", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { // when const result = testObj.match({}); @@ -36,7 +36,7 @@ describe("AnyStringMatcher", () => { }); describe("checking if empty string matches", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match(""); const notResult = notTestObj.match(""); @@ -48,7 +48,7 @@ describe("AnyStringMatcher", () => { }); describe("checking if sample string matches", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match("sampleString"); const notResult = notTestObj.match("sampleString"); diff --git a/test/matcher/type/AnythingMatcher.spec.ts b/test/matcher/type/AnythingMatcher.spec.ts index 8d5c6ec..af37299 100644 --- a/test/matcher/type/AnythingMatcher.spec.ts +++ b/test/matcher/type/AnythingMatcher.spec.ts @@ -11,7 +11,7 @@ describe("AnythingMatcher", () => { }); describe("checking if number matches", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match(3); const notResult = notTestObj.match(3); @@ -23,7 +23,7 @@ describe("AnythingMatcher", () => { }); describe("checking if object matches", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match({}); const notResult = notTestObj.match({}); @@ -35,7 +35,7 @@ describe("AnythingMatcher", () => { }); describe("checking if empty string matches", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match(""); const notResult = notTestObj.match(""); @@ -47,7 +47,7 @@ describe("AnythingMatcher", () => { }); describe("checking if sample string matches", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match("sampleString"); const notResult = notTestObj.match("sampleString"); @@ -59,7 +59,7 @@ describe("AnythingMatcher", () => { }); describe("checking if null matches", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match(null); const notResult = notTestObj.match(null); diff --git a/test/matcher/type/BetweenMatcher.spec.ts b/test/matcher/type/BetweenMatcher.spec.ts index 4c4e1fb..7962f4e 100644 --- a/test/matcher/type/BetweenMatcher.spec.ts +++ b/test/matcher/type/BetweenMatcher.spec.ts @@ -12,7 +12,7 @@ describe("BetweenMatcher", () => { }); describe("when given value is lower than min", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { // when const result = testObj.match(4); const notResult = notTestObj.match(4); @@ -24,7 +24,7 @@ describe("BetweenMatcher", () => { }); describe("when given value is equal to min ", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match(5); const notResult = notTestObj.match(5); @@ -36,7 +36,7 @@ describe("BetweenMatcher", () => { }); describe("when given value is equal grater then min but lower than max", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match(7); const notResult = notTestObj.match(7); @@ -48,7 +48,7 @@ describe("BetweenMatcher", () => { }); describe("when given value is equal to max", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match(10); const notResult = notTestObj.match(10); @@ -60,7 +60,7 @@ describe("BetweenMatcher", () => { }); describe("when given value is greater than max", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match(11); const notResult = notTestObj.match(11); diff --git a/test/matcher/type/DeepEqualMatcher.spec.ts b/test/matcher/type/DeepEqualMatcher.spec.ts index 97a7234..f549b2d 100644 --- a/test/matcher/type/DeepEqualMatcher.spec.ts +++ b/test/matcher/type/DeepEqualMatcher.spec.ts @@ -3,7 +3,7 @@ import {anyString, deepEqual, not} from "../../../src/ts-mockito"; describe("DeepEqualMatcher", () => { describe("checking if two different instances of same number matches", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // given const firstValue = 3; const secondValue = 3; @@ -21,7 +21,7 @@ describe("DeepEqualMatcher", () => { }); describe("checking if two different instances of same string matches", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // given const firstValue = "sampleString"; const secondValue = "sampleString"; @@ -39,7 +39,7 @@ describe("DeepEqualMatcher", () => { }); describe("checking if two different instances of same nested objects matches", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // given const firstValue = {a: 1, b: {c: 2}}; const secondValue = {a: 1, b: {c: 2}}; @@ -57,7 +57,7 @@ describe("DeepEqualMatcher", () => { }); describe("checking if two nested objects matches when one leaf value is different", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // given const firstValue = {a: 1, b: {c: 2}}; const secondValue = {a: 1, b: {c: 99999}}; @@ -75,7 +75,7 @@ describe("DeepEqualMatcher", () => { }); describe("checking if expected value has Matcher as a value", () => { - it("returns true if matcher returns true", () => { + it("returns true if matcher returns true for original matcher and false for not().", () => { // given const firstValue = {a: 1, b: anyString()}; const secondValue = {a: 1, b: "2"}; @@ -91,7 +91,7 @@ describe("DeepEqualMatcher", () => { expect(notResult).toBeFalsy(); }); - it("returns false if matcher returns false", () => { + it("returns false if matcher returns false for original matcher and true for not().", () => { // given const firstValue = {a: 1, b: anyString()}; const secondValue = {a: 1, b: 2}; diff --git a/test/matcher/type/MatchMatcher.spec.ts b/test/matcher/type/MatchMatcher.spec.ts index 2c73a1a..97f8a82 100644 --- a/test/matcher/type/MatchMatcher.spec.ts +++ b/test/matcher/type/MatchMatcher.spec.ts @@ -7,7 +7,7 @@ describe("MatchingMatcher", () => { const notTestObj: Matcher = not().match(/\w123/); describe("when given value matches regexp", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match("a123"); const notResult = notTestObj.match("a123"); @@ -19,7 +19,7 @@ describe("MatchingMatcher", () => { }); describe("when given value doesn\'t match regexp", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { // when const result = testObj.match("123"); const notResult = notTestObj.match("123"); diff --git a/test/matcher/type/NotNullMatcher.spec.ts b/test/matcher/type/NotNullMatcher.spec.ts index 6182acd..c4e5242 100644 --- a/test/matcher/type/NotNullMatcher.spec.ts +++ b/test/matcher/type/NotNullMatcher.spec.ts @@ -11,7 +11,7 @@ describe("NotNullMatcher", () => { }); describe("checking if null matches", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { // when const result = testObj.match(null); const notResult = notTestObj.match(null); @@ -23,7 +23,7 @@ describe("NotNullMatcher", () => { }); describe("checking if false matches", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match(false); const notResult = notTestObj.match(false); @@ -35,7 +35,7 @@ describe("NotNullMatcher", () => { }); describe("checking if zero matches", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match(0); const notResult = notTestObj.match(0); @@ -47,7 +47,7 @@ describe("NotNullMatcher", () => { }); describe("checking if sample object matches", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match({}); const notResult = notTestObj.match({}); @@ -59,7 +59,7 @@ describe("NotNullMatcher", () => { }); describe("checking if sample string matches", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match("sampleString"); const notResult = notTestObj.match("sampleString"); diff --git a/test/matcher/type/ObjectContainingMatcher.spec.ts b/test/matcher/type/ObjectContainingMatcher.spec.ts index f510276..898ead2 100644 --- a/test/matcher/type/ObjectContainingMatcher.spec.ts +++ b/test/matcher/type/ObjectContainingMatcher.spec.ts @@ -7,7 +7,7 @@ describe("ObjectContainingMatcher", () => { const notTestObj: Matcher = not().objectContaining({b: {c: "c", d: {}}}); describe("when given value contains given object", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match({a: "a", b: {c: "c", d: {}}}); const notResult = notTestObj.match({a: "a", b: {c: "c", d: {}}}); @@ -17,7 +17,7 @@ describe("ObjectContainingMatcher", () => { expect(notResult).toBeFalsy(); }); - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // when const result = testObj.match({b: {c: "c", d: {}}}); const notResult = notTestObj.match({b: {c: "c", d: {}}}); @@ -29,7 +29,7 @@ describe("ObjectContainingMatcher", () => { }); describe("when given value doesn't contain given object", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { // when const result = testObj.match({b: {c: "c"}}); const notResult = notTestObj.match({b: {c: "c"}}); diff --git a/test/matcher/type/StrictEqualMatcher.spec.ts b/test/matcher/type/StrictEqualMatcher.spec.ts index becd21a..28f8dbd 100644 --- a/test/matcher/type/StrictEqualMatcher.spec.ts +++ b/test/matcher/type/StrictEqualMatcher.spec.ts @@ -3,7 +3,7 @@ import {not, strictEqual} from "../../../src/ts-mockito"; describe("StrictEqualMatcher", () => { describe("checking if string representation of number matches with number", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { // given const testObj: Matcher = strictEqual("5"); const notTestObj: Matcher = not().strictEqual("5"); @@ -19,7 +19,7 @@ describe("StrictEqualMatcher", () => { }); describe("checking if false matches with zero", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { // given const testObj: Matcher = strictEqual(false); const notTestObj: Matcher = not().strictEqual(false); @@ -35,7 +35,7 @@ describe("StrictEqualMatcher", () => { }); describe("checking if true matches with one", () => { - it("returns false", () => { + it("returns false for original matcher and true for not().", () => { // given const testObj: Matcher = strictEqual(true); const notTestObj: Matcher = not().strictEqual(true); @@ -51,7 +51,7 @@ describe("StrictEqualMatcher", () => { }); describe("checking if same strings matches", () => { - it("returns true", () => { + it("returns true for original matcher and false for not().", () => { // given const testObj: Matcher = strictEqual("5"); const notTestObj: Matcher = not().strictEqual("5"); From b581f47ee5c5cb79b676038c5fa0e9f655f8dcf1 Mon Sep 17 00:00:00 2001 From: cakeinpanic Date: Tue, 6 Feb 2018 14:31:13 +0300 Subject: [PATCH 12/12] rename MatchMatcher files --- src/matcher/type/MatchMatcher.ts | 2 +- src/matcher/type/Not.ts | 4 ++-- src/ts-mockito.ts | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/matcher/type/MatchMatcher.ts b/src/matcher/type/MatchMatcher.ts index d42a434..af6069d 100644 --- a/src/matcher/type/MatchMatcher.ts +++ b/src/matcher/type/MatchMatcher.ts @@ -1,6 +1,6 @@ import {Matcher} from "./Matcher"; -export class MatchStringMatcher extends Matcher { +export class MatchMatcher extends Matcher { constructor(private expectedValue: any) { super(); } diff --git a/src/matcher/type/Not.ts b/src/matcher/type/Not.ts index 4731a64..272c51a 100644 --- a/src/matcher/type/Not.ts +++ b/src/matcher/type/Not.ts @@ -6,7 +6,7 @@ import {AnythingMatcher} from "./AnythingMatcher"; import {BetweenMatcher} from "./BetweenMatcher"; import {DeepEqualMatcher} from "./DeepEqualMatcher"; import {Matcher} from "./Matcher"; -import {MatchStringMatcher} from "./MatchMatcher"; +import {MatchMatcher} from "./MatchMatcher"; import {NotNullMatcher} from "./NotNullMatcher"; import {ObjectContainingMatcher} from "./ObjectContainingMatcher"; import {StrictEqualMatcher} from "./StrictEqualMatcher"; @@ -50,7 +50,7 @@ export class Not { } public match(expectedValue: string | RegExp): Matcher { - return (new MatchStringMatcher(expectedValue)).reverse(); + return (new MatchMatcher(expectedValue)).reverse(); } public objectContaining(expectedValue: Object): Matcher { diff --git a/src/ts-mockito.ts b/src/ts-mockito.ts index 41df6c8..bbce2fd 100644 --- a/src/ts-mockito.ts +++ b/src/ts-mockito.ts @@ -19,7 +19,7 @@ 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 {MatchStringMatcher} from "./matcher/type/MatchMatcher"; +import {MatchMatcher} from "./matcher/type/MatchMatcher"; import {Not} from "./matcher/type/Not"; import {NotNullMatcher} from "./matcher/type/NotNullMatcher"; import {ObjectContainingMatcher} from "./matcher/type/ObjectContainingMatcher"; @@ -116,7 +116,7 @@ export function strictEqual(expectedValue: any): Matcher { } export function match(expectedValue: RegExp | string): Matcher { - return new MatchStringMatcher(expectedValue); + return new MatchMatcher(expectedValue); } export function objectContaining(expectedValue: Object): Matcher {