Include property getter values in pretty-printed objects

We already call getters when comparing objects for equality and generating
diffs, so it should be safe to do it here too.

See #1966.
This commit is contained in:
Steve Gravrock
2022-05-12 17:14:13 -07:00
parent 68eaa64c31
commit bb4d18f959
3 changed files with 19 additions and 53 deletions

View File

@@ -7649,18 +7649,10 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
SinglePrettyPrintRun.prototype.iterateObject = function(obj, fn) { SinglePrettyPrintRun.prototype.iterateObject = function(obj, fn) {
var objKeys = j$.MatchersUtil.keys(obj, j$.isArray_(obj)); var objKeys = j$.MatchersUtil.keys(obj, j$.isArray_(obj));
var isGetter = function isGetter(prop) {};
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); var length = Math.min(objKeys.length, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH);
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
var property = objKeys[i]; fn(objKeys[i]);
fn(property, isGetter(property));
} }
return objKeys.length > length; return objKeys.length > length;
@@ -7693,14 +7685,14 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
var self = this; var self = this;
var first = array.length === 0; var first = array.length === 0;
var truncated = this.iterateObject(array, function(property, isGetter) { var truncated = this.iterateObject(array, function(property) {
if (first) { if (first) {
first = false; first = false;
} else { } else {
self.append(', '); self.append(', ');
} }
self.formatProperty(array, property, isGetter); self.formatProperty(array, property);
}); });
if (truncated) { if (truncated) {
@@ -7779,14 +7771,14 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
this.append('({ '); this.append('({ ');
var first = true; var first = true;
var truncated = this.iterateObject(obj, function(property, isGetter) { var truncated = this.iterateObject(obj, function(property) {
if (first) { if (first) {
first = false; first = false;
} else { } else {
self.append(', '); self.append(', ');
} }
self.formatProperty(obj, property, isGetter); self.formatProperty(obj, property);
}); });
if (truncated) { if (truncated) {
@@ -7838,11 +7830,7 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
this.append(out); this.append(out);
}; };
SinglePrettyPrintRun.prototype.formatProperty = function( SinglePrettyPrintRun.prototype.formatProperty = function(obj, property) {
obj,
property,
isGetter
) {
if (typeof property === 'symbol') { if (typeof property === 'symbol') {
this.append(property.toString()); this.append(property.toString());
} else { } else {
@@ -7850,12 +7838,7 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
} }
this.append(': '); this.append(': ');
this.format(obj[property]);
if (isGetter) {
this.append('<getter>');
} else {
this.format(obj[property]);
}
}; };
SinglePrettyPrintRun.prototype.append = function(value) { SinglePrettyPrintRun.prototype.append = function(value) {

View File

@@ -323,16 +323,16 @@ describe('PrettyPrinter', function() {
); );
}); });
it('should indicate getters on objects as such', function() { it('should use the return value of getters', function() {
const pp = jasmineUnderTest.makePrettyPrinter(); const pp = jasmineUnderTest.makePrettyPrinter();
const sampleValue = { const sampleValue = {
id: 1, id: 1,
get calculatedValue() { get calculatedValue() {
throw new Error("don't call me!"); return 'the getter return value';
} }
}; };
expect(pp(sampleValue)).toEqual( expect(pp(sampleValue)).toEqual(
'Object({ id: 1, calculatedValue: <getter> })' "Object({ id: 1, calculatedValue: 'the getter return value' })"
); );
}); });

View File

@@ -107,18 +107,10 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
SinglePrettyPrintRun.prototype.iterateObject = function(obj, fn) { SinglePrettyPrintRun.prototype.iterateObject = function(obj, fn) {
var objKeys = j$.MatchersUtil.keys(obj, j$.isArray_(obj)); var objKeys = j$.MatchersUtil.keys(obj, j$.isArray_(obj));
var isGetter = function isGetter(prop) {};
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); var length = Math.min(objKeys.length, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH);
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
var property = objKeys[i]; fn(objKeys[i]);
fn(property, isGetter(property));
} }
return objKeys.length > length; return objKeys.length > length;
@@ -151,14 +143,14 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
var self = this; var self = this;
var first = array.length === 0; var first = array.length === 0;
var truncated = this.iterateObject(array, function(property, isGetter) { var truncated = this.iterateObject(array, function(property) {
if (first) { if (first) {
first = false; first = false;
} else { } else {
self.append(', '); self.append(', ');
} }
self.formatProperty(array, property, isGetter); self.formatProperty(array, property);
}); });
if (truncated) { if (truncated) {
@@ -237,14 +229,14 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
this.append('({ '); this.append('({ ');
var first = true; var first = true;
var truncated = this.iterateObject(obj, function(property, isGetter) { var truncated = this.iterateObject(obj, function(property) {
if (first) { if (first) {
first = false; first = false;
} else { } else {
self.append(', '); self.append(', ');
} }
self.formatProperty(obj, property, isGetter); self.formatProperty(obj, property);
}); });
if (truncated) { if (truncated) {
@@ -296,11 +288,7 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
this.append(out); this.append(out);
}; };
SinglePrettyPrintRun.prototype.formatProperty = function( SinglePrettyPrintRun.prototype.formatProperty = function(obj, property) {
obj,
property,
isGetter
) {
if (typeof property === 'symbol') { if (typeof property === 'symbol') {
this.append(property.toString()); this.append(property.toString());
} else { } else {
@@ -308,12 +296,7 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
} }
this.append(': '); this.append(': ');
this.format(obj[property]);
if (isGetter) {
this.append('<getter>');
} else {
this.format(obj[property]);
}
}; };
SinglePrettyPrintRun.prototype.append = function(value) { SinglePrettyPrintRun.prototype.append = function(value) {