var path = require('path'),
    sys = require('sys'),
    assert = require('assert'),
    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) {
        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) {
                        // 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);

                                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);
                    });
                }
            });
        });

        beforeExit(function() {
            if (!completed && renderResult) {
                console.warn(helper.stylize('renderer produced:', 'bold'));
                console.warn(renderResult);
            }
            assert.ok(completed, 'Rendering finished.');
        });
    }
});


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]";
            }
        });
    });
}

@@ -1384,16 +1384,79 @@
 essXML);
+%0A                                removeAbsoluteImages(messXML);
 %0A%0A      
@@ -1710,83 +1710,204 @@
 arn(
-helper.stylize(%22Failure%22, 'red') + ': ' + helper.stylize(file, 'underline')
+%0A                                        helper.stylize(%22Failure%22, 'red') + ': '%0A                                        + helper.stylize(file, 'underline')%0A                                       
  + '
@@ -2626,16 +2626,553 @@
 %7D%0A%7D);%0A%0A%0A
+function removeAbsoluteImages(xml) %7B%0A    (Array.isArray(xml.Style) ? xml.Style : %5B xml.Style %5D).forEach(function(style) %7B%0A        if (style && style.Rule) %7B%0A            for (i in style.Rule) %7B%0A                if (style.Rule%5Bi%5D.attr) %7B%0A                    for (j in style.Rule%5Bi%5D.attr) %7B%0A                        if (j == 'file' && style.Rule%5Bi%5D.attr%5Bj%5D%5B0%5D == '/') %7B%0A                            style.Rule%5Bi%5D.attr%5Bj%5D = %22%5Babsolute path%5D%22;%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A    %7D);%0A%7D%0A%0A
 function

Test for remote resources. Fixes #27.