Skip to content

Commit

Permalink
feat(matchers): numeric matchers
Browse files Browse the repository at this point in the history
The following matchers has been added:

* `greaterThan()`
* `greaterThanOrEqual()`
* `lowerThan()`
* `lowerThanOrEqual()`
  • Loading branch information
leon19 authored and johanblumenberg committed Jun 28, 2022
1 parent 15d35ac commit 32f4f7a
Show file tree
Hide file tree
Showing 10 changed files with 423 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/matcher/type/number/GreaterThanMatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {NumberMatcher} from "./NumberMatcher";

export class GreaterThanMatcher extends NumberMatcher {
public match(value: number): boolean {
return value > this.value;
}

public toString(): string {
return `greaterThan(${this.value})`;
}
}
11 changes: 11 additions & 0 deletions src/matcher/type/number/GreaterThanOrEqualMatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {NumberMatcher} from "./NumberMatcher";

export class GreaterThanOrEqualMatcher extends NumberMatcher {
public match(value: number): boolean {
return value >= this.value;
}

public toString(): string {
return `greaterThanOrEqual(${this.value})`;
}
}
11 changes: 11 additions & 0 deletions src/matcher/type/number/LowerThanMatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {NumberMatcher} from "./NumberMatcher";

export class LowerThanMatcher extends NumberMatcher {
public match(value: number): boolean {
return value < this.value;
}

public toString(): string {
return `lowerThan(${this.value})`;
}
}
11 changes: 11 additions & 0 deletions src/matcher/type/number/LowerThanOrEqualMatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {NumberMatcher} from "./NumberMatcher";

export class LowerThanOrEqualMatcher extends NumberMatcher {
public match(value: number): boolean {
return value <= this.value;
}

public toString(): string {
return `lowerThanOrEqual(${this.value})`;
}
}
7 changes: 7 additions & 0 deletions src/matcher/type/number/NumberMatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {Matcher} from "../Matcher";

export abstract class NumberMatcher extends Matcher {
constructor(protected readonly value: number) {
super();
}
}
24 changes: 24 additions & 0 deletions src/ts-mockito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ 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 {GreaterThanMatcher} from "./matcher/type/number/GreaterThanMatcher";
import {GreaterThanOrEqualMatcher} from "./matcher/type/number/GreaterThanOrEqualMatcher";
import {LowerThanMatcher} from "./matcher/type/number/LowerThanMatcher";
import {LowerThanOrEqualMatcher} from "./matcher/type/number/LowerThanOrEqualMatcher";
import {ObjectContainingMatcher} from "./matcher/type/ObjectContainingMatcher";
import {StartsWithMatcher} from "./matcher/type/StartsWithMatcher";
import {StrictEqualMatcher} from "./matcher/type/StrictEqualMatcher";
Expand Down Expand Up @@ -218,6 +222,22 @@ export function isBeforeOrEqual(date: MaybeDate): Date {
return new IsBeforeOrEqualMatcher(date) as any;
}

export function greaterThan(value: number): number {
return new GreaterThanMatcher(value) as any;
}

export function greaterThanOrEqual(value: number): number {
return new GreaterThanOrEqualMatcher(value) as any;
}

export function lowerThan(value: number): number {
return new LowerThanMatcher(value) as any;
}

export function lowerThanOrEqual(value: number): number {
return new LowerThanOrEqualMatcher(value) as any;
}

// Export default object with all members (ember-browserify doesn't support named exports).
export default {
spy,
Expand Down Expand Up @@ -251,4 +271,8 @@ export default {
isAfterOrEqual,
isBefore,
isBeforeOrEqual,
greaterThan,
greaterThanOrEqual,
lowerThan,
lowerThanOrEqual,
};
87 changes: 87 additions & 0 deletions test/matcher/type/number/GreterThan.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {expectType} from "ts-expect";
import {GreaterThanMatcher} from "../../../../src/matcher/type/number/GreaterThanMatcher";
import {greaterThan, imock, instance, when} from "../../../../src/ts-mockito";

describe("GreaterThanMatcher", () => {
describe("greaterThan()", () => {
it("should be a IfAfterOrEqualMatcher instance", () => {
expect((greaterThan(0) as any) instanceof GreaterThanMatcher).toBe(true);
});

it("should be a number type", () => {
expectType<number>(greaterThan(0));
});
});

describe("#match()", () => {
it("should return true when the given number is greater than the expected number", () => {
const matcher = new GreaterThanMatcher(0);

expect(matcher.match(1)).toBe(true);
});

it("should return false when the given number is equal to the expected number", () => {
const matcher = new GreaterThanMatcher(0);

expect(matcher.match(0)).toBe(false);
});

it("should return false when the given number is lower than the expected number", () => {
const matcher = new GreaterThanMatcher(0);

expect(matcher.match(-1)).toBe(false);
});
});

describe("#toString()", () => {
it("should correctly get the string representation", () => {
const expected = 0;

expect(new GreaterThanMatcher(expected).toString()).toBe(`greaterThan(${expected})`);
});
});

describe("stubbing a method", () => {
let testServiceMock: TestService;
let testService: TestService;

beforeEach(() => {
testServiceMock = imock();
testService = instance(testServiceMock);
});

it("should pass the verification when the given number is greater than the expected number", () => {
const expected = 0;

when(testServiceMock.testMethod(greaterThan(expected))).thenReturn(true);

const result = testService.testMethod(1);

expect(result).toBe(true);
});

it("should not pass the verification when the given number is equal to the expected number", () => {
const expected = 0;

when(testServiceMock.testMethod(greaterThan(expected))).thenReturn(true);

const result = testService.testMethod(expected);

expect(result).toBeNull();
});

it("should not pass the verification when the given number is lower than the expected number", () => {
const expected = 0;

when(testServiceMock.testMethod(greaterThan(expected))).thenReturn(true);

const result = testService.testMethod(-1);

expect(result).toBeNull();
});
});
});

interface TestService {
testMethod(date: number): boolean;
}
87 changes: 87 additions & 0 deletions test/matcher/type/number/GreterThanOrEqual.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {expectType} from "ts-expect";
import {GreaterThanOrEqualMatcher} from "../../../../src/matcher/type/number/GreaterThanOrEqualMatcher";
import {greaterThanOrEqual, imock, instance, when} from "../../../../src/ts-mockito";

describe("GreaterThanOrEqualMatcher", () => {
describe("greaterThanOrEqual()", () => {
it("should be a IfAfterOrEqualMatcher instance", () => {
expect((greaterThanOrEqual(0) as any) instanceof GreaterThanOrEqualMatcher).toBe(true);
});

it("should be a number type", () => {
expectType<number>(greaterThanOrEqual(0));
});
});

describe("#match()", () => {
it("should return true when the given number is greater than the expected number", () => {
const matcher = new GreaterThanOrEqualMatcher(0);

expect(matcher.match(1)).toBe(true);
});

it("should return true when the given number is equal to the expected number", () => {
const matcher = new GreaterThanOrEqualMatcher(0);

expect(matcher.match(0)).toBe(true);
});

it("should return false when the given number is lower than the expected number", () => {
const matcher = new GreaterThanOrEqualMatcher(0);

expect(matcher.match(-1)).toBe(false);
});
});

describe("#toString()", () => {
it("should correctly get the string representation", () => {
const expected = 0;

expect(new GreaterThanOrEqualMatcher(expected).toString()).toBe(`greaterThanOrEqual(${expected})`);
});
});

describe("stubbing a method", () => {
let testServiceMock: TestService;
let testService: TestService;

beforeEach(() => {
testServiceMock = imock();
testService = instance(testServiceMock);
});

it("should pass the verification when the given number is greater than the expected number", () => {
const expected = 0;

when(testServiceMock.testMethod(greaterThanOrEqual(expected))).thenReturn(true);

const result = testService.testMethod(1);

expect(result).toBe(true);
});

it("should pass the verification when the given number is equal to the expected number", () => {
const expected = 0;

when(testServiceMock.testMethod(greaterThanOrEqual(expected))).thenReturn(true);

const result = testService.testMethod(expected);

expect(result).toBe(true);
});

it("should not pass the verification when the given number is lower than the expected number", () => {
const expected = 0;

when(testServiceMock.testMethod(greaterThanOrEqual(expected))).thenReturn(true);

const result = testService.testMethod(-1);

expect(result).toBeNull();
});
});
});

interface TestService {
testMethod(date: number): boolean;
}
87 changes: 87 additions & 0 deletions test/matcher/type/number/LowerThan.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {expectType} from "ts-expect";
import {LowerThanMatcher} from "../../../../src/matcher/type/number/LowerThanMatcher";
import {imock, instance, lowerThan, when} from "../../../../src/ts-mockito";

describe("LowerThanMatcher", () => {
describe("lowerThan()", () => {
it("should be a IfAfterOrEqualMatcher instance", () => {
expect((lowerThan(0) as any) instanceof LowerThanMatcher).toBe(true);
});

it("should be a number type", () => {
expectType<number>(lowerThan(0));
});
});

describe("#match()", () => {
it("should return true when the given number is lower than the expected number", () => {
const matcher = new LowerThanMatcher(0);

expect(matcher.match(-1)).toBe(true);
});

it("should return false when the given number is equal to the expected number", () => {
const matcher = new LowerThanMatcher(0);

expect(matcher.match(0)).toBe(false);
});

it("should return false when the given number is greater than the expected number", () => {
const matcher = new LowerThanMatcher(0);

expect(matcher.match(1)).toBe(false);
});
});

describe("#toString()", () => {
it("should correctly get the string representation", () => {
const expected = 0;

expect(new LowerThanMatcher(expected).toString()).toBe(`lowerThan(${expected})`);
});
});

describe("stubbing a method", () => {
let testServiceMock: TestService;
let testService: TestService;

beforeEach(() => {
testServiceMock = imock();
testService = instance(testServiceMock);
});

it("should pass the verification when the given number is lower than the expected number", () => {
const expected = 0;

when(testServiceMock.testMethod(lowerThan(expected))).thenReturn(true);

const result = testService.testMethod(-1);

expect(result).toBe(true);
});

it("should not pass the verification when the given number is equal to the expected number", () => {
const expected = 0;

when(testServiceMock.testMethod(lowerThan(expected))).thenReturn(true);

const result = testService.testMethod(expected);

expect(result).toBeNull();
});

it("should not pass the verification when the given number is greater than the expected number", () => {
const expected = 0;

when(testServiceMock.testMethod(lowerThan(expected))).thenReturn(true);

const result = testService.testMethod(1);

expect(result).toBeNull();
});
});
});

interface TestService {
testMethod(date: number): boolean;
}
Loading

0 comments on commit 32f4f7a

Please sign in to comment.