Refactor prettyPrinter to work with immutable objects

[#50766813][#266]
This commit is contained in:
Greg Cobb and Luan Santos
2014-03-12 11:41:18 -07:00
parent 9e927af56e
commit c9e37a2a1c
4 changed files with 57 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ getJasmineRequireObj().pp = function(j$) {
function PrettyPrinter() {
this.ppNestLevel_ = 0;
this.seen = [];
}
PrettyPrinter.prototype.format = function(value) {
@@ -29,16 +30,16 @@ getJasmineRequireObj().pp = function(j$) {
this.emitScalar('HTMLNode');
} else if (value instanceof Date) {
this.emitScalar('Date(' + value + ')');
} else if (value.__Jasmine_been_here_before__) {
} else if (j$.util.arrayContains(this.seen, value)) {
this.emitScalar('<circular reference: ' + (j$.isArray_(value) ? 'Array' : 'Object') + '>');
} else if (j$.isArray_(value) || j$.isA_('Object', value)) {
value.__Jasmine_been_here_before__ = true;
this.seen.push(value);
if (j$.isArray_(value)) {
this.emitArray(value);
} else {
this.emitObject(value);
}
delete value.__Jasmine_been_here_before__;
this.seen.pop();
} else {
this.emitScalar(value.toString());
}
@@ -50,7 +51,6 @@ getJasmineRequireObj().pp = function(j$) {
PrettyPrinter.prototype.iterateObject = function(obj, fn) {
for (var property in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, property)) { continue; }
if (property == '__Jasmine_been_here_before__') { continue; }
fn(property, obj.__lookupGetter__ ? (!j$.util.isUndefined(obj.__lookupGetter__(property)) &&
obj.__lookupGetter__(property) !== null) : false);
}

View File

@@ -30,5 +30,15 @@ getJasmineRequireObj().util = function() {
return obj === void 0;
};
util.arrayContains = function(array, search) {
var i = array.length;
while (i--) {
if (array[i] == search) {
return true;
}
}
return false;
}
return util;
};