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.
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.
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
22 class TestFileReader
{
25 private $section = null;
26 /** String|null: current test section being analyzed */
27 private $sectionData = [];
28 private $sectionLineNum = [];
33 private $articles = [];
34 private $requirements = [];
37 public static function read( $file, array $options = [] ) {
38 $reader = new self( $file, $options );
42 foreach ( $reader->requirements
as $type => $reqsOfType ) {
43 foreach ( $reqsOfType as $name => $unused ) {
52 'requirements' => $requirements,
53 'tests' => $reader->tests
,
54 'articles' => $reader->articles
58 private function __construct( $file, $options ) {
60 $this->fh
= fopen( $this->file
, "rt" );
63 throw new MWException( "Couldn't open file '$file'\n" );
66 $options = $options +
[
67 'runDisabled' => false,
70 $this->runDisabled
= $options['runDisabled'];
71 $this->regex
= $options['regex'];
74 private function addCurrentTest() {
75 // "input" and "result" are old section names allowed
76 // for backwards-compatibility.
77 $input = $this->checkSection( [ 'wikitext', 'input' ], false );
78 $nonTidySection = $this->checkSection(
79 [ 'html/php', 'html/*', 'html', 'result' ], false );
80 // Some tests have "with tidy" and "without tidy" variants
81 $tidySection = $this->checkSection( [ 'html/php+tidy', 'html+tidy' ], false );
83 // Remove trailing newline
84 $data = array_map( 'ParserTestRunner::chomp', $this->sectionData
);
92 if ( $input === false ) {
93 throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
94 "lacks input section" );
97 if ( preg_match( '/\\bdisabled\\b/i', $data['options'] ) && !$this->runDisabled
) {
102 if ( $tidySection === false && $nonTidySection === false ) {
103 if ( isset( $data['html/parsoid'] ) ||
isset( $data['wikitext/edited'] ) ) {
107 throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
108 "lacks result section" );
112 if ( !preg_match( $this->regex
, $data['test'] ) ) {
118 'test' => $data['test'],
119 'desc' => $data['test'],
120 'input' => $data[$input],
121 'options' => $data['options'],
122 'config' => $data['config'],
123 'line' => $this->sectionLineNum
['test'],
124 'file' => $this->file
127 if ( $nonTidySection !== false ) {
130 'result' => $data[$nonTidySection],
131 'resultSection' => $nonTidySection
134 if ( $tidySection !== false ) {
137 'desc' => $data['test'] . ' (with tidy)',
138 'result' => $data[$tidySection],
139 'resultSection' => $tidySection,
140 'options' => $data['options'] . ' tidy',
144 } elseif ( $tidySection !== false ) {
145 // No need to override desc when there is no subtest
147 'result' => $data[$tidySection],
148 'resultSection' => $tidySection,
149 'options' => $data['options'] . ' tidy'
152 throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
153 "lacks result section" );
157 private function execute() {
158 while ( ( $line = fgets( $this->fh
) ) !== false ) {
162 if ( preg_match( '/^!!\s*(\S+)/', $line, $matches ) ) {
163 $this->section
= strtolower( $matches[1] );
165 if ( $this->section
== 'endarticle' ) {
166 $this->checkSection( 'text' );
167 $this->checkSection( 'article' );
170 ParserTestRunner
::chomp( $this->sectionData
['article'] ),
171 $this->sectionData
['text'], $this->lineNum
);
173 $this->clearSection();
178 if ( $this->section
== 'endhooks' ) {
179 $this->checkSection( 'hooks' );
181 foreach ( explode( "\n", $this->sectionData
['hooks'] ) as $line ) {
182 $line = trim( $line );
185 $this->addRequirement( 'hook', $line );
189 $this->clearSection();
194 if ( $this->section
== 'endfunctionhooks' ) {
195 $this->checkSection( 'functionhooks' );
197 foreach ( explode( "\n", $this->sectionData
['functionhooks'] ) as $line ) {
198 $line = trim( $line );
201 $this->addRequirement( 'functionHook', $line );
205 $this->clearSection();
210 if ( $this->section
== 'endtransparenthooks' ) {
211 $this->checkSection( 'transparenthooks' );
213 foreach ( explode( "\n", $this->sectionData
['transparenthooks'] ) as $line ) {
214 $line = trim( $line );
217 $this->addRequirement( 'transparentHook', $line );
221 $this->clearSection();
226 if ( $this->section
== 'end' ) {
227 $this->checkSection( 'test' );
228 $this->addCurrentTest();
229 $this->clearSection();
233 if ( isset( $this->sectionData
[$this->section
] ) ) {
234 throw new MWException( "duplicate section '$this->section' "
235 . "at line {$this->lineNum} of $this->file\n" );
238 $this->sectionLineNum
[$this->section
] = $this->lineNum
;
239 $this->sectionData
[$this->section
] = '';
244 if ( $this->section
) {
245 $this->sectionData
[$this->section
] .= $line;
251 * Clear section name and its data
253 private function clearSection() {
254 $this->sectionLineNum
= [];
255 $this->sectionData
= [];
256 $this->section
= null;
260 * Verify the current section data has some value for the given token
261 * name(s) (first parameter).
262 * Throw an exception if it is not set, referencing current section
263 * and adding the current file name and line number
265 * @param string|array $tokens Expected token(s) that should have been
266 * mentioned before closing this section
267 * @param bool $fatal True iff an exception should be thrown if
268 * the section is not found.
269 * @return bool|string
270 * @throws MWException
272 private function checkSection( $tokens, $fatal = true ) {
273 if ( is_null( $this->section
) ) {
274 throw new MWException( __METHOD__
. " can not verify a null section!\n" );
276 if ( !is_array( $tokens ) ) {
277 $tokens = [ $tokens ];
279 if ( count( $tokens ) == 0 ) {
280 throw new MWException( __METHOD__
. " can not verify zero sections!\n" );
283 $data = $this->sectionData
;
284 $tokens = array_filter( $tokens, function ( $token ) use ( $data ) {
285 return isset( $data[$token] );
288 if ( count( $tokens ) == 0 ) {
292 throw new MWException( sprintf(
293 "'%s' without '%s' at line %s of %s\n",
295 implode( ',', $tokens ),
300 if ( count( $tokens ) > 1 ) {
301 throw new MWException( sprintf(
302 "'%s' with unexpected tokens '%s' at line %s of %s\n",
304 implode( ',', $tokens ),
310 return array_values( $tokens )[0];
313 private function addArticle( $name, $text, $line ) {
314 $this->articles
[] = [
318 'file' => $this->file
322 private function addRequirement( $type, $name ) {
323 $this->requirements
[$type][$name] = true;