Support custom equality testers and custom matchers improvements

- Added support for custom equality testers. Fixed existing function
  signature and added interfaces
- Improved typings for custom matchers to be more strict and modeled the
  construction process via corresponding interfaces
- Added tests for the custom equality testers and custom matchers
This commit is contained in:
CallMeTango
2015-04-30 17:10:21 +02:00
parent 00e7f3fede
commit 2017b81457
2 changed files with 115 additions and 3 deletions
+76
View File
@@ -748,6 +748,82 @@ describe("Fail", function () {
});
// test based on http://jasmine.github.io/2.2/custom_equality.html
describe("custom equality", function() {
var myCustomEquality: jasmine.CustomEqualityTester = function(first: any, second: any): boolean {
if (typeof first == "string" && typeof second == "string") {
return first[0] == second[1];
}
};
beforeEach(function() {
jasmine.addCustomEqualityTester(myCustomEquality);
});
it("should be custom equal", function() {
expect("abc").toEqual("aaa");
});
it("should be custom not equal", function() {
expect("abc").not.toEqual("abc");
});
});
// test based on http://jasmine.github.io/2.2/custom_matcher.html
var customMatchers: jasmine.CustomMatcherFactories = {
toBeGoofy: function (util: jasmine.MatchersUtil, customEqualityTesters: Array<jasmine.CustomEqualityTester>) {
return {
compare: function (actual: any, expected: any): jasmine.CustomMatcherResult {
if (expected === undefined) {
expected = '';
}
var result: jasmine.CustomMatcherResult = { pass: false, message: ''};
result.pass = util.equals(actual.hyuk, "gawrsh" + expected, customEqualityTesters);
if (result.pass) {
result.message = "Expected " + actual + " not to be quite so goofy";
} else {
result.message = "Expected " + actual + " to be goofy, but it was not very goofy";
}
return result;
}
};
}
};
// add the custom matchers to interface jasmine.Matchers via TypeScript declaration merging
declare module jasmine {
interface Matchers {
toBeGoofy(expected?: any): boolean;
}
}
describe("Custom matcher: 'toBeGoofy'", function () {
beforeEach(function () {
jasmine.addMatchers(customMatchers);
});
it("is available on an expectation", function () {
expect({
hyuk: 'gawrsh'
}).toBeGoofy();
});
it("can take an 'expected' parameter", function () {
expect({
hyuk: 'gawrsh is fun'
}).toBeGoofy(' is fun');
});
it("can be negated", function () {
expect({
hyuk: 'this is fun'
}).not.toBeGoofy();
});
});
(() => {
// from boot.js
var env = jasmine.getEnv();
+39 -3
View File
@@ -52,7 +52,8 @@ declare module jasmine {
function createSpyObj<T>(baseName: string, methodNames: any[]): T;
function pp(value: any): string;
function getEnv(): Env;
function addMatchers(matchers: any): Any;
function addCustomEqualityTester(equalityTester: CustomEqualityTester): void;
function addMatchers(matchers: CustomMatcherFactories): void;
interface Any {
@@ -62,6 +63,12 @@ declare module jasmine {
jasmineToString(): string;
}
// taken from TypeScript lib.core.es6.d.ts, applicable to CustomMatchers.contains()
interface ArrayLike<T> {
length: number;
[n: number]: T;
}
interface ObjectContaining {
new (sample: any): any;
@@ -91,6 +98,34 @@ declare module jasmine {
tick(ms: number): void;
}
interface CustomEqualityTester {
(first: any, second: any): boolean;
}
interface CustomMatcher {
compare<T>(actual: T, expected: T): CustomMatcherResult;
compare(actual: any, expected: any): CustomMatcherResult;
}
interface CustomMatcherFactory {
(util: MatchersUtil, customEqualityTesters: Array<CustomEqualityTester>): CustomMatcher;
}
interface CustomMatcherFactories {
[index: string]: CustomMatcherFactory;
}
interface CustomMatcherResult {
pass: boolean;
message: string;
}
interface MatchersUtil {
equals(a: any, b: any, customTesters?: Array<CustomEqualityTester>): boolean;
contains<T>(haystack: ArrayLike<T> | string, needle: any, customTesters?: Array<CustomEqualityTester>): boolean;
buildFailureMessage(matcherName: string, isNot: boolean, actual: any, ...expected: Array<any>): string;
}
interface Env {
setTimeout: any;
clearTimeout: void;
@@ -122,7 +157,8 @@ declare module jasmine {
compareObjects_(a: any, b: any, mismatchKeys: string[], mismatchValues: string[]): boolean;
equals_(a: any, b: any, mismatchKeys: string[], mismatchValues: string[]): boolean;
contains_(haystack: any, needle: any): boolean;
addEqualityTester(equalityTester: (a: any, b: any, env: Env, mismatchKeys: string[], mismatchValues: string[]) => boolean): void;
addCustomEqualityTester(equalityTester: CustomEqualityTester): void;
addMatchers(matchers: CustomMatcherFactories): void;
specFilter(spec: Spec): boolean;
}
@@ -320,7 +356,7 @@ declare module jasmine {
waitsFor(latchFunction: SpecFunction, timeoutMessage?: string, timeout?: number): Spec;
fail(e?: any): void;
getMatchersClass_(): Matchers;
addMatchers(matchersPrototype: any): void;
addMatchers(matchersPrototype: CustomMatcherFactories): void;
finishCallback(): void;
finish(onComplete?: () => void): void;
after(doAfter: SpecFunction): void;