Start work on porting ParserTests completely into PHPunit.
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / NewParserHelpers.php
1 <?php
2
3 class ParserTestFileIterator implements Iterator {
4
5 protected $file;
6 protected $fh;
7 protected $parserTest; /* An instance of ParserTest (parserTests.php) or MediaWikiParserTest (phpunit) */
8 protected $index = 0;
9 protected $test;
10 protected $lineNum;
11 protected $eof;
12
13 function __construct( $file, $parserTest ) {
14 global $IP;
15
16 $this->file = $file;
17 $this->fh = fopen( $this->file, "rt" );
18
19 if ( !$this->fh ) {
20 wfDie( "Couldn't open file '$file'\n" );
21 }
22
23 $this->parserTest = $parserTest;
24 //$this->parserTest->showRunFile( wfRelativePath( $this->file, $IP ) );
25
26 $this->lineNum = $this->index = 0;
27 }
28
29 function rewind() {
30 if ( fseek( $this->fh, 0 ) ) {
31 wfDie( "Couldn't fseek to the start of '$this->file'\n" );
32 }
33
34 $this->index = -1;
35 $this->lineNum = 0;
36 $this->eof = false;
37 $this->next();
38
39 return true;
40 }
41
42 function current() {
43 return $this->test;
44 }
45
46 function key() {
47 return $this->index;
48 }
49
50 function next() {
51 if ( $this->readNextTest() ) {
52 $this->index++;
53 return true;
54 } else {
55 $this->eof = true;
56 }
57 }
58
59 function valid() {
60 return $this->eof != true;
61 }
62
63 function readNextTest() {
64 $data = array();
65 $section = null;
66
67 while ( false !== ( $line = fgets( $this->fh ) ) ) {
68 $this->lineNum++;
69 $matches = array();
70
71 if ( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
72 $section = strtolower( $matches[1] );
73
74 if ( $section == 'endarticle' ) {
75 if ( !isset( $data['text'] ) ) {
76 wfDie( "'endarticle' without 'text' at line {$this->lineNum} of $this->file\n" );
77 }
78
79 if ( !isset( $data['article'] ) ) {
80 wfDie( "'endarticle' without 'article' at line {$this->lineNum} of $this->file\n" );
81 }
82
83 $this->parserTest->addArticle( $this->parserTest->removeEndingNewline( $data['article'] ), $data['text'], $this->lineNum );
84
85
86 $data = array();
87 $section = null;
88
89 continue;
90 }
91
92 if ( $section == 'endhooks' ) {
93 if ( !isset( $data['hooks'] ) ) {
94 wfDie( "'endhooks' without 'hooks' at line {$this->lineNum} of $this->file\n" );
95 }
96
97 foreach ( explode( "\n", $data['hooks'] ) as $line ) {
98 $line = trim( $line );
99
100 if ( $line ) {
101 if ( !$this->parserTest->requireHook( $line ) ) {
102 return false;
103 }
104 }
105 }
106
107 $data = array();
108 $section = null;
109
110 continue;
111 }
112
113 if ( $section == 'endfunctionhooks' ) {
114 if ( !isset( $data['functionhooks'] ) ) {
115 wfDie( "'endfunctionhooks' without 'functionhooks' at line {$this->lineNum} of $this->file\n" );
116 }
117
118 foreach ( explode( "\n", $data['functionhooks'] ) as $line ) {
119 $line = trim( $line );
120
121 if ( $line ) {
122 if ( !$this->parserTest->requireFunctionHook( $line ) ) {
123 return false;
124 }
125 }
126 }
127
128 $data = array();
129 $section = null;
130
131 continue;
132 }
133
134 if ( $section == 'end' ) {
135 if ( !isset( $data['test'] ) ) {
136 wfDie( "'end' without 'test' at line {$this->lineNum} of $this->file\n" );
137 }
138
139 if ( !isset( $data['input'] ) ) {
140 wfDie( "'end' without 'input' at line {$this->lineNum} of $this->file\n" );
141 }
142
143 if ( !isset( $data['result'] ) ) {
144 wfDie( "'end' without 'result' at line {$this->lineNum} of $this->file\n" );
145 }
146
147 if ( !isset( $data['options'] ) ) {
148 $data['options'] = '';
149 }
150
151 if ( !isset( $data['config'] ) )
152 $data['config'] = '';
153
154 if ( ( preg_match( '/\\bdisabled\\b/i', $data['options'] ) && !$this->parserTest->runDisabled )
155 || !preg_match( "/" . $this->parserTest->regex . "/i", $data['test'] ) ) {
156 # disabled test
157 $data = array();
158 $section = null;
159
160 continue;
161 }
162
163 global $wgUseTeX;
164
165 if ( preg_match( '/\\bmath\\b/i', $data['options'] ) && !$wgUseTeX ) {
166 # don't run math tests if $wgUseTeX is set to false in LocalSettings
167 $data = array();
168 $section = null;
169
170 continue;
171 }
172
173 $this->test = array(
174 'test' => $this->parserTest->removeEndingNewline( $data['test'] ),
175 'input' => $this->parserTest->removeEndingNewline( $data['input'] ),
176 'result' => $this->parserTest->removeEndingNewline( $data['result'] ),
177 'options' => $this->parserTest->removeEndingNewline( $data['options'] ),
178 'config' => $this->parserTest->removeEndingNewline( $data['config'] ) );
179
180 return true;
181 }
182
183 if ( isset ( $data[$section] ) ) {
184 wfDie( "duplicate section '$section' at line {$this->lineNum} of $this->file\n" );
185 }
186
187 $data[$section] = '';
188
189 continue;
190 }
191
192 if ( $section ) {
193 $data[$section] .= $line;
194 }
195 }
196
197 return false;
198 }
199 }