5 * @subpackage Maintenance
9 require_once( 'commandLine.inc' );
10 include_once( 'InitialiseMessages.inc' );
12 $wgTitle = Title
::newFromText( 'Parser test script' );
15 function runTestsFromFile( $filename ) {
16 $infile = fopen( $filename, 'rt' );
18 die( "Couldn't open parserTests.txt\n" );
26 while( false !== ($line = fgets( $infile ) ) ) {
28 if( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
29 $section = strtolower( $matches[1] );
30 if( $section == 'end' ) {
31 if (isset ($data['disabled'])) {
37 if( !isset( $data['test'] ) ) {
38 die( "'end' without 'test' at line $n\n" );
40 if( !isset( $data['input'] ) ) {
41 die( "'end' without 'input' at line $n\n" );
43 if( !isset( $data['result'] ) ) {
44 die( "'end' without 'result' at line $n\n" );
47 rtrim( $data['test'] ),
48 rtrim( $data['input'] ),
49 $this->resultTransform(rtrim( $data['result'] ) ) ) ) {
61 $data[$section] .= $line;
65 $ratio = IntVal( 100.0 * $success / $total );
66 print "\nPassed $success of $total tests ($ratio%)\n";
71 * Substitute simple variables to allow for slightly more
72 * sophisticated tests.
75 function resultTransform($text) {
77 '__SCRIPT__' => $GLOBALS['wgScript']
79 $text = str_replace(array_keys($rep), array_values($rep), $text);
84 * @param string $input Wikitext to try rendering
85 * @param string $result Result to output
88 function runTest( $desc, $input, $result ) {
89 print "Running test $desc...";
92 $options =& ParserOptions
::newFromUser( $user );
93 $parser =& new Parser();
94 $title =& Title
::makeTitle( NS_MAIN
, 'Parser_test' );
96 $output =& $parser->parse( $input, $title, $options );
98 $html = $output->getText();
99 # $languageLinks = $output->getLanguageLinks();
100 # $categoryLinks = $output->getCategoryLinks();
102 $op = new OutputPage();
103 $op->replaceLinkHolders($html);
107 # Using Parser here is probably theoretically
108 # wrong, because we shouldn't use Parser to
109 # validate itself, but this should be safe
111 $result = Parser
::tidy($result);
114 if( rtrim($result) === rtrim($html) ) {
115 return $this->showSuccess( $desc );
117 return $this->showFailure( $desc, $result, $html );
121 function showSuccess( $desc ) {
126 function showFailure( $desc, $result, $html ) {
128 print "!! Expected:\n$result\n";
129 print "!! Received:\n$html\n!!\n";
134 $tester =& new ParserTest();
135 $tester->runTestsFromFile( 'maintenance/parserTests.txt' );