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
implements Iterator
{
26 * @var ParserTestRunner|ParserTestTopLevelSuite An instance of ParserTestRunner
27 * (parserTests.php) or ParserTestTopLevelSuite (phpunit)
32 private $section = null;
33 /** String|null: current test section being analyzed */
34 private $sectionData = [];
37 # Create a fake parser tests which never run anything unless
38 # asked to do so. This will avoid running hooks for a disabled test
39 private $delayedParserTest;
40 private $nextSubTest = 0;
42 function __construct( $file, $parserTest ) {
44 $this->fh
= fopen( $this->file
, "rt" );
47 throw new MWException( "Couldn't open file '$file'\n" );
50 $this->parserTest
= $parserTest;
51 $this->delayedParserTest
= new DelayedParserTest();
53 $this->lineNum
= $this->index
= 0;
57 if ( fseek( $this->fh
, 0 ) ) {
58 throw new MWException( "Couldn't fseek to the start of '$this->file'\n" );
78 if ( $this->readNextTest() ) {
87 return $this->eof
!= true;
90 function setupCurrentTest() {
91 // "input" and "result" are old section names allowed
92 // for backwards-compatibility.
93 $input = $this->checkSection( [ 'wikitext', 'input' ], false );
94 $result = $this->checkSection( [ 'html/php', 'html/*', 'html', 'result' ], false );
95 // some tests have "with tidy" and "without tidy" variants
96 $tidy = $this->checkSection( [ 'html/php+tidy', 'html+tidy' ], false );
97 if ( $tidy != false ) {
98 if ( $this->nextSubTest
== 0 ) {
99 if ( $result != false ) {
100 $this->nextSubTest
= 1; // rerun non-tidy variant later
104 $this->nextSubTest
= 0; // go on to next test after this
109 if ( !isset( $this->sectionData
['options'] ) ) {
110 $this->sectionData
['options'] = '';
113 if ( !isset( $this->sectionData
['config'] ) ) {
114 $this->sectionData
['config'] = '';
117 $isDisabled = preg_match( '/\\bdisabled\\b/i', $this->sectionData
['options'] ) &&
118 !$this->parserTest
->runDisabled
;
119 $isParsoidOnly = preg_match( '/\\bparsoid\\b/i', $this->sectionData
['options'] ) &&
121 !$this->parserTest
->runParsoid
;
122 $isFiltered = !preg_match( "/" . $this->parserTest
->regex
. "/i", $this->sectionData
['test'] );
123 if ( $input == false ||
$result == false ||
$isDisabled ||
$isParsoidOnly ||
$isFiltered ) {
128 # We are really going to run the test, run pending hooks and hooks function
129 wfDebug( __METHOD__
. " unleashing delayed test for: {$this->sectionData['test']}" );
130 $hooksResult = $this->delayedParserTest
->unleash( $this->parserTest
);
131 if ( !$hooksResult ) {
132 # Some hook reported an issue. Abort.
133 throw new MWException( "Problem running requested parser hook from the test file" );
137 'test' => ParserTestRunner
::chomp( $this->sectionData
['test'] ),
138 'subtest' => $this->nextSubTest
,
139 'input' => ParserTestRunner
::chomp( $this->sectionData
[$input] ),
140 'result' => ParserTestRunner
::chomp( $this->sectionData
[$result] ),
141 'options' => ParserTestRunner
::chomp( $this->sectionData
['options'] ),
142 'config' => ParserTestRunner
::chomp( $this->sectionData
['config'] ),
144 if ( $tidy != false ) {
145 $this->test
['options'] .= " tidy";
150 function readNextTest() {
151 # Run additional subtests of previous test
152 while ( $this->nextSubTest
> 0 ) {
153 if ( $this->setupCurrentTest() ) {
158 $this->clearSection();
159 # Reset hooks for the delayed test object
160 $this->delayedParserTest
->reset();
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' );
173 $this->parserTest
->addArticle(
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->delayedParserTest
->requireHook( $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->delayedParserTest
->requireFunctionHook( $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->delayedParserTest
->requireTransparentHook( $line );
225 $this->clearSection();
230 if ( $this->section
== 'end' ) {
231 $this->checkSection( 'test' );
233 if ( $this->setupCurrentTest() ) {
236 } while ( $this->nextSubTest
> 0 );
237 # go on to next test (since this was disabled)
238 $this->clearSection();
239 $this->delayedParserTest
->reset();
243 if ( isset( $this->sectionData
[$this->section
] ) ) {
244 throw new MWException( "duplicate section '$this->section' "
245 . "at line {$this->lineNum} of $this->file\n" );
248 $this->sectionData
[$this->section
] = '';
253 if ( $this->section
) {
254 $this->sectionData
[$this->section
] .= $line;
262 * Clear section name and its data
264 private function clearSection() {
265 $this->sectionData
= [];
266 $this->section
= null;
271 * Verify the current section data has some value for the given token
272 * name(s) (first parameter).
273 * Throw an exception if it is not set, referencing current section
274 * and adding the current file name and line number
276 * @param string|array $tokens Expected token(s) that should have been
277 * mentioned before closing this section
278 * @param bool $fatal True iff an exception should be thrown if
279 * the section is not found.
280 * @return bool|string
281 * @throws MWException
283 private function checkSection( $tokens, $fatal = true ) {
284 if ( is_null( $this->section
) ) {
285 throw new MWException( __METHOD__
. " can not verify a null section!\n" );
287 if ( !is_array( $tokens ) ) {
288 $tokens = [ $tokens ];
290 if ( count( $tokens ) == 0 ) {
291 throw new MWException( __METHOD__
. " can not verify zero sections!\n" );
294 $data = $this->sectionData
;
295 $tokens = array_filter( $tokens, function ( $token ) use ( $data ) {
296 return isset( $data[$token] );
299 if ( count( $tokens ) == 0 ) {
303 throw new MWException( sprintf(
304 "'%s' without '%s' at line %s of %s\n",
306 implode( ',', $tokens ),
311 if ( count( $tokens ) > 1 ) {
312 throw new MWException( sprintf(
313 "'%s' with unexpected tokens '%s' at line %s of %s\n",
315 implode( ',', $tokens ),
321 return array_values( $tokens )[0];