From d0ad9b98cde20845c780a49e8517fdb429fe477d Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Fri, 25 Sep 2015 19:43:19 +0100 Subject: [PATCH] Use toString for objects if it has been overriden --- spec/core/PrettyPrintSpec.js | 8 ++++++++ src/core/PrettyPrinter.js | 2 ++ 2 files changed, 10 insertions(+) diff --git a/spec/core/PrettyPrintSpec.js b/spec/core/PrettyPrintSpec.js index 2686c154..6d818a8f 100644 --- a/spec/core/PrettyPrintSpec.js +++ b/spec/core/PrettyPrintSpec.js @@ -181,6 +181,14 @@ describe("j$.pp", function () { expect(j$.pp(obj)).toEqual("strung"); }); + it("should stringify objects that implement custom toString", function () { + var obj = { + toString: function () { return "my toString"; } + }; + + expect(j$.pp(obj)).toEqual("my toString"); + }); + it("should handle objects with null prototype", function() { if (jasmine.getEnv().ieVersion < 9) { return; } diff --git a/src/core/PrettyPrinter.js b/src/core/PrettyPrinter.js index 8f86b5d8..e9452961 100644 --- a/src/core/PrettyPrinter.js +++ b/src/core/PrettyPrinter.js @@ -30,6 +30,8 @@ getJasmineRequireObj().pp = function(j$) { this.emitScalar('HTMLNode'); } else if (value instanceof Date) { this.emitScalar('Date(' + value + ')'); + } else if (value.toString && typeof value === 'object' && !(value instanceof Array) && value.toString !== Object.prototype.toString) { + this.emitScalar(value.toString()); } else if (j$.util.arrayContains(this.seen, value)) { this.emitScalar(''); } else if (j$.isArray_(value) || j$.isA_('Object', value)) {