diff --git a/spec/core/MatchersSpec.js b/spec/core/MatchersSpec.js index ceaafc01..0326b68f 100644 --- a/spec/core/MatchersSpec.js +++ b/spec/core/MatchersSpec.js @@ -914,6 +914,47 @@ describe("jasmine.Matchers", function() { }); }); + describe("Matchers.Any", function () { + var any; + describe(".jasmineToString", function () { + describe("with Object", function () { + it ("says it's looking for an object", function () { + any = jasmine.any(Object); + expect(any.jasmineToString()).toMatch(//); + }); + }); + + describe("with Function", function () { + it ("says it's looking for a function", function () { + any = jasmine.any(Function); + expect(any.jasmineToString()).toMatch(//); + }); + }); + + describe("with String", function () { + it ("says it's looking for a string", function () { + any = jasmine.any(String); + expect(any.jasmineToString()).toMatch(//); + }); + }); + + describe("with Number", function () { + it ("says it's looking for a number", function () { + any = jasmine.any(Number); + expect(any.jasmineToString()).toMatch(//); + }); + }); + + describe("with some other defined 'class'", function () { + it ("says it's looking for an object", function () { + function MyClass () {} + any = jasmine.any(MyClass); + expect(any.jasmineToString()).toMatch(//); + }); + }); + }); + }); + describe("all matchers", function() { it("should return null, for future-proofing, since we might eventually allow matcher chaining", function() { expect(match(true).toBe(true)).toBeUndefined(); diff --git a/spec/core/PrettyPrintSpec.js b/spec/core/PrettyPrintSpec.js index 2faea310..becf8a61 100644 --- a/spec/core/PrettyPrintSpec.js +++ b/spec/core/PrettyPrintSpec.js @@ -83,12 +83,12 @@ describe("jasmine.pp", function () { expect(jasmine.pp(jasmine.createSpy("something"))).toEqual("spy on something"); }); - it("calls toString for ObjectContaining objects", function () { - var containing = new jasmine.Matchers.ObjectContaining({}); - spyOn(containing, "toString").andReturn("stringified!"); + it("should stringify objects that implement jasmineToString", function () { + var obj = { + jasmineToString: function () { return "strung"; } + }; - expect(jasmine.pp(containing)).toEqual("stringified!"); - expect(containing.toString).toHaveBeenCalled(); + expect(jasmine.pp(obj)).toEqual("strung"); }); }); diff --git a/src/core/Matchers.js b/src/core/Matchers.js index ddcc6600..e2072021 100644 --- a/src/core/Matchers.js +++ b/src/core/Matchers.js @@ -365,7 +365,7 @@ jasmine.Matchers.Any.prototype.matches = function(other) { return other instanceof this.expectedClass; }; -jasmine.Matchers.Any.prototype.toString = function() { +jasmine.Matchers.Any.prototype.jasmineToString = function() { return ''; }; diff --git a/src/core/PrettyPrinter.js b/src/core/PrettyPrinter.js index d44bb24d..c7845107 100644 --- a/src/core/PrettyPrinter.js +++ b/src/core/PrettyPrinter.js @@ -23,10 +23,8 @@ jasmine.PrettyPrinter.prototype.format = function(value) { this.emitScalar('null'); } else if (value === jasmine.getGlobal()) { this.emitScalar(''); - } else if (value instanceof jasmine.Matchers.Any) { - this.emitScalar(value.toString()); - } else if (value instanceof jasmine.Matchers.ObjectContaining) { - this.emitScalar(value.toString()); + } else if (value.hasOwnProperty("jasmineToString")) { + this.emitScalar(value.jasmineToString()); } else if (typeof value === 'string') { this.emitString(value); } else if (jasmine.isSpy(value)) {