forked from NagRock/ts-mockito
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The following matchers has been added: * `greaterThan()` * `greaterThanOrEqual()` * `lowerThan()` * `lowerThanOrEqual()`
- Loading branch information
1 parent
15d35ac
commit 32f4f7a
Showing
10 changed files
with
423 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.