Merge branch 'enelson/pp' of https://github.com/elliot-nelson/jasmine into elliot-nelson-enelson/pp
- Merges #1711 from @elliot-nelson - Closes #1700 - Closes #1575
This commit is contained in:
@@ -333,4 +333,31 @@ describe("jasmineUnderTest.pp", function () {
|
|||||||
|
|
||||||
expect(jasmineUnderTest.pp(obj)).toEqual("null({ foo: 'bar' })");
|
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() {
|
||||||
|
// Really invalid: a nested bad toString().
|
||||||
|
return {
|
||||||
|
toString: function() {
|
||||||
|
return new Date();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// 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 Object], baz: 3, qux: Error: bar })");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -266,6 +266,12 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
PrettyPrinter.prototype.append = function(value) {
|
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);
|
var result = truncate(value, j$.MAX_PRETTY_PRINT_CHARS - this.length);
|
||||||
this.length += result.value.length;
|
this.length += result.value.length;
|
||||||
this.stringParts.push(result.value);
|
this.stringParts.push(result.value);
|
||||||
|
|||||||
Reference in New Issue
Block a user