Pretty printer will now use MAX_PRETTY_PRINT_ARRAY_LENGTH for objects
- Fixes #1291 - Fixes #1360
This commit is contained in:
@@ -3699,11 +3699,23 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
PrettyPrinter.prototype.iterateObject = function(obj, fn) {
|
PrettyPrinter.prototype.iterateObject = function(obj, fn) {
|
||||||
for (var property in obj) {
|
var objKeys = keys(obj, j$.isArray_(obj));
|
||||||
if (!Object.prototype.hasOwnProperty.call(obj, property)) { continue; }
|
var isGetter = function isGetter(prop) {};
|
||||||
fn(property, obj.__lookupGetter__ ? (!j$.util.isUndefined(obj.__lookupGetter__(property)) &&
|
|
||||||
obj.__lookupGetter__(property) !== null) : false);
|
if (obj.__lookupGetter__) {
|
||||||
|
isGetter = function isGetter(prop) {
|
||||||
|
var getter = obj.__lookupGetter__(prop);
|
||||||
|
return !j$.util.isUndefined(getter) && getter !== null;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
var length = Math.min(objKeys.length, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH);
|
||||||
|
for (var i = 0; i < length; i++) {
|
||||||
|
var property = objKeys[i];
|
||||||
|
fn(property, isGetter(property));
|
||||||
|
}
|
||||||
|
|
||||||
|
return objKeys.length > length;
|
||||||
};
|
};
|
||||||
|
|
||||||
PrettyPrinter.prototype.emitArray = j$.unimplementedMethod_;
|
PrettyPrinter.prototype.emitArray = j$.unimplementedMethod_;
|
||||||
@@ -3716,7 +3728,7 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
function StringPrettyPrinter() {
|
function StringPrettyPrinter() {
|
||||||
PrettyPrinter.call(this);
|
PrettyPrinter.call(this);
|
||||||
|
|
||||||
this.string = '';
|
this.stringParts = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
j$.util.inherit(StringPrettyPrinter, PrettyPrinter);
|
j$.util.inherit(StringPrettyPrinter, PrettyPrinter);
|
||||||
@@ -3748,11 +3760,7 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
var first = array.length === 0;
|
var first = array.length === 0;
|
||||||
this.iterateObject(array, function(property, isGetter) {
|
var truncated = this.iterateObject(array, function(property, isGetter) {
|
||||||
if (property.match(/^\d+$/)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
} else {
|
} else {
|
||||||
@@ -3762,6 +3770,8 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
self.formatProperty(array, property, isGetter);
|
self.formatProperty(array, property, isGetter);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (truncated) { this.append(', ...'); }
|
||||||
|
|
||||||
this.append(' ]');
|
this.append(' ]');
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -3823,7 +3833,7 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
this.append('({ ');
|
this.append('({ ');
|
||||||
var first = true;
|
var first = true;
|
||||||
|
|
||||||
this.iterateObject(obj, function(property, isGetter) {
|
var truncated = this.iterateObject(obj, function(property, isGetter) {
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
} else {
|
} else {
|
||||||
@@ -3833,6 +3843,8 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
self.formatProperty(obj, property, isGetter);
|
self.formatProperty(obj, property, isGetter);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (truncated) { this.append(', ...'); }
|
||||||
|
|
||||||
this.append(' })');
|
this.append(' })');
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -3847,13 +3859,42 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
StringPrettyPrinter.prototype.append = function(value) {
|
StringPrettyPrinter.prototype.append = function(value) {
|
||||||
this.string += value;
|
this.stringParts.push(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function keys(obj, isArray) {
|
||||||
|
var allKeys = Object.keys ? Object.keys(obj) :
|
||||||
|
(function(o) {
|
||||||
|
var keys = [];
|
||||||
|
for (var key in o) {
|
||||||
|
if (j$.util.has(o, key)) {
|
||||||
|
keys.push(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return keys;
|
||||||
|
})(obj);
|
||||||
|
|
||||||
|
if (!isArray) {
|
||||||
|
return allKeys;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (allKeys.length === 0) {
|
||||||
|
return allKeys;
|
||||||
|
}
|
||||||
|
|
||||||
|
var extraKeys = [];
|
||||||
|
for (var i = 0; i < allKeys.length; i++) {
|
||||||
|
if (!/^[0-9]+$/.test(allKeys[i])) {
|
||||||
|
extraKeys.push(allKeys[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return extraKeys;
|
||||||
|
}
|
||||||
return function(value) {
|
return function(value) {
|
||||||
var stringPrettyPrinter = new StringPrettyPrinter();
|
var stringPrettyPrinter = new StringPrettyPrinter();
|
||||||
stringPrettyPrinter.format(value);
|
stringPrettyPrinter.format(value);
|
||||||
return stringPrettyPrinter.string;
|
return stringPrettyPrinter.stringParts.join('');
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -119,6 +119,18 @@ describe("jasmineUnderTest.pp", function () {
|
|||||||
}, bar: [1, 2, 3]})).toEqual("Object({ foo: Function, bar: [ 1, 2, 3 ] })");
|
}, bar: [1, 2, 3]})).toEqual("Object({ foo: Function, bar: [ 1, 2, 3 ] })");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should truncate objects with too many keys", function () {
|
||||||
|
var originalMaxLength = jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH;
|
||||||
|
var long = {a: 1, b: 2, c: 3};
|
||||||
|
|
||||||
|
try {
|
||||||
|
jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = 2;
|
||||||
|
expect(jasmineUnderTest.pp(long)).toEqual("Object({ a: 1, b: 2, ... })");
|
||||||
|
} finally {
|
||||||
|
jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = originalMaxLength;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
it("should print 'null' as the constructor of an object with its own constructor property", function() {
|
it("should print 'null' as the constructor of an object with its own constructor property", function() {
|
||||||
expect(jasmineUnderTest.pp({constructor: function() {}})).toContain("null({");
|
expect(jasmineUnderTest.pp({constructor: function() {}})).toContain("null({");
|
||||||
expect(jasmineUnderTest.pp({constructor: 'foo'})).toContain("null({");
|
expect(jasmineUnderTest.pp({constructor: 'foo'})).toContain("null({");
|
||||||
|
|||||||
@@ -61,11 +61,23 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
PrettyPrinter.prototype.iterateObject = function(obj, fn) {
|
PrettyPrinter.prototype.iterateObject = function(obj, fn) {
|
||||||
for (var property in obj) {
|
var objKeys = keys(obj, j$.isArray_(obj));
|
||||||
if (!Object.prototype.hasOwnProperty.call(obj, property)) { continue; }
|
var isGetter = function isGetter(prop) {};
|
||||||
fn(property, obj.__lookupGetter__ ? (!j$.util.isUndefined(obj.__lookupGetter__(property)) &&
|
|
||||||
obj.__lookupGetter__(property) !== null) : false);
|
if (obj.__lookupGetter__) {
|
||||||
|
isGetter = function isGetter(prop) {
|
||||||
|
var getter = obj.__lookupGetter__(prop);
|
||||||
|
return !j$.util.isUndefined(getter) && getter !== null;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
var length = Math.min(objKeys.length, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH);
|
||||||
|
for (var i = 0; i < length; i++) {
|
||||||
|
var property = objKeys[i];
|
||||||
|
fn(property, isGetter(property));
|
||||||
|
}
|
||||||
|
|
||||||
|
return objKeys.length > length;
|
||||||
};
|
};
|
||||||
|
|
||||||
PrettyPrinter.prototype.emitArray = j$.unimplementedMethod_;
|
PrettyPrinter.prototype.emitArray = j$.unimplementedMethod_;
|
||||||
@@ -78,7 +90,7 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
function StringPrettyPrinter() {
|
function StringPrettyPrinter() {
|
||||||
PrettyPrinter.call(this);
|
PrettyPrinter.call(this);
|
||||||
|
|
||||||
this.string = '';
|
this.stringParts = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
j$.util.inherit(StringPrettyPrinter, PrettyPrinter);
|
j$.util.inherit(StringPrettyPrinter, PrettyPrinter);
|
||||||
@@ -110,11 +122,7 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
var first = array.length === 0;
|
var first = array.length === 0;
|
||||||
this.iterateObject(array, function(property, isGetter) {
|
var truncated = this.iterateObject(array, function(property, isGetter) {
|
||||||
if (property.match(/^\d+$/)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
} else {
|
} else {
|
||||||
@@ -124,6 +132,8 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
self.formatProperty(array, property, isGetter);
|
self.formatProperty(array, property, isGetter);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (truncated) { this.append(', ...'); }
|
||||||
|
|
||||||
this.append(' ]');
|
this.append(' ]');
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -185,7 +195,7 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
this.append('({ ');
|
this.append('({ ');
|
||||||
var first = true;
|
var first = true;
|
||||||
|
|
||||||
this.iterateObject(obj, function(property, isGetter) {
|
var truncated = this.iterateObject(obj, function(property, isGetter) {
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
} else {
|
} else {
|
||||||
@@ -195,6 +205,8 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
self.formatProperty(obj, property, isGetter);
|
self.formatProperty(obj, property, isGetter);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (truncated) { this.append(', ...'); }
|
||||||
|
|
||||||
this.append(' })');
|
this.append(' })');
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -209,12 +221,41 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
StringPrettyPrinter.prototype.append = function(value) {
|
StringPrettyPrinter.prototype.append = function(value) {
|
||||||
this.string += value;
|
this.stringParts.push(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function keys(obj, isArray) {
|
||||||
|
var allKeys = Object.keys ? Object.keys(obj) :
|
||||||
|
(function(o) {
|
||||||
|
var keys = [];
|
||||||
|
for (var key in o) {
|
||||||
|
if (j$.util.has(o, key)) {
|
||||||
|
keys.push(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return keys;
|
||||||
|
})(obj);
|
||||||
|
|
||||||
|
if (!isArray) {
|
||||||
|
return allKeys;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (allKeys.length === 0) {
|
||||||
|
return allKeys;
|
||||||
|
}
|
||||||
|
|
||||||
|
var extraKeys = [];
|
||||||
|
for (var i = 0; i < allKeys.length; i++) {
|
||||||
|
if (!/^[0-9]+$/.test(allKeys[i])) {
|
||||||
|
extraKeys.push(allKeys[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return extraKeys;
|
||||||
|
}
|
||||||
return function(value) {
|
return function(value) {
|
||||||
var stringPrettyPrinter = new StringPrettyPrinter();
|
var stringPrettyPrinter = new StringPrettyPrinter();
|
||||||
stringPrettyPrinter.format(value);
|
stringPrettyPrinter.format(value);
|
||||||
return stringPrettyPrinter.string;
|
return stringPrettyPrinter.stringParts.join('');
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
|
|||||||
j$.MAX_PRETTY_PRINT_DEPTH = 40;
|
j$.MAX_PRETTY_PRINT_DEPTH = 40;
|
||||||
/**
|
/**
|
||||||
* Maximum number of array elements to display when pretty printing objects.
|
* Maximum number of array elements to display when pretty printing objects.
|
||||||
|
* This will also limit the number of keys and values displayed for an object.
|
||||||
* Elements past this number will be ellipised.
|
* Elements past this number will be ellipised.
|
||||||
* @name jasmine.MAX_PRETTY_PRINT_ARRAY_LENGTH
|
* @name jasmine.MAX_PRETTY_PRINT_ARRAY_LENGTH
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user