2022-10-12 07:30:22 +00:00
|
|
|
<?php
|
|
|
|
|
2023-11-24 09:30:05 +00:00
|
|
|
namespace MediaWiki\Extension\Math\Tests\WikiTexVC;
|
2022-10-12 07:30:22 +00:00
|
|
|
|
2023-11-24 09:30:05 +00:00
|
|
|
use MediaWiki\Extension\Math\WikiTexVC\TexVC;
|
2022-10-12 07:30:22 +00:00
|
|
|
use MediaWikiUnitTestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently WIP functionalities of en-wiki-formulae.js testsuite.
|
|
|
|
* All assertions are currently deactivated, cause high memory load on CI.
|
|
|
|
* These tests can be run locally by enabling the ACTIVE flag.
|
|
|
|
* File download of the json-input can be done by running:
|
2022-10-20 19:07:58 +00:00
|
|
|
* $ cd maintenance && ./downloadMoreTexVCtests.sh
|
2023-11-24 09:30:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\WikiTexVC\Parser
|
2024-02-18 19:06:53 +00:00
|
|
|
*
|
2022-10-12 07:30:22 +00:00
|
|
|
* @group Stub
|
|
|
|
*/
|
|
|
|
class EnWikiFormulaeTest extends MediaWikiUnitTestCase {
|
2024-02-18 19:06:53 +00:00
|
|
|
private const FILEPATH = __DIR__ . '/en-wiki-formulae-good.json';
|
|
|
|
private const REF_FILEPATH = __DIR__ . '/en-wiki-formulae-good-reference.json';
|
|
|
|
private const CHUNK_SIZE = 1000;
|
2022-10-12 07:30:22 +00:00
|
|
|
|
2024-02-18 19:06:53 +00:00
|
|
|
public static function setUpBeforeClass(): void {
|
|
|
|
self::checkFiles();
|
|
|
|
parent::setUpBeforeClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function checkFiles() {
|
|
|
|
if ( !file_exists( self::FILEPATH ) || !file_exists( self::REF_FILEPATH ) ) {
|
|
|
|
self::markTestSkipped( 'Missing test files. Required: ' .
|
|
|
|
self::FILEPATH . ' and ' .
|
|
|
|
self::REF_FILEPATH );
|
2022-10-12 07:30:22 +00:00
|
|
|
}
|
2024-02-18 19:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads the json file to an object
|
|
|
|
* @param string $filePath file to be read
|
|
|
|
* @return array json with testcases
|
|
|
|
*/
|
|
|
|
private static function getJSON( $filePath ): array {
|
|
|
|
self::checkFiles();
|
2022-10-12 07:30:22 +00:00
|
|
|
$file = file_get_contents( $filePath );
|
2024-02-18 19:06:53 +00:00
|
|
|
return json_decode( $file, true );
|
2022-10-12 07:30:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 21:04:15 +00:00
|
|
|
public static function provideTestCases(): \Generator {
|
2022-10-12 07:30:22 +00:00
|
|
|
$group = [];
|
2022-12-06 07:56:37 +00:00
|
|
|
$groupNo = 1;
|
2024-02-18 19:06:53 +00:00
|
|
|
$references = self::getJSON( self::REF_FILEPATH );
|
|
|
|
foreach ( self::getJSON( self::FILEPATH ) as $key => $elem ) {
|
2024-03-10 20:59:19 +00:00
|
|
|
$group[$key] = [ $elem, $references[ $key ] ];
|
2024-02-18 19:06:53 +00:00
|
|
|
if ( count( $group ) >= self::CHUNK_SIZE ) {
|
2022-12-30 12:25:34 +00:00
|
|
|
yield "Group $groupNo" => [ $group ];
|
2022-12-06 07:56:37 +00:00
|
|
|
$groupNo++;
|
2022-10-12 07:30:22 +00:00
|
|
|
$group = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( count( $group ) > 0 ) {
|
2022-12-30 12:25:34 +00:00
|
|
|
yield "Group $groupNo" => [ $group ];
|
2022-10-12 07:30:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 07:56:37 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideTestCases
|
|
|
|
*/
|
|
|
|
public function testRunCases( $testcase ) {
|
2022-10-12 07:30:22 +00:00
|
|
|
$texVC = new TexVC();
|
|
|
|
|
2022-12-30 12:25:34 +00:00
|
|
|
foreach ( $testcase as $hash => [ $tex, $ref ] ) {
|
2022-12-06 07:56:37 +00:00
|
|
|
try {
|
|
|
|
$result = $texVC->check( $tex, [
|
|
|
|
"debug" => false,
|
|
|
|
"usemathrm" => false,
|
|
|
|
"oldtexvc" => false
|
|
|
|
] );
|
|
|
|
|
2022-12-30 12:25:34 +00:00
|
|
|
$this->assertEquals( '+', $result["status"],
|
|
|
|
$hash . " failed. Input: " . $tex );
|
|
|
|
if ( preg_match( '/\\\\definecolor \{/m', $ref ) ) {
|
|
|
|
// crop long numbers in color codes from 16 to 14 digits
|
|
|
|
// while this heuristic might produce false positivies in general, it is sufficient
|
|
|
|
// for this dataset
|
|
|
|
$ref = preg_replace( '/(0.\d{14})\d{2}([,\}])/m', '$1$2', $ref );
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals( $ref, $result["output"],
|
|
|
|
$hash . " does not match reference." );
|
|
|
|
|
2022-12-06 07:56:37 +00:00
|
|
|
$r1 = $texVC->check( $result["output"] );
|
|
|
|
$this->assertEquals( "+", $r1["status"],
|
|
|
|
"error rechecking output: " . $tex . " -> " . $result["output"] );
|
2024-11-25 09:11:13 +00:00
|
|
|
$mathml = $result["input"]->renderMML();
|
|
|
|
$this->assertStringNotContainsString( 'merror', $mathml,
|
|
|
|
"error rendering MathML: " . $tex . " -> " . $result["output"] );
|
|
|
|
|
2022-12-06 07:56:37 +00:00
|
|
|
} catch ( PhpPegJs\SyntaxError $ex ) {
|
|
|
|
$message = "Syntax error: " . $ex->getMessage() .
|
|
|
|
' at line ' . $ex->grammarLine . ' column ' .
|
|
|
|
$ex->grammarColumn . ' offset ' . $ex->grammarOffset;
|
|
|
|
|
2024-03-10 20:59:19 +00:00
|
|
|
$this->assertTrue( false, $message );
|
2022-10-12 07:30:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|