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'),
sys = require('sys'),
assert = require('assert'),
fs = require('fs'),
xml2js = require('xml2js');
fs = require('fs');
var mess = require('mess');
var tree = require('mess/tree');
@ -25,69 +24,30 @@ helper.files('rendering', 'mml', function(file) {
} else {
renderResult = output;
var result = helper.resultFile(file);
helper.file(result, function(result) {
// Parse the XML file.
var resultParser = new xml2js.Parser();
resultParser.addListener('end', function(resultXML) {
var messParser = new xml2js.Parser();
messParser.addListener('end', function(messXML) {
removeAbsoluteDatasources(messXML);
removeAbsoluteImages(messXML);
completed = true;
try {
assert.deepEqual(messXML, resultXML);
} catch (e) {
console.warn(
helper.stylize("Failure", 'red') + ': '
+ helper.stylize(file, 'underline')
+ ' differs from expected result.');
helper.showDifferences(e);
throw '';
}
});
messParser.parseString(output);
});
resultParser.parseString(result);
});
helper.compareToXMLFile(result, output, function(err) {
completed = true;
if (err) {
console.warn(
helper.stylize("Failure", 'red') + ': '
+ helper.stylize(file, 'underline')
+ ' differs from expected result.');
helper.showDifferences(err);
throw '';
}
}, [
helper.removeAbsoluteImages,
helper.removeAbsoluteDatasources
]);
}
});
});
beforeExit(function() {
if (!completed && renderResult) {
console.warn(helper.stylize('renderer produced:', 'bold'));
console.warn(renderResult);
// console.warn(helper.stylize('renderer produced:', 'bold'));
// console.warn(renderResult);
}
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'),
assert = require('assert'),
crypto = require('crypto'),
xml2js = require('xml2js'),
diff = require('./diff').diff;
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) {
return path.join(path.dirname(file), path.basename(file).replace(/\.\w+$/, '.result'));
};
@ -117,3 +142,30 @@ exports.md5File = function(file, md5, context) {
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]";
}
});
});
};