Make toEqual matcher report the difference between objects

- Mismatches deep within object/array structures are pinpointed with a JsonPath-like syntax.
This commit is contained in:
Ben Christel
2016-07-18 15:17:00 -07:00
committed by Ben Christel
parent 5a76e59d5b
commit d5e6bf47ed
17 changed files with 2251 additions and 1156 deletions

View File

@@ -0,0 +1,47 @@
getJasmineRequireObj().ObjectPath = function(j$) {
function ObjectPath(components) {
this.components = components || [];
}
ObjectPath.prototype.toString = function() {
if (this.components.length) {
return '$' + map(this.components, formatPropertyAccess).join('');
} else {
return '';
}
};
ObjectPath.prototype.add = function(component) {
return new ObjectPath(this.components.concat([component]));
};
ObjectPath.prototype.depth = function() {
return this.components.length;
};
function formatPropertyAccess(prop) {
if (typeof prop === 'number') {
return '[' + prop + ']';
}
if (isValidIdentifier(prop)) {
return '.' + prop;
}
return '[\'' + prop + '\']';
}
function map(array, fn) {
var results = [];
for (var i = 0; i < array.length; i++) {
results.push(fn(array[i]));
}
return results;
}
function isValidIdentifier(string) {
return /^[A-Za-z\$_][A-Za-z0-9\$_]*$/.test(string);
}
return ObjectPath;
};