| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math\Tests\WikiTexVC; |
| 4 |
|
| 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 MediaWiki\Extension\Math\WikiTexVC\TexUtil; |
| 9 |
use MediaWiki\Extension\Math\WikiTexVC\TexVC; |
| 10 |
use MediaWikiUnitTestCase; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class MMLGenerationTexUtilTest extends MediaWikiUnitTestCase { |
| 32 |
|
| 33 |
private static $SIMILARITYTRESH = 0.7; |
| 34 |
|
| 35 |
private static $SKIPXMLVALIDATION = true; |
| 36 |
|
| 37 |
private static $APPLYFILTER = false; |
| 38 |
|
| 39 |
private static $APPLYCATEGORYFILTER = false; |
| 40 |
|
| 41 |
private static $SKIPPEDCATEGORIES = [ "mhchemtexified_required" ]; |
| 42 |
|
| 43 |
private static $FILTERSTART = 38; |
| 44 |
|
| 45 |
private static $FILTERLENGTH = 1; |
| 46 |
|
| 47 |
|
| 48 |
private static $GENERATEHTML = false; |
| 49 |
|
| 50 |
private static $GENERATEDHTMLFILE = __DIR__ . "/MMLGenerationTexUtilTest-Output.html"; |
| 51 |
|
| 52 |
private static $MMLREFFILE = __DIR__ . "/TexUtil-Ref.json"; |
| 53 |
|
| 54 |
|
| 55 |
private static $EXPORT_KEYS = false; |
| 56 |
|
| 57 |
|
| 58 |
private static $SKIPPEDINDICES = [ 434, 489 ]; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function testTexVC( $title, $input ) { |
| 64 |
if ( in_array( $input->ctr, self::$SKIPPEDINDICES, true ) ) { |
| 65 |
MMLTestUtilHTML::generateHTMLtableRow( self::$GENERATEDHTMLFILE, [ $title, $input->tex, $input->mmlLaTeXML, |
| 66 |
$input->mmlMathoid, "skipped", "skipped" ], false, self::$GENERATEHTML ); |
| 67 |
$this->addToAssertionCount( 1 ); |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
$texVC = new TexVC(); |
| 72 |
$useMHChem = self::getMHChem( $title ) || $input->type === "chem"; |
| 73 |
|
| 74 |
|
| 75 |
$resultT = $texVC->check( $input->tex, [ |
| 76 |
'debug' => false, |
| 77 |
'usemathrm' => false, |
| 78 |
'oldtexvc' => false, |
| 79 |
'usemhchem' => $useMHChem, |
| 80 |
'usemhchemtexified' => true |
| 81 |
] ); |
| 82 |
|
| 83 |
$mathMLtexVC = isset( $resultT["input"] ) ? MMLTestUtil::getMMLwrapped( $resultT["input"] ) : |
| 84 |
"<math> error texvc </math>"; |
| 85 |
|
| 86 |
$mmlComparator = new MMLComparator(); |
| 87 |
$usedMMLRef = $input->mmlMathoid ?? $input->mmlLaTeXML ?? "<math><merror> error no ref </merror></math>"; |
| 88 |
|
| 89 |
if ( !$usedMMLRef ) { |
| 90 |
$usedMMLRef = $input->mmlLaTeXML; |
| 91 |
} |
| 92 |
$compRes = $mmlComparator->compareMathML( $usedMMLRef, $mathMLtexVC ); |
| 93 |
MMLTestUtilHTML::generateHTMLtableRow( self::$GENERATEDHTMLFILE, [ $title, $input->tex, $input->mmlLaTeXML ?? |
| 94 |
"no ref", $input->mmlMathoid ?? "no ref", $mathMLtexVC, $compRes['similarityF'] ], |
| 95 |
false, self::$GENERATEHTML ); |
| 96 |
|
| 97 |
|
| 98 |
if ( !self::$SKIPXMLVALIDATION ) { |
| 99 |
if ( $compRes['similarityF'] >= self::$SIMILARITYTRESH ) { |
| 100 |
$this->addToAssertionCount( 1 ); |
| 101 |
} else { |
| 102 |
$this->assertXmlStringEqualsXmlString( $usedMMLRef, $mathMLtexVC ); |
| 103 |
} |
| 104 |
} else { |
| 105 |
$this->addToAssertionCount( 1 ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
private const SETS = [ |
| 110 |
'big_literals', |
| 111 |
'box_functions', |
| 112 |
'color_function', |
| 113 |
'declh_function', |
| 114 |
'definecolor_function', |
| 115 |
'fun_ar1', |
| 116 |
'fun_ar1nb', |
| 117 |
'fun_ar1opt', |
| 118 |
'fun_ar2', |
| 119 |
'fun_ar2nb', |
| 120 |
'fun_infix', |
| 121 |
'fun_mhchem', |
| 122 |
'hline_function', |
| 123 |
'latex_function_names', |
| 124 |
'left_function', |
| 125 |
'mediawiki_function_names', |
| 126 |
'mhchem_bond', |
| 127 |
'mhchem_macro_1p', |
| 128 |
'mhchem_macro_2p', |
| 129 |
'mhchem_macro_2pc', |
| 130 |
'mhchem_macro_2pu', |
| 131 |
'mhchem_single_macro', |
| 132 |
"mhchemtexified_required", |
| 133 |
'nullary_macro', |
| 134 |
'nullary_macro_in_mbox', |
| 135 |
'other_delimiters1', |
| 136 |
'other_delimiters2', |
| 137 |
'right_function' |
| 138 |
]; |
| 139 |
|
| 140 |
private const ARG_CNTS = [ |
| 141 |
"big_literals" => 1, |
| 142 |
"box_functions" => 1, |
| 143 |
"color_function" => 1, |
| 144 |
"definecolor_function" => 1, |
| 145 |
"fun_ar1" => 1, |
| 146 |
"fun_ar1nb" => 1, |
| 147 |
"fun_ar1opt" => 1, |
| 148 |
"fun_ar2" => 2, |
| 149 |
"fun_infix" => 1, |
| 150 |
"fun_ar2nb" => 5, |
| 151 |
"fun_mhchem" => 1, |
| 152 |
"left_function" => 1, |
| 153 |
"right_function" => 1, |
| 154 |
"mhchem_bond" => 1, |
| 155 |
"mhchem_macro_1p" => 1, |
| 156 |
"mhchem_macro_2p" => 2, |
| 157 |
"mhchem_macro_2pu" => 1 |
| 158 |
]; |
| 159 |
private const OTHER_ARGS = [ |
| 160 |
"declh_function" => true, |
| 161 |
]; |
| 162 |
|
| 163 |
private const SAMPLE_ARGS_RIGHT = [ |
| 164 |
"big_literals" => '(', |
| 165 |
"fun_ar2nb" => '{_1^2}{_3^4}\\sum', |
| 166 |
"left_function" => '( \\right.', |
| 167 |
"mhchem_bond" => '{-}', |
| 168 |
"right_function" => ')', |
| 169 |
|
| 170 |
]; |
| 171 |
|
| 172 |
private const SAMPLE_ARGS_LEFT = [ |
| 173 |
"right_function" => '\\left(', |
| 174 |
]; |
| 175 |
|
| 176 |
private const ENTRY_ARGS = [ |
| 177 |
"\\atop" => "{ a \\atop b }", |
| 178 |
"\\choose" => "{ a \\choose b }", |
| 179 |
"\\over" => "{a \\over b }", |
| 180 |
"\\color" => "a {b \\color{red} c} d", |
| 181 |
"\\ce{\\color}" => "\\ce{a {b \\color{red} c} d}", |
| 182 |
"\\definecolor" => "\\definecolor{ultramarine}{RGB}{0,32,96} a {b \\color{ultramarine} c} d", |
| 183 |
"\\pagecolor" => "\\pagecolor{red} e^{i \\pi}", |
| 184 |
"\\hline" => "\n\\begin{array}{|c||c|} a & b \\\\\n\\hline\n1&2 \n\\end{array}\n", |
| 185 |
"\\nolimits" => " \mathop{\\rm cos}\\nolimits^2", |
| 186 |
"\\llap" => "\\llap{40}", |
| 187 |
"\\rlap" => "\\rlap{120}", |
| 188 |
"\\smash" => "\\smash[t]{2}", |
| 189 |
"\\lower" => "\\lower{1em}{-}", |
| 190 |
"\\raise" => "\\raise{1em}{-}", |
| 191 |
"\\mkern" => "\\mkern{3mu}", |
| 192 |
"\\kern" => "\\kern{1.5mu}", |
| 193 |
"\\mskip" => "\\mskip{2mu}", |
| 194 |
"\\longLeftrightharpoons" => "\\longLeftrightharpoons{}", |
| 195 |
"\\longRightleftharpoons" => "\\longRightleftharpoons{}", |
| 196 |
"\\longleftrightarrows" => "\\longleftrightarrows{}", |
| 197 |
"\\longrightleftharpoons" => "\\longleftrightarrows{}", |
| 198 |
"\\tripledash" => "\\tripledash", |
| 199 |
"\\mathchoice" => "\\mathchoice{a}{b}{c}{d}", |
| 200 |
|
| 201 |
"\\limits" => "\\lim\\limits_{x \\to 2}", |
| 202 |
"\\displaystyle" => "\\frac{\\displaystyle \\sum_{k=1}^N k^2}{a}", |
| 203 |
"\\scriptscriptstyle" => "\\frac ab + \\scriptscriptstyle{\\frac cd + \\frac ef} + \\frac gh", |
| 204 |
"\\scriptstyle" => "{\\scriptstyle \\partial \\Omega}", |
| 205 |
"\\textstyle" => "\\textstyle \\sum_{k=1}^N k^2", |
| 206 |
|
| 207 |
|
| 208 |
"\\vbox" => "\\vbox{ab}", |
| 209 |
"\\emph" => "\\mathit{\\emph{a}} \\emph{b}", |
| 210 |
|
| 211 |
|
| 212 |
"\\vline" => "\n\\begin{array}{|c||c|} a & b \\vline c \\\\\n\\hline\n1&2 \n\\end{array}\n", |
| 213 |
]; |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public static function getMHChem( string $title ): bool { |
| 222 |
$useMHChem = false; |
| 223 |
if ( str_contains( $title, "chem" ) ) { |
| 224 |
$useMHChem = true; |
| 225 |
} |
| 226 |
return $useMHChem; |
| 227 |
} |
| 228 |
|
| 229 |
public static function setUpBeforeClass(): void { |
| 230 |
MMLTestUtilHTML::generateHTMLstart( self::$GENERATEDHTMLFILE, [ "name", "TeX-Input", "MathML(LaTeXML)", |
| 231 |
"MathML(Mathoid)", "MathML(WikiTexVC)", "F-Similarity" ], self::$GENERATEHTML ); |
| 232 |
} |
| 233 |
|
| 234 |
public static function tearDownAfterClass(): void { |
| 235 |
MMLTestUtilHTML::generateHTMLEnd( self::$GENERATEDHTMLFILE, self::$GENERATEHTML ); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public static function provideTestCases() { |
| 244 |
$refFileContent = (array)MMLTestUtil::getJSON( self::$MMLREFFILE ); |
| 245 |
$refAssociative = []; |
| 246 |
foreach ( $refFileContent as $entry ) { |
| 247 |
$refAssociative[$entry->tex] = $entry; |
| 248 |
} |
| 249 |
|
| 250 |
$groups = self::createGroups(); |
| 251 |
$overAllCtr = 0; |
| 252 |
$finalCases = []; |
| 253 |
foreach ( $groups as $category => $group ) { |
| 254 |
if ( self::$APPLYCATEGORYFILTER && in_array( $category, self::$SKIPPEDCATEGORIES, true ) ) { |
| 255 |
continue; |
| 256 |
} |
| 257 |
$indexCtr = 0; |
| 258 |
foreach ( $group as $case ) { |
| 259 |
$title = "set#" . $overAllCtr . ": " . $category . $indexCtr; |
| 260 |
if ( $refAssociative[$case] ) { |
| 261 |
$finalCase = $refAssociative[$case]; |
| 262 |
} else { |
| 263 |
$type = str_starts_with( $case, "ce" ) ? "chem" : "tex"; |
| 264 |
$finalCase = (object)[ "tex" => $case, "type" => $type, "ctr" => null ]; |
| 265 |
} |
| 266 |
|
| 267 |
if ( $category === "mhchemtexified_required" ) { |
| 268 |
$finalCase->type = "chem"; |
| 269 |
} |
| 270 |
$finalCase->ctr = $overAllCtr; |
| 271 |
|
| 272 |
$finalCases[$title] = [ $title, $finalCase ]; |
| 273 |
$indexCtr++; |
| 274 |
$overAllCtr++; |
| 275 |
} |
| 276 |
} |
| 277 |
if ( self::$APPLYFILTER ) { |
| 278 |
$finalCases = array_slice( $finalCases, self::$FILTERSTART, self::$FILTERLENGTH ); |
| 279 |
} |
| 280 |
if ( self::$EXPORT_KEYS ) { |
| 281 |
|
| 282 |
$dataToExport = []; |
| 283 |
foreach ( $finalCases as $case ) { |
| 284 |
$dataToExport[$case[1]->tex] = $case[1]->type; |
| 285 |
} |
| 286 |
self::writeToFile( __DIR__ . "/ExportedTexUtilKeys.json", $dataToExport ); |
| 287 |
} |
| 288 |
return $finalCases; |
| 289 |
} |
| 290 |
|
| 291 |
public static function writeToFile( string $fullPath, array $allEntries ): void { |
| 292 |
$jsonData = json_encode( $allEntries, JSON_PRETTY_PRINT ); |
| 293 |
file_put_contents( $fullPath, $jsonData ); |
| 294 |
} |
| 295 |
|
| 296 |
private static function addArgs( $set, $entry ) { |
| 297 |
if ( isset( self::ENTRY_ARGS[$entry] ) ) { |
| 298 |
|
| 299 |
if ( str_starts_with( $set, "mhchem" ) ) { |
| 300 |
return '\\ce{' . self::ENTRY_ARGS[$entry] . '}'; |
| 301 |
} else { |
| 302 |
return ( self::ENTRY_ARGS[$entry] ); |
| 303 |
} |
| 304 |
} |
| 305 |
$count = !isset( self::ARG_CNTS[$set] ) ? 0 : self::ARG_CNTS[$set]; |
| 306 |
$argsR = ''; |
| 307 |
$argsL = ''; |
| 308 |
if ( !isset( self::SAMPLE_ARGS_RIGHT[$set] ) ) { |
| 309 |
for ( $i = 0; $i < $count; $i++ ) { |
| 310 |
$argsR .= '{' . chr( 97 + $i ) . '}'; |
| 311 |
} |
| 312 |
} else { |
| 313 |
$argsR = self::SAMPLE_ARGS_RIGHT[$set]; |
| 314 |
} |
| 315 |
if ( isset( self::SAMPLE_ARGS_LEFT[$set] ) ) { |
| 316 |
$argsL = self::SAMPLE_ARGS_LEFT[$set]; |
| 317 |
} |
| 318 |
if ( $argsR == '' && isset( self::OTHER_ARGS[$set] ) ) { |
| 319 |
if ( self::OTHER_ARGS[$set] ) { |
| 320 |
return "{" . $entry . " a }"; |
| 321 |
} |
| 322 |
} |
| 323 |
if ( str_starts_with( $set, "mhchem" ) ) { |
| 324 |
$rendering = '\\ce{' . $argsL . $entry . $argsR . '}'; |
| 325 |
} else { |
| 326 |
$rendering = $argsL . $entry . $argsR; |
| 327 |
} |
| 328 |
return $rendering; |
| 329 |
} |
| 330 |
|
| 331 |
private static function createGroups() { |
| 332 |
$groups = []; |
| 333 |
foreach ( self::SETS as $set ) { |
| 334 |
$entries = array_keys( TexUtil::getInstance()->getBaseElements()[$set] ); |
| 335 |
foreach ( $entries as &$entry ) { |
| 336 |
$entry = self::addArgs( $set, $entry ); |
| 337 |
} |
| 338 |
$groups[$set] = $entries; |
| 339 |
} |
| 340 |
return $groups; |
| 341 |
} |
| 342 |
} |