| 1 |
<?php |
| 2 |
namespace MediaWiki\Extension\Math\WikiTexVC; |
| 3 |
|
| 4 |
use InvalidArgumentException; |
| 5 |
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLComparator; |
| 6 |
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLTestUtil; |
| 7 |
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLTestUtilHTML; |
| 8 |
use MediaWikiUnitTestCase; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
final class MMLFullCoverageTest extends MediaWikiUnitTestCase { |
| 25 |
|
| 26 |
private static $SIMILARITYTRESH = 0.7; |
| 27 |
|
| 28 |
private static $SKIPXMLVALIDATION = true; |
| 29 |
|
| 30 |
private static $FILENAMELATEXML = __DIR__ . "/mmlRes-latexml-FullCoverage.json"; |
| 31 |
|
| 32 |
private static $FILENAMEMATHOID = __DIR__ . "/mmlRes-mathml-FullCoverage.json"; |
| 33 |
|
| 34 |
private static $APPLYFILTER = false; |
| 35 |
|
| 36 |
private static $FILTERSTART = 0; |
| 37 |
|
| 38 |
private static $FILTERLENGTH = 60; |
| 39 |
|
| 40 |
private static $GENERATEHTML = false; |
| 41 |
|
| 42 |
private static $GENERATEDHTMLFILE = __DIR__ . "/MMLFullCoverageTest-Output.html"; |
| 43 |
|
| 44 |
private static $GENERATEEVAL = false; |
| 45 |
|
| 46 |
private static $GENERATEDEVALFILE = __DIR__ . "/MMLFullCoverageEval.json"; |
| 47 |
|
| 48 |
private static $SKIPPEDINDICES = []; |
| 49 |
|
| 50 |
|
| 51 |
private static $FILTERMML = true; |
| 52 |
|
| 53 |
public static function setUpBeforeClass(): void { |
| 54 |
MMLTestUtilHTML::generateHTMLstart( self::$GENERATEDHTMLFILE, [ "name", "TeX-Input", "MathML(LaTeXML)", |
| 55 |
"MathML(Mathoid)", "MathML(WikiTexVC)", "F-Similarity" ], self::$GENERATEHTML ); |
| 56 |
if ( self::$GENERATEEVAL ) { |
| 57 |
MMLTestUtil::deleteFile( self::$GENERATEDEVALFILE ); |
| 58 |
MMLTestUtil::createJSONstartEnd( true, self::$GENERATEDEVALFILE ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
public static function tearDownAfterClass(): void { |
| 63 |
MMLTestUtilHTML::generateHTMLEnd( self::$GENERATEDHTMLFILE, self::$GENERATEHTML ); |
| 64 |
if ( self::$GENERATEEVAL ) { |
| 65 |
MMLTestUtil::createJSONstartEnd( false, self::$GENERATEDEVALFILE ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function testTexVC( $title, $tc ) { |
| 73 |
$texVC = new TexVC(); |
| 74 |
|
| 75 |
if ( in_array( $tc->ctr, self::$SKIPPEDINDICES, true ) ) { |
| 76 |
MMLTestUtilHTML::generateHTMLtableRow( self::$GENERATEDHTMLFILE, [ $tc->ctr, $tc->tex, |
| 77 |
"skipped", "skipped", "skipped" ], false, self::$GENERATEHTML ); |
| 78 |
$this->addToAssertionCount( 1 ); |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$resultT = $texVC->check( $tc->tex, [ |
| 83 |
'debug' => false, |
| 84 |
'usemathrm' => $tc->usemathrm ?? false, |
| 85 |
'oldtexvc' => $tc->oldtexvc ?? false |
| 86 |
] ); |
| 87 |
|
| 88 |
$mml_latexml = self::$FILTERMML ? self::loadXMLandDeleteAttrs( $tc->mml_latexml ) : $tc->mml_latexml; |
| 89 |
$mathMLtexVC = MMLTestUtil::getMMLwrapped( $resultT["input"] ); |
| 90 |
$this->assertStringNotContainsString( 'merror', $mathMLtexVC, |
| 91 |
"tc $$tc->tex$: MathML $mathMLtexVC contains error" ); |
| 92 |
$mmlComparator = new MMLComparator(); |
| 93 |
$compRes = $mmlComparator->compareMathML( $tc->mml_mathoid, $mathMLtexVC ); |
| 94 |
|
| 95 |
if ( self::$GENERATEEVAL ) { |
| 96 |
$entry = [ |
| 97 |
"testname" => "FullCoverageTest", |
| 98 |
"ctr" => $tc->ctr, |
| 99 |
"tex" => $tc->tex, |
| 100 |
"mml_latcompareMathMLexml" => $mml_latexml, |
| 101 |
"mml_mathoid" => $tc->mml_mathoid, |
| 102 |
"mml_wikitexvc" => $mathMLtexVC, |
| 103 |
"tree_bracket_wikitexvc" => MMLComparator::functionObtainTreeInBrackets( $mathMLtexVC ), |
| 104 |
"tree_bracket_latexml" => MMLComparator::functionObtainTreeInBrackets( $mml_latexml ), |
| 105 |
"tree_bracket_mathoid" => MMLComparator::functionObtainTreeInBrackets( $tc->mml_mathoid ) |
| 106 |
]; |
| 107 |
MMLTestUtil::appendToJSONFile( $entry, self::$GENERATEDEVALFILE ); |
| 108 |
} |
| 109 |
|
| 110 |
MMLTestUtilHTML::generateHTMLtableRow( self::$GENERATEDHTMLFILE, [ $tc->ctr, $tc->tex, $mml_latexml, |
| 111 |
$tc->mml_mathoid, $mathMLtexVC, $compRes['similarityF'] ], false, self::$GENERATEHTML ); |
| 112 |
|
| 113 |
if ( !self::$SKIPXMLVALIDATION ) { |
| 114 |
if ( !$tc->mml_mathoid ) { |
| 115 |
$this->fail( "No Mathoid reference found for: " . $tc->tex ); |
| 116 |
} |
| 117 |
if ( $compRes['similarityF'] >= self::$SIMILARITYTRESH ) { |
| 118 |
$this->addToAssertionCount( 1 ); |
| 119 |
} else { |
| 120 |
$this->assertXmlStringEqualsXmlString( $tc->mml_mathoid, $mathMLtexVC ); |
| 121 |
} |
| 122 |
} else { |
| 123 |
$this->addToAssertionCount( 1 ); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public static function loadXMLandDeleteAttrs( $mml ) { |
| 133 |
$xml = simplexml_load_string( $mml ); |
| 134 |
self::unsetAttrs( $xml ); |
| 135 |
|
| 136 |
self::deleteAttributes( $xml ); |
| 137 |
return $xml->asXML(); |
| 138 |
} |
| 139 |
|
| 140 |
public static function deleteAttributes( &$xml ) { |
| 141 |
foreach ( $xml as $node ) { |
| 142 |
self::unsetAttrs( $node ); |
| 143 |
self::deleteAttributes( $node ); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
public static function unsetAttrs( $node ): void { |
| 148 |
$attrs = $node->attributes(); |
| 149 |
unset( $attrs['id'] ); |
| 150 |
unset( $attrs['xref'] ); |
| 151 |
} |
| 152 |
|
| 153 |
public static function provideTestCases() { |
| 154 |
$resMathoid = MMLTestUtil::getJSON( self::$FILENAMEMATHOID ); |
| 155 |
$resLaTeXML = MMLTestUtil::getJSON( self::$FILENAMELATEXML ); |
| 156 |
if ( count( $resMathoid ) != count( $resLaTeXML ) ) { |
| 157 |
throw new InvalidArgumentException( "Test files dont have the same number of entries." ); |
| 158 |
} |
| 159 |
$f = []; |
| 160 |
|
| 161 |
foreach ( $resMathoid as $index => $tcMathoid ) { |
| 162 |
$tcLaTeXML = $resLaTeXML[$index]; |
| 163 |
$tc = [ |
| 164 |
"ctr" => $index, |
| 165 |
"tex" => $tcMathoid->tex, |
| 166 |
"type" => $tcMathoid->type, |
| 167 |
"mml_mathoid" => $tcMathoid->mml, |
| 168 |
"mml_latexml" => $tcLaTeXML->mml, |
| 169 |
]; |
| 170 |
array_push( $f, [ "title N/A", (object)$tc ] ); |
| 171 |
} |
| 172 |
|
| 173 |
if ( self::$APPLYFILTER ) { |
| 174 |
$f = array_slice( $f, self::$FILTERSTART, self::$FILTERLENGTH ); |
| 175 |
} |
| 176 |
return $f; |
| 177 |
} |
| 178 |
} |