Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP 0.3.0 #11

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
JSON comparator added
  • Loading branch information
Olegas committed Apr 27, 2017
commit 5b39c034b413e4c8557b98b4d3a7bfc8ee80495c
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
compare: require(libPrefix + '/compare'),
XMLSerializer: require(libPrefix + '/canonizer'),
revXPath: require(libPrefix + '/revxpath'),
GroupingReporter: require(libPrefix + '/reporters/groupingReporter.js')
GroupingReporter: require(libPrefix + '/reporters/groupingReporter.js'),
comparators: {
jsonComparator: require(libPrefix + '/comparators/jsonComparator.js')
}
};

})();
43 changes: 43 additions & 0 deletions lib/comparators/jsonComparator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var jsonDiff = require('rfc6902-json-diff');

module.exports = function nodeValueJSONComparator(nodeNamesToCompare) {

function mustCompareNode(node) {
if (nodeNamesToCompare && nodeNamesToCompare.length) {
return nodeNamesToCompare.indexOf(node.nodeName) !== -1;
} else {
return true;
}
}

return function nodeValueJSONComparator(expected, actual) {

var
node = expected || actual,
eVal = expected && expected.nodeValue,
aVal = actual && actual.nodeValue,
result, diff;


if (mustCompareNode(node) && eVal && aVal) {
try {
eVal = eVal && JSON.parse(eVal);
aVal = aVal && JSON.parse(aVal);

diff = jsonDiff(aVal, eVal);

if (diff.length > 0) {
result = 'Attribute "config" differs. Expected:\n' + JSON.stringify(eVal, null, 2);
result += '\nActual:\n' + JSON.stringify(aVal, null, 2);
result += '\nDiff:\n' + JSON.stringify(diff, null, 2);

return result;
} else {
return true;
}
} catch (e) {
// ignore
}
}
};
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"dependencies": {
"argparse": "1.0.2",
"tensor-xmldom": "0.1.18",
"colors": "0.6.2"
"colors": "0.6.2",
"rfc6902-json-diff": "0.1.x"
},
"devDependencies": {
"istanbul": "0.3.8",
Expand Down