161 lines
4.4 KiB
JavaScript
161 lines
4.4 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(', ...');
|
|
}
|
|
|
|
var self = this;
|
|
var first = array.length === 0;
|
|
this.iterateObject(array, function(property, isGetter) {
|
|
if (property.match(/^\d+$/)) {
|
|
return;
|
|
}
|
|
|
|
if (first) {
|
|
first = false;
|
|
} else {
|
|
self.append(', ');
|
|
}
|
|
|
|
self.formatProperty(array, property, isGetter);
|
|
});
|
|
|
|
this.append(' ]');
|
|
};
|
|
|
|
StringPrettyPrinter.prototype.emitObject = function(obj) {
|
|
var constructorName = obj.constructor ? j$.fnNameFor(obj.constructor) : 'null';
|
|
this.append(constructorName);
|
|
|
|
if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) {
|
|
return;
|
|
}
|
|
|
|
var self = this;
|
|
this.append('({ ');
|
|
var first = true;
|
|
|
|
this.iterateObject(obj, function(property, isGetter) {
|
|
if (first) {
|
|
first = false;
|
|
} else {
|
|
self.append(', ');
|
|
}
|
|
|
|
self.formatProperty(obj, property, isGetter);
|
|
});
|
|
|
|
this.append(' })');
|
|
};
|
|
|
|
StringPrettyPrinter.prototype.formatProperty = function(obj, property, isGetter) {
|
|
this.append(property);
|
|
this.append(': ');
|
|
if (isGetter) {
|
|
this.append('<getter>');
|
|
} else {
|
|
this.format(obj[property]);
|
|
}
|
|
};
|
|
|
|
StringPrettyPrinter.prototype.append = function(value) {
|
|
this.string += value;
|
|
};
|
|
|
|
return function(value) {
|
|
var stringPrettyPrinter = new StringPrettyPrinter();
|
|
stringPrettyPrinter.format(value);
|
|
return stringPrettyPrinter.string;
|
|
};
|
|
};
|