Created
April 28, 2015 03:02
-
-
Save rogeriochaves/4cb72f3306c0209800b1 to your computer and use it in GitHub Desktop.
Jasmine Pretty Printer when comparing objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jasmine.MAX_PRETTY_PRINT_DEPTH = 3; | |
jasmine.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, padding, self, first, i, max; | |
constructorName = obj.constructor ? j$.fnNameFor(obj.constructor) : 'null'; | |
this.append(constructorName); | |
if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) { | |
this.append('...'); | |
return; | |
} | |
padding = ''; | |
for(var i = 0, max = this.ppNestLevel_; i < max; i++){ | |
padding += '\t'; | |
} | |
self = this; | |
this.append('({'); | |
first = true; | |
this.iterateObject(obj, function(property, isGetter) { | |
if (first) { | |
first = false; | |
} else { | |
self.append(','); | |
} | |
self.append('\n'); | |
self.formatProperty(obj, property, isGetter, padding); | |
}); | |
if (first) { | |
this.append(' '); | |
} else { | |
this.append('\n' + padding.substring(1)); | |
} | |
this.append('})'); | |
}; | |
StringPrettyPrinter.prototype.formatProperty = function(obj, property, isGetter, padding) { | |
this.append(padding + property + ': '); | |
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; | |
}; | |
})(jasmine); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment