Skip to content

Commit c9a95bf

Browse files
Add startsWith() and endsWith() string matchers
1 parent 245892c commit c9a95bf

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

src/matcher/type/EndsWithMatcher.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Matcher } from "./Matcher";
2+
3+
export class EndsWithMatcher extends Matcher {
4+
constructor(private expectedValue: any) {
5+
super();
6+
}
7+
8+
public match(value: Object): boolean {
9+
return value && (typeof value === "string") && value.endsWith(this.expectedValue);
10+
}
11+
12+
public toString(): string {
13+
return `endsWith(${this.expectedValue})`;
14+
}
15+
}

src/matcher/type/StartsWithMatcher.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Matcher } from "./Matcher";
2+
3+
export class StartsWithMatcher extends Matcher {
4+
constructor(private expectedValue: any) {
5+
super();
6+
}
7+
8+
public match(value: Object): boolean {
9+
return value && (typeof value === "string") && value.startsWith(this.expectedValue);
10+
}
11+
12+
public toString(): string {
13+
return `startsWith(${this.expectedValue})`;
14+
}
15+
}

src/ts-mockito.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import {AnyStringMatcher} from "./matcher/type/AnyStringMatcher";
1818
import {AnythingMatcher} from "./matcher/type/AnythingMatcher";
1919
import {BetweenMatcher} from "./matcher/type/BetweenMatcher";
2020
import {DeepEqualMatcher} from "./matcher/type/DeepEqualMatcher";
21+
import {EndsWithMatcher} from "./matcher/type/EndsWithMatcher";
2122
import {MatchingStringMatcher} from "./matcher/type/MatchingStringMatcher";
2223
import {NotNullMatcher} from "./matcher/type/NotNullMatcher";
2324
import {ObjectContainingMatcher} from "./matcher/type/ObjectContainingMatcher";
25+
import {StartsWithMatcher} from "./matcher/type/StartsWithMatcher";
2426
import {StrictEqualMatcher} from "./matcher/type/StrictEqualMatcher";
2527
import {MethodStubSetter} from "./MethodStubSetter";
2628
import {MethodStubVerificator} from "./MethodStubVerificator";
@@ -119,6 +121,14 @@ export function match(expectedValue: RegExp | string): any {
119121
return new MatchingStringMatcher(expectedValue) as any;
120122
}
121123

124+
export function startsWith(expectedValue: string): string {
125+
return new StartsWithMatcher(expectedValue) as any;
126+
}
127+
128+
export function endsWith(expectedValue: string): string {
129+
return new EndsWithMatcher(expectedValue) as any;
130+
}
131+
122132
export function objectContaining(expectedValue: Object): any {
123133
return new ObjectContainingMatcher(expectedValue) as any;
124134
}
@@ -143,5 +153,7 @@ export default {
143153
notNull,
144154
strictEqual,
145155
match,
156+
startsWith,
157+
endsWith,
146158
objectContaining,
147159
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {Matcher} from "../../../src/matcher/type/Matcher";
2+
import {endsWith} from "../../../src/ts-mockito";
3+
4+
describe("EndsWithMatcher", () => {
5+
describe("checking if value starts with given value", () => {
6+
const testObj: Matcher = endsWith("abc") as any;
7+
8+
describe("when given value ends with string", () => {
9+
it("returns true", () => {
10+
// when
11+
const result = testObj.match("123 abc");
12+
13+
// then
14+
expect(result).toBeTruthy();
15+
});
16+
});
17+
18+
describe("when given value doesn\'t end with string", () => {
19+
it("returns false", () => {
20+
// when
21+
const result = testObj.match("abc 123");
22+
23+
// then
24+
expect(result).toBeFalsy();
25+
});
26+
});
27+
});
28+
29+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {Matcher} from "../../../src/matcher/type/Matcher";
2+
import {startsWith} from "../../../src/ts-mockito";
3+
4+
describe("StartsWithMatcher", () => {
5+
describe("checking if value starts with given value", () => {
6+
const testObj: Matcher = startsWith("abc") as any;
7+
8+
describe("when given value starts with string", () => {
9+
it("returns true", () => {
10+
// when
11+
const result = testObj.match("abc 123");
12+
13+
// then
14+
expect(result).toBeTruthy();
15+
});
16+
});
17+
18+
describe("when given value doesn\'t start with string", () => {
19+
it("returns false", () => {
20+
// when
21+
const result = testObj.match("123 abc");
22+
23+
// then
24+
expect(result).toBeFalsy();
25+
});
26+
});
27+
});
28+
29+
});

0 commit comments

Comments
 (0)