2022-12-28 09:53:49 +00:00
|
|
|
<?php
|
2023-11-24 09:30:05 +00:00
|
|
|
namespace MediaWiki\Extension\Math\WikiTexVC;
|
2022-12-28 09:53:49 +00:00
|
|
|
|
2023-11-24 09:30:05 +00:00
|
|
|
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLComparator;
|
|
|
|
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLTestUtil;
|
|
|
|
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLTestUtilHTML;
|
2022-12-28 09:53:49 +00:00
|
|
|
use MediaWikiUnitTestCase;
|
|
|
|
use Psr\Log\InvalidArgumentException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a very basic test for running more cases for MathML generation.
|
|
|
|
* WIP: This tests is for running the specified testfiles in development.
|
|
|
|
* Categories can be selected within 'provideTestCases' functions.
|
2023-11-24 09:30:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\WikiTexVC\TexVC
|
2022-12-28 09:53:49 +00:00
|
|
|
* @group stub
|
|
|
|
*/
|
2023-01-26 15:34:27 +00:00
|
|
|
final class MMLGenerationParserTest extends MediaWikiUnitTestCase {
|
2023-02-03 09:25:09 +00:00
|
|
|
private static $SIMILARITYTRESH = 0.7;
|
|
|
|
private static $SKIPXMLVALIDATION = true;
|
2023-02-14 12:46:07 +00:00
|
|
|
|
|
|
|
private static $SELECTEDCATEGORY1 = "texvctreebugs";
|
2022-12-28 09:53:49 +00:00
|
|
|
private static $FILENAME1 = __DIR__ . "/tex-2-mml.json";
|
2023-01-26 15:34:27 +00:00
|
|
|
private static $FILENAME2 = __DIR__ . "/ParserTest-Ref.json";
|
2022-12-28 09:53:49 +00:00
|
|
|
private static $SELECTEDFILE = 0; // 0 , 1 ... for selecting file
|
|
|
|
private static $APPLYFILTER = false;
|
|
|
|
private static $FILTERSTART = 0;
|
2023-01-26 15:34:27 +00:00
|
|
|
private static $FILTERLENGTH = 50;
|
2022-12-28 09:53:49 +00:00
|
|
|
|
2023-09-25 15:57:36 +00:00
|
|
|
private static $GENERATEHTML = false;
|
2023-01-26 15:34:27 +00:00
|
|
|
private static $GENERATEDHTMLFILE = __DIR__ . "/MMLGenerationParserTest-Output.html";
|
2022-12-28 09:53:49 +00:00
|
|
|
|
|
|
|
protected function setUp(): void {
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown(): void {
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function setUpBeforeClass(): void {
|
2023-01-20 14:48:41 +00:00
|
|
|
MMLTestUtilHTML::generateHTMLstart( self::$GENERATEDHTMLFILE, [ "name","Tex-Input",
|
2023-11-24 09:30:05 +00:00
|
|
|
"MathML(LaTeXML)", "MathML(Mathoid)", "MathML(WikiTexVC)", "F-Similarity" ], self::$GENERATEHTML );
|
2022-12-28 09:53:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function tearDownAfterClass(): void {
|
|
|
|
MMLTestUtilHTML::generateHTMLEnd( self::$GENERATEDHTMLFILE, self::$GENERATEHTML );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestCases
|
|
|
|
*/
|
|
|
|
public function testTexVC( $title, $tc ) {
|
|
|
|
$texVC = new TexVC();
|
|
|
|
|
|
|
|
if ( $tc->skipped ?? false ) {
|
2023-01-20 14:48:41 +00:00
|
|
|
MMLTestUtilHTML::generateHTMLtableRow( self::$GENERATEDHTMLFILE, [ $tc->ctr, $tc->input,
|
|
|
|
"skipped", "skipped" ], false, self::$GENERATEHTML );
|
2022-12-28 09:53:49 +00:00
|
|
|
$this->assertTrue( true );
|
|
|
|
return;
|
|
|
|
}
|
2023-11-24 09:30:05 +00:00
|
|
|
# Fetch result from WikiTexVC(PHP)
|
2022-12-28 09:53:49 +00:00
|
|
|
$resultT = $texVC->check( $tc->input, [
|
|
|
|
'debug' => false,
|
|
|
|
'usemathrm' => $tc->usemathrm ?? false,
|
|
|
|
'oldtexvc' => $tc->oldtexvc ?? false
|
|
|
|
] );
|
2023-02-03 09:25:09 +00:00
|
|
|
|
2022-12-28 09:53:49 +00:00
|
|
|
$mathMLtexVC = MMLTestUtil::getMMLwrapped( $resultT["input"] );
|
2023-02-03 09:25:09 +00:00
|
|
|
if ( self::$SELECTEDFILE == 0 ) {
|
|
|
|
// File 0 has no refs, is just for checking basics.
|
|
|
|
MMLTestUtilHTML::generateHTMLtableRow( self::$GENERATEDHTMLFILE, [ $tc->ctr,
|
|
|
|
$tc->input,$tc->mmlLaTeXML ?? "tbd" ,"tbd",
|
|
|
|
$mathMLtexVC, -0.0 ], false, self::$GENERATEHTML );
|
|
|
|
$this->assertTrue( true );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$mmlComparator = new MMLComparator();
|
|
|
|
$compRes = $mmlComparator->compareMathML( $tc->mmlMathoid, $mathMLtexVC );
|
2023-01-26 15:34:27 +00:00
|
|
|
MMLTestUtilHTML::generateHTMLtableRow( self::$GENERATEDHTMLFILE, [ $tc->ctr,
|
|
|
|
$tc->input,$tc->mmlLaTeXML ?? "tbd" ,$tc->mmlMathoid ?? "tbd",
|
2023-02-03 09:25:09 +00:00
|
|
|
$mathMLtexVC, $compRes['similarityF'] ], false, self::$GENERATEHTML );
|
|
|
|
|
|
|
|
if ( !self::$SKIPXMLVALIDATION ) {
|
|
|
|
if ( !$tc->mmlMathoid ) {
|
|
|
|
$this->fail( "No Mathoid reference found for: " . $tc->input );
|
|
|
|
}
|
|
|
|
if ( $compRes['similarityF'] >= self::$SIMILARITYTRESH ) {
|
|
|
|
$this->assertTrue( true );
|
|
|
|
} else {
|
|
|
|
$this->assertXmlStringEqualsXmlString( $tc->mmlMathoid, $mathMLtexVC );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->assertTrue( true );
|
|
|
|
}
|
2022-12-28 09:53:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function provideTestCases() {
|
|
|
|
switch ( self::$SELECTEDFILE ) {
|
|
|
|
case 0:
|
|
|
|
return self::provideTestCases1();
|
|
|
|
case 1:
|
|
|
|
return self::provideTestCases2();
|
|
|
|
default:
|
|
|
|
self::throwException( new InvalidArgumentException( "No correct file specified" ) );
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide testcases and filter and format them for
|
|
|
|
* the first testfile 'tex-2-mml.json'.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function provideTestCases1() {
|
|
|
|
$res = MMLTestUtil::getJSON( self::$FILENAME1 );
|
2023-02-14 12:46:07 +00:00
|
|
|
$f = $res->{self::$SELECTEDCATEGORY1};
|
|
|
|
|
2022-12-28 09:53:49 +00:00
|
|
|
// Adding running indices for location of tests.
|
|
|
|
$indexCtr = 0;
|
|
|
|
foreach ( $f as $tc ) {
|
|
|
|
$tc[1]->ctr = $indexCtr;
|
|
|
|
$indexCtr += 1;
|
|
|
|
}
|
|
|
|
// Filtering results by index if necessary
|
|
|
|
if ( self::$APPLYFILTER ) {
|
|
|
|
$f = array_slice( $f, self::$FILTERSTART, self::$FILTERLENGTH );
|
|
|
|
}
|
|
|
|
return $f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide testcases and filter and format them for
|
|
|
|
* the second testfile 'ParserTest.json'.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function provideTestCases2() {
|
|
|
|
$res = MMLTestUtil::getJSON( self::$FILENAME2 );
|
|
|
|
$f = [];
|
|
|
|
// Adding running indices for location of tests.
|
|
|
|
$indexCtr = 0;
|
|
|
|
foreach ( $res as $tc ) {
|
|
|
|
$tc->ctr = $indexCtr;
|
2023-01-26 15:34:27 +00:00
|
|
|
$tc->input = $tc->tex; // Just to have uniform access here
|
2022-12-28 09:53:49 +00:00
|
|
|
$indexCtr += 1;
|
|
|
|
array_push( $f, [ "title N/A", $tc ] );
|
|
|
|
}
|
|
|
|
// Filtering results by index if necessary
|
|
|
|
if ( self::$APPLYFILTER ) {
|
|
|
|
$f = array_slice( $f, self::$FILTERSTART, self::$FILTERLENGTH );
|
|
|
|
}
|
|
|
|
return $f;
|
|
|
|
}
|
|
|
|
}
|