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