Add a newline to the diff temp files to remove the annoying message about it
[lhc/web/wiklou.git] / maintenance / parserTests.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
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.
9 #
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.
14 #
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
19
20 /**
21 * @todo Make this more independent of the configuration (and if possible the database)
22 * @todo document
23 * @package MediaWiki
24 * @subpackage Maintenance
25 */
26
27 /** */
28 require_once( 'commandLine.inc' );
29 include_once( 'InitialiseMessages.inc' );
30
31 $wgTitle = Title::newFromText( 'Parser test script' );
32
33 class ParserTest {
34 function runTestsFromFile( $filename ) {
35 $infile = fopen( $filename, 'rt' );
36 if( !$infile ) {
37 die( "Couldn't open parserTests.txt\n" );
38 }
39
40 $data = array();
41 $section = null;
42 $success = 0;
43 $total = 0;
44 $n = 0;
45 while( false !== ($line = fgets( $infile ) ) ) {
46 $n++;
47 if( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
48 $section = strtolower( $matches[1] );
49 if( $section == 'end' ) {
50 if (isset ($data['disabled'])) {
51 # disabled test
52 $data = array();
53 $section = null;
54 continue;
55 }
56 if( !isset( $data['test'] ) ) {
57 die( "'end' without 'test' at line $n\n" );
58 }
59 if( !isset( $data['input'] ) ) {
60 die( "'end' without 'input' at line $n\n" );
61 }
62 if( !isset( $data['result'] ) ) {
63 die( "'end' without 'result' at line $n\n" );
64 }
65 if( $this->runTest(
66 rtrim( $data['test'] ),
67 rtrim( $data['input'] ),
68 rtrim( $data['result'] ) ) ) {
69 $success++;
70 }
71 $total++;
72 $data = array();
73 $section = null;
74 continue;
75 }
76 $data[$section] = '';
77 continue;
78 }
79 if( $section ) {
80 $data[$section] .= $line;
81 }
82 }
83 if( $total > 0 ) {
84 $ratio = IntVal( 100.0 * $success / $total );
85 print "\nPassed $success of $total tests ($ratio%)\n";
86 return ($success == $total);
87 } else {
88 die( "No tests found.\n" );
89 }
90 }
91
92 /**
93 * @param string $input Wikitext to try rendering
94 * @param string $result Result to output
95 * @return bool
96 */
97 function runTest( $desc, $input, $result ) {
98 print "Running test $desc...";
99
100 $this->setupGlobals();
101
102 $user =& new User();
103 $options =& ParserOptions::newFromUser( $user );
104 $parser =& new Parser();
105 $title =& Title::makeTitle( NS_MAIN, 'Parser_test' );
106
107 $output =& $parser->parse( $input, $title, $options );
108
109 $html = $output->getText();
110 # $languageLinks = $output->getLanguageLinks();
111 # $categoryLinks = $output->getCategoryLinks();
112
113 $op = new OutputPage();
114 $op->replaceLinkHolders($html);
115
116 global $wgUseTidy;
117 if ($wgUseTidy) {
118 # Using Parser here is probably theoretically
119 # wrong, because we shouldn't use Parser to
120 # validate itself, but this should be safe
121 # in practice.
122 $result = Parser::tidy($result);
123 }
124
125 $this->teardownGlobals();
126
127 if( rtrim($result) === rtrim($html) ) {
128 return $this->showSuccess( $desc );
129 } else {
130 return $this->showFailure( $desc, $result, $html );
131 }
132 }
133
134 function setupGlobals() {
135 static $settings = array(
136 'wgServer' => 'http://localhost',
137 'wgScript' => '/index.php',
138 'wgScriptPath' => '/',
139 'wgArticlePath' => '/wiki/$1',
140 );
141 $this->savedGlobals = array();
142 foreach( $settings as $var => $val ) {
143 $this->savedGlobals[$var] = $GLOBALS[$var];
144 $GLOBALS[$var] = $val;
145 }
146 }
147
148 function teardownGlobals() {
149 foreach( $this->savedGlobals as $var => $val ) {
150 $GLOBALS[$var] = $val;
151 }
152 }
153
154 function showSuccess( $desc ) {
155 print "ok\n";
156 return true;
157 }
158
159 function showFailure( $desc, $result, $html ) {
160 print "FAILED\n";
161 #print "!! Expected:\n$result\n";
162 #print "!! Received:\n$html\n!!\n";
163 print $this->quickDiff( $result, $html );
164 return false;
165 }
166
167 function quickDiff( $input, $output ) {
168 $prefix = "/tmp/mwParser-" . mt_rand();
169
170 $infile = "$prefix-in";
171 $this->dumpToFile( $input, $infile );
172
173 $outfile = "$prefix-out";
174 $this->dumpToFile( $output, $outfile );
175
176 $diff = `diff -u $infile $outfile`;
177 unlink( $infile );
178 unlink( $outfile );
179
180 return $diff;
181 }
182
183 function dumpToFile( $data, $filename ) {
184 $file = fopen( $filename, "wt" );
185 fwrite( $file, rtrim( $data ) . "\n" );
186 fclose( $file );
187 }
188 }
189
190 $tester =& new ParserTest();
191
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' );
195
196 exit ($ok ? 0 : -1);
197
198 ?>