will print out. Currently, jasmine's pretty printer will iterate over an entire array, formatting every element recursively. For very large arrays, this can crash the page, or cause a 'slow script' warning. This commit exposes a 'MAX_PRETTY_PRINT_ARRAY_LENGTH' option. If an array larger than this is encountered, recursion will stop and the array length will be printed instead e.g. "Array[20000000]". The 'MAX_PRETTY_PRINT_ARRAY_LENGTH' option defaults to 100. This is length of array will not kill your browser, but will allow you to see big arrays, if you can stomach the output.
138 lines
3.9 KiB
JavaScript
138 lines
3.9 KiB
JavaScript
getJasmineRequireObj().pp = function(j$) {
|
|
|
|
function PrettyPrinter() {
|
|
this.ppNestLevel_ = 0;
|
|
this.seen = [];
|
|
}
|
|
|
|
PrettyPrinter.prototype.format = function(value) {
|
|
this.ppNestLevel_++;
|
|
try {
|
|
if (j$.util.isUndefined(value)) {
|
|
this.emitScalar('undefined');
|
|
} else if (value === null) {
|
|
this.emitScalar('null');
|
|
} else if (value === 0 && 1/value === -Infinity) {
|
|
this.emitScalar('-0');
|
|
} else if (value === j$.getGlobal()) {
|
|
this.emitScalar('<global>');
|
|
} else if (value.jasmineToString) {
|
|
this.emitScalar(value.jasmineToString());
|
|
} else if (typeof value === 'string') {
|
|
this.emitString(value);
|
|
} else if (j$.isSpy(value)) {
|
|
this.emitScalar('spy on ' + value.and.identity());
|
|
} else if (value instanceof RegExp) {
|
|
this.emitScalar(value.toString());
|
|
} else if (typeof value === 'function') {
|
|
this.emitScalar('Function');
|
|
} else if (typeof value.nodeType === 'number') {
|
|
this.emitScalar('HTMLNode');
|
|
} else if (value instanceof Date) {
|
|
this.emitScalar('Date(' + value + ')');
|
|
} 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)) {
|
|
this.seen.push(value);
|
|
if (j$.isArray_(value)) {
|
|
this.emitArray(value);
|
|
} else {
|
|
this.emitObject(value);
|
|
}
|
|
this.seen.pop();
|
|
} else {
|
|
this.emitScalar(value.toString());
|
|
}
|
|
} finally {
|
|
this.ppNestLevel_--;
|
|
}
|
|
};
|
|
|
|
PrettyPrinter.prototype.iterateObject = function(obj, fn) {
|
|
for (var property in obj) {
|
|
if (!Object.prototype.hasOwnProperty.call(obj, property)) { continue; }
|
|
fn(property, obj.__lookupGetter__ ? (!j$.util.isUndefined(obj.__lookupGetter__(property)) &&
|
|
obj.__lookupGetter__(property) !== null) : false);
|
|
}
|
|
};
|
|
|
|
PrettyPrinter.prototype.emitArray = j$.unimplementedMethod_;
|
|
PrettyPrinter.prototype.emitObject = j$.unimplementedMethod_;
|
|
PrettyPrinter.prototype.emitScalar = j$.unimplementedMethod_;
|
|
PrettyPrinter.prototype.emitString = j$.unimplementedMethod_;
|
|
|
|
function StringPrettyPrinter() {
|
|
PrettyPrinter.call(this);
|
|
|
|
this.string = '';
|
|
}
|
|
|
|
j$.util.inherit(StringPrettyPrinter, PrettyPrinter);
|
|
|
|
StringPrettyPrinter.prototype.emitScalar = function(value) {
|
|
this.append(value);
|
|
};
|
|
|
|
StringPrettyPrinter.prototype.emitString = function(value) {
|
|
this.append('\'' + value + '\'');
|
|
};
|
|
|
|
StringPrettyPrinter.prototype.emitArray = function(array) {
|
|
if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) {
|
|
this.append('Array');
|
|
return;
|
|
}
|
|
var length = Math.min(array.length, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH);
|
|
this.append('[ ');
|
|
for (var i = 0; i < length; i++) {
|
|
if (i > 0) {
|
|
this.append(', ');
|
|
}
|
|
this.format(array[i]);
|
|
}
|
|
if(array.length > length){
|
|
this.append(', ...');
|
|
}
|
|
this.append(' ]');
|
|
};
|
|
|
|
StringPrettyPrinter.prototype.emitObject = function(obj) {
|
|
if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) {
|
|
this.append('Object');
|
|
return;
|
|
}
|
|
|
|
var self = this;
|
|
this.append('{ ');
|
|
var first = true;
|
|
|
|
this.iterateObject(obj, function(property, isGetter) {
|
|
if (first) {
|
|
first = false;
|
|
} else {
|
|
self.append(', ');
|
|
}
|
|
|
|
self.append(property);
|
|
self.append(': ');
|
|
if (isGetter) {
|
|
self.append('<getter>');
|
|
} else {
|
|
self.format(obj[property]);
|
|
}
|
|
});
|
|
|
|
this.append(' }');
|
|
};
|
|
|
|
StringPrettyPrinter.prototype.append = function(value) {
|
|
this.string += value;
|
|
};
|
|
|
|
return function(value) {
|
|
var stringPrettyPrinter = new StringPrettyPrinter();
|
|
stringPrettyPrinter.format(value);
|
|
return stringPrettyPrinter.string;
|
|
};
|
|
};
|