-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Johanblumenberg matcher types #8
base: master
Are you sure you want to change the base?
Changes from 3 commits
4477bb1
e59b0c8
3026558
b4cd9f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Matcher } from "./Matcher"; | ||
|
||
export class EndsWithMatcher extends Matcher { | ||
constructor(private expectedValue: any) { | ||
super(); | ||
} | ||
|
||
public match(value: Object): boolean { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. value should be string IMO |
||
return value && (typeof value === "string") && value.endsWith(this.expectedValue); | ||
} | ||
|
||
public toString(): string { | ||
return `endsWith(${this.expectedValue})`; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Matcher } from "./Matcher"; | ||
|
||
export class StartsWithMatcher extends Matcher { | ||
constructor(private expectedValue: any) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same type related comments as in endsWithMatcher.. |
||
super(); | ||
} | ||
|
||
public match(value: Object): boolean { | ||
return value && (typeof value === "string") && value.startsWith(this.expectedValue); | ||
} | ||
|
||
public toString(): string { | ||
return `startsWith(${this.expectedValue})`; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
@@ -89,19 +91,19 @@ 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; | ||
} | ||
|
||
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; | ||
} | ||
|
||
|
@@ -113,14 +115,22 @@ export function notNull(): any { | |
return new NotNullMatcher() as any; | ||
} | ||
|
||
export function strictEqual(expectedValue: any): any { | ||
export function strictEqual<T>(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; | ||
} | ||
|
||
export function startsWith(expectedValue: string): string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should return Matcher and no need for the any casting |
||
return new StartsWithMatcher(expectedValue) as any; | ||
} | ||
|
||
export function endsWith(expectedValue: string): string { | ||
return new EndsWithMatcher(expectedValue) as any; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
} | ||
|
||
export function objectContaining(expectedValue: Object): any { | ||
return new ObjectContainingMatcher(expectedValue) as any; | ||
} | ||
|
@@ -145,5 +155,7 @@ export default { | |
notNull, | ||
strictEqual, | ||
match, | ||
startsWith, | ||
endsWith, | ||
objectContaining, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. strictEqual should return Matcher and there's no need for casting to 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(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expectedValue should be string