From bab332e1bee56686fc93af4b7e94d3b0b744de83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Wed, 26 Jan 2011 17:56:16 -0500 Subject: [PATCH] move xml comparison to helper --- test/rendering.test.js | 74 ++++++++++-------------------------------- test/support/helper.js | 54 +++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 58 deletions(-) diff --git a/test/rendering.test.js b/test/rendering.test.js index 094e2d6..d373b70 100644 --- a/test/rendering.test.js +++ b/test/rendering.test.js @@ -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]"; - } - }); - }); -} diff --git a/test/support/helper.js b/test/support/helper.js index 801f707..d2313b7 100644 --- a/test/support/helper.js +++ b/test/support/helper.js @@ -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')); }; @@ -116,4 +141,31 @@ exports.md5File = function(file, md5, context) { assert.equal(hash, md5); context.tests++; }); -} \ No newline at end of file +} + + +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]"; + } + }); + }); +};