| 1 |
<?php |
| 2 |
namespace MediaWiki\Extension\Math\WikiTexVC\Mhchem; |
| 3 |
|
| 4 |
use Exception; |
| 5 |
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLTestUtil; |
| 6 |
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLTestUtilHTML; |
| 7 |
use MediaWiki\Extension\Math\WikiTexVC\TexVC; |
| 8 |
use MediaWikiUnitTestCase; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
final class MMLmhchemTest extends MediaWikiUnitTestCase { |
| 24 |
private static bool $LOGMHCHEM = false; |
| 25 |
private static bool $SKIPXMLVALIDATION = false; |
| 26 |
private static string $FILENAMEREF = __DIR__ . "/Mhchemv4mml.json"; |
| 27 |
|
| 28 |
private static bool $APPLYFILTER = false; |
| 29 |
private static int $FILTERSTART = 92; |
| 30 |
private static int $FILTERLENGTH = 1; |
| 31 |
private static bool $GENERATEFILES = false; |
| 32 |
private static string $GENERATEDHTMLFILE = __DIR__ . "/MMLmhchemTest-Output.html"; |
| 33 |
private static string $GENERATEDWIKIFILE = __DIR__ . "/chemtest.wiki"; |
| 34 |
|
| 35 |
private static array $SKIPPEDINDICES = [ 36, 40, 50, 51, 91, 92, 93, 94 ]; |
| 36 |
|
| 37 |
public static function writeWikifileHeader( $filename ) { |
| 38 |
unlink( $filename ); |
| 39 |
self::writeToFile( $filename, "{| class=\"wikitable\"\n|-\n !colspan=\"3\"|\n" . |
| 40 |
"|+ comparison of rendering for chemical formulas \n" . |
| 41 |
"|- \n ! TeX !! Rendering(native) !! Rendering(default)}" ); |
| 42 |
} |
| 43 |
|
| 44 |
public static function writeWikifileRow( $filename, $tex ) { |
| 45 |
$text = "|- \n" |
| 46 |
. "| <syntaxhighlight lang=\"latex\" enclose=\"none\">" . $tex . "</syntaxhighlight> \n" |
| 47 |
. "|<chem forcemathmode=\"native\">" . $tex . "</chem> \n" |
| 48 |
. "|<chem>" . $tex . "</chem>"; |
| 49 |
self::writeToFile( $filename, $text ); |
| 50 |
} |
| 51 |
|
| 52 |
public static function writeToFile( $filename, $line ) { |
| 53 |
$myfile = fopen( $filename, "a" ); |
| 54 |
fwrite( $myfile, $line . "\n" ); |
| 55 |
fclose( $myfile ); |
| 56 |
} |
| 57 |
|
| 58 |
public static function setUpBeforeClass(): void { |
| 59 |
MMLTestUtilHTML::generateHTMLstart( self::$GENERATEDHTMLFILE, [ "name", "TeX-Input", |
| 60 |
"Tex-MhchemParser", "Tex-PHP-Mhchem", "MathML-LaTeXML", "MathML-WikiTexVC" ], |
| 61 |
self::$GENERATEFILES ); |
| 62 |
|
| 63 |
if ( self::$GENERATEFILES ) { |
| 64 |
self::writeWikifileHeader( self::$GENERATEDWIKIFILE ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public static function tearDownAfterClass(): void { |
| 69 |
MMLTestUtilHTML::generateHTMLEnd( self::$GENERATEDHTMLFILE, self::$GENERATEFILES ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function testTexVC( $title, $tc ) { |
| 77 |
$texVC = new TexVC(); |
| 78 |
|
| 79 |
if ( in_array( $tc->ctr, self::$SKIPPEDINDICES ) ) { |
| 80 |
MMLTestUtilHTML::generateHTMLtableRow( self::$GENERATEDHTMLFILE, [ $tc->ctr, $tc->tex, |
| 81 |
"skipped", "skipped", "skipped", "skipped" ], false, self::$GENERATEFILES ); |
| 82 |
$this->addToAssertionCount( 1 ); |
| 83 |
return; |
| 84 |
} |
| 85 |
if ( self::$GENERATEFILES ) { |
| 86 |
self::writeWikifileRow( self::$GENERATEDWIKIFILE, $tc->tex ); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
$mhchemParser = new MhchemParser( self::$LOGMHCHEM ); |
| 91 |
$mhchemOutput = $mhchemParser->toTex( $tc->tex, $tc->typeC, true ); |
| 92 |
|
| 93 |
$warnings = []; |
| 94 |
$resTexVC = $texVC->check( $mhchemOutput, [ |
| 95 |
'debug' => false, |
| 96 |
'usemathrm' => true, |
| 97 |
'oldtexvc' => false, |
| 98 |
'usemhchem' => true, |
| 99 |
"usemhchemtexified" => true |
| 100 |
], $warnings, false ); |
| 101 |
$mathMLtexVC = isset( $resTexVC["input"] ) ? MMLTestUtil::getMMLwrapped( $resTexVC["input"] ) : |
| 102 |
"<math> error texvc </math>"; |
| 103 |
|
| 104 |
MMLTestUtilHTML::generateHTMLtableRow( |
| 105 |
self::$GENERATEDHTMLFILE, [ $title, $tc->tex, $tc->texNew, $mhchemOutput, $tc->mml_latexml ?? "no mathml", |
| 106 |
$mathMLtexVC ], |
| 107 |
false, self::$GENERATEFILES ); |
| 108 |
|
| 109 |
if ( !self::$SKIPXMLVALIDATION ) { |
| 110 |
$this->assertEquals( $tc->texNew, $mhchemOutput ); |
| 111 |
} else { |
| 112 |
$this->addToAssertionCount( 1 ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
public static function provideTestCases() { |
| 117 |
$fileTestcases = MMLTestUtil::getJSON( self::$FILENAMEREF ); |
| 118 |
|
| 119 |
$f = []; |
| 120 |
|
| 121 |
$ctr = 0; |
| 122 |
foreach ( $fileTestcases as $tcF ) { |
| 123 |
$tc = [ |
| 124 |
"ctr" => $ctr, |
| 125 |
"tex" => $tcF->tex, |
| 126 |
"texNew" => $tcF->texNew, |
| 127 |
"type" => $tcF->type, |
| 128 |
"typeC" => $tcF->typeC, |
| 129 |
"mml_mathoid" => $tcF->mmlMathoid, |
| 130 |
"mml_latexml" => $tcF->mmlLaTeXML, |
| 131 |
]; |
| 132 |
$f[] = [ "tc#" . str_pad( $ctr, 3, '0', STR_PAD_LEFT ) . " " . $tcF->description, |
| 133 |
(object)$tc ]; |
| 134 |
$ctr++; |
| 135 |
} |
| 136 |
|
| 137 |
if ( self::$APPLYFILTER ) { |
| 138 |
$f = array_slice( $f, self::$FILTERSTART, self::$FILTERLENGTH ); |
| 139 |
} |
| 140 |
return $f; |
| 141 |
} |
| 142 |
} |