First pass updating parserTests to verify dom->wikitext serialization.

- Just a quick first pass updating the parserTests.js script so we can
  test DOM -> wikitext serialization (but which in effect really tests
  roundtripping).
- There is no output normalization yet which is needed for now since we
  are not yet preserving white-space.

Change-Id: Ie52058e0dc3330f852c24fa05641dced19f950e0
This commit is contained in:
Subramanya Sastry 2012-05-17 18:19:55 -05:00 committed by Gabriel Wicke
parent 58da03bc85
commit 9b84e931db

View file

@ -61,6 +61,7 @@ var testWhiteList = require(__dirname + '/parserTests-whitelist.js').testWhiteLi
_import(pj('parser', 'mediawiki.parser.environment.js'), ['MWParserEnvironment']);
_import(pj('parser', 'mediawiki.parser.js'), ['ParserPipelineFactory']);
_import(pj('parser', 'mediawiki.WikitextSerializer.js'), ['WikitextSerializer']);
// WikiDom and serializers
//_require(pj('es', 'es.js'));
@ -122,6 +123,11 @@ function ParserTests () {
'default': false,
'boolean': true
},
'roundtrip': {
description: 'Roundtrip testing: Wikitext -> DOM -> wikitext',
'default': false,
'boolean': true
},
'debug': {
description: 'Print debugging information',
'default': false,
@ -381,6 +387,10 @@ ParserTests.prototype.processResult = function ( index, item, doc ) {
this.printTitle(item);
this.failParseTests++;
console.log('PARSE FAIL', res.err);
} else {
if (this.argv.roundtrip) {
var rt_wikiText = new WikitextSerializer().serializeDOM(doc.body);
this.checkRoundTripResult(item, rt_wikiText);
} else {
// Check the result vs. the expected result.
this.checkResult( item, doc.body.innerHTML );
@ -389,6 +399,7 @@ ParserTests.prototype.processResult = function ( index, item, doc ) {
// Test HTML DOM -> WikiDOM conversion
this.printWikiDom( parserPipeline.getWikiDom() );
}
}
}
// Now call schedule the next test, if any
@ -465,6 +476,55 @@ ParserTests.prototype.checkResult = function ( item, out ) {
}
};
ParserTests.prototype.checkRoundTripResult = function ( item, out ) {
var normalizedOut = out; // FIXME: normalization not in place yet
var normalizedExpected = item.input; // FIXME: normalization not in place yet
if ( normalizedOut !== normalizedExpected ) {
this.printTitle( item, this.argv.quick );
this.failOutputTests++;
if( !this.argv.quick ) {
console.log('RAW EXPECTED'.cyan + ':');
console.log(item.input + "\n");
console.log('RAW RENDERED'.cyan + ':');
console.log(out + "\n");
console.log('NORMALIZED EXPECTED'.magenta + ':');
console.log(normalizedExpected + "\n");
console.log('NORMALIZED RENDERED'.magenta + ':');
console.log(normalizedOut + "\n");
var patch = jsDiff.createPatch('wikitext.txt', normalizedExpected, normalizedOut, 'before', 'after');
console.log('DIFF'.cyan +': ');
// Strip the header from the patch, we know how diffs work..
patch = patch.replace(/^[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n/, '');
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;
}
}).join( "\n" );
console.log( colored_diff );
}
} else {
this.passedTests++;
if( !this.argv.quiet ) {
console.log( 'PASSED'.green + ': ' + item.title.yellow );
}
}
};
/**
* Print out a WikiDom conversion of the HTML DOM
@ -582,9 +642,10 @@ ParserTests.prototype.processCase = function ( i ) {
break;
case 'hooks':
console.warn('parserTests: Unhandled hook ' + JSON.stringify( item ) );
break;
case 'functionhooks':
console.warn('parserTests: Unhandled functionhook '
+ JSON.stringify( item ) );
console.warn('parserTests: Unhandled functionhook ' + JSON.stringify( item ) );
break;
default:
this.comments = [];
process.nextTick( this.processCase.bind( this, i + 1 ) );