2011-11-03 13:55:48 +00:00
|
|
|
/**
|
|
|
|
* Initial parser tests runner for experimental JS parser
|
|
|
|
*
|
|
|
|
* This pulls all the parserTests.txt items and runs them through the JS
|
|
|
|
* parser and JS HTML renderer. Currently no comparison is done on output,
|
|
|
|
* as a direct string comparison won't be very encouraging. :)
|
|
|
|
*
|
|
|
|
* Needs smarter compare, as well as search-y helpers.
|
|
|
|
*
|
2011-12-28 01:36:36 +00:00
|
|
|
* @author Brion Vibber <brion@pobox.com>
|
|
|
|
* @author Gabriel Wicke <gwicke@wikimedia.org>
|
|
|
|
* @author Neil Kandalgaonkar <neilk@wikimedia.org>
|
2011-11-03 13:55:48 +00:00
|
|
|
*/
|
|
|
|
|
2011-11-28 11:10:39 +00:00
|
|
|
(function() {
|
2011-12-28 01:36:36 +00:00
|
|
|
|
2011-12-28 01:36:58 +00:00
|
|
|
|
|
|
|
|
2011-11-30 15:33:56 +00:00
|
|
|
console.log( "Starting up JS parser tests" );
|
2011-11-18 13:57:07 +00:00
|
|
|
|
2011-11-03 13:55:48 +00:00
|
|
|
var fs = require('fs'),
|
2011-11-28 11:10:39 +00:00
|
|
|
path = require('path'),
|
2011-11-28 11:19:50 +00:00
|
|
|
jsDiff = require('diff'),
|
2011-11-30 15:20:46 +00:00
|
|
|
colors = require('colors'),
|
2011-12-06 10:23:58 +00:00
|
|
|
util = require( 'util' ),
|
2012-01-27 13:32:44 +00:00
|
|
|
jsdom = require( 'jsdom' ),
|
2011-12-28 01:37:06 +00:00
|
|
|
HTML5 = require('html5').HTML5, //TODO is this fixup for tests only, or part of real parsing...
|
2011-12-12 14:03:54 +00:00
|
|
|
PEG = require('pegjs'),
|
|
|
|
// Handle options/arguments with optimist module
|
|
|
|
optimist = require('optimist');
|
2011-11-03 13:55:48 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
// track files imported / required
|
|
|
|
var fileDependencies = [];
|
|
|
|
|
|
|
|
// Fetch up some of our wacky parser bits...
|
|
|
|
|
|
|
|
var basePath = path.join(path.dirname(path.dirname(process.cwd())), 'modules');
|
2011-12-28 01:36:50 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
function _require(filename) {
|
|
|
|
var fullpath = path.join( basePath, filename );
|
|
|
|
fileDependencies.push( fullpath );
|
|
|
|
return require( fullpath );
|
|
|
|
}
|
|
|
|
|
|
|
|
function _import(filename, symbols) {
|
|
|
|
var module = _require(filename);
|
|
|
|
symbols.forEach(function(symbol) {
|
|
|
|
global[symbol] = module[symbol];
|
|
|
|
});
|
|
|
|
}
|
2011-12-01 17:51:07 +00:00
|
|
|
|
2011-12-01 15:23:23 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
// For now most modules only need this for $.extend and $.each :)
|
|
|
|
global.$ = require('jquery');
|
|
|
|
|
|
|
|
var pj = path.join;
|
|
|
|
|
|
|
|
// Our code...
|
|
|
|
|
2012-03-24 23:31:48 +00:00
|
|
|
var testWhiteList = require(__dirname + '/parserTests-whitelist.js').testWhiteList;
|
2011-12-12 14:03:54 +00:00
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
_import(pj('parser', 'mediawiki.parser.environment.js'), ['MWParserEnvironment']);
|
2012-01-04 08:39:45 +00:00
|
|
|
_import(pj('parser', 'mediawiki.parser.js'), ['ParserPipeline']);
|
2011-12-13 14:48:47 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
// WikiDom and serializers
|
2011-12-13 11:17:33 +00:00
|
|
|
//_require(pj('es', 'es.js'));
|
|
|
|
//_require(pj('es', 'es.Html.js'));
|
|
|
|
//_require(pj('es', 'serializers', 'es.AnnotationSerializer.js'));
|
|
|
|
//_require(pj('es', 'serializers', 'es.HtmlSerializer.js'));
|
|
|
|
//_require(pj('es', 'serializers', 'es.WikitextSerializer.js'));
|
|
|
|
//_require(pj('es', 'serializers', 'es.JsonSerializer.js'));
|
2011-11-30 16:33:26 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
|
|
|
|
function ParserTests () {
|
|
|
|
|
|
|
|
this.argv = optimist.usage( 'Usage: $0', {
|
2011-11-30 17:18:24 +00:00
|
|
|
'quick': {
|
|
|
|
description: 'Suppress diff output of failed tests',
|
2011-12-28 01:36:58 +00:00
|
|
|
'boolean': true,
|
|
|
|
'default': false
|
2011-11-30 17:18:24 +00:00
|
|
|
},
|
2011-11-30 17:10:07 +00:00
|
|
|
'quiet': {
|
|
|
|
description: 'Suppress notification of passed tests (shows only failed tests)',
|
2011-12-28 01:36:58 +00:00
|
|
|
'boolean': true,
|
|
|
|
'default': false
|
2011-11-30 17:10:07 +00:00
|
|
|
},
|
2011-12-01 12:30:15 +00:00
|
|
|
'color': {
|
|
|
|
description: 'Enable color output Ex: --no-color',
|
2011-12-28 01:36:58 +00:00
|
|
|
'boolean': true,
|
|
|
|
'default': true
|
2011-12-01 12:30:15 +00:00
|
|
|
},
|
2011-12-01 17:51:07 +00:00
|
|
|
'cache': {
|
2011-12-12 14:03:54 +00:00
|
|
|
description: 'Get tests cases from cache file ' + this.cache_file,
|
2011-12-28 01:36:58 +00:00
|
|
|
'boolean': true,
|
|
|
|
'default': false
|
2011-12-01 17:51:07 +00:00
|
|
|
},
|
2011-11-30 16:33:26 +00:00
|
|
|
'filter': {
|
2011-11-30 17:03:29 +00:00
|
|
|
description: 'Only run tests whose descriptions which match given regex',
|
2011-12-12 14:03:54 +00:00
|
|
|
alias: 'regex'
|
2011-11-30 16:33:26 +00:00
|
|
|
},
|
2011-12-01 11:08:05 +00:00
|
|
|
'whitelist': {
|
2011-12-01 14:25:59 +00:00
|
|
|
description: 'Alternatively compare against manually verified parser output from whitelist',
|
2011-12-28 01:36:58 +00:00
|
|
|
'default': true,
|
|
|
|
'boolean': true
|
2011-12-01 11:08:05 +00:00
|
|
|
},
|
2011-11-30 16:33:26 +00:00
|
|
|
'help': {
|
|
|
|
description: 'Show this help message',
|
2011-12-12 14:03:54 +00:00
|
|
|
alias: 'h'
|
2011-11-30 16:33:26 +00:00
|
|
|
},
|
|
|
|
'disabled': {
|
2011-12-01 12:30:15 +00:00
|
|
|
description: 'Run disabled tests (option not implemented)',
|
2011-12-28 01:36:58 +00:00
|
|
|
'default': false,
|
|
|
|
'boolean': true
|
2011-11-30 16:33:26 +00:00
|
|
|
},
|
2011-12-01 14:25:59 +00:00
|
|
|
'printwhitelist': {
|
|
|
|
description: 'Print out a whitelist entry for failing tests. Default false.',
|
2011-12-28 01:36:58 +00:00
|
|
|
'default': false,
|
|
|
|
'boolean': true
|
2011-12-14 15:15:41 +00:00
|
|
|
},
|
|
|
|
'wikidom': {
|
|
|
|
description: 'Print out a WikiDom conversion of the HTML DOM',
|
2011-12-28 01:36:58 +00:00
|
|
|
'default': false,
|
|
|
|
'boolean': true
|
2012-02-01 17:02:37 +00:00
|
|
|
},
|
|
|
|
'debug': {
|
|
|
|
description: 'Print debugging information',
|
|
|
|
'default': false,
|
|
|
|
'boolean': true
|
|
|
|
},
|
|
|
|
'trace': {
|
|
|
|
description: 'Print trace information (light debugging)',
|
|
|
|
'default': false,
|
|
|
|
'boolean': true
|
2011-12-12 14:03:54 +00:00
|
|
|
}
|
2011-11-30 16:33:26 +00:00
|
|
|
}
|
2011-11-30 17:03:29 +00:00
|
|
|
).check( function(argv) {
|
|
|
|
if( argv.filter === true ) {
|
|
|
|
throw "--filter need an argument";
|
|
|
|
}
|
|
|
|
}
|
2011-12-12 14:03:54 +00:00
|
|
|
).argv; // keep that
|
2011-11-30 16:33:26 +00:00
|
|
|
|
2011-12-01 12:30:15 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
if( this.argv.help ) {
|
|
|
|
optimist.showHelp();
|
|
|
|
process.exit( 0 );
|
|
|
|
}
|
|
|
|
this.test_filter = null;
|
2011-12-28 01:36:58 +00:00
|
|
|
if( this.argv.filter ) { // null is the 'default' by definition
|
2011-12-12 14:03:54 +00:00
|
|
|
try {
|
|
|
|
this.test_filter = new RegExp( this.argv.filter );
|
|
|
|
} catch(e) {
|
|
|
|
console.error( "\nERROR> --filter was given an invalid regular expression.");
|
|
|
|
console.error( "ERROR> See below for JS engine error:\n" + e + "\n" );
|
|
|
|
process.exit( 1 );
|
|
|
|
}
|
|
|
|
console.log( "Filtering title test using Regexp " + this.test_filter );
|
|
|
|
}
|
|
|
|
if( !this.argv.color ) {
|
|
|
|
colors.mode = 'none';
|
2011-12-01 15:45:40 +00:00
|
|
|
}
|
2011-12-01 17:51:07 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
// Name of file used to cache the parser tests cases
|
|
|
|
this.cache_file = "parserTests.cache";
|
2011-11-03 13:55:48 +00:00
|
|
|
|
|
|
|
|
2012-03-24 23:31:48 +00:00
|
|
|
this.testFileName = __dirname+'/parserTests.txt';
|
2011-12-01 17:51:07 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
if (this.argv._[0]) {
|
|
|
|
// hack :D
|
|
|
|
this.testFileName = this.argv._[0] ;
|
|
|
|
}
|
2011-11-03 13:55:48 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
try {
|
2012-03-24 23:31:48 +00:00
|
|
|
this.testParser = PEG.buildParser(fs.readFileSync(__dirname+'/parserTests.pegjs', 'utf8'));
|
2012-01-27 13:32:44 +00:00
|
|
|
} catch (e2) {
|
|
|
|
console.log(e2);
|
2011-12-12 14:03:54 +00:00
|
|
|
}
|
2011-11-03 13:55:48 +00:00
|
|
|
|
2012-02-22 15:59:11 +00:00
|
|
|
this.cases = this.getTests() || [];
|
2011-11-03 13:55:48 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
this.articles = {};
|
2011-11-03 13:55:48 +00:00
|
|
|
|
2012-01-27 13:32:44 +00:00
|
|
|
//this.htmlwindow = jsdom.jsdom(null, null, {parser: HTML5}).createWindow();
|
|
|
|
//this.htmlparser = new HTML5.Parser({document: this.htmlwindow.document});
|
2012-02-18 17:28:35 +00:00
|
|
|
//this.htmlparser = new HTML5.Parser()
|
|
|
|
// Use a patched version until https://github.com/aredridel/html5/issues/44 is merged
|
2012-03-24 23:31:48 +00:00
|
|
|
require('coffee-script');
|
|
|
|
this.htmlparser = require(__dirname+'/__patched-html5-parser')(HTML5);
|
2011-11-18 13:57:07 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
// Test statistics
|
|
|
|
this.passedTests = 0;
|
|
|
|
this.passedTestsManual = 0;
|
|
|
|
this.failParseTests = 0;
|
|
|
|
this.failTreeTests = 0;
|
|
|
|
this.failOutputTests = 0;
|
2011-11-03 13:55:48 +00:00
|
|
|
|
2012-01-14 00:58:20 +00:00
|
|
|
// Create a new parser environment
|
2012-01-20 18:47:17 +00:00
|
|
|
this.env = new MWParserEnvironment({
|
|
|
|
fetchTemplates: false,
|
2012-02-01 17:02:37 +00:00
|
|
|
debug: this.argv.debug,
|
2012-02-07 10:28:23 +00:00
|
|
|
trace: this.argv.trace,
|
2012-03-06 18:02:35 +00:00
|
|
|
wgUploadPath: 'http://example.com/images'
|
2012-01-20 18:47:17 +00:00
|
|
|
});
|
2011-11-03 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
|
2011-12-01 17:51:07 +00:00
|
|
|
/**
|
|
|
|
* Get an object holding our tests cases. Eventually from a cache file
|
|
|
|
*/
|
2011-12-12 14:03:54 +00:00
|
|
|
ParserTests.prototype.getTests = function () {
|
2011-12-01 17:51:07 +00:00
|
|
|
|
|
|
|
// Startup by loading .txt test file
|
|
|
|
var testFile;
|
|
|
|
try {
|
2011-12-12 14:03:54 +00:00
|
|
|
testFile = fs.readFileSync(this.testFileName, 'utf8');
|
|
|
|
fileDependencies.push( this.testFileName );
|
2011-12-01 17:51:07 +00:00
|
|
|
} catch (e) {
|
2012-03-24 23:31:48 +00:00
|
|
|
console.log( e );
|
2011-12-01 17:51:07 +00:00
|
|
|
}
|
2011-12-12 14:03:54 +00:00
|
|
|
if( !this.argv.cache ) {
|
2011-12-01 17:51:07 +00:00
|
|
|
// Cache not wanted, parse file and return object
|
2011-12-12 14:03:54 +00:00
|
|
|
return this.parseTestCase( testFile );
|
2011-12-01 17:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find out modification time of all files depencies and then hashes those
|
|
|
|
// as a unique value using sha1.
|
|
|
|
var mtimes = '';
|
|
|
|
fileDependencies.sort().forEach( function (file) {
|
2011-12-12 14:03:54 +00:00
|
|
|
mtimes += fs.statSync( file ).mtime;
|
2011-12-01 17:51:07 +00:00
|
|
|
});
|
|
|
|
var sha1 = require('crypto').createHash('sha1')
|
|
|
|
.update( mtimes ).digest( 'hex' );
|
|
|
|
|
|
|
|
// Look for a cache_file
|
|
|
|
var cache_content;
|
|
|
|
var cache_file_digest;
|
|
|
|
try {
|
2011-12-12 14:03:54 +00:00
|
|
|
console.log( "Looking for cache file " + this.cache_file );
|
|
|
|
cache_content = fs.readFileSync( this.cache_file, 'utf8' );
|
2011-12-01 17:51:07 +00:00
|
|
|
// Fetch previous digest
|
|
|
|
cache_file_digest = cache_content.match( /^CACHE: (\w+)\n/ )[1];
|
2012-01-27 13:32:44 +00:00
|
|
|
} catch( e4 ) {
|
2011-12-01 17:51:07 +00:00
|
|
|
// cache file does not exist
|
|
|
|
}
|
|
|
|
|
|
|
|
if( cache_file_digest === sha1 ) {
|
|
|
|
// cache file match our digest.
|
|
|
|
console.log( "Loaded tests cases from cache file" );
|
|
|
|
// Return contained object after removing first line (CACHE: <sha1>)
|
|
|
|
return JSON.parse( cache_content.replace( /.*\n/, '' ) );
|
|
|
|
} else {
|
|
|
|
// Write new file cache, content preprended with current digest
|
|
|
|
console.log( "Cache file either inexistant or outdated" );
|
2011-12-12 14:03:54 +00:00
|
|
|
var parse = this.parseTestCase( testFile );
|
2011-12-28 01:36:58 +00:00
|
|
|
if ( parse !== undefined ) {
|
|
|
|
console.log( "Writing parse result to " + this.cache_file );
|
|
|
|
fs.writeFileSync( this.cache_file,
|
|
|
|
"CACHE: " + sha1 + "\n" + JSON.stringify( parse ),
|
|
|
|
'utf8'
|
|
|
|
);
|
|
|
|
}
|
2011-12-01 17:51:07 +00:00
|
|
|
// We can now return the parsed object
|
|
|
|
return parse;
|
2011-11-28 11:41:47 +00:00
|
|
|
}
|
2011-12-12 14:03:54 +00:00
|
|
|
};
|
2011-11-03 13:55:48 +00:00
|
|
|
|
2011-12-01 17:51:07 +00:00
|
|
|
/**
|
|
|
|
* Parse given tests cases given as plaintext
|
|
|
|
*/
|
2011-12-12 14:03:54 +00:00
|
|
|
ParserTests.prototype.parseTestCase = function ( content ) {
|
2011-12-01 17:51:07 +00:00
|
|
|
console.log( "Parsing tests case from file, this takes a few seconds ..." );
|
|
|
|
try {
|
|
|
|
console.log( "Done parsing." );
|
2011-12-12 14:03:54 +00:00
|
|
|
return this.testParser.parse(content);
|
2011-12-01 17:51:07 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2011-12-28 01:36:58 +00:00
|
|
|
return undefined;
|
2011-12-12 14:03:54 +00:00
|
|
|
};
|
2011-12-01 17:51:07 +00:00
|
|
|
|
2012-01-18 23:46:01 +00:00
|
|
|
ParserTests.prototype.processArticle = function( index, item ) {
|
2012-01-14 00:58:20 +00:00
|
|
|
var norm = this.env.normalizeTitle(item.title);
|
|
|
|
//console.log( 'processArticle ' + norm );
|
2011-12-12 14:03:54 +00:00
|
|
|
this.articles[norm] = item.text;
|
2012-01-18 23:46:01 +00:00
|
|
|
process.nextTick( this.processCase.bind( this, index + 1 ) );
|
2011-12-12 14:03:54 +00:00
|
|
|
};
|
2011-11-03 13:55:48 +00:00
|
|
|
|
2011-11-29 15:11:51 +00:00
|
|
|
|
2011-11-28 14:00:14 +00:00
|
|
|
/* Normalize the expected parser output by parsing it using a HTML5 parser and
|
|
|
|
* re-serializing it to HTML. Ideally, the parser would normalize inter-tag
|
|
|
|
* whitespace for us. For now, we fake that by simply stripping all newlines.
|
|
|
|
*/
|
2011-12-12 14:03:54 +00:00
|
|
|
ParserTests.prototype.normalizeHTML = function (source) {
|
2011-11-28 11:10:39 +00:00
|
|
|
// TODO: Do not strip newlines in pre and nowiki blocks!
|
2011-12-22 11:43:55 +00:00
|
|
|
source = source.replace(/[\r\n]/g, '');
|
2011-11-28 14:00:14 +00:00
|
|
|
try {
|
2011-12-12 14:03:54 +00:00
|
|
|
this.htmlparser.parse('<body>' + source + '</body>');
|
2012-01-27 13:32:44 +00:00
|
|
|
return this.htmlparser.document.getElementsByTagName('body')[0]
|
2011-11-29 15:11:51 +00:00
|
|
|
.innerHTML
|
|
|
|
// a few things we ignore for now..
|
2012-03-05 12:00:38 +00:00
|
|
|
//.replace(/\/wiki\/Main_Page/g, 'Main Page')
|
2011-12-06 11:39:53 +00:00
|
|
|
// do not expect a toc for now
|
|
|
|
.replace(/<table[^>]+?id="toc"[^>]*>.+?<\/table>/mg, '')
|
|
|
|
// do not expect section editing for now
|
|
|
|
.replace(/(<span class="editsection">\[.*?<\/span> *)?<span[^>]+class="mw-headline"[^>]*>(.*?)<\/span>/g, '$2')
|
|
|
|
// general class and titles, typically on links
|
2011-11-30 13:40:17 +00:00
|
|
|
.replace(/(title|class|rel)="[^"]+"/g, '')
|
2011-12-05 17:07:06 +00:00
|
|
|
// strip red link markup, we do not check if a page exists yet
|
2012-03-05 12:00:38 +00:00
|
|
|
.replace(/\/index.php\?title=([^']+?)&action=edit&redlink=1/g, '/wiki/$1')
|
2011-11-30 18:21:53 +00:00
|
|
|
// the expected html has some extra space in tags, strip it
|
2011-11-30 13:40:17 +00:00
|
|
|
.replace(/<a +href/g, '<a href')
|
|
|
|
.replace(/" +>/g, '">');
|
2011-11-28 14:00:14 +00:00
|
|
|
} catch(e) {
|
2011-11-29 15:11:51 +00:00
|
|
|
console.log("normalizeHTML failed on" +
|
|
|
|
source + " with the following error: " + e);
|
2011-11-28 14:53:07 +00:00
|
|
|
console.trace();
|
2011-11-28 14:00:14 +00:00
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
};
|
2011-11-28 11:10:39 +00:00
|
|
|
|
2011-11-28 14:53:07 +00:00
|
|
|
// Specialized normalization of the wiki parser output, mostly to ignore a few
|
2011-11-28 11:10:39 +00:00
|
|
|
// known-ok differences.
|
2011-12-12 14:03:54 +00:00
|
|
|
ParserTests.prototype.normalizeOut = function ( out ) {
|
2011-11-28 11:10:39 +00:00
|
|
|
// TODO: Do not strip newlines in pre and nowiki blocks!
|
2012-04-02 16:12:49 +00:00
|
|
|
return out.replace(/[\r\n]| data-mw="[^">]*"/g, '')
|
2012-02-28 13:21:01 +00:00
|
|
|
.replace(/<!--.*?-->\n?/gm, '')
|
|
|
|
.replace(/<\/?meta[^>]*>/g, '');
|
2011-12-12 14:03:54 +00:00
|
|
|
};
|
2011-11-28 11:10:39 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
ParserTests.prototype.formatHTML = function ( source ) {
|
2011-11-28 14:53:07 +00:00
|
|
|
// Quick hack to insert newlines before some block level start tags
|
2011-12-04 19:23:24 +00:00
|
|
|
return source.replace(
|
|
|
|
/(?!^)<((div|dd|dt|li|p|table|tr|td|tbody|dl|ol|ul|h1|h2|h3|h4|h5|h6)[^>]*)>/g,
|
2011-11-28 14:00:14 +00:00
|
|
|
'\n<$1>');
|
2011-12-12 14:03:54 +00:00
|
|
|
};
|
2011-11-28 11:10:39 +00:00
|
|
|
|
2011-11-03 13:55:48 +00:00
|
|
|
|
2011-12-12 10:47:42 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
ParserTests.prototype.printTitle = function( item, failure_only ) {
|
|
|
|
if( failure_only ) {
|
|
|
|
console.log('FAILED'.red + ': ' + item.title.yellow);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log('=====================================================');
|
|
|
|
console.log('FAILED'.red + ': ' + item.title.yellow);
|
|
|
|
console.log(item.comments.join('\n'));
|
|
|
|
if (item.options) {
|
|
|
|
console.log("OPTIONS".cyan + ":");
|
|
|
|
console.log(item.options + '\n');
|
|
|
|
}
|
|
|
|
console.log("INPUT".cyan + ":");
|
|
|
|
console.log(item.input + "\n");
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-18 23:46:01 +00:00
|
|
|
ParserTests.prototype.processTest = function ( index, item ) {
|
|
|
|
|
2011-11-03 13:55:48 +00:00
|
|
|
if (!('title' in item)) {
|
|
|
|
console.log(item);
|
|
|
|
throw new Error('Missing title from test case.');
|
|
|
|
}
|
|
|
|
if (!('input' in item)) {
|
|
|
|
console.log(item);
|
|
|
|
throw new Error('Missing input from test case ' + item.title);
|
|
|
|
}
|
|
|
|
if (!('result' in item)) {
|
|
|
|
console.log(item);
|
|
|
|
throw new Error('Missing input from test case ' + item.title);
|
|
|
|
}
|
|
|
|
|
2012-01-18 23:46:01 +00:00
|
|
|
this.parserPipeline.once( 'document',
|
|
|
|
this.processResult.bind( this, index, item )
|
|
|
|
);
|
|
|
|
|
|
|
|
// Start the pipeline by feeding it the input
|
|
|
|
this.parserPipeline.parse( item.input );
|
2011-11-18 13:57:07 +00:00
|
|
|
|
2012-01-18 23:46:01 +00:00
|
|
|
};
|
2011-12-06 10:23:58 +00:00
|
|
|
|
2012-01-18 23:46:01 +00:00
|
|
|
ParserTests.prototype.processResult = function ( index, item, doc ) {
|
2011-12-12 14:03:54 +00:00
|
|
|
// Check for errors
|
2012-01-03 18:44:31 +00:00
|
|
|
if (doc.err) {
|
2011-12-12 14:03:54 +00:00
|
|
|
this.printTitle(item);
|
|
|
|
this.failParseTests++;
|
|
|
|
console.log('PARSE FAIL', res.err);
|
|
|
|
} else {
|
2012-01-03 18:44:31 +00:00
|
|
|
// Check the result vs. the expected result.
|
2012-01-18 23:46:01 +00:00
|
|
|
this.checkResult( item, doc.body.innerHTML );
|
2011-12-28 01:36:58 +00:00
|
|
|
|
|
|
|
if ( this.argv.wikidom ) {
|
|
|
|
// Test HTML DOM -> WikiDOM conversion
|
2012-01-04 08:39:45 +00:00
|
|
|
this.printWikiDom( parserPipeline.getWikiDom() );
|
2011-12-28 01:36:58 +00:00
|
|
|
}
|
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
}
|
2012-01-18 23:46:01 +00:00
|
|
|
// Now call schedule the next test, if any
|
|
|
|
process.nextTick( this.processCase.bind( this, index + 1 ) );
|
2011-12-12 14:03:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ParserTests.prototype.checkResult = function ( item, out ) {
|
|
|
|
var normalizedOut = this.normalizeOut(out);
|
|
|
|
var normalizedExpected = this.normalizeHTML(item.result);
|
|
|
|
if ( normalizedOut !== normalizedExpected ) {
|
|
|
|
if (this.argv.whitelist &&
|
|
|
|
item.title in testWhiteList &&
|
|
|
|
this.normalizeOut(testWhiteList[item.title]) === normalizedOut) {
|
|
|
|
if( !this.argv.quiet ) {
|
|
|
|
console.log( 'PASSED (whiteList)'.green + ': ' + item.title.yellow );
|
|
|
|
}
|
|
|
|
this.passedTestsManual++;
|
|
|
|
return;
|
2011-12-01 10:58:12 +00:00
|
|
|
}
|
2011-12-12 14:03:54 +00:00
|
|
|
this.printTitle( item, this.argv.quick );
|
|
|
|
this.failOutputTests++;
|
2011-11-30 17:18:24 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
if( !this.argv.quick ) {
|
|
|
|
console.log('RAW EXPECTED'.cyan + ':');
|
|
|
|
console.log(item.result + "\n");
|
2011-11-28 11:10:39 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
console.log('RAW RENDERED'.cyan + ':');
|
|
|
|
console.log(this.formatHTML(out) + "\n");
|
2011-11-28 11:10:39 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
var a = this.formatHTML(normalizedExpected);
|
2011-11-28 11:19:50 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
console.log('NORMALIZED EXPECTED'.magenta + ':');
|
|
|
|
console.log(a + "\n");
|
2011-11-28 11:19:50 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
var b = this.formatHTML(normalizedOut);
|
2011-11-28 11:10:39 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
console.log('NORMALIZED RENDERED'.magenta + ':');
|
|
|
|
console.log(this.formatHTML(this.normalizeOut(out)) + "\n");
|
|
|
|
var patch = jsDiff.createPatch('wikitext.txt', a, b, 'before', 'after');
|
2011-11-28 11:19:50 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
console.log('DIFF'.cyan +': ');
|
2011-11-30 15:20:46 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
// Strip the header from the patch, we know how diffs work..
|
|
|
|
patch = patch.replace(/^[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n/, '');
|
2011-11-30 18:21:53 +00:00
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
var colored_diff = patch.split( '\n' ).map( function(line) {
|
|
|
|
// Add some colors to diff output
|
|
|
|
switch( line.charAt(0) ) {
|
|
|
|
case '-':
|
|
|
|
return line.red;
|
|
|
|
case '+':
|
|
|
|
return line.blue;
|
|
|
|
default:
|
|
|
|
return line;
|
2011-11-30 17:10:07 +00:00
|
|
|
}
|
2011-12-12 14:03:54 +00:00
|
|
|
}).join( "\n" );
|
|
|
|
|
|
|
|
|
|
|
|
console.log( colored_diff );
|
|
|
|
|
|
|
|
if(this.argv.printwhitelist) {
|
|
|
|
console.log("WHITELIST ENTRY:".cyan);
|
|
|
|
console.log("testWhiteList[" +
|
|
|
|
JSON.stringify(item.title) + "] = " +
|
|
|
|
JSON.stringify(out) +
|
|
|
|
";\n");
|
2011-11-28 14:00:14 +00:00
|
|
|
}
|
2011-11-03 13:55:48 +00:00
|
|
|
}
|
2011-12-12 14:03:54 +00:00
|
|
|
} else {
|
|
|
|
this.passedTests++;
|
|
|
|
if( !this.argv.quiet ) {
|
|
|
|
console.log( 'PASSED'.green + ': ' + item.title.yellow );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-03 13:55:48 +00:00
|
|
|
|
2011-12-14 15:15:41 +00:00
|
|
|
/**
|
|
|
|
* Print out a WikiDom conversion of the HTML DOM
|
|
|
|
*/
|
|
|
|
ParserTests.prototype.printWikiDom = function ( body ) {
|
|
|
|
console.log('WikiDom'.cyan + ':');
|
2011-12-28 01:36:58 +00:00
|
|
|
console.log( body );
|
2011-12-14 15:15:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-11-30 17:43:54 +00:00
|
|
|
/**
|
|
|
|
* Colorize given number if <> 0
|
|
|
|
*
|
|
|
|
* @param count Integer: a number to colorize
|
|
|
|
* @param color String: 'green' or 'red'
|
|
|
|
*/
|
2011-12-12 14:03:54 +00:00
|
|
|
ParserTests.prototype.ColorizeCount = function ( count, color ) {
|
2011-11-30 17:43:54 +00:00
|
|
|
if( count === 0 ) {
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need a string to use colors methods
|
|
|
|
count = count.toString();
|
|
|
|
// FIXME there must be a wait to call a method by its name
|
|
|
|
switch( color ) {
|
|
|
|
case 'green': return count.green;
|
|
|
|
case 'red': return count.red;
|
|
|
|
|
|
|
|
default: return count;
|
|
|
|
}
|
2011-12-12 14:03:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ParserTests.prototype.reportSummary = function () {
|
|
|
|
|
|
|
|
var failTotalTests = (this.failParseTests + this.failTreeTests +
|
|
|
|
this.failOutputTests);
|
|
|
|
|
|
|
|
console.log( "==========================================================");
|
|
|
|
console.log( "SUMMARY: ");
|
|
|
|
|
|
|
|
if( failTotalTests !== 0 ) {
|
|
|
|
console.log( this.ColorizeCount( this.passedTests , 'green' ) +
|
|
|
|
" passed");
|
|
|
|
console.log( this.ColorizeCount( this.passedTestsManual , 'green' ) +
|
|
|
|
" passed from whitelist");
|
|
|
|
console.log( this.ColorizeCount( this.failParseTests , 'red' ) +
|
|
|
|
" parse failures");
|
|
|
|
console.log( this.ColorizeCount( this.failTreeTests , 'red' ) +
|
|
|
|
" tree build failures");
|
|
|
|
console.log( this.ColorizeCount( this.failOutputTests, 'red' ) +
|
|
|
|
" output differences");
|
|
|
|
console.log( "\n" );
|
|
|
|
console.log( this.ColorizeCount( this.passedTests + this.passedTestsManual , 'green' ) +
|
|
|
|
' total passed tests, ' +
|
|
|
|
this.ColorizeCount( failTotalTests , 'red' ) + " total failures");
|
2011-11-30 17:43:54 +00:00
|
|
|
|
|
|
|
} else {
|
2011-12-12 14:03:54 +00:00
|
|
|
if( this.test_filter !== null ) {
|
2012-02-07 09:27:47 +00:00
|
|
|
console.log( "Passed " + ( this.passedTests + this.passedTestsManual ) +
|
|
|
|
" of " + this.passedTests + " tests matching " + this.test_filter +
|
2011-12-12 14:03:54 +00:00
|
|
|
"... " + "ALL TESTS PASSED!".green );
|
|
|
|
} else {
|
|
|
|
// Should not happen if it does: Champagne!
|
|
|
|
console.log( "Passed " + this.passedTests + " of " + this.passedTests +
|
|
|
|
" tests... " + "ALL TESTS PASSED!".green );
|
|
|
|
}
|
2011-11-30 17:43:54 +00:00
|
|
|
}
|
2011-12-12 14:03:54 +00:00
|
|
|
console.log( "==========================================================");
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
ParserTests.prototype.main = function () {
|
|
|
|
console.log( "Initialisation complete. Now launching tests." );
|
2011-12-28 01:37:06 +00:00
|
|
|
//var parserEnv = new MWParserEnvironment({
|
|
|
|
// tagHooks: {
|
|
|
|
// 'ref': MWRefTagHook,
|
|
|
|
// 'references': MWReferencesTagHook
|
|
|
|
// }
|
|
|
|
//});
|
2011-12-12 14:03:54 +00:00
|
|
|
|
2012-01-14 00:58:20 +00:00
|
|
|
this.env.pageCache = this.articles;
|
2012-01-18 23:46:01 +00:00
|
|
|
this.parserPipeline = new ParserPipeline( this.env );
|
2011-12-28 01:36:58 +00:00
|
|
|
|
2012-01-18 23:46:01 +00:00
|
|
|
this.comments = [];
|
|
|
|
|
|
|
|
this.processCase( 0 );
|
|
|
|
};
|
|
|
|
|
|
|
|
ParserTests.prototype.processCase = function ( i ) {
|
|
|
|
if ( i < this.cases.length ) {
|
|
|
|
var item = this.cases[i];
|
|
|
|
//console.log( 'processCase ' + i + JSON.stringify( item ) );
|
|
|
|
if ( typeof item == 'object' ) {
|
2011-12-12 14:03:54 +00:00
|
|
|
switch(item.type) {
|
|
|
|
case 'article':
|
2012-01-18 23:46:01 +00:00
|
|
|
this.comments = [];
|
|
|
|
this.processArticle( i, item );
|
2011-12-12 14:03:54 +00:00
|
|
|
break;
|
|
|
|
case 'test':
|
2012-01-18 23:46:01 +00:00
|
|
|
if( this.test_filter &&
|
|
|
|
-1 === item.title.search( this.test_filter ) ) {
|
2011-12-12 14:03:54 +00:00
|
|
|
// Skip test whose title does not match --filter
|
2012-01-18 23:46:01 +00:00
|
|
|
process.nextTick( this.processCase.bind( this, i + 1 ) );
|
2011-12-12 14:03:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Add comments to following test.
|
2012-01-18 23:46:01 +00:00
|
|
|
item.comments = this.comments;
|
|
|
|
this.comments = [];
|
|
|
|
this.processTest( i, item );
|
2011-12-12 14:03:54 +00:00
|
|
|
break;
|
|
|
|
case 'comment':
|
2012-01-18 23:46:01 +00:00
|
|
|
this.comments.push( item.comment );
|
|
|
|
process.nextTick( this.processCase.bind( this, i + 1 ) );
|
2011-12-12 14:03:54 +00:00
|
|
|
break;
|
2012-02-22 15:59:11 +00:00
|
|
|
case 'hooks':
|
|
|
|
console.warn('parserTests: Unhandled hook ' + JSON.stringify( item ) );
|
|
|
|
case 'functionhooks':
|
|
|
|
console.warn('parserTests: Unhandled functionhook '
|
|
|
|
+ JSON.stringify( item ) );
|
2011-12-12 14:03:54 +00:00
|
|
|
default:
|
2012-01-18 23:46:01 +00:00
|
|
|
this.comments = [];
|
|
|
|
process.nextTick( this.processCase.bind( this, i + 1 ) );
|
2011-12-12 14:03:54 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-01-18 23:46:01 +00:00
|
|
|
} else {
|
|
|
|
process.nextTick( this.processCase.bind( this, i + 1 ) );
|
2011-12-12 14:03:54 +00:00
|
|
|
}
|
2012-01-18 23:46:01 +00:00
|
|
|
} else {
|
|
|
|
// print out the summary
|
|
|
|
this.reportSummary();
|
|
|
|
}
|
2011-12-12 14:03:54 +00:00
|
|
|
};
|
|
|
|
|
2011-12-12 20:53:14 +00:00
|
|
|
// Construct the ParserTests object and run the parser tests
|
|
|
|
new ParserTests().main();
|
2011-12-12 14:03:54 +00:00
|
|
|
|
2011-11-28 14:53:07 +00:00
|
|
|
|
2011-11-28 11:10:39 +00:00
|
|
|
})();
|