PrettyPrinter survives if objects throw in toString
This commit is contained in:
@@ -457,11 +457,17 @@ describe('jasmineUnderTest.pp', function() {
|
|||||||
// Valid: an actual number
|
// Valid: an actual number
|
||||||
baz: 3,
|
baz: 3,
|
||||||
// Valid: an actual Error object
|
// Valid: an actual Error object
|
||||||
qux: new Error('bar')
|
qux: new Error('bar'),
|
||||||
|
//
|
||||||
|
baddy: {
|
||||||
|
toString: function() {
|
||||||
|
throw new Error('I am a bad toString');
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(jasmineUnderTest.pp(obj)).toEqual(
|
expect(jasmineUnderTest.pp(obj)).toEqual(
|
||||||
'Object({ foo: [object Number], bar: [object Object], baz: 3, qux: Error: bar })'
|
'Object({ foo: [object Number], bar: [object Object], baz: 3, qux: Error: bar, baddy: has-invalid-toString-method })'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -9,11 +9,16 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
function hasCustomToString(value) {
|
function hasCustomToString(value) {
|
||||||
// value.toString !== Object.prototype.toString if value has no custom toString but is from another context (e.g.
|
// value.toString !== Object.prototype.toString if value has no custom toString but is from another context (e.g.
|
||||||
// iframe, web worker)
|
// iframe, web worker)
|
||||||
return (
|
try {
|
||||||
j$.isFunction_(value.toString) &&
|
return (
|
||||||
value.toString !== Object.prototype.toString &&
|
j$.isFunction_(value.toString) &&
|
||||||
value.toString() !== Object.prototype.toString.call(value)
|
value.toString !== Object.prototype.toString &&
|
||||||
);
|
value.toString() !== Object.prototype.toString.call(value)
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
// The custom toString() threw.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PrettyPrinter.prototype.format = function(value) {
|
PrettyPrinter.prototype.format = function(value) {
|
||||||
@@ -59,7 +64,11 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
!j$.isArray_(value) &&
|
!j$.isArray_(value) &&
|
||||||
hasCustomToString(value)
|
hasCustomToString(value)
|
||||||
) {
|
) {
|
||||||
this.emitScalar(value.toString());
|
try {
|
||||||
|
this.emitScalar(value.toString());
|
||||||
|
} catch (e) {
|
||||||
|
this.emitScalar('has-invalid-toString-method');
|
||||||
|
}
|
||||||
} else if (j$.util.arrayContains(this.seen, value)) {
|
} else if (j$.util.arrayContains(this.seen, value)) {
|
||||||
this.emitScalar(
|
this.emitScalar(
|
||||||
'<circular reference: ' +
|
'<circular reference: ' +
|
||||||
|
|||||||
Reference in New Issue
Block a user