| 1 |
<?php |
| 2 |
namespace MediaWiki\Extension\Math\WikiTexVC; |
| 3 |
|
| 4 |
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLComparator; |
| 5 |
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLTestUtil; |
| 6 |
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLTestUtilHTML; |
| 7 |
use MediaWikiUnitTestCase; |
| 8 |
use Psr\Log\InvalidArgumentException; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
final class MMLGenerationParserTest extends MediaWikiUnitTestCase { |
| 17 |
|
| 18 |
private static $SIMILARITYTRESH = 0.7; |
| 19 |
|
| 20 |
private static $SKIPXMLVALIDATION = true; |
| 21 |
|
| 22 |
|
| 23 |
private static $SELECTEDCATEGORY1 = "texvctreebugs"; |
| 24 |
|
| 25 |
private static $FILENAME1 = __DIR__ . "/tex-2-mml.json"; |
| 26 |
|
| 27 |
private static $FILENAME2 = __DIR__ . "/ParserTest-Ref.json"; |
| 28 |
|
| 29 |
private static $SELECTEDFILE = 0; |
| 30 |
|
| 31 |
private static $APPLYFILTER = false; |
| 32 |
|
| 33 |
private static $FILTERSTART = 0; |
| 34 |
|
| 35 |
private static $FILTERLENGTH = 50; |
| 36 |
|
| 37 |
|
| 38 |
private static $GENERATEHTML = false; |
| 39 |
|
| 40 |
private static $GENERATEDHTMLFILE = __DIR__ . "/MMLGenerationParserTest-Output.html"; |
| 41 |
|
| 42 |
protected function setUp(): void { |
| 43 |
parent::setUp(); |
| 44 |
} |
| 45 |
|
| 46 |
protected function tearDown(): void { |
| 47 |
parent::tearDown(); |
| 48 |
} |
| 49 |
|
| 50 |
public static function setUpBeforeClass(): void { |
| 51 |
MMLTestUtilHTML::generateHTMLstart( self::$GENERATEDHTMLFILE, [ "name", "Tex-Input", |
| 52 |
"MathML(LaTeXML)", "MathML(Mathoid)", "MathML(WikiTexVC)", "F-Similarity" ], self::$GENERATEHTML ); |
| 53 |
} |
| 54 |
|
| 55 |
public static function tearDownAfterClass(): void { |
| 56 |
MMLTestUtilHTML::generateHTMLEnd( self::$GENERATEDHTMLFILE, self::$GENERATEHTML ); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public function testTexVC( $title, $tc ) { |
| 63 |
$texVC = new TexVC(); |
| 64 |
|
| 65 |
if ( $tc->skipped ?? false ) { |
| 66 |
MMLTestUtilHTML::generateHTMLtableRow( self::$GENERATEDHTMLFILE, [ $tc->ctr, $tc->input, |
| 67 |
"skipped", "skipped" ], false, self::$GENERATEHTML ); |
| 68 |
$this->addToAssertionCount( 1 ); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$resultT = $texVC->check( $tc->input, [ |
| 73 |
'debug' => false, |
| 74 |
'usemathrm' => $tc->usemathrm ?? false, |
| 75 |
'oldtexvc' => $tc->oldtexvc ?? false |
| 76 |
] ); |
| 77 |
|
| 78 |
$mathMLtexVC = MMLTestUtil::getMMLwrapped( $resultT["input"] ); |
| 79 |
if ( self::$SELECTEDFILE == 0 ) { |
| 80 |
|
| 81 |
MMLTestUtilHTML::generateHTMLtableRow( self::$GENERATEDHTMLFILE, [ $tc->ctr, |
| 82 |
$tc->input, $tc->mmlLaTeXML ?? "tbd", "tbd", |
| 83 |
$mathMLtexVC, -0.0 ], false, self::$GENERATEHTML ); |
| 84 |
$this->addToAssertionCount( 1 ); |
| 85 |
return; |
| 86 |
} |
| 87 |
$mmlComparator = new MMLComparator(); |
| 88 |
$compRes = $mmlComparator->compareMathML( $tc->mmlMathoid, $mathMLtexVC ); |
| 89 |
MMLTestUtilHTML::generateHTMLtableRow( self::$GENERATEDHTMLFILE, [ $tc->ctr, |
| 90 |
$tc->input, $tc->mmlLaTeXML ?? "tbd", $tc->mmlMathoid ?? "tbd", |
| 91 |
$mathMLtexVC, $compRes['similarityF'] ], false, self::$GENERATEHTML ); |
| 92 |
|
| 93 |
if ( !self::$SKIPXMLVALIDATION ) { |
| 94 |
if ( !$tc->mmlMathoid ) { |
| 95 |
$this->fail( "No Mathoid reference found for: " . $tc->input ); |
| 96 |
} |
| 97 |
if ( $compRes['similarityF'] >= self::$SIMILARITYTRESH ) { |
| 98 |
$this->addToAssertionCount( 1 ); |
| 99 |
} else { |
| 100 |
$this->assertXmlStringEqualsXmlString( $tc->mmlMathoid, $mathMLtexVC ); |
| 101 |
} |
| 102 |
} else { |
| 103 |
$this->addToAssertionCount( 1 ); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public static function provideTestCases() { |
| 108 |
switch ( self::$SELECTEDFILE ) { |
| 109 |
case 0: |
| 110 |
return self::provideTestCases1(); |
| 111 |
case 1: |
| 112 |
return self::provideTestCases2(); |
| 113 |
default: |
| 114 |
self::throwException( new InvalidArgumentException( "No correct file specified" ) ); |
| 115 |
return []; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public static function provideTestCases1() { |
| 125 |
$res = MMLTestUtil::getJSON( self::$FILENAME1 ); |
| 126 |
$f = $res->{self::$SELECTEDCATEGORY1}; |
| 127 |
|
| 128 |
|
| 129 |
$indexCtr = 0; |
| 130 |
foreach ( $f as $tc ) { |
| 131 |
$tc[1]->ctr = $indexCtr; |
| 132 |
$indexCtr += 1; |
| 133 |
} |
| 134 |
|
| 135 |
if ( self::$APPLYFILTER ) { |
| 136 |
$f = array_slice( $f, self::$FILTERSTART, self::$FILTERLENGTH ); |
| 137 |
} |
| 138 |
return $f; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public static function provideTestCases2() { |
| 147 |
$res = MMLTestUtil::getJSON( self::$FILENAME2 ); |
| 148 |
$f = []; |
| 149 |
|
| 150 |
$indexCtr = 0; |
| 151 |
foreach ( $res as $tc ) { |
| 152 |
$tc->ctr = $indexCtr; |
| 153 |
$tc->input = $tc->tex; |
| 154 |
$indexCtr += 1; |
| 155 |
array_push( $f, [ "title N/A", $tc ] ); |
| 156 |
} |
| 157 |
|
| 158 |
if ( self::$APPLYFILTER ) { |
| 159 |
$f = array_slice( $f, self::$FILTERSTART, self::$FILTERLENGTH ); |
| 160 |
} |
| 161 |
return $f; |
| 162 |
} |
| 163 |
} |