7b718e5f6b7c2bc0df933ff30f55bc6d8cd3180e
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 require_once( 'languages/LanguageUtf8.php' );
34 * Sets terminal colorization and diff/quick modes depending on OS and
35 * command-line options (--color and --quick).
39 function ParserTest() {
40 if( isset( $_SERVER['argv'] ) && in_array( '--color', $_SERVER['argv'] ) ) {
42 } elseif( isset( $_SERVER['argv'] ) && in_array( '--color=yes', $_SERVER['argv'] ) ) {
44 } elseif( isset( $_SERVER['argv'] ) && in_array( '--color=no', $_SERVER['argv'] ) ) {
46 } elseif( wfIsWindows() ) {
52 if( isset( $_SERVER['argv'] ) && in_array( '--quick', $_SERVER['argv'] ) ) {
53 $this->showDiffs
= false;
55 $this->showDiffs
= true;
61 * Run a series of tests listed in the given text file.
62 * Each test consists of a brief description, wikitext input,
63 * and the expected HTML output.
65 * Prints status updates on stdout and counts up the total
66 * number and percentage of passed tests.
68 * @param string $filename
69 * @return bool True if passed all tests, false if any tests failed.
72 function runTestsFromFile( $filename ) {
73 $infile = fopen( $filename, 'rt' );
75 die( "Couldn't open parserTests.txt\n" );
83 while( false !== ($line = fgets( $infile ) ) ) {
85 if( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
86 $section = strtolower( $matches[1] );
87 if( $section == 'end' ) {
88 if (isset ($data['disabled'])) {
94 if( !isset( $data['test'] ) ) {
95 die( "'end' without 'test' at line $n\n" );
97 if( !isset( $data['input'] ) ) {
98 die( "'end' without 'input' at line $n\n" );
100 if( !isset( $data['result'] ) ) {
101 die( "'end' without 'result' at line $n\n" );
104 rtrim( $data['test'] ),
105 rtrim( $data['input'] ),
106 rtrim( $data['result'] ) ) ) {
114 $data[$section] = '';
118 $data[$section] .= $line;
122 $ratio = IntVal( 100.0 * $success / $total );
123 print "\nPassed $success of $total tests ($ratio%)\n";
124 return ($success == $total);
126 die( "No tests found.\n" );
131 * Run a given wikitext input through a freshly-constructed wiki parser,
132 * and compare the output against the expected results.
133 * Prints status and explanatory messages to stdout.
135 * @param string $input Wikitext to try rendering
136 * @param string $result Result to output
139 function runTest( $desc, $input, $result ) {
140 print "Running test $desc... ";
142 $this->setupGlobals();
145 $options =& ParserOptions
::newFromUser( $user );
146 $parser =& new Parser();
147 $title =& Title
::makeTitle( NS_MAIN
, 'Parser_test' );
149 $output =& $parser->parse( $input, $title, $options );
151 $html = $output->getText();
152 # $languageLinks = $output->getLanguageLinks();
153 # $categoryLinks = $output->getCategoryLinks();
155 $op = new OutputPage();
156 $op->replaceLinkHolders($html);
160 # Using Parser here is probably theoretically
161 # wrong, because we shouldn't use Parser to
162 # validate itself, but this should be safe
164 $result = Parser
::tidy($result);
167 $this->teardownGlobals();
169 if( rtrim($result) === rtrim($html) ) {
170 return $this->showSuccess( $desc );
172 return $this->showFailure( $desc, $result, $html );
177 * Set up the global variables for a consistent environment for each test.
178 * Ideally this should replace the global configuration entirely.
182 function setupGlobals() {
184 'wgServer' => 'http://localhost',
185 'wgScript' => '/index.php',
186 'wgScriptPath' => '/',
187 'wgArticlePath' => '/wiki/$1',
188 'wgSitename' => 'MediaWiki',
189 'wgLanguageCode' => 'en',
190 'wgUseLatin1' => false,
191 'wgLang' => new LanguageUtf8(),
193 $this->savedGlobals
= array();
194 foreach( $settings as $var => $val ) {
195 $this->savedGlobals
[$var] = $GLOBALS[$var];
196 $GLOBALS[$var] = $val;
201 * Restore default values and perform any necessary clean-up
202 * after each test runs.
206 function teardownGlobals() {
207 foreach( $this->savedGlobals
as $var => $val ) {
208 $GLOBALS[$var] = $val;
213 * Print a happy success message.
215 * @param string $desc The test name
219 function showSuccess( $desc ) {
220 print $this->termColor( '1;32' ) . 'PASSED' . $this->termReset() . "\n";
225 * Print a failure message and provide some explanatory output
226 * about what went wrong if so configured.
228 * @param string $desc The test name
229 * @param string $result Expected HTML output
230 * @param string $html Actual HTML output
234 function showFailure( $desc, $result, $html ) {
235 print $this->termColor( '1;31' ) . 'FAILED!' . $this->termReset() . "\n";
236 if( $this->showDiffs
) {
237 print $this->quickDiff( $result, $html );
243 * Run given strings through a diff and return the (colorized) output.
244 * Requires writable /tmp directory and a 'diff' command in the PATH.
246 * @param string $input
247 * @param string $output
251 function quickDiff( $input, $output ) {
252 $prefix = "/tmp/mwParser-" . mt_rand();
254 $infile = "$prefix-expected";
255 $this->dumpToFile( $input, $infile );
257 $outfile = "$prefix-actual";
258 $this->dumpToFile( $output, $outfile );
260 $diff = `diff
-u
$infile $outfile`
;
264 return $this->colorDiff( $diff );
268 * Write the given string to a file, adding a final newline.
270 * @param string $data
271 * @param string $filename
274 function dumpToFile( $data, $filename ) {
275 $file = fopen( $filename, "wt" );
276 fwrite( $file, rtrim( $data ) . "\n" );
281 * Return ANSI terminal escape code for changing text attribs/color,
282 * or empty string if color output is disabled.
284 * @param string $color Semicolon-separated list of attribute/color codes
288 function termColor( $color ) {
289 return $this->color ?
"\x1b[{$color}m" : '';
293 * Return ANSI terminal escape code for restoring default text attributes,
294 * or empty string if color output is disabled.
299 function termReset() {
300 return $this->color ?
"\x1b[0m" : '';
304 * Colorize unified diff output if set for ANSI color output.
305 * Subtractions are colored blue, additions red.
307 * @param string $text
311 function colorDiff( $text ) {
313 array( '/^(-.*)$/m', '/^(\+.*)$/m' ),
314 array( $this->termColor( 34 ) . '$1' . $this->termReset(),
315 $this->termColor( 31 ) . '$1' . $this->termReset() ),
320 $wgTitle = Title
::newFromText( 'Parser test script' );
321 $tester =& new ParserTest();
323 # Note: the command line setup changes the current working directory
324 # to the parent, which is why we have to put the subdir here:
325 $ok = $tester->runTestsFromFile( 'maintenance/parserTests.txt' );