From 9f704b6f3a72a377f11406945bd0b1cedb99df87 Mon Sep 17 00:00:00 2001 From: Elliot Nelson Date: Mon, 20 May 2019 09:01:10 -0400 Subject: [PATCH 1/2] PrettyPrinter handles objects with invalid toString implementations --- spec/core/PrettyPrintSpec.js | 23 +++++++++++++++++++++++ src/core/PrettyPrinter.js | 6 ++++++ 2 files changed, 29 insertions(+) diff --git a/spec/core/PrettyPrintSpec.js b/spec/core/PrettyPrintSpec.js index 47a5758a..64dea0fd 100644 --- a/spec/core/PrettyPrintSpec.js +++ b/spec/core/PrettyPrintSpec.js @@ -333,4 +333,27 @@ describe("jasmineUnderTest.pp", function () { expect(jasmineUnderTest.pp(obj)).toEqual("null({ foo: 'bar' })"); }); + + it("should gracefully handle objects with invalid toString implementations", function () { + var obj = { + foo: { + toString: function() { + // Invalid: toString returning a number + return 3; + } + }, + bar: { + toString: function() { + // Invalid: toString returning an object + return new Error("bar"); + } + }, + // Valid: an actual number + baz: 3, + // Valid: an actual Error object + qux: new Error("bar") + }; + + expect(jasmineUnderTest.pp(obj)).toEqual("Object({ foo: [object Number], bar: [object Error], baz: 3, qux: Error: bar })"); + }); }); diff --git a/src/core/PrettyPrinter.js b/src/core/PrettyPrinter.js index cb738d28..42b89784 100644 --- a/src/core/PrettyPrinter.js +++ b/src/core/PrettyPrinter.js @@ -266,6 +266,12 @@ getJasmineRequireObj().pp = function(j$) { }; PrettyPrinter.prototype.append = function(value) { + // This check protects us from the rare case where an object has overriden + // `toString()` with an invalid implementation (returning a non-string). + if (typeof value !== 'string') { + value = Object.prototype.toString.call(value); + } + var result = truncate(value, j$.MAX_PRETTY_PRINT_CHARS - this.length); this.length += result.value.length; this.stringParts.push(result.value); From 8225bb935fb249ddf0010f32b0f34a370e3c3892 Mon Sep 17 00:00:00 2001 From: Elliot Nelson Date: Mon, 20 May 2019 09:09:07 -0400 Subject: [PATCH 2/2] Explicitly test pretty printing objects with nested invalid toString --- spec/core/PrettyPrintSpec.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/spec/core/PrettyPrintSpec.js b/spec/core/PrettyPrintSpec.js index 64dea0fd..4accef96 100644 --- a/spec/core/PrettyPrintSpec.js +++ b/spec/core/PrettyPrintSpec.js @@ -344,8 +344,12 @@ describe("jasmineUnderTest.pp", function () { }, bar: { toString: function() { - // Invalid: toString returning an object - return new Error("bar"); + // Really invalid: a nested bad toString(). + return { + toString: function() { + return new Date(); + } + }; } }, // Valid: an actual number @@ -354,6 +358,6 @@ describe("jasmineUnderTest.pp", function () { qux: new Error("bar") }; - expect(jasmineUnderTest.pp(obj)).toEqual("Object({ foo: [object Number], bar: [object Error], baz: 3, qux: Error: bar })"); + expect(jasmineUnderTest.pp(obj)).toEqual("Object({ foo: [object Number], bar: [object Object], baz: 3, qux: Error: bar })"); }); });