Inject a per-runable pretty printer into MatchersUtil
This will allow us to add support for custom object formatters, which will be a per-runable resource like custom matchers, by injecting them into the pretty-printer.
This commit is contained in:
committed by
Steve Gravrock
parent
dec67bd535
commit
1f23f1e4d2
@@ -1,5 +1,5 @@
|
||||
getJasmineRequireObj().pp = function(j$) {
|
||||
function PrettyPrinter() {
|
||||
getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
function SinglePrettyPrintRun() {
|
||||
this.ppNestLevel_ = 0;
|
||||
this.seen = [];
|
||||
this.length = 0;
|
||||
@@ -21,7 +21,7 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
}
|
||||
}
|
||||
|
||||
PrettyPrinter.prototype.format = function(value) {
|
||||
SinglePrettyPrintRun.prototype.format = function(value) {
|
||||
this.ppNestLevel_++;
|
||||
try {
|
||||
if (j$.util.isUndefined(value)) {
|
||||
@@ -95,7 +95,7 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
}
|
||||
};
|
||||
|
||||
PrettyPrinter.prototype.iterateObject = function(obj, fn) {
|
||||
SinglePrettyPrintRun.prototype.iterateObject = function(obj, fn) {
|
||||
var objKeys = keys(obj, j$.isArray_(obj));
|
||||
var isGetter = function isGetter(prop) {};
|
||||
|
||||
@@ -114,15 +114,15 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
return objKeys.length > length;
|
||||
};
|
||||
|
||||
PrettyPrinter.prototype.emitScalar = function(value) {
|
||||
SinglePrettyPrintRun.prototype.emitScalar = function(value) {
|
||||
this.append(value);
|
||||
};
|
||||
|
||||
PrettyPrinter.prototype.emitString = function(value) {
|
||||
SinglePrettyPrintRun.prototype.emitString = function(value) {
|
||||
this.append("'" + value + "'");
|
||||
};
|
||||
|
||||
PrettyPrinter.prototype.emitArray = function(array) {
|
||||
SinglePrettyPrintRun.prototype.emitArray = function(array) {
|
||||
if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) {
|
||||
this.append('Array');
|
||||
return;
|
||||
@@ -158,7 +158,7 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
this.append(' ]');
|
||||
};
|
||||
|
||||
PrettyPrinter.prototype.emitSet = function(set) {
|
||||
SinglePrettyPrintRun.prototype.emitSet = function(set) {
|
||||
if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) {
|
||||
this.append('Set');
|
||||
return;
|
||||
@@ -183,7 +183,7 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
this.append(' )');
|
||||
};
|
||||
|
||||
PrettyPrinter.prototype.emitMap = function(map) {
|
||||
SinglePrettyPrintRun.prototype.emitMap = function(map) {
|
||||
if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) {
|
||||
this.append('Map');
|
||||
return;
|
||||
@@ -208,7 +208,7 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
this.append(' )');
|
||||
};
|
||||
|
||||
PrettyPrinter.prototype.emitObject = function(obj) {
|
||||
SinglePrettyPrintRun.prototype.emitObject = function(obj) {
|
||||
var ctor = obj.constructor,
|
||||
constructorName;
|
||||
|
||||
@@ -244,7 +244,7 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
this.append(' })');
|
||||
};
|
||||
|
||||
PrettyPrinter.prototype.emitTypedArray = function(arr) {
|
||||
SinglePrettyPrintRun.prototype.emitTypedArray = function(arr) {
|
||||
var constructorName = j$.fnNameFor(arr.constructor),
|
||||
limitedArray = Array.prototype.slice.call(
|
||||
arr,
|
||||
@@ -260,7 +260,7 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
this.append(constructorName + ' [ ' + itemsString + ' ]');
|
||||
};
|
||||
|
||||
PrettyPrinter.prototype.emitDomElement = function(el) {
|
||||
SinglePrettyPrintRun.prototype.emitDomElement = function(el) {
|
||||
var tagName = el.tagName.toLowerCase(),
|
||||
attrs = el.attributes,
|
||||
i,
|
||||
@@ -286,7 +286,11 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
this.append(out);
|
||||
};
|
||||
|
||||
PrettyPrinter.prototype.formatProperty = function(obj, property, isGetter) {
|
||||
SinglePrettyPrintRun.prototype.formatProperty = function(
|
||||
obj,
|
||||
property,
|
||||
isGetter
|
||||
) {
|
||||
this.append(property);
|
||||
this.append(': ');
|
||||
if (isGetter) {
|
||||
@@ -296,7 +300,7 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
}
|
||||
};
|
||||
|
||||
PrettyPrinter.prototype.append = function(value) {
|
||||
SinglePrettyPrintRun.prototype.append = function(value) {
|
||||
// This check protects us from the rare case where an object has overriden
|
||||
// `toString()` with an invalid implementation (returning a non-string).
|
||||
if (typeof value !== 'string') {
|
||||
@@ -360,9 +364,12 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
|
||||
return extraKeys;
|
||||
}
|
||||
return function(value) {
|
||||
var prettyPrinter = new PrettyPrinter();
|
||||
prettyPrinter.format(value);
|
||||
return prettyPrinter.stringParts.join('');
|
||||
|
||||
return function() {
|
||||
return function(value) {
|
||||
var prettyPrinter = new SinglePrettyPrintRun();
|
||||
prettyPrinter.format(value);
|
||||
return prettyPrinter.stringParts.join('');
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user