Skip to content

Commit 32f4f7a

Browse files
leon19johanblumenberg
authored andcommitted
feat(matchers): numeric matchers
The following matchers has been added: * `greaterThan()` * `greaterThanOrEqual()` * `lowerThan()` * `lowerThanOrEqual()`
1 parent 15d35ac commit 32f4f7a

File tree

10 files changed

+423
-0
lines changed

10 files changed

+423
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {NumberMatcher} from "./NumberMatcher";
2+
3+
export class GreaterThanMatcher extends NumberMatcher {
4+
public match(value: number): boolean {
5+
return value > this.value;
6+
}
7+
8+
public toString(): string {
9+
return `greaterThan(${this.value})`;
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {NumberMatcher} from "./NumberMatcher";
2+
3+
export class GreaterThanOrEqualMatcher extends NumberMatcher {
4+
public match(value: number): boolean {
5+
return value >= this.value;
6+
}
7+
8+
public toString(): string {
9+
return `greaterThanOrEqual(${this.value})`;
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {NumberMatcher} from "./NumberMatcher";
2+
3+
export class LowerThanMatcher extends NumberMatcher {
4+
public match(value: number): boolean {
5+
return value < this.value;
6+
}
7+
8+
public toString(): string {
9+
return `lowerThan(${this.value})`;
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {NumberMatcher} from "./NumberMatcher";
2+
3+
export class LowerThanOrEqualMatcher extends NumberMatcher {
4+
public match(value: number): boolean {
5+
return value <= this.value;
6+
}
7+
8+
public toString(): string {
9+
return `lowerThanOrEqual(${this.value})`;
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {Matcher} from "../Matcher";
2+
3+
export abstract class NumberMatcher extends Matcher {
4+
constructor(protected readonly value: number) {
5+
super();
6+
}
7+
}

src/ts-mockito.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ import {DeepEqualMatcher} from "./matcher/type/DeepEqualMatcher";
2626
import {EndsWithMatcher} from "./matcher/type/EndsWithMatcher";
2727
import {MatchingStringMatcher} from "./matcher/type/MatchingStringMatcher";
2828
import {NotNullMatcher} from "./matcher/type/NotNullMatcher";
29+
import {GreaterThanMatcher} from "./matcher/type/number/GreaterThanMatcher";
30+
import {GreaterThanOrEqualMatcher} from "./matcher/type/number/GreaterThanOrEqualMatcher";
31+
import {LowerThanMatcher} from "./matcher/type/number/LowerThanMatcher";
32+
import {LowerThanOrEqualMatcher} from "./matcher/type/number/LowerThanOrEqualMatcher";
2933
import {ObjectContainingMatcher} from "./matcher/type/ObjectContainingMatcher";
3034
import {StartsWithMatcher} from "./matcher/type/StartsWithMatcher";
3135
import {StrictEqualMatcher} from "./matcher/type/StrictEqualMatcher";
@@ -218,6 +222,22 @@ export function isBeforeOrEqual(date: MaybeDate): Date {
218222
return new IsBeforeOrEqualMatcher(date) as any;
219223
}
220224

225+
export function greaterThan(value: number): number {
226+
return new GreaterThanMatcher(value) as any;
227+
}
228+
229+
export function greaterThanOrEqual(value: number): number {
230+
return new GreaterThanOrEqualMatcher(value) as any;
231+
}
232+
233+
export function lowerThan(value: number): number {
234+
return new LowerThanMatcher(value) as any;
235+
}
236+
237+
export function lowerThanOrEqual(value: number): number {
238+
return new LowerThanOrEqualMatcher(value) as any;
239+
}
240+
221241
// Export default object with all members (ember-browserify doesn't support named exports).
222242
export default {
223243
spy,
@@ -251,4 +271,8 @@ export default {
251271
isAfterOrEqual,
252272
isBefore,
253273
isBeforeOrEqual,
274+
greaterThan,
275+
greaterThanOrEqual,
276+
lowerThan,
277+
lowerThanOrEqual,
254278
};
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import {expectType} from "ts-expect";
2+
import {GreaterThanMatcher} from "../../../../src/matcher/type/number/GreaterThanMatcher";
3+
import {greaterThan, imock, instance, when} from "../../../../src/ts-mockito";
4+
5+
describe("GreaterThanMatcher", () => {
6+
describe("greaterThan()", () => {
7+
it("should be a IfAfterOrEqualMatcher instance", () => {
8+
expect((greaterThan(0) as any) instanceof GreaterThanMatcher).toBe(true);
9+
});
10+
11+
it("should be a number type", () => {
12+
expectType<number>(greaterThan(0));
13+
});
14+
});
15+
16+
describe("#match()", () => {
17+
it("should return true when the given number is greater than the expected number", () => {
18+
const matcher = new GreaterThanMatcher(0);
19+
20+
expect(matcher.match(1)).toBe(true);
21+
});
22+
23+
it("should return false when the given number is equal to the expected number", () => {
24+
const matcher = new GreaterThanMatcher(0);
25+
26+
expect(matcher.match(0)).toBe(false);
27+
});
28+
29+
it("should return false when the given number is lower than the expected number", () => {
30+
const matcher = new GreaterThanMatcher(0);
31+
32+
expect(matcher.match(-1)).toBe(false);
33+
});
34+
});
35+
36+
describe("#toString()", () => {
37+
it("should correctly get the string representation", () => {
38+
const expected = 0;
39+
40+
expect(new GreaterThanMatcher(expected).toString()).toBe(`greaterThan(${expected})`);
41+
});
42+
});
43+
44+
describe("stubbing a method", () => {
45+
let testServiceMock: TestService;
46+
let testService: TestService;
47+
48+
beforeEach(() => {
49+
testServiceMock = imock();
50+
testService = instance(testServiceMock);
51+
});
52+
53+
it("should pass the verification when the given number is greater than the expected number", () => {
54+
const expected = 0;
55+
56+
when(testServiceMock.testMethod(greaterThan(expected))).thenReturn(true);
57+
58+
const result = testService.testMethod(1);
59+
60+
expect(result).toBe(true);
61+
});
62+
63+
it("should not pass the verification when the given number is equal to the expected number", () => {
64+
const expected = 0;
65+
66+
when(testServiceMock.testMethod(greaterThan(expected))).thenReturn(true);
67+
68+
const result = testService.testMethod(expected);
69+
70+
expect(result).toBeNull();
71+
});
72+
73+
it("should not pass the verification when the given number is lower than the expected number", () => {
74+
const expected = 0;
75+
76+
when(testServiceMock.testMethod(greaterThan(expected))).thenReturn(true);
77+
78+
const result = testService.testMethod(-1);
79+
80+
expect(result).toBeNull();
81+
});
82+
});
83+
});
84+
85+
interface TestService {
86+
testMethod(date: number): boolean;
87+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import {expectType} from "ts-expect";
2+
import {GreaterThanOrEqualMatcher} from "../../../../src/matcher/type/number/GreaterThanOrEqualMatcher";
3+
import {greaterThanOrEqual, imock, instance, when} from "../../../../src/ts-mockito";
4+
5+
describe("GreaterThanOrEqualMatcher", () => {
6+
describe("greaterThanOrEqual()", () => {
7+
it("should be a IfAfterOrEqualMatcher instance", () => {
8+
expect((greaterThanOrEqual(0) as any) instanceof GreaterThanOrEqualMatcher).toBe(true);
9+
});
10+
11+
it("should be a number type", () => {
12+
expectType<number>(greaterThanOrEqual(0));
13+
});
14+
});
15+
16+
describe("#match()", () => {
17+
it("should return true when the given number is greater than the expected number", () => {
18+
const matcher = new GreaterThanOrEqualMatcher(0);
19+
20+
expect(matcher.match(1)).toBe(true);
21+
});
22+
23+
it("should return true when the given number is equal to the expected number", () => {
24+
const matcher = new GreaterThanOrEqualMatcher(0);
25+
26+
expect(matcher.match(0)).toBe(true);
27+
});
28+
29+
it("should return false when the given number is lower than the expected number", () => {
30+
const matcher = new GreaterThanOrEqualMatcher(0);
31+
32+
expect(matcher.match(-1)).toBe(false);
33+
});
34+
});
35+
36+
describe("#toString()", () => {
37+
it("should correctly get the string representation", () => {
38+
const expected = 0;
39+
40+
expect(new GreaterThanOrEqualMatcher(expected).toString()).toBe(`greaterThanOrEqual(${expected})`);
41+
});
42+
});
43+
44+
describe("stubbing a method", () => {
45+
let testServiceMock: TestService;
46+
let testService: TestService;
47+
48+
beforeEach(() => {
49+
testServiceMock = imock();
50+
testService = instance(testServiceMock);
51+
});
52+
53+
it("should pass the verification when the given number is greater than the expected number", () => {
54+
const expected = 0;
55+
56+
when(testServiceMock.testMethod(greaterThanOrEqual(expected))).thenReturn(true);
57+
58+
const result = testService.testMethod(1);
59+
60+
expect(result).toBe(true);
61+
});
62+
63+
it("should pass the verification when the given number is equal to the expected number", () => {
64+
const expected = 0;
65+
66+
when(testServiceMock.testMethod(greaterThanOrEqual(expected))).thenReturn(true);
67+
68+
const result = testService.testMethod(expected);
69+
70+
expect(result).toBe(true);
71+
});
72+
73+
it("should not pass the verification when the given number is lower than the expected number", () => {
74+
const expected = 0;
75+
76+
when(testServiceMock.testMethod(greaterThanOrEqual(expected))).thenReturn(true);
77+
78+
const result = testService.testMethod(-1);
79+
80+
expect(result).toBeNull();
81+
});
82+
});
83+
});
84+
85+
interface TestService {
86+
testMethod(date: number): boolean;
87+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import {expectType} from "ts-expect";
2+
import {LowerThanMatcher} from "../../../../src/matcher/type/number/LowerThanMatcher";
3+
import {imock, instance, lowerThan, when} from "../../../../src/ts-mockito";
4+
5+
describe("LowerThanMatcher", () => {
6+
describe("lowerThan()", () => {
7+
it("should be a IfAfterOrEqualMatcher instance", () => {
8+
expect((lowerThan(0) as any) instanceof LowerThanMatcher).toBe(true);
9+
});
10+
11+
it("should be a number type", () => {
12+
expectType<number>(lowerThan(0));
13+
});
14+
});
15+
16+
describe("#match()", () => {
17+
it("should return true when the given number is lower than the expected number", () => {
18+
const matcher = new LowerThanMatcher(0);
19+
20+
expect(matcher.match(-1)).toBe(true);
21+
});
22+
23+
it("should return false when the given number is equal to the expected number", () => {
24+
const matcher = new LowerThanMatcher(0);
25+
26+
expect(matcher.match(0)).toBe(false);
27+
});
28+
29+
it("should return false when the given number is greater than the expected number", () => {
30+
const matcher = new LowerThanMatcher(0);
31+
32+
expect(matcher.match(1)).toBe(false);
33+
});
34+
});
35+
36+
describe("#toString()", () => {
37+
it("should correctly get the string representation", () => {
38+
const expected = 0;
39+
40+
expect(new LowerThanMatcher(expected).toString()).toBe(`lowerThan(${expected})`);
41+
});
42+
});
43+
44+
describe("stubbing a method", () => {
45+
let testServiceMock: TestService;
46+
let testService: TestService;
47+
48+
beforeEach(() => {
49+
testServiceMock = imock();
50+
testService = instance(testServiceMock);
51+
});
52+
53+
it("should pass the verification when the given number is lower than the expected number", () => {
54+
const expected = 0;
55+
56+
when(testServiceMock.testMethod(lowerThan(expected))).thenReturn(true);
57+
58+
const result = testService.testMethod(-1);
59+
60+
expect(result).toBe(true);
61+
});
62+
63+
it("should not pass the verification when the given number is equal to the expected number", () => {
64+
const expected = 0;
65+
66+
when(testServiceMock.testMethod(lowerThan(expected))).thenReturn(true);
67+
68+
const result = testService.testMethod(expected);
69+
70+
expect(result).toBeNull();
71+
});
72+
73+
it("should not pass the verification when the given number is greater than the expected number", () => {
74+
const expected = 0;
75+
76+
when(testServiceMock.testMethod(lowerThan(expected))).thenReturn(true);
77+
78+
const result = testService.testMethod(1);
79+
80+
expect(result).toBeNull();
81+
});
82+
});
83+
});
84+
85+
interface TestService {
86+
testMethod(date: number): boolean;
87+
}

0 commit comments

Comments
 (0)