diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 18abd74d..3439c5e1 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -3680,7 +3680,7 @@ getJasmineRequireObj().pp = function(j$) { function hasCustomToString(value) { // value.toString !== Object.prototype.toString if value has no custom toString but is from another context (e.g. // iframe, web worker) - return value.toString !== Object.prototype.toString && (value.toString() !== Object.prototype.toString.call(value)); + return j$.isFunction_(value.toString) && value.toString !== Object.prototype.toString && (value.toString() !== Object.prototype.toString.call(value)); } PrettyPrinter.prototype.format = function(value) { @@ -3708,9 +3708,9 @@ getJasmineRequireObj().pp = function(j$) { this.emitScalar('HTMLNode'); } else if (value instanceof Date) { this.emitScalar('Date(' + value + ')'); - } else if (value.toString && value.toString() == '[object Set]') { + } else if (j$.getType_(value) == '[object Set]') { this.emitSet(value); - } else if (value.toString && value.toString() == '[object Map]') { + } else if (j$.getType_(value) == '[object Map]') { this.emitMap(value); } else if (value.toString && typeof value === 'object' && !j$.isArray_(value) && hasCustomToString(value)) { this.emitScalar(value.toString()); diff --git a/spec/core/PrettyPrintSpec.js b/spec/core/PrettyPrintSpec.js index 905c27ec..e3e64f01 100644 --- a/spec/core/PrettyPrintSpec.js +++ b/spec/core/PrettyPrintSpec.js @@ -257,6 +257,14 @@ describe("jasmineUnderTest.pp", function () { } }); + it("should stringify objects have have a toString that isn't a function", function() { + var obj = { + toString: "foo" + }; + + expect(jasmineUnderTest.pp(obj)).toEqual("Object({ toString: 'foo' })"); + }); + it("should stringify objects from anonymous constructors with custom toString", function () { var MyAnonymousConstructor = (function() { return function () {}; })(); MyAnonymousConstructor.toString = function () { return ''; }; diff --git a/src/core/PrettyPrinter.js b/src/core/PrettyPrinter.js index bd4d8360..5df4166a 100644 --- a/src/core/PrettyPrinter.js +++ b/src/core/PrettyPrinter.js @@ -8,7 +8,7 @@ getJasmineRequireObj().pp = function(j$) { function hasCustomToString(value) { // value.toString !== Object.prototype.toString if value has no custom toString but is from another context (e.g. // iframe, web worker) - return value.toString !== Object.prototype.toString && (value.toString() !== Object.prototype.toString.call(value)); + return j$.isFunction_(value.toString) && value.toString !== Object.prototype.toString && (value.toString() !== Object.prototype.toString.call(value)); } PrettyPrinter.prototype.format = function(value) { @@ -36,9 +36,9 @@ getJasmineRequireObj().pp = function(j$) { this.emitScalar('HTMLNode'); } else if (value instanceof Date) { this.emitScalar('Date(' + value + ')'); - } else if (value.toString && value.toString() == '[object Set]') { + } else if (j$.getType_(value) == '[object Set]') { this.emitSet(value); - } else if (value.toString && value.toString() == '[object Map]') { + } else if (j$.getType_(value) == '[object Map]') { this.emitMap(value); } else if (value.toString && typeof value === 'object' && !j$.isArray_(value) && hasCustomToString(value)) { this.emitScalar(value.toString());