Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Math\Tests\WikiTexVC;
4
5 use InvalidArgumentException;
6 use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\BaseParsing;
7 use MediaWiki\Extension\Math\WikiTexVC\TexUtil;
8 use MediaWikiUnitTestCase;
9
10 /**
11 * @covers \MediaWiki\Extension\Math\WikiTexVC\TexUtil
12 */
13 class TexUtilTest extends MediaWikiUnitTestCase {
14
15 /**
16 * Basic test for TexUtil
17 */
18 public function testTexUtil() {
19 TexUtil::removeInstance();
20 $tu = TexUtil::getInstance();
21 // Testing all functions
22 $this->assertTrue( $tu->getAllFunctionsAt( "\\AA" ) );
23 $this->assertFalse( $tu->getAllFunctionsAt( "\\notlisted" ) );
24 // Testing other functions
25 $this->assertTrue( $tu->mhchem_macro_2pc( "\\color" ) );
26 $this->assertFalse( $tu->mhchem_macro_2pc( "not listed" ) );
27 }
28
29 public function testInvalidCall() {
30 TexUtil::removeInstance();
31 $tu = TexUtil::getInstance();
32 // Testing all functions
33 $this->expectException( InvalidArgumentException::class );
34 $tu->__call( '\\notlisted', [] );
35 }
36
37 public function testUnicodeDefined() {
38 $tu = TexUtil::getInstance();
39 $sets = [ 'nullary_macro_in_mbox' ];
40 foreach ( $sets as $set ) {
41 $baseElements = $tu->getBaseElements();
42 foreach ( $baseElements[$set] as $key => $value ) {
43 $this->assertIsString( $tu->unicode_char( $key ),
44 "unicode_char should return a string for $key ($set)" );
45 }
46 }
47 }
48
49 public function testBaseElements() {
50 $tu = TexUtil::getInstance();
51
52 $sets = [
53 'ams_required',
54 'big_literals',
55 'box_functions',
56 'callback',
57 'cancel_required',
58 'color',
59 'color_function',
60 'color_required',
61 'declh_function',
62 'definecolor_function',
63 'delimiter',
64 'deprecated_nullary_macro_aliase',
65 'euro_required',
66 'fun_ar1',
67 'fun_ar1nb',
68 'fun_ar1opt',
69 'fun_ar2',
70 'fun_ar2nb',
71 'fun_ar4',
72 'fun_infix',
73 'fun_mhchem',
74 'hline_function',
75 'identifier',
76 'ignore_identifier',
77 'intent_required',
78 'is_letter_mod',
79 'is_literal',
80 'latex_function_names',
81 'left_function',
82 'mathchar',
83 'mathoid_required',
84 'mediawiki_function_names',
85 'mhchem_bond',
86 'mhchem_macro_1p',
87 'mhchem_macro_2p',
88 'mhchem_macro_2pc',
89 'mhchem_macro_2pu',
90 'mhchem_required',
91 'mhchem_single_macro',
92 'mhchemtexified_required',
93 'nullary_macro',
94 'nullary_macro_aliase',
95 'nullary_macro_in_mbox',
96 'operator',
97 'operator_infix',
98 'operator_rendering',
99 'other_delimiters1',
100 'other_delimiters2',
101 'other_fun_ar1',
102 'over_operator',
103 'right_function',
104 'stix_required',
105 'teubner_required',
106 'unicode_char',
107 ];
108 $baseElements = $tu->getBaseElements();
109 ksort( $baseElements );
110 $this->assertEquals( $sets, array_keys( $baseElements ) );
111 }
112
113 /**
114 * Testing a checksum for the parsed object against a checksum of the json file contents.
115 * @return void
116 */
117 public function testChecksum() {
118 $tu = TexUtil::getInstance();
119
120 $out = [];
121 $baseElements = $tu->getBaseElements();
122
123 // Reading data from TexUtil.
124 foreach ( $baseElements as $group => $set ) {
125 foreach ( $set as $key => $value ) {
126 if ( !array_key_exists( $key, $out ) ) {
127 $out[$key] = [];
128 }
129 $out[$key][$group] = $value;
130 }
131 }
132
133 // Sorting output alphabetically encP - out not sorted correctly
134 ksort( $out );
135 foreach ( $out as &$op ) {
136 ksort( $op );
137 }
138
139 // Loading local json file
140 $file = TexUtil::getJsonFile();
141 // json_encode cannot generate tabs required by WMF convention https://github.com/php/php-src/issues/8864
142 $encP = json_encode( $out, JSON_PRETTY_PRINT );
143 $encP = preg_replace( '/\n\s+/', "\n", $encP ) . "\n";
144 // unescape slashes for comparison as escaping is not allowed in the json file
145 $encP = str_replace( '\/', '/', $encP );
146 $file = preg_replace( '/\n\s+/', "\n", $file );
147 $hashOutput = $this->getHash( $encP );
148 $hashFile = $this->getHash( $file );
149 // uncomment the following lines to spot differences in your IDE
150 // $this->assertEquals( $encP, $file );
151 $this->assertEquals( $hashFile, $hashOutput );
152 }
153
154 public function testMethodNamesExist() {
155 $tu = TexUtil::getInstance();
156 $sets = [
157 'callback',
158 ];
159 foreach ( $sets as $set ) {
160 $functions = $tu->getBaseElements()[ $set ];
161 foreach ( $functions as $symbol => $function ) {
162 $methodName = is_array( $function ) ? $function[0] : $function;
163
164 $this->assertTrue( method_exists( BaseParsing::class, $methodName ),
165 'Method ' . $methodName . ' for symbol ' . $symbol . ' does not exist in BaseParsing' );
166 }
167 }
168 }
169
170 public function testDataType() {
171 $types = [
172 'mathchar' => 'string',
173 'color' => 'string',
174 'operator_infix' => 'array'
175 ];
176 $tu = TexUtil::getInstance();
177 $baseElements = $tu->getBaseElements();
178 foreach ( $types as $set => $type ) {
179 foreach ( $baseElements[$set] as $key => $value ) {
180 $this->assertEquals( $type, gettype( $tu->$set( $key ) ),
181 "$set should return a $type for $key" );
182 }
183 }
184 }
185
186 public function testGetOperatorByKey() {
187 $this->assertEquals( '&#x221A;', TexUtil::getInstance()->operator_rendering( '\\surd' )[0] );
188 $this->assertEquals( '&#x2212;', TexUtil::getInstance()->operator_rendering( '-' )[0] );
189 }
190
191 public function testGetColorByKey() {
192 $this->assertEquals( '#ED1B23', TexUtil::getInstance()->color( 'Red' ) );
193 }
194
195 private function getHash( $input ) {
196 return hash( 'sha256', $input );
197 }
198 }