Jasmine matchers can accept many arguments

refs #21327
This commit is contained in:
Jamie Mason
2017-12-17 20:49:58 +00:00
parent 6d9978eb72
commit 3bdc4ff651
4 changed files with 20 additions and 6 deletions
+2 -2
View File
@@ -219,8 +219,8 @@ declare namespace jasmine {
type CustomEqualityTester = (first: any, second: any) => boolean | void;
interface CustomMatcher {
compare<T>(actual: T, expected: T): CustomMatcherResult;
compare(actual: any, expected: any): CustomMatcherResult;
compare<T>(actual: T, expected: T, ...args: any[]): CustomMatcherResult;
compare(actual: any, ...expected: any[]): CustomMatcherResult;
}
type CustomMatcherFactory = (util: MatchersUtil, customEqualityTesters: CustomEqualityTester[]) => CustomMatcher;
+14
View File
@@ -963,6 +963,15 @@ var customMatchers: jasmine.CustomMatcherFactories = {
return result;
}
};
},
toBeWithinRange: (util: jasmine.MatchersUtil, customEqualityTesters: jasmine.CustomEqualityTester[]) => {
return {
compare: (actual: any, floor: number, ceiling: number): jasmine.CustomMatcherResult => {
const pass = actual >= floor && actual <= ceiling;
const message = `expected ${actual} ${pass ? 'not ' : ''} to be within range ${floor}-${ceiling}`;
return { message, pass };
}
};
}
};
// add the custom matchers to interface jasmine.Matchers via TypeScript declaration merging
@@ -977,6 +986,7 @@ var customMatchers: jasmine.CustomMatcherFactories = {
declare namespace jasmine {
interface Matchers<T> {
toBeGoofy(expected?: jasmine.Expected<T>): boolean;
toBeWithinRange(expected?: jasmine.Expected<T>, floor?: number, ceiling?: number): boolean;
}
}
@@ -997,6 +1007,10 @@ describe("Custom matcher: 'toBeGoofy'", () => {
}).toBeGoofy({ hyuk: ' is fun' });
});
it("can take many 'expected' parameters", () => {
expect(2).toBeWithinRange(1, 3);
});
it("can be negated", () => {
expect({
hyuk: 'this is fun'
+2 -2
View File
@@ -714,8 +714,8 @@ declare namespace jasmine {
type CustomEqualityTester = (first: any, second: any) => boolean;
interface CustomMatcher {
compare<T>(actual: T, expected: T): CustomMatcherResult;
compare(actual: any, expected: any): CustomMatcherResult;
compare<T>(actual: T, expected: T, ...args: any[]): CustomMatcherResult;
compare(actual: any, ...expected: any[]): CustomMatcherResult;
}
interface CustomMatcherResult {
+2 -2
View File
@@ -287,8 +287,8 @@ declare namespace jasmine {
}
interface CustomMatcher {
compare<T>(actual: T, expected: T): CustomMatcherResult;
compare(actual: any, expected: any): CustomMatcherResult;
compare<T>(actual: T, expected: T, ...args: any[]): CustomMatcherResult;
compare(actual: any, ...expected: any[]): CustomMatcherResult;
}
interface CustomMatcherResult {