move xml comparison to helper

This commit is contained in:
Konstantin Käfer 2011-01-26 17:56:16 -05:00
parent e812970561
commit bab332e1be
2 changed files with 70 additions and 58 deletions

View File

@ -1,8 +1,7 @@
var path = require('path'), var path = require('path'),
sys = require('sys'), sys = require('sys'),
assert = require('assert'), assert = require('assert'),
fs = require('fs'), fs = require('fs');
xml2js = require('xml2js');
var mess = require('mess'); var mess = require('mess');
var tree = require('mess/tree'); var tree = require('mess/tree');
@ -25,69 +24,30 @@ helper.files('rendering', 'mml', function(file) {
} else { } else {
renderResult = output; renderResult = output;
var result = helper.resultFile(file); var result = helper.resultFile(file);
helper.file(result, function(result) { helper.compareToXMLFile(result, output, function(err) {
// Parse the XML file. completed = true;
var resultParser = new xml2js.Parser(); if (err) {
resultParser.addListener('end', function(resultXML) { console.warn(
var messParser = new xml2js.Parser(); helper.stylize("Failure", 'red') + ': '
messParser.addListener('end', function(messXML) { + helper.stylize(file, 'underline')
removeAbsoluteDatasources(messXML); + ' differs from expected result.');
removeAbsoluteImages(messXML); helper.showDifferences(err);
throw '';
completed = true; }
try { }, [
assert.deepEqual(messXML, resultXML); helper.removeAbsoluteImages,
} catch (e) { helper.removeAbsoluteDatasources
console.warn( ]);
helper.stylize("Failure", 'red') + ': '
+ helper.stylize(file, 'underline')
+ ' differs from expected result.');
helper.showDifferences(e);
throw '';
}
});
messParser.parseString(output);
});
resultParser.parseString(result);
});
} }
}); });
}); });
beforeExit(function() { beforeExit(function() {
if (!completed && renderResult) { if (!completed && renderResult) {
console.warn(helper.stylize('renderer produced:', 'bold')); // console.warn(helper.stylize('renderer produced:', 'bold'));
console.warn(renderResult); // console.warn(renderResult);
} }
assert.ok(completed, 'Rendering finished.'); assert.ok(completed, 'Rendering finished.');
}); });
} }
}); });
function removeAbsoluteImages(xml) {
(Array.isArray(xml.Style) ? xml.Style : [ xml.Style ]).forEach(function(style) {
if (style && style.Rule) {
for (i in style.Rule) {
if (style.Rule[i].attr) {
for (j in style.Rule[i].attr) {
if (j == 'file' && style.Rule[i].attr[j][0] == '/') {
style.Rule[i].attr[j] = "[absolute path]";
}
}
}
}
}
});
}
function removeAbsoluteDatasources(xml) {
(Array.isArray(xml.Layer) ? xml.Layer : [ xml.Layer ]).forEach(function(layer) {
layer.Datasource.Parameter.forEach(function(param) {
if (param.attr && param.attr.name === 'file') {
param.text = "[absolute path]";
}
});
});
}

View File

@ -2,6 +2,7 @@ var path = require('path'),
fs = require('fs'), fs = require('fs'),
assert = require('assert'), assert = require('assert'),
crypto = require('crypto'), crypto = require('crypto'),
xml2js = require('xml2js'),
diff = require('./diff').diff; diff = require('./diff').diff;
var helper = exports; var helper = exports;
@ -62,6 +63,30 @@ exports.compareToFile = function(value, originalFile, resultFile) {
}); });
}; };
exports.compareToXMLFile = function(filename, second, callback, processors) {
helper.file(filename, function(first) {
// Parse the XML file.
var firstParser = new xml2js.Parser();
firstParser.addListener('end', function(firstXML) {
var secondParser = new xml2js.Parser();
secondParser.addListener('end', function(secondXML) {
processors.forEach(function(processor) {
processor(secondXML);
});
try {
assert.deepEqual(secondXML, firstXML);
callback(null);
} catch (err) {
callback(err);
}
});
secondParser.parseString(second);
});
firstParser.parseString(first);
});
}
exports.resultFile = function(file) { exports.resultFile = function(file) {
return path.join(path.dirname(file), path.basename(file).replace(/\.\w+$/, '.result')); return path.join(path.dirname(file), path.basename(file).replace(/\.\w+$/, '.result'));
}; };
@ -116,4 +141,31 @@ exports.md5File = function(file, md5, context) {
assert.equal(hash, md5); assert.equal(hash, md5);
context.tests++; context.tests++;
}); });
} }
helper.removeAbsoluteImages = function(xml) {
(Array.isArray(xml.Style) ? xml.Style : [ xml.Style ]).forEach(function(style) {
if (style && style.Rule) {
for (i in style.Rule) {
if (style.Rule[i].attr) {
for (j in style.Rule[i].attr) {
if (j == 'file' && style.Rule[i].attr[j][0] == '/') {
style.Rule[i].attr[j] = "[absolute path]";
}
}
}
}
}
});
};
helper.removeAbsoluteDatasources = function(xml) {
(Array.isArray(xml.Layer) ? xml.Layer : [ xml.Layer ]).forEach(function(layer) {
layer.Datasource.Parameter.forEach(function(param) {
if (param.attr && param.attr.name === 'file') {
param.text = "[absolute path]";
}
});
});
};