Merge pull request #290 from maxbrunsfeld/smaller-pretty-print2
smaller, more configurable pretty-printing of objects
This commit is contained in:
@@ -32,6 +32,36 @@ describe("jasmine.pp", function () {
|
||||
}, bar: [1, 2, 3]})).toEqual("{ foo : Function, bar : [ 1, 2, 3 ] }");
|
||||
});
|
||||
|
||||
it("should not include inherited properties when stringifying an object", function() {
|
||||
var SomeClass = function() {};
|
||||
SomeClass.prototype.foo = "inherited foo";
|
||||
var instance = new SomeClass();
|
||||
instance.bar = "my own bar";
|
||||
expect(jasmine.pp(instance)).toEqual("{ bar : 'my own bar' }");
|
||||
});
|
||||
|
||||
it("should not recurse objects and arrays more deeply than jasmine.MAX_PRETTY_PRINT_DEPTH", function() {
|
||||
var originalMaxDepth = jasmine.MAX_PRETTY_PRINT_DEPTH;
|
||||
var nestedObject = { level1: { level2: { level3: { level4: "leaf" } } } };
|
||||
var nestedArray = [1, [2, [3, [4, "leaf"]]]];
|
||||
|
||||
try {
|
||||
jasmine.MAX_PRETTY_PRINT_DEPTH = 2;
|
||||
expect(jasmine.pp(nestedObject)).toEqual("{ level1 : { level2 : Object } }");
|
||||
expect(jasmine.pp(nestedArray)).toEqual("[ 1, [ 2, Array ] ]");
|
||||
|
||||
jasmine.MAX_PRETTY_PRINT_DEPTH = 3;
|
||||
expect(jasmine.pp(nestedObject)).toEqual("{ level1 : { level2 : { level3 : Object } } }");
|
||||
expect(jasmine.pp(nestedArray)).toEqual("[ 1, [ 2, [ 3, Array ] ] ]");
|
||||
|
||||
jasmine.MAX_PRETTY_PRINT_DEPTH = 4;
|
||||
expect(jasmine.pp(nestedObject)).toEqual("{ level1 : { level2 : { level3 : { level4 : 'leaf' } } } }");
|
||||
expect(jasmine.pp(nestedArray)).toEqual("[ 1, [ 2, [ 3, [ 4, 'leaf' ] ] ] ]");
|
||||
} finally {
|
||||
jasmine.MAX_PRETTY_PRINT_DEPTH = originalMaxDepth;
|
||||
}
|
||||
});
|
||||
|
||||
it("should stringify RegExp objects properly", function() {
|
||||
expect(jasmine.pp(/x|y|z/)).toEqual("/x|y|z/");
|
||||
});
|
||||
|
||||
@@ -11,10 +11,6 @@ jasmine.PrettyPrinter = function() {
|
||||
* @param value
|
||||
*/
|
||||
jasmine.PrettyPrinter.prototype.format = function(value) {
|
||||
if (this.ppNestLevel_ > 40) {
|
||||
throw new Error('jasmine.PrettyPrinter: format() nested too deeply!');
|
||||
}
|
||||
|
||||
this.ppNestLevel_++;
|
||||
try {
|
||||
if (value === jasmine.undefined) {
|
||||
@@ -57,6 +53,7 @@ jasmine.PrettyPrinter.prototype.format = function(value) {
|
||||
|
||||
jasmine.PrettyPrinter.prototype.iterateObject = function(obj, fn) {
|
||||
for (var property in obj) {
|
||||
if (!obj.hasOwnProperty(property)) continue;
|
||||
if (property == '__Jasmine_been_here_before__') continue;
|
||||
fn(property, obj.__lookupGetter__ ? (obj.__lookupGetter__(property) !== jasmine.undefined &&
|
||||
obj.__lookupGetter__(property) !== null) : false);
|
||||
@@ -84,6 +81,11 @@ jasmine.StringPrettyPrinter.prototype.emitString = function(value) {
|
||||
};
|
||||
|
||||
jasmine.StringPrettyPrinter.prototype.emitArray = function(array) {
|
||||
if (this.ppNestLevel_ > jasmine.MAX_PRETTY_PRINT_DEPTH) {
|
||||
this.append("Array");
|
||||
return;
|
||||
}
|
||||
|
||||
this.append('[ ');
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
if (i > 0) {
|
||||
@@ -95,6 +97,11 @@ jasmine.StringPrettyPrinter.prototype.emitArray = function(array) {
|
||||
};
|
||||
|
||||
jasmine.StringPrettyPrinter.prototype.emitObject = function(obj) {
|
||||
if (this.ppNestLevel_ > jasmine.MAX_PRETTY_PRINT_DEPTH) {
|
||||
this.append("Object");
|
||||
return;
|
||||
}
|
||||
|
||||
var self = this;
|
||||
this.append('{ ');
|
||||
var first = true;
|
||||
|
||||
@@ -34,6 +34,11 @@ jasmine.VERBOSE = false;
|
||||
*/
|
||||
jasmine.DEFAULT_UPDATE_INTERVAL = 250;
|
||||
|
||||
/**
|
||||
* Maximum levels of nesting that will be included when an object is pretty-printed
|
||||
*/
|
||||
jasmine.MAX_PRETTY_PRINT_DEPTH = 40;
|
||||
|
||||
/**
|
||||
* Default timeout interval in milliseconds for waitsFor() blocks.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user