<?php
+# Copyright (C) 2004 Brion Vibber <brion@pobox.com>
+# http://www.mediawiki.org/
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+# http://www.gnu.org/copyleft/gpl.html
+
/**
- * @todo document
- * @package MediaWiki
- * @subpackage Maintenance
+ * @addtogroup Maintenance
*/
/** */
-require_once( 'commandLine.inc' );
-include_once( 'InitialiseMessages.inc' );
-
-$wgTitle = Title::newFromText( 'Parser test script' );
-
-class ParserTest {
- function runTestsFromFile( $filename ) {
- $infile = fopen( $filename, 'rt' );
- if( !$infile ) {
- die( "Couldn't open parserTests.txt\n" );
- }
-
- $data = array();
- $section = null;
- $success = 0;
- $total = 0;
- $n = 0;
- while( false !== ($line = fgets( $infile ) ) ) {
- $n++;
- if( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
- $section = strtolower( $matches[1] );
- if( $section == 'end' ) {
- if (isset ($data['disabled'])) {
- # disabled test
- $data = array();
- $section = null;
- continue;
- }
- if( !isset( $data['test'] ) ) {
- die( "'end' without 'test' at line $n\n" );
- }
- if( !isset( $data['input'] ) ) {
- die( "'end' without 'input' at line $n\n" );
- }
- if( !isset( $data['result'] ) ) {
- die( "'end' without 'result' at line $n\n" );
- }
- if( $this->runTest(
- rtrim( $data['test'] ),
- rtrim( $data['input'] ),
- $this->resultTransform(rtrim( $data['result'] ) ) ) ) {
- $success++;
- }
- $total++;
- $data = array();
- $section = null;
- continue;
- }
- $data[$section] = '';
- continue;
- }
- if( $section ) {
- $data[$section] .= $line;
- }
- }
- if( $total > 0 ) {
- $ratio = IntVal( 100.0 * $success / $total );
- print "\nPassed $success of $total tests ($ratio%)\n";
- }
- }
+require('parserTests.inc');
- /**
- * Substitute simple variables to allow for slightly more
- * sophisticated tests.
- * @access private
- */
- function resultTransform($text) {
- $rep = array (
- '__SCRIPT__' => $GLOBALS['wgScript']
- );
- $text = str_replace(array_keys($rep), array_values($rep), $text);
- return $text;
- }
+if( isset( $options['help'] ) ) {
+ echo <<<ENDS
+MediaWiki $wgVersion parser test suite
+Usage: php parserTests.php [--quick] [--quiet] [--show-output]
+ [--color[=(yes|no)]]
+ [--regex=<expression>] [--file=<testfile>]
+ [--record] [--compare]
+ [--help]
+Options:
+ --quick Suppress diff output of failed tests
+ --quiet Suppress notification of passed tests (shows only failed tests)
+ --show-output Show expected and actual output
+ --color Override terminal detection and force color output on or off
+ use wgCommandLineDarkBg = true; if your term is dark
+ --regex Only run tests whose descriptions which match given regex
+ --file Run test cases from a custom file instead of parserTests.txt
+ --record Record tests in database
+ --compare Compare with recorded results, without updating the database.
+ --help Show this help message
- /**
- * @param string $input Wikitext to try rendering
- * @param string $result Result to output
- * @return bool
- */
- function runTest( $desc, $input, $result ) {
- print "Running test $desc...";
- $user =& new User();
- $options =& ParserOptions::newFromUser( $user );
- $parser =& new Parser();
- $title =& Title::makeTitle( NS_MAIN, 'Parser_test' );
-
- $output =& $parser->parse( $input, $title, $options );
-
- $html = $output->getText();
- # $languageLinks = $output->getLanguageLinks();
- # $categoryLinks = $output->getCategoryLinks();
+ENDS;
+ exit( 0 );
+}
- $op = new OutputPage();
- $op->replaceLinkHolders($html);
+# There is a convention that the parser should never
+# refer to $wgTitle directly, but instead use the title
+# passed to it.
+$wgTitle = Title::newFromText( 'Parser test script do not use' );
+$tester = new ParserTest();
- global $wgUseTidy;
- if ($wgUseTidy) {
- # Using Parser here is probably theoretically
- # wrong, because we shouldn't use Parser to
- # validate itself, but this should be safe
- # in practice.
- $result = Parser::tidy($result);
- }
-
- if( rtrim($result) === rtrim($html) ) {
- return $this->showSuccess( $desc );
- } else {
- return $this->showFailure( $desc, $result, $html );
- }
- }
-
- function showSuccess( $desc ) {
- print "ok\n";
- return true;
- }
-
- function showFailure( $desc, $result, $html ) {
- print "FAILED\n";
- print "!! Expected:\n$result\n";
- print "!! Received:\n$html\n!!\n";
- return false;
- }
+if( isset( $options['file'] ) ) {
+ $files = array( $options['file'] );
+} else {
+ // Default parser tests and any set from extensions or local config
+ $files = $wgParserTestFiles;
}
-$tester =& new ParserTest();
-$tester->runTestsFromFile( 'maintenance/parserTests.txt' );
+# Print out software version to assist with locating regressions
+$version = SpecialVersion::getVersion();
+echo( "This is MediaWiki version {$version}.\n\n" );
+$ok = $tester->runTestsFromFiles( $files );
+exit ($ok ? 0 : -1);
?>