diff --git a/jasmine/jasmine-tests.ts b/jasmine/jasmine-tests.ts index 8e3365eaf8..1bdf964dd9 100644 --- a/jasmine/jasmine-tests.ts +++ b/jasmine/jasmine-tests.ts @@ -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) { + 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(); diff --git a/jasmine/jasmine.d.ts b/jasmine/jasmine.d.ts index 64936f442b..43831c68a1 100644 --- a/jasmine/jasmine.d.ts +++ b/jasmine/jasmine.d.ts @@ -52,7 +52,8 @@ declare module jasmine { function createSpyObj(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 { + 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(actual: T, expected: T): CustomMatcherResult; + compare(actual: any, expected: any): CustomMatcherResult; + } + + interface CustomMatcherFactory { + (util: MatchersUtil, customEqualityTesters: Array): CustomMatcher; + } + + interface CustomMatcherFactories { + [index: string]: CustomMatcherFactory; + } + + interface CustomMatcherResult { + pass: boolean; + message: string; + } + + interface MatchersUtil { + equals(a: any, b: any, customTesters?: Array): boolean; + contains(haystack: ArrayLike | string, needle: any, customTesters?: Array): boolean; + buildFailureMessage(matcherName: string, isNot: boolean, actual: any, ...expected: Array): 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;