PrettyPrinter allows an object to have a toString that isn't a function

- Fixes #1389
This commit is contained in:
Gregg Van Hove
2017-07-17 22:01:06 -07:00
parent aa72b0b7cf
commit 22e9d6df20
3 changed files with 14 additions and 6 deletions

View File

@@ -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());

View File

@@ -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 ''; };

View File

@@ -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());