allow simple variables in expected test results; use tidy on expected result when
[lhc/web/wiklou.git] / maintenance / parserTests.php
1 <?php
2 /**
3 * @todo document
4 * @package MediaWiki
5 * @subpackage Maintenance
6 */
7
8 /** */
9 require_once( 'commandLine.inc' );
10 include_once( 'InitialiseMessages.inc' );
11
12 $wgTitle = Title::newFromText( 'Parser test script' );
13
14 class ParserTest {
15 function runTestsFromFile( $filename ) {
16 $infile = fopen( $filename, 'rt' );
17 if( !$infile ) {
18 die( "Couldn't open parserTests.txt\n" );
19 }
20
21 $data = array();
22 $section = null;
23 $success = 0;
24 $total = 0;
25 $n = 0;
26 while( false !== ($line = fgets( $infile ) ) ) {
27 $n++;
28 if( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
29 $section = strtolower( $matches[1] );
30 if( $section == 'end' ) {
31 if (isset ($data['disabled'])) {
32 # disabled test
33 $data = array();
34 $section = null;
35 continue;
36 }
37 if( !isset( $data['test'] ) ) {
38 die( "'end' without 'test' at line $n\n" );
39 }
40 if( !isset( $data['input'] ) ) {
41 die( "'end' without 'input' at line $n\n" );
42 }
43 if( !isset( $data['result'] ) ) {
44 die( "'end' without 'result' at line $n\n" );
45 }
46 if( $this->runTest(
47 rtrim( $data['test'] ),
48 rtrim( $data['input'] ),
49 $this->resultTransform(rtrim( $data['result'] ) ) ) ) {
50 $success++;
51 }
52 $total++;
53 $data = array();
54 $section = null;
55 continue;
56 }
57 $data[$section] = '';
58 continue;
59 }
60 if( $section ) {
61 $data[$section] .= $line;
62 }
63 }
64 if( $total > 0 ) {
65 $ratio = IntVal( 100.0 * $success / $total );
66 print "\nPassed $success of $total tests ($ratio%)\n";
67 }
68 }
69
70 /**
71 * Substitute simple variables to allow for slightly more
72 * sophisticated tests.
73 * @access private
74 */
75 function resultTransform($text) {
76 $rep = array (
77 '__SCRIPT__' => $GLOBALS['wgScript']
78 );
79 $text = str_replace(array_keys($rep), array_values($rep), $text);
80 return $text;
81 }
82
83 /**
84 * @param string $input Wikitext to try rendering
85 * @param string $result Result to output
86 * @return bool
87 */
88 function runTest( $desc, $input, $result ) {
89 print "Running test $desc...";
90
91 $user =& new User();
92 $options =& ParserOptions::newFromUser( $user );
93 $parser =& new Parser();
94 $title =& Title::makeTitle( NS_MAIN, 'Parser_test' );
95
96 $output =& $parser->parse( $input, $title, $options );
97
98 $html = $output->getText();
99 # $languageLinks = $output->getLanguageLinks();
100 # $categoryLinks = $output->getCategoryLinks();
101
102 $op = new OutputPage();
103 $op->replaceLinkHolders($html);
104
105 global $wgUseTidy;
106 if ($wgUseTidy) {
107 # Using Parser here is probably theoretically
108 # wrong, because we shouldn't use Parser to
109 # validate itself, but this should be safe
110 # in practice.
111 $result = Parser::tidy($result);
112 }
113
114 if( rtrim($result) === rtrim($html) ) {
115 return $this->showSuccess( $desc );
116 } else {
117 return $this->showFailure( $desc, $result, $html );
118 }
119 }
120
121 function showSuccess( $desc ) {
122 print "ok\n";
123 return true;
124 }
125
126 function showFailure( $desc, $result, $html ) {
127 print "FAILED\n";
128 print "!! Expected:\n$result\n";
129 print "!! Received:\n$html\n!!\n";
130 return false;
131 }
132 }
133
134 $tester =& new ParserTest();
135 $tester->runTestsFromFile( 'maintenance/parserTests.txt' );
136
137 ?>