mediawiki-extensions-Visual.../tests/parser/roundtrip-test.js
Gabriel Wicke 0d30a5528e First combination of WikiDom serializers with existing parser in
tests/parser/parserTests.js.

* Removed var from es in es.js to allow node.js to access it as global. Only
  alternative solution appears to be a node-specific 'exports' construct:
  http://nodejs.org/docs/v0.3.1/api/modules.html
* Added es.Document.js and es.Document.Serializer.js in es/bases. Not sure if
  this is the desired location.
* Changed es.extend to es.extendClass in the serializers
* Modified the first parser test to include the WikiDom modules and call the
  new HTML serializer
2011-11-03 13:55:48 +00:00

63 lines
1.6 KiB
JavaScript

module.exports.init = function(worker) {
var fs = require('fs'),
path = require('path');
// Fetch up some of our wacky parser bits...
//var basePath = '../modules/';
var basePath = path.join(path.dirname(process.cwd()), 'modules');
function _require(filename) {
return require(path.join(basePath, filename));
}
function _import(filename, symbols) {
var module = _require(filename);
symbols.forEach(function(symbol) {
global[symbol] = module[symbol];
})
}
// For now most modules only need this for $.extend and $.each :)
global.$ = require('jquery');
// Local CommonJS-friendly libs
global.PEG = _require('lib.pegjs.js');
// Our code...
_import('ext.parserPlayground.serializer.js', ['MWTreeSerializer']);
_import('ext.parserPlayground.pegParser.js', ['PegParser']);
// Preload the grammar file...
PegParser.src = fs.readFileSync(path.join(basePath, 'pegParser.pegjs.txt'), 'utf8');
var parser = new PegParser(),
serializer = new MWTreeSerializer();
function sendResult(expected, received, msg) {
worker.postMessage({
expected: expected,
received: received,
msg: msg
});
}
roundTripTest = function(text, msg) {
parser.parseToTree(text, function(tree, err) {
if (err) throw new Error(err);
serializer.treeToSource(tree, function(newText, err) {
if (err) throw new Error(err);
sendResult(text, newText, msg);
})
})
}
worker.onmessage = function(msg) {
var data = msg.data;
if (data.action == 'roundTrip') {
roundTripTest(data.text, data.msg);
} else {
throw new Error('unknown action ' + data.action);
}
}
};