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