Debian Codegraph @

extensions/Math/tests/phpunit/unit/WikiTexVC/MMLGenerationParserTest.php

raw symbols SCIP
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 * This is a very basic test for running more cases for MathML generation.
12 * WIP: This tests is for running the specified testfiles in development.
13 * Categories can be selected within 'provideTestCases' functions.
14 * @covers \MediaWiki\Extension\Math\WikiTexVC\TexVC
15 */
16 final class MMLGenerationParserTest extends MediaWikiUnitTestCase {
17 /** @var float */
18 private static $SIMILARITYTRESH = 0.7;
19 /** @var bool */
20 private static $SKIPXMLVALIDATION = true;
21
22 /** @var string */
23 private static $SELECTEDCATEGORY1 = "texvctreebugs";
24 /** @var string */
25 private static $FILENAME1 = __DIR__ . "/tex-2-mml.json";
26 /** @var string */
27 private static $FILENAME2 = __DIR__ . "/ParserTest-Ref.json";
28 /** @var int */
29 private static $SELECTEDFILE = 0; // 0 , 1 ... for selecting file
30 /** @var bool */
31 private static $APPLYFILTER = false;
32 /** @var int */
33 private static $FILTERSTART = 0;
34 /** @var int */
35 private static $FILTERLENGTH = 50;
36
37 /** @var bool */
38 private static $GENERATEHTML = false;
39 /** @var string */
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 * @dataProvider provideTestCases
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 # Fetch result from WikiTexVC(PHP)
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 // File 0 has no refs, is just for checking basics.
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 * Provide testcases and filter and format them for
121 * the first testfile 'tex-2-mml.json'.
122 * @return array
123 */
124 public static function provideTestCases1() {
125 $res = MMLTestUtil::getJSON( self::$FILENAME1 );
126 $f = $res->{self::$SELECTEDCATEGORY1};
127
128 // Adding running indices for location of tests.
129 $indexCtr = 0;
130 foreach ( $f as $tc ) {
131 $tc[1]->ctr = $indexCtr;
132 $indexCtr += 1;
133 }
134 // Filtering results by index if necessary
135 if ( self::$APPLYFILTER ) {
136 $f = array_slice( $f, self::$FILTERSTART, self::$FILTERLENGTH );
137 }
138 return $f;
139 }
140
141 /**
142 * Provide testcases and filter and format them for
143 * the second testfile 'ParserTest.json'.
144 * @return array
145 */
146 public static function provideTestCases2() {
147 $res = MMLTestUtil::getJSON( self::$FILENAME2 );
148 $f = [];
149 // Adding running indices for location of tests.
150 $indexCtr = 0;
151 foreach ( $res as $tc ) {
152 $tc->ctr = $indexCtr;
153 $tc->input = $tc->tex; // Just to have uniform access here
154 $indexCtr += 1;
155 array_push( $f, [ "title N/A", $tc ] );
156 }
157 // Filtering results by index if necessary
158 if ( self::$APPLYFILTER ) {
159 $f = array_slice( $f, self::$FILTERSTART, self::$FILTERLENGTH );
160 }
161 return $f;
162 }
163 }