Allow comments beginning with '#' between sections, for more detailed
[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 (is_null($section) && preg_match('/^#/', $line)) {
29 # skip comment
30 continue;
31 }
32 if( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
33 $section = strtolower( $matches[1] );
34 if( $section == 'end' ) {
35 if( !isset( $data['test'] ) ) {
36 die( "'end' without 'test' at line $n\n" );
37 }
38 if( !isset( $data['input'] ) ) {
39 die( "'end' without 'input' at line $n\n" );
40 }
41 if( !isset( $data['result'] ) ) {
42 die( "'end' without 'result' at line $n\n" );
43 }
44 if( $this->runTest(
45 rtrim( $data['test'] ),
46 rtrim( $data['input'] ),
47 rtrim( $data['result'] ) ) ) {
48 $success++;
49 }
50 $total++;
51 $data = array();
52 $section = null;
53 continue;
54 }
55 $data[$section] = '';
56 continue;
57 }
58 if( $section ) {
59 $data[$section] .= $line;
60 }
61 }
62 if( $total > 0 ) {
63 $ratio = IntVal( 100.0 * $success / $total );
64 print "\nPassed $success of $total tests ($ratio%)\n";
65 }
66 }
67
68 /**
69 * @param string $input Wikitext to try rendering
70 * @param string $result Result to output
71 * @return bool
72 */
73 function runTest( $desc, $input, $result ) {
74 print "Running test $desc...";
75
76 $user =& new User();
77 $options =& ParserOptions::newFromUser( $user );
78 $parser =& new Parser();
79 $title =& Title::makeTitle( NS_MAIN, 'Parser_test' );
80
81 $output =& $parser->parse( $input, $title, $options );
82
83 $html = $output->getText();
84 # $languageLinks = $output->getLanguageLinks();
85 # $categoryLinks = $output->getCategoryLinks();
86
87 if( $result == rtrim( $html ) ) {
88 return $this->showSuccess( $desc );
89 } else {
90 return $this->showFailure( $desc, $result, $html );
91 }
92 }
93
94 function showSuccess( $desc ) {
95 print "ok\n";
96 return true;
97 }
98
99 function showFailure( $desc, $result, $html ) {
100 print "FAILED\n";
101 print "!! Expected:\n$result\n";
102 print "!! Received:\n$html\n!!\n";
103 return false;
104 }
105 }
106
107 $tester =& new ParserTest();
108 $tester->runTestsFromFile( 'maintenance/parserTests.txt' );
109
110 ?>