Debian Codegraph @

extensions/Math/tests/phpunit/unit/WikiTexVC/Intent/IntentParserTest.php

raw symbols SCIP
1 <?php
2 namespace MediaWiki\Extension\Math\WikiTexVC\Intent;
3
4 use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLTestUtil;
5 use MediaWiki\Extension\Math\WikiTexVC\TexVC;
6 use MediaWikiUnitTestCase;
7
8 /**
9 * This is a testbench for the currently experimental intent annotation feature.
10 * The base files for intent tests and intent annotated are extracted from W3 examples in HTML
11 * To (re-)generate the json testfiles from HTML an external program in nodejs is used, this can
12 * be found in GitHub: ..... tbd .... add link
13 *
14 * The output of this test is finally used to create a speech comparison with the mentioned program.
15 *
16 * @covers \MediaWiki\Extension\Math\WikiTexVC\TexVC
17 */
18 final class IntentParserTest extends MediaWikiUnitTestCase {
19 /** @var string */
20 private static $FILENAMEINTENTTESTS = __DIR__ . "/intent_mathml_testing_extracted.json";
21 /** @var string */
22 private static $FILENAMEINTENTANNOTATED = __DIR__ . "/intent_mathml_testing_latex_annotated.json";
23 /** @var bool */
24 private static $APPLYFILTER = false;
25 /** @var int */
26 private static $FILTERSTART = 3;
27 /** @var int */
28 private static $FILTERLENGTH = 2;
29 /** @var bool */
30 private static $GENERATEJSONFILE = false;
31 /** @var string */
32 private static $GENERATEDJSONFILE = __DIR__ . "/IntentParserTestLocal-Output.json";
33 /** @var int[] */
34 private static $SKIPPEDINDICES = [ 67 ];
35
36 public static function setUpBeforeClass(): void {
37 self::writeToFile( "[\n", "w" );
38 }
39
40 public static function tearDownAfterClass(): void {
41 self::writeToFile( "\n]", "a" );
42 }
43
44 public static function writeToFile( $data, $mode ): void {
45 if ( !self::$GENERATEJSONFILE ) {
46 return;
47 }
48 $fp = fopen( self::$GENERATEDJSONFILE, $mode );
49 fwrite( $fp, $data );
50 fclose( $fp );
51 }
52
53 public static function writeToJSONFile( $jsonData, $addComma = false ): void {
54 if ( !self::$GENERATEJSONFILE ) {
55 return;
56 }
57 $jsonString = json_encode( $jsonData, JSON_PRETTY_PRINT );
58 $fp = fopen( self::$GENERATEDJSONFILE, 'a' );
59 if ( $addComma ) {
60 $jsonString = $jsonString . ",";
61 }
62 fwrite( $fp, $jsonString );
63 fclose( $fp );
64 }
65
66 /**
67 * @dataProvider provideTestCases
68 */
69 public function testTexVC( $title, $tc ) {
70 $texVC = new TexVC();
71 if ( $tc->skipped == true || in_array( $tc->ctr, self::$SKIPPEDINDICES, true ) ) {
72 $this->addToAssertionCount( 1 );
73 return;
74 }
75 # Fetch result from TexVC(PHP)
76 $resultT = $texVC->check( $tc->latexi, [
77 'debug' => false,
78 'usemathrm' => $tc->usemathrm ?? false,
79 "useintent" => true,
80 "usemhchem" => $tc->usemhchem,
81 'oldtexvc' => $tc->oldtexvc ?? false
82 ] );
83 if ( !isset( $resultT["input"] ) ) {
84 if ( $tc->shouldfail ) {
85 $this->addToAssertionCount( 1 );
86 return;
87 }
88 }
89 $mathMLtexVC = MMLTestUtil::getMMLwrapped( $resultT["input"] );
90 $writeObj = [
91 "id" => $tc->id,
92 "latex" => $tc->latex,
93 "latexi" => $tc->latexi,
94 "MathML_texvc" => $mathMLtexVC,
95 "MathML_default" => $tc->mathML_default,
96 "MathML_explicit" => $tc->mathML_explicit,
97 "Name" => $tc->name,
98 ];
99 self::writeToJSONFile( $writeObj, $tc->ctr != 86 ? true : false );
100 $this->addToAssertionCount( 1 );
101 }
102
103 public static function provideTestCases() {
104 $resIntent = MMLTestUtil::getJSON( self::$FILENAMEINTENTTESTS );
105 $resIntent2 = MMLTestUtil::getJSON( self::$FILENAMEINTENTANNOTATED );
106 $f = [];
107 // Adding running indices for location of tests.
108 foreach ( $resIntent as $index => $tcIntent ) {
109 $tcIntent->ctr = $index;
110 $tcIntent2 = $resIntent2[$index];
111 $tcIntent->latexi = isset( $tcIntent2->latexi ) ? $tcIntent2->latexi : "";
112 $tcIntent->shouldfail = isset( $tcIntent2->shouldfail ) ? $tcIntent2->shouldfail : false;
113 $tcIntent->skipped = isset( $tcIntent2->skipped ) ? $tcIntent2->skipped : false;
114 $tcIntent->usemhchem = isset( $tcIntent2->usemhchem ) ? $tcIntent2->usemhchem : false;
115 array_push( $f, [ $tcIntent->name, (object)$tcIntent ] );
116 }
117 // Filtering results by index if necessary
118 if ( self::$APPLYFILTER ) {
119 $f = array_slice( $f, self::$FILTERSTART, self::$FILTERLENGTH );
120 }
121 return $f;
122 }
123 }