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 = [];
34 private $articles = [];
35 private $requirements = [];
38 public static function read( $file, array $options = [] ) {
39 $reader = new self( $file, $options );
43 foreach ( $reader->requirements
as $type => $reqsOfType ) {
44 foreach ( $reqsOfType as $name => $unused ) {
53 'requirements' => $requirements,
54 'tests' => $reader->tests
,
55 'articles' => $reader->articles
59 private function __construct( $file, $options ) {
61 $this->fh
= fopen( $this->file
, "rt" );
64 throw new MWException( "Couldn't open file '$file'\n" );
67 $options = $options +
[
68 'runDisabled' => false,
69 'runParsoid' => false,
72 $this->runDisabled
= $options['runDisabled'];
73 $this->runParsoid
= $options['runParsoid'];
74 $this->regex
= $options['regex'];
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 );
86 // Remove trailing newline
87 $data = array_map( 'ParserTestRunner::chomp', $this->sectionData
);
95 if ( $input === false ) {
96 throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
97 "lacks input section" );
100 if ( preg_match( '/\\bdisabled\\b/i', $data['options'] ) && !$this->runDisabled
) {
105 if ( $tidySection === false && $nonTidySection === false ) {
106 if ( isset( $data['html/parsoid'] ) ||
isset( $data['wikitext/edited'] ) ) {
110 throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
111 "lacks result section" );
115 if ( preg_match( '/\\bparsoid\\b/i', $data['options'] ) && $nonTidySection === 'html'
116 && !$this->runParsoid
118 // A test which normally runs on Parsoid but can optionally be run with MW
122 if ( !preg_match( $this->regex
, $data['test'] ) ) {
128 'test' => $data['test'],
129 'desc' => $data['test'],
130 'input' => $data[$input],
131 'options' => $data['options'],
132 'config' => $data['config'],
135 if ( $nonTidySection !== false ) {
138 'result' => $data[$nonTidySection],
141 if ( $tidySection !== false ) {
144 'desc' => $data['test'] . ' (with tidy)',
145 'result' => $data[$tidySection],
146 'options' => $data['options'] . ' tidy',
149 } elseif ( $tidySection !== false ) {
150 // No need to override desc when there is no subtest
152 'result' => $data[$tidySection],
153 'options' => $data['options'] . ' tidy'
156 throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
157 "lacks result section" );
161 private function execute() {
162 while ( false !== ( $line = fgets( $this->fh
) ) ) {
166 if ( preg_match( '/^!!\s*(\S+)/', $line, $matches ) ) {
167 $this->section
= strtolower( $matches[1] );
169 if ( $this->section
== 'endarticle' ) {
170 $this->checkSection( 'text' );
171 $this->checkSection( 'article' );
174 ParserTestRunner
::chomp( $this->sectionData
['article'] ),
175 $this->sectionData
['text'], $this->lineNum
);
177 $this->clearSection();
182 if ( $this->section
== 'endhooks' ) {
183 $this->checkSection( 'hooks' );
185 foreach ( explode( "\n", $this->sectionData
['hooks'] ) as $line ) {
186 $line = trim( $line );
189 $this->addRequirement( 'hook', $line );
193 $this->clearSection();
198 if ( $this->section
== 'endfunctionhooks' ) {
199 $this->checkSection( 'functionhooks' );
201 foreach ( explode( "\n", $this->sectionData
['functionhooks'] ) as $line ) {
202 $line = trim( $line );
205 $this->addRequirement( 'functionHook', $line );
209 $this->clearSection();
214 if ( $this->section
== 'endtransparenthooks' ) {
215 $this->checkSection( 'transparenthooks' );
217 foreach ( explode( "\n", $this->sectionData
['transparenthooks'] ) as $line ) {
218 $line = trim( $line );
221 $this->addRequirement( 'transparentHook', $line );
225 $this->clearSection();
230 if ( $this->section
== 'end' ) {
231 $this->checkSection( 'test' );
232 $this->addCurrentTest();
233 $this->clearSection();
237 if ( isset( $this->sectionData
[$this->section
] ) ) {
238 throw new MWException( "duplicate section '$this->section' "
239 . "at line {$this->lineNum} of $this->file\n" );
242 $this->sectionLineNum
[$this->section
] = $this->lineNum
;
243 $this->sectionData
[$this->section
] = '';
248 if ( $this->section
) {
249 $this->sectionData
[$this->section
] .= $line;
255 * Clear section name and its data
257 private function clearSection() {
258 $this->sectionLineNum
= [];
259 $this->sectionData
= [];
260 $this->section
= null;
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
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
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" );
281 if ( !is_array( $tokens ) ) {
282 $tokens = [ $tokens ];
284 if ( count( $tokens ) == 0 ) {
285 throw new MWException( __METHOD__
. " can not verify zero sections!\n" );
288 $data = $this->sectionData
;
289 $tokens = array_filter( $tokens, function ( $token ) use ( $data ) {
290 return isset( $data[$token] );
293 if ( count( $tokens ) == 0 ) {
297 throw new MWException( sprintf(
298 "'%s' without '%s' at line %s of %s\n",
300 implode( ',', $tokens ),
305 if ( count( $tokens ) > 1 ) {
306 throw new MWException( sprintf(
307 "'%s' with unexpected tokens '%s' at line %s of %s\n",
309 implode( ',', $tokens ),
315 return array_values( $tokens )[0];
318 private function addArticle( $name, $text, $line ) {
319 $this->articles
[] = [
323 'file' => $this->file
327 private function addRequirement( $type, $name ) {
328 $this->requirements
[$type][$name] = true;