wiki.techinc.nl/tests/parser/TestFileReader.php
C. Scott Ananian 585cbcd77f Use parser test file parser from Parsoid
One (test file) parser to rule them all.  Reduce a little bit of
redundant code between core and Parsoid by using Parsoid's parser test
file parser to run core's parser tests.

This should have no effect on users of TestFileReader::read() *except*
that Parsoid's test file reader is more strict about bogus lines in
the test file, including duplicate test names, and we've removed support
for the old v1 format (hard deprecated in 1.35).

Next step will be to be able to execute parser tests on extensions
using Parsoid's parser as well.

Bug: T254181
Depends-On: I8ab4a8c59ed1b6837dba428f96a8ba0084b7fb68
Change-Id: I5acaf82819ae964895a831be4f28c31c77a09e84
2020-08-17 14:52:08 -04:00

103 lines
2.9 KiB
PHP

<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Testing
*/
use Wikimedia\Parsoid\ParserTests\TestFileReader as ParsoidTestFileReader;
class TestFileReader {
/**
* Read and parse the parser test file named by $file.
* @internal
* @param string $file
* @param array $options
* @return array
*/
public static function read( $file, array $options = [] ) {
$options = $options + [
'runDisabled' => false,
'filter' => false,
];
if ( isset( $options['regex'] ) ) {
$options['filter'] = [ 'regex' => $options['regex'] ];
}
$parsoidReader = ParsoidTestFileReader::read( $file, function ( $msg ) {
wfDeprecatedMsg( $msg, '1.35', false, false );
} );
if ( $parsoidReader->testFormat < 2 ) {
throw new MWException(
"Support for the parserTest v1 file format was removed in MediaWiki 1.36"
);
}
$tests = [];
foreach ( $parsoidReader->testCases as $t ) {
self::addTest( $tests, $t, $options );
}
$articles = [];
foreach ( $parsoidReader->articles as $a ) {
$articles[] = [
'name' => $a->title,
'text' => $a->text,
'line' => $a->lineNumStart,
'file' => $a->filename,
];
}
return [
'requirements' => $parsoidReader->requirements,
'tests' => $tests,
'articles' => $articles,
];
}
private static function addTest( array &$tests, Wikimedia\Parsoid\ParserTests\Test $t, array $options ) {
if ( $t->wikitext === false ) {
$t->error( "Test lacks wikitext section", $t->testName );
}
if ( isset( $t->options['disabled'] ) && !$options['runDisabled'] ) {
// Disabled
return;
}
if ( $t->legacyHtml === null ) {
if ( isset( $t->sections['html/parsoid'] ) || isset( $t->sections['wikitext/edited'] ) ) {
// Parsoid only
return;
} else {
$t->error( "Test lacks html section", $t->testName );
}
}
if ( !$t->matchesFilter( $options['filter'] ) ) {
// Filtered test
return;
}
$tests[] = [
'test' => $t->testName,
'desc' => ( $t->comment ?? '' ) . $t->testName,
'input' => $t->wikitext,
'result' => $t->legacyHtml,
'options' => $t->options,
'config' => $t->config ?? '',
'line' => $t->lineNumStart,
'file' => $t->filename,
];
}
}