2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
21 * @todo Make this more independent of the configuration (and if possible the database)
24 * @subpackage Maintenance
28 require_once( 'commandLine.inc' );
29 include_once( 'InitialiseMessages.inc' );
31 $wgTitle = Title
::newFromText( 'Parser test script' );
34 function runTestsFromFile( $filename ) {
35 $infile = fopen( $filename, 'rt' );
37 die( "Couldn't open parserTests.txt\n" );
45 while( false !== ($line = fgets( $infile ) ) ) {
47 if( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
48 $section = strtolower( $matches[1] );
49 if( $section == 'end' ) {
50 if (isset ($data['disabled'])) {
56 if( !isset( $data['test'] ) ) {
57 die( "'end' without 'test' at line $n\n" );
59 if( !isset( $data['input'] ) ) {
60 die( "'end' without 'input' at line $n\n" );
62 if( !isset( $data['result'] ) ) {
63 die( "'end' without 'result' at line $n\n" );
66 rtrim( $data['test'] ),
67 rtrim( $data['input'] ),
68 rtrim( $data['result'] ) ) ) {
80 $data[$section] .= $line;
84 $ratio = IntVal( 100.0 * $success / $total );
85 print "\nPassed $success of $total tests ($ratio%)\n";
86 return ($success == $total);
88 die( "No tests found.\n" );
93 * @param string $input Wikitext to try rendering
94 * @param string $result Result to output
97 function runTest( $desc, $input, $result ) {
98 print "Running test $desc...";
100 $this->setupGlobals();
103 $options =& ParserOptions
::newFromUser( $user );
104 $parser =& new Parser();
105 $title =& Title
::makeTitle( NS_MAIN
, 'Parser_test' );
107 $output =& $parser->parse( $input, $title, $options );
109 $html = $output->getText();
110 # $languageLinks = $output->getLanguageLinks();
111 # $categoryLinks = $output->getCategoryLinks();
113 $op = new OutputPage();
114 $op->replaceLinkHolders($html);
118 # Using Parser here is probably theoretically
119 # wrong, because we shouldn't use Parser to
120 # validate itself, but this should be safe
122 $result = Parser
::tidy($result);
125 $this->teardownGlobals();
127 if( rtrim($result) === rtrim($html) ) {
128 return $this->showSuccess( $desc );
130 return $this->showFailure( $desc, $result, $html );
134 function setupGlobals() {
135 static $settings = array(
136 'wgServer' => 'http://localhost',
137 'wgScript' => '/index.php',
138 'wgScriptPath' => '/',
139 'wgArticlePath' => '/wiki/$1',
141 $this->savedGlobals
= array();
142 foreach( $settings as $var => $val ) {
143 $this->savedGlobals
[$var] = $GLOBALS[$var];
144 $GLOBALS[$var] = $val;
148 function teardownGlobals() {
149 foreach( $this->savedGlobals
as $var => $val ) {
150 $GLOBALS[$var] = $val;
154 function showSuccess( $desc ) {
159 function showFailure( $desc, $result, $html ) {
161 #print "!! Expected:\n$result\n";
162 #print "!! Received:\n$html\n!!\n";
163 print $this->quickDiff( $result, $html );
167 function quickDiff( $input, $output ) {
168 $prefix = "/tmp/mwParser-" . mt_rand();
170 $infile = "$prefix-in";
171 $this->dumpToFile( $input, $infile );
173 $outfile = "$prefix-out";
174 $this->dumpToFile( $output, $outfile );
176 $diff = `diff
-u
$infile $outfile`
;
183 function dumpToFile( $data, $filename ) {
184 $file = fopen( $filename, "wt" );
185 fwrite( $file, rtrim( $data ) . "\n" );
190 $tester =& new ParserTest();
192 # Note: the command line setup changes the current working directory
193 # to the parent, which is why we have to put the subdir here:
194 $ok = $tester->runTestsFromFile( 'maintenance/parserTests.txt' );