carto/test/rendering.test.js

94 lines
3.4 KiB
JavaScript
Raw Normal View History

var path = require('path'),
sys = require('sys'),
assert = require('assert'),
2011-01-25 07:50:27 +08:00
fs = require('fs'),
xml2js = require('xml2js');
var mess = require('mess');
var tree = require('mess/tree');
var helper = require('./support/helper');
helper.files('rendering', 'mml', function(file) {
exports['test rendering ' + file] = function(beforeExit) {
2011-01-25 07:50:27 +08:00
var completed = false;
var renderResult;
helper.file(file, function(mml) {
new mess.Renderer({
paths: [ path.dirname(file) ],
data_dir: path.join(__dirname, '../data'),
local_data_dir: path.join(__dirname, 'rendering'),
filename: file
}).render(mml, function (err, output) {
if (err) {
throw err;
} else {
renderResult = output;
var result = helper.resultFile(file);
helper.file(result, function(result) {
2011-01-25 07:50:27 +08:00
// Parse the XML file.
var resultParser = new xml2js.Parser();
resultParser.addListener('end', function(resultXML) {
var messParser = new xml2js.Parser();
messParser.addListener('end', function(messXML) {
2011-01-26 04:49:22 +08:00
removeAbsoluteDatasources(messXML);
2011-01-27 01:53:57 +08:00
removeAbsoluteImages(messXML);
2011-01-26 04:49:22 +08:00
2011-01-25 07:50:27 +08:00
completed = true;
try {
assert.deepEqual(messXML, resultXML);
} catch (e) {
2011-01-27 01:53:57 +08:00
console.warn(
helper.stylize("Failure", 'red') + ': '
+ helper.stylize(file, 'underline')
+ ' differs from expected result.');
2011-01-25 07:50:27 +08:00
helper.showDifferences(e);
throw '';
}
2011-01-26 04:49:22 +08:00
2011-01-25 07:50:27 +08:00
});
messParser.parseString(output);
});
resultParser.parseString(result);
});
}
});
});
beforeExit(function() {
if (!completed && renderResult) {
console.warn(helper.stylize('renderer produced:', 'bold'));
console.warn(renderResult);
}
2011-01-26 07:14:40 +08:00
assert.ok(completed, 'Rendering finished.');
});
}
});
2011-01-26 04:49:22 +08:00
2011-01-27 01:53:57 +08:00
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]";
}
}
}
}
}
});
}
2011-01-26 04:49:22 +08:00
function removeAbsoluteDatasources(xml) {
(Array.isArray(xml.Layer) ? xml.Layer : [ xml.Layer ]).forEach(function(layer) {
2011-01-26 04:49:22 +08:00
layer.Datasource.Parameter.forEach(function(param) {
if (param.attr && param.attr.name === 'file') {
2011-01-26 04:49:22 +08:00
param.text = "[absolute path]";
}
});
});
2011-01-26 07:14:40 +08:00
}