Merge "Introduce InterwikiLookupAdapter on top of SiteLookup"
[lhc/web/wiklou.git] / tests / parser / TestFileReader.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Testing
20 */
21
22 class TestFileReader {
23 private $file;
24 private $fh;
25 private $section = null;
26 /** String|null: current test section being analyzed */
27 private $sectionData = [];
28 private $sectionLineNum = [];
29 private $lineNum = 0;
30 private $runDisabled;
31 private $runParsoid;
32 private $regex;
33
34 private $articles = [];
35 private $requirements = [];
36 private $tests = [];
37
38 public static function read( $file, array $options = [] ) {
39 $reader = new self( $file, $options );
40 $reader->execute();
41
42 $requirements = [];
43 foreach ( $reader->requirements as $type => $reqsOfType ) {
44 foreach ( $reqsOfType as $name => $unused ) {
45 $requirements[] = [
46 'type' => $type,
47 'name' => $name
48 ];
49 }
50 }
51
52 return [
53 'requirements' => $requirements,
54 'tests' => $reader->tests,
55 'articles' => $reader->articles
56 ];
57 }
58
59 private function __construct( $file, $options ) {
60 $this->file = $file;
61 $this->fh = fopen( $this->file, "rt" );
62
63 if ( !$this->fh ) {
64 throw new MWException( "Couldn't open file '$file'\n" );
65 }
66
67 $options = $options + [
68 'runDisabled' => false,
69 'runParsoid' => false,
70 'regex' => '//',
71 ];
72 $this->runDisabled = $options['runDisabled'];
73 $this->runParsoid = $options['runParsoid'];
74 $this->regex = $options['regex'];
75 }
76
77 private function addCurrentTest() {
78 // "input" and "result" are old section names allowed
79 // for backwards-compatibility.
80 $input = $this->checkSection( [ 'wikitext', 'input' ], false );
81 $nonTidySection = $this->checkSection(
82 [ 'html/php', 'html/*', 'html', 'result' ], false );
83 // Some tests have "with tidy" and "without tidy" variants
84 $tidySection = $this->checkSection( [ 'html/php+tidy', 'html+tidy' ], false );
85
86 // Remove trailing newline
87 $data = array_map( 'ParserTestRunner::chomp', $this->sectionData );
88
89 // Apply defaults
90 $data += [
91 'options' => '',
92 'config' => ''
93 ];
94
95 if ( $input === false ) {
96 throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
97 "lacks input section" );
98 }
99
100 if ( preg_match( '/\\bdisabled\\b/i', $data['options'] ) && !$this->runDisabled ) {
101 // Disabled
102 return;
103 }
104
105 if ( $tidySection === false && $nonTidySection === false ) {
106 if ( isset( $data['html/parsoid'] ) || isset( $data['wikitext/edited'] ) ) {
107 // Parsoid only
108 return;
109 } else {
110 throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
111 "lacks result section" );
112 }
113 }
114
115 if ( preg_match( '/\\bparsoid\\b/i', $data['options'] ) && $nonTidySection === 'html'
116 && !$this->runParsoid
117 ) {
118 // A test which normally runs on Parsoid but can optionally be run with MW
119 return;
120 }
121
122 if ( !preg_match( $this->regex, $data['test'] ) ) {
123 // Filtered test
124 return;
125 }
126
127 $commonInfo = [
128 'test' => $data['test'],
129 'desc' => $data['test'],
130 'input' => $data[$input],
131 'options' => $data['options'],
132 'config' => $data['config'],
133 ];
134
135 if ( $nonTidySection !== false ) {
136 // Add non-tidy test
137 $this->tests[] = [
138 'result' => $data[$nonTidySection],
139 ] + $commonInfo;
140
141 if ( $tidySection !== false ) {
142 // Add tidy subtest
143 $this->tests[] = [
144 'desc' => $data['test'] . ' (with tidy)',
145 'result' => $data[$tidySection],
146 'options' => $data['options'] . ' tidy',
147 ] + $commonInfo;
148 }
149 } elseif ( $tidySection !== false ) {
150 // No need to override desc when there is no subtest
151 $this->tests[] = [
152 'result' => $data[$tidySection],
153 'options' => $data['options'] . ' tidy'
154 ] + $commonInfo;
155 } else {
156 throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
157 "lacks result section" );
158 }
159 }
160
161 private function execute() {
162 while ( false !== ( $line = fgets( $this->fh ) ) ) {
163 $this->lineNum++;
164 $matches = [];
165
166 if ( preg_match( '/^!!\s*(\S+)/', $line, $matches ) ) {
167 $this->section = strtolower( $matches[1] );
168
169 if ( $this->section == 'endarticle' ) {
170 $this->checkSection( 'text' );
171 $this->checkSection( 'article' );
172
173 $this->addArticle(
174 ParserTestRunner::chomp( $this->sectionData['article'] ),
175 $this->sectionData['text'], $this->lineNum );
176
177 $this->clearSection();
178
179 continue;
180 }
181
182 if ( $this->section == 'endhooks' ) {
183 $this->checkSection( 'hooks' );
184
185 foreach ( explode( "\n", $this->sectionData['hooks'] ) as $line ) {
186 $line = trim( $line );
187
188 if ( $line ) {
189 $this->addRequirement( 'hook', $line );
190 }
191 }
192
193 $this->clearSection();
194
195 continue;
196 }
197
198 if ( $this->section == 'endfunctionhooks' ) {
199 $this->checkSection( 'functionhooks' );
200
201 foreach ( explode( "\n", $this->sectionData['functionhooks'] ) as $line ) {
202 $line = trim( $line );
203
204 if ( $line ) {
205 $this->addRequirement( 'functionHook', $line );
206 }
207 }
208
209 $this->clearSection();
210
211 continue;
212 }
213
214 if ( $this->section == 'endtransparenthooks' ) {
215 $this->checkSection( 'transparenthooks' );
216
217 foreach ( explode( "\n", $this->sectionData['transparenthooks'] ) as $line ) {
218 $line = trim( $line );
219
220 if ( $line ) {
221 $this->addRequirement( 'transparentHook', $line );
222 }
223 }
224
225 $this->clearSection();
226
227 continue;
228 }
229
230 if ( $this->section == 'end' ) {
231 $this->checkSection( 'test' );
232 $this->addCurrentTest();
233 $this->clearSection();
234 continue;
235 }
236
237 if ( isset( $this->sectionData[$this->section] ) ) {
238 throw new MWException( "duplicate section '$this->section' "
239 . "at line {$this->lineNum} of $this->file\n" );
240 }
241
242 $this->sectionLineNum[$this->section] = $this->lineNum;
243 $this->sectionData[$this->section] = '';
244
245 continue;
246 }
247
248 if ( $this->section ) {
249 $this->sectionData[$this->section] .= $line;
250 }
251 }
252 }
253
254 /**
255 * Clear section name and its data
256 */
257 private function clearSection() {
258 $this->sectionLineNum = [];
259 $this->sectionData = [];
260 $this->section = null;
261
262 }
263
264 /**
265 * Verify the current section data has some value for the given token
266 * name(s) (first parameter).
267 * Throw an exception if it is not set, referencing current section
268 * and adding the current file name and line number
269 *
270 * @param string|array $tokens Expected token(s) that should have been
271 * mentioned before closing this section
272 * @param bool $fatal True iff an exception should be thrown if
273 * the section is not found.
274 * @return bool|string
275 * @throws MWException
276 */
277 private function checkSection( $tokens, $fatal = true ) {
278 if ( is_null( $this->section ) ) {
279 throw new MWException( __METHOD__ . " can not verify a null section!\n" );
280 }
281 if ( !is_array( $tokens ) ) {
282 $tokens = [ $tokens ];
283 }
284 if ( count( $tokens ) == 0 ) {
285 throw new MWException( __METHOD__ . " can not verify zero sections!\n" );
286 }
287
288 $data = $this->sectionData;
289 $tokens = array_filter( $tokens, function ( $token ) use ( $data ) {
290 return isset( $data[$token] );
291 } );
292
293 if ( count( $tokens ) == 0 ) {
294 if ( !$fatal ) {
295 return false;
296 }
297 throw new MWException( sprintf(
298 "'%s' without '%s' at line %s of %s\n",
299 $this->section,
300 implode( ',', $tokens ),
301 $this->lineNum,
302 $this->file
303 ) );
304 }
305 if ( count( $tokens ) > 1 ) {
306 throw new MWException( sprintf(
307 "'%s' with unexpected tokens '%s' at line %s of %s\n",
308 $this->section,
309 implode( ',', $tokens ),
310 $this->lineNum,
311 $this->file
312 ) );
313 }
314
315 return array_values( $tokens )[0];
316 }
317
318 private function addArticle( $name, $text, $line ) {
319 $this->articles[] = [
320 'name' => $name,
321 'text' => $text,
322 'line' => $line,
323 'file' => $this->file
324 ];
325 }
326
327 private function addRequirement( $type, $name ) {
328 $this->requirements[$type][$name] = true;
329 }
330 }
331