mediawiki-extensions-Visual.../tests/parser/roundtrip.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

112 lines
2.7 KiB
JavaScript

var fs = require('fs'),
jsDiff = require('diff'),
Worker = require('webworker').Worker,
DumpReader = require('./dumpReader.js').DumpReader;
function runTests() {
function compareTest(a, b, msg) {
if (a === b) {
console.log('OK: ', msg);
return true;
} else {
console.log('MISMATCH: ', msg);
var patch = jsDiff.createPatch('wikitext.txt', a, b, 'before', 'after');
console.log(patch);
return false;
}
}
var state = {
doneReading: false,
revsIn: 0,
revsOut: 0
};
function checkState() {
var remaining = state.revsIn - state.revsOut;
console.log(remaining + ' in queue... ' + (state.revsOut + '/' + state.revsIn));
if (remaining == 0) {
console.log('are we done?', state.doneReading);
}
if (state.doneReading) {
if (remaining <= 0) {
console.log('done!');
process.exit(0);
}
} else {
if (remaining < queueLength && process.stdin.readable) {
process.stdin.resume();
}
}
}
var nWorkers = 8;
var queueLength = nWorkers * 2;
var workerDir = __dirname;
var workers = [];
workerJs = require('path').join(workerDir, 'worker.js');
for (var i = 0; i < nWorkers; i++) {
var worker = new Worker(workerJs);
worker.onerror = function(err) {
console.log('worker error', err);
process.exit(1);
};
worker.onclose = function() {
console.log('worker closed');
};
worker.onmessage = function(msg) {
var data = msg.data;
compareTest(data.expected, data.received, data.msg);
state.revsOut++;
checkState();
};
workers[i] = worker;
}
//var worker = require(workerJs);
function roundTripTest(text, msg) {
var worker = workers[state.revsIn % nWorkers];
state.revsIn++;
var remaining = state.revsIn - state.revsOut;
if (remaining >= queueLength) {
// Throttle the input until we catch up!
process.stdin.pause();
}
worker.postMessage({
action: 'roundTrip',
text: text,
msg: msg
});
}
// We need to tell the child process where its working directory is.
workers.forEach(function(worker) {
worker.postMessage({
action: 'init',
dir: workerDir
});
});
roundTripTest('A plain single line paragraph.', 'single-line paragraph');
//roundTripTest('A plain single line paragraph.\n\nA second paragraph after a blank.', 'two single-line paragraphs');
var reader = new DumpReader();
reader.on('end', function() {
console.log('done reading!');
state.doneReading = true;
checkState();
});
reader.on('error', function(err) {
console.log('error!', err);
process.exit(1);
});
reader.on('revision', function(revision) {
roundTripTest(revision.text, 'revision id ' + revision.id)
});
console.log('Reading!');
process.stdin.setEncoding('utf8');
process.stdin.resume();
reader.read(process.stdin);
}
runTests();