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 = [];
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,
68 'runParsoid' => false,
71 $this->runDisabled
= $options['runDisabled'];
72 $this->runParsoid
= $options['runParsoid'];
73 $this->regex
= $options['regex'];
76 private function addCurrentTest() {
77 // "input" and "result" are old section names allowed
78 // for backwards-compatibility.
79 $input = $this->checkSection( [ 'wikitext', 'input' ], false );
80 $result = $this->checkSection( [ 'html/php', 'html/*', 'html', 'result' ], false );
81 // Some tests have "with tidy" and "without tidy" variants
82 $tidy = $this->checkSection( [ 'html/php+tidy', 'html+tidy' ], false );
84 if ( !isset( $this->sectionData
['options'] ) ) {
85 $this->sectionData
['options'] = '';
88 if ( !isset( $this->sectionData
['config'] ) ) {
89 $this->sectionData
['config'] = '';
92 $isDisabled = preg_match( '/\\bdisabled\\b/i', $this->sectionData
['options'] ) &&
94 $isParsoidOnly = preg_match( '/\\bparsoid\\b/i', $this->sectionData
['options'] ) &&
97 $isFiltered = !preg_match( $this->regex
, $this->sectionData
['test'] );
98 if ( $input == false ||
$result == false ||
$isDisabled ||
$isParsoidOnly ||
$isFiltered ) {
104 'test' => ParserTestRunner
::chomp( $this->sectionData
['test'] ),
105 'input' => ParserTestRunner
::chomp( $this->sectionData
[$input] ),
106 'result' => ParserTestRunner
::chomp( $this->sectionData
[$result] ),
107 'options' => ParserTestRunner
::chomp( $this->sectionData
['options'] ),
108 'config' => ParserTestRunner
::chomp( $this->sectionData
['config'] ),
110 $test['desc'] = $test['test'];
111 $this->tests
[] = $test;
113 if ( $tidy !== false ) {
114 $test['options'] .= " tidy";
115 $test['desc'] .= ' (with tidy)';
116 $test['result'] = ParserTestRunner
::chomp( $this->sectionData
[$tidy] );
117 $this->tests
[] = $test;
121 private function execute() {
122 while ( false !== ( $line = fgets( $this->fh
) ) ) {
126 if ( preg_match( '/^!!\s*(\S+)/', $line, $matches ) ) {
127 $this->section
= strtolower( $matches[1] );
129 if ( $this->section
== 'endarticle' ) {
130 $this->checkSection( 'text' );
131 $this->checkSection( 'article' );
134 ParserTestRunner
::chomp( $this->sectionData
['article'] ),
135 $this->sectionData
['text'], $this->lineNum
);
137 $this->clearSection();
142 if ( $this->section
== 'endhooks' ) {
143 $this->checkSection( 'hooks' );
145 foreach ( explode( "\n", $this->sectionData
['hooks'] ) as $line ) {
146 $line = trim( $line );
149 $this->addRequirement( 'hook', $line );
153 $this->clearSection();
158 if ( $this->section
== 'endfunctionhooks' ) {
159 $this->checkSection( 'functionhooks' );
161 foreach ( explode( "\n", $this->sectionData
['functionhooks'] ) as $line ) {
162 $line = trim( $line );
165 $this->addRequirement( 'functionHook', $line );
169 $this->clearSection();
174 if ( $this->section
== 'endtransparenthooks' ) {
175 $this->checkSection( 'transparenthooks' );
177 foreach ( explode( "\n", $this->sectionData
['transparenthooks'] ) as $line ) {
178 $line = trim( $line );
181 $this->addRequirement( 'transparentHook', $line );
185 $this->clearSection();
190 if ( $this->section
== 'end' ) {
191 $this->checkSection( 'test' );
192 $this->addCurrentTest();
193 $this->clearSection();
197 if ( isset( $this->sectionData
[$this->section
] ) ) {
198 throw new MWException( "duplicate section '$this->section' "
199 . "at line {$this->lineNum} of $this->file\n" );
202 $this->sectionData
[$this->section
] = '';
207 if ( $this->section
) {
208 $this->sectionData
[$this->section
] .= $line;
214 * Clear section name and its data
216 private function clearSection() {
217 $this->sectionData
= [];
218 $this->section
= null;
223 * Verify the current section data has some value for the given token
224 * name(s) (first parameter).
225 * Throw an exception if it is not set, referencing current section
226 * and adding the current file name and line number
228 * @param string|array $tokens Expected token(s) that should have been
229 * mentioned before closing this section
230 * @param bool $fatal True iff an exception should be thrown if
231 * the section is not found.
232 * @return bool|string
233 * @throws MWException
235 private function checkSection( $tokens, $fatal = true ) {
236 if ( is_null( $this->section
) ) {
237 throw new MWException( __METHOD__
. " can not verify a null section!\n" );
239 if ( !is_array( $tokens ) ) {
240 $tokens = [ $tokens ];
242 if ( count( $tokens ) == 0 ) {
243 throw new MWException( __METHOD__
. " can not verify zero sections!\n" );
246 $data = $this->sectionData
;
247 $tokens = array_filter( $tokens, function ( $token ) use ( $data ) {
248 return isset( $data[$token] );
251 if ( count( $tokens ) == 0 ) {
255 throw new MWException( sprintf(
256 "'%s' without '%s' at line %s of %s\n",
258 implode( ',', $tokens ),
263 if ( count( $tokens ) > 1 ) {
264 throw new MWException( sprintf(
265 "'%s' with unexpected tokens '%s' at line %s of %s\n",
267 implode( ',', $tokens ),
273 return array_values( $tokens )[0];
276 private function addArticle( $name, $text, $line ) {
277 $this->articles
[] = [
281 'file' => $this->file
285 private function addRequirement( $type, $name ) {
286 $this->requirements
[$type][$name] = true;