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 ParserTest() {
35 if( isset( $_SERVER['argv'] ) && in_array( '--color', $_SERVER['argv'] ) ) {
37 } elseif( isset( $_SERVER['argv'] ) && in_array( '--color=yes', $_SERVER['argv'] ) ) {
39 } elseif( isset( $_SERVER['argv'] ) && in_array( '--color=no', $_SERVER['argv'] ) ) {
41 } elseif( wfIsWindows() ) {
47 if( isset( $_SERVER['argv'] ) && in_array( '--quick', $_SERVER['argv'] ) ) {
48 $this->showDiffs
= false;
50 $this->showDiffs
= true;
54 function runTestsFromFile( $filename ) {
55 $infile = fopen( $filename, 'rt' );
57 die( "Couldn't open parserTests.txt\n" );
65 while( false !== ($line = fgets( $infile ) ) ) {
67 if( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
68 $section = strtolower( $matches[1] );
69 if( $section == 'end' ) {
70 if (isset ($data['disabled'])) {
76 if( !isset( $data['test'] ) ) {
77 die( "'end' without 'test' at line $n\n" );
79 if( !isset( $data['input'] ) ) {
80 die( "'end' without 'input' at line $n\n" );
82 if( !isset( $data['result'] ) ) {
83 die( "'end' without 'result' at line $n\n" );
86 rtrim( $data['test'] ),
87 rtrim( $data['input'] ),
88 rtrim( $data['result'] ) ) ) {
100 $data[$section] .= $line;
104 $ratio = IntVal( 100.0 * $success / $total );
105 print "\nPassed $success of $total tests ($ratio%)\n";
106 return ($success == $total);
108 die( "No tests found.\n" );
113 * @param string $input Wikitext to try rendering
114 * @param string $result Result to output
117 function runTest( $desc, $input, $result ) {
118 print "Running test $desc... ";
120 $this->setupGlobals();
123 $options =& ParserOptions
::newFromUser( $user );
124 $parser =& new Parser();
125 $title =& Title
::makeTitle( NS_MAIN
, 'Parser_test' );
127 $output =& $parser->parse( $input, $title, $options );
129 $html = $output->getText();
130 # $languageLinks = $output->getLanguageLinks();
131 # $categoryLinks = $output->getCategoryLinks();
133 $op = new OutputPage();
134 $op->replaceLinkHolders($html);
138 # Using Parser here is probably theoretically
139 # wrong, because we shouldn't use Parser to
140 # validate itself, but this should be safe
142 $result = Parser
::tidy($result);
145 $this->teardownGlobals();
147 if( rtrim($result) === rtrim($html) ) {
148 return $this->showSuccess( $desc );
150 return $this->showFailure( $desc, $result, $html );
154 function setupGlobals() {
156 'wgServer' => 'http://localhost',
157 'wgScript' => '/index.php',
158 'wgScriptPath' => '/',
159 'wgArticlePath' => '/wiki/$1',
160 'wgSitename' => 'MediaWiki',
161 'wgLanguageCode' => 'en',
162 'wgLang' => new LanguageUtf8(),
164 $this->savedGlobals
= array();
165 foreach( $settings as $var => $val ) {
166 $this->savedGlobals
[$var] = $GLOBALS[$var];
167 $GLOBALS[$var] = $val;
171 function teardownGlobals() {
172 foreach( $this->savedGlobals
as $var => $val ) {
173 $GLOBALS[$var] = $val;
177 function showSuccess( $desc ) {
178 print $this->termColor( '1;32' ) . 'PASSED' . $this->termReset() . "\n";
182 function showFailure( $desc, $result, $html ) {
183 print $this->termColor( '1;31' ) . 'FAILED!' . $this->termReset() . "\n";
184 if( $this->showDiffs
) {
185 print $this->quickDiff( $result, $html );
190 function quickDiff( $input, $output ) {
191 $prefix = "/tmp/mwParser-" . mt_rand();
193 $infile = "$prefix-expected";
194 $this->dumpToFile( $input, $infile );
196 $outfile = "$prefix-actual";
197 $this->dumpToFile( $output, $outfile );
199 $diff = `diff
-u
$infile $outfile`
;
203 return $this->colorDiff( $diff );
206 function dumpToFile( $data, $filename ) {
207 $file = fopen( $filename, "wt" );
208 fwrite( $file, rtrim( $data ) . "\n" );
212 function termColor( $color ) {
213 return $this->color ?
"\x1b[{$color}m" : '';
216 function termReset() {
217 return $this->color ?
"\x1b[0m" : '';
220 function colorDiff( $text ) {
222 array( '/^(-.*)$/m', '/^(\+.*)$/m' ),
223 array( $this->termColor( 34 ) . '$1' . $this->termReset(),
224 $this->termColor( 31 ) . '$1' . $this->termReset() ),
229 $tester =& new ParserTest();
231 # Note: the command line setup changes the current working directory
232 # to the parent, which is why we have to put the subdir here:
233 $ok = $tester->runTestsFromFile( 'maintenance/parserTests.txt' );