2022-08-17 13:26:25 +00:00
|
|
|
<?php
|
|
|
|
|
2023-11-24 09:30:05 +00:00
|
|
|
namespace MediaWiki\Extension\Math\Tests\WikiTexVC;
|
2022-08-17 13:26:25 +00:00
|
|
|
|
2023-07-18 10:30:28 +00:00
|
|
|
use InvalidArgumentException;
|
2023-11-24 09:30:05 +00:00
|
|
|
use MediaWiki\Extension\Math\WikiTexVC\TexUtil;
|
2022-08-17 13:26:25 +00:00
|
|
|
use MediaWikiUnitTestCase;
|
|
|
|
|
|
|
|
/**
|
2023-11-24 09:30:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\WikiTexVC\TexUtil
|
2022-08-17 13:26:25 +00:00
|
|
|
*/
|
|
|
|
class TexUtilTest extends MediaWikiUnitTestCase {
|
|
|
|
|
|
|
|
/**
|
2022-10-24 10:55:50 +00:00
|
|
|
* Basic test for TexUtil
|
2022-08-17 13:26:25 +00:00
|
|
|
*/
|
|
|
|
public function testTexUtil() {
|
2022-10-24 10:55:50 +00:00
|
|
|
TexUtil::removeInstance();
|
|
|
|
$tu = TexUtil::getInstance();
|
2022-08-17 13:26:25 +00:00
|
|
|
// Testing all functions
|
|
|
|
$this->assertTrue( $tu->getAllFunctionsAt( "\\AA" ) );
|
|
|
|
$this->assertFalse( $tu->getAllFunctionsAt( "\\notlisted" ) );
|
|
|
|
// Testing other functions
|
|
|
|
$this->assertTrue( $tu->mhchem_macro_2pc( "\\color" ) );
|
|
|
|
$this->assertFalse( $tu->mhchem_macro_2pc( "not listed" ) );
|
|
|
|
}
|
|
|
|
|
2023-07-18 10:30:28 +00:00
|
|
|
public function testInvalidCall() {
|
|
|
|
TexUtil::removeInstance();
|
|
|
|
$tu = TexUtil::getInstance();
|
|
|
|
// Testing all functions
|
|
|
|
$this->expectException( InvalidArgumentException::class );
|
|
|
|
$tu->__call( '\\notlisted', [] );
|
|
|
|
}
|
|
|
|
|
2022-08-17 13:26:25 +00:00
|
|
|
/**
|
|
|
|
* Testing a checksum for the parsed object against a checksum of the json file contents.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testChecksum() {
|
2022-10-24 10:55:50 +00:00
|
|
|
$tu = TexUtil::getInstance();
|
2022-08-17 13:26:25 +00:00
|
|
|
|
2022-10-10 14:03:14 +00:00
|
|
|
$out = [];
|
2022-08-17 13:26:25 +00:00
|
|
|
$sets = [
|
|
|
|
'ams_required',
|
|
|
|
'big_literals',
|
|
|
|
'box_functions',
|
|
|
|
'cancel_required',
|
|
|
|
'color_function',
|
|
|
|
'color_required',
|
|
|
|
'declh_function',
|
|
|
|
'definecolor_function',
|
|
|
|
'euro_required',
|
|
|
|
'fun_ar1',
|
|
|
|
'fun_ar1nb',
|
|
|
|
'fun_ar1opt',
|
|
|
|
'fun_ar2',
|
|
|
|
'fun_ar2nb',
|
2023-07-18 11:17:36 +00:00
|
|
|
'fun_ar4',
|
2022-08-17 13:26:25 +00:00
|
|
|
'fun_infix',
|
|
|
|
'fun_mhchem',
|
|
|
|
'hline_function',
|
|
|
|
'ignore_identifier',
|
2023-12-20 10:27:35 +00:00
|
|
|
'intent_required',
|
2022-08-17 13:26:25 +00:00
|
|
|
'latex_function_names',
|
|
|
|
'left_function',
|
|
|
|
'mathoid_required',
|
|
|
|
'mediawiki_function_names',
|
|
|
|
'mhchem_bond',
|
|
|
|
'mhchem_macro_1p',
|
|
|
|
'mhchem_macro_2p',
|
|
|
|
'mhchem_macro_2pc',
|
|
|
|
'mhchem_macro_2pu',
|
|
|
|
'mhchem_required',
|
2023-07-14 20:26:30 +00:00
|
|
|
'mhchemtexified_required',
|
2022-08-17 13:26:25 +00:00
|
|
|
'mhchem_single_macro',
|
|
|
|
'nullary_macro',
|
|
|
|
'nullary_macro_in_mbox',
|
|
|
|
'other_delimiters1',
|
|
|
|
'other_delimiters2',
|
|
|
|
'right_function',
|
|
|
|
'teubner_required',
|
2023-10-08 09:03:01 +00:00
|
|
|
'stix_required',
|
2022-08-17 13:26:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// Reading data from TexUtil.
|
|
|
|
foreach ( $sets as $set ) {
|
|
|
|
$baseElements = $tu->getBaseElements();
|
|
|
|
foreach ( $baseElements[$set] as $key => $value ) {
|
|
|
|
if ( !array_key_exists( $key, $out ) ) {
|
|
|
|
$out[$key] = [];
|
|
|
|
}
|
|
|
|
$out[$key][$set] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$maps = [
|
|
|
|
'deprecated_nullary_macro_aliase',
|
|
|
|
'nullary_macro_aliase',
|
|
|
|
'other_delimiters2',
|
|
|
|
'other_fun_ar1',
|
|
|
|
'is_literal',
|
2023-12-20 10:27:35 +00:00
|
|
|
'is_letter_mod',
|
|
|
|
"intent_required"
|
2022-08-17 13:26:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
foreach ( $maps as $map ) {
|
|
|
|
$baseElements = $tu->getBaseElements();
|
|
|
|
foreach ( $baseElements[$map] as $key => $value ) {
|
|
|
|
if ( !array_key_exists( $key, $out ) ) {
|
|
|
|
$out[$key] = [];
|
|
|
|
}
|
|
|
|
$out[$key][$map] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sorting output alphabetically encP - out not sorted correctly
|
|
|
|
ksort( $out );
|
|
|
|
foreach ( $out as &$op ) {
|
|
|
|
ksort( $op );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loading local json file
|
2023-07-16 08:04:51 +00:00
|
|
|
$file = TexUtil::getJsonFile();
|
2023-12-20 10:27:35 +00:00
|
|
|
$fileP = str_replace( [ "\n", "\t", "\r", " " ], "", $file );
|
2022-08-17 13:26:25 +00:00
|
|
|
|
|
|
|
$encP = json_encode( $out );
|
|
|
|
$hashOutput = $this->getHash( $encP );
|
|
|
|
$hashFile = $this->getHash( $fileP );
|
2023-07-18 11:17:36 +00:00
|
|
|
// uncomment the following lines to spot differences in your IDE
|
2023-12-20 10:27:35 +00:00
|
|
|
//$this->assertEquals( str_replace( "\t", " ", $file ), json_encode($out,
|
|
|
|
// JSON_PRETTY_PRINT) . "\n" );
|
2022-08-17 13:26:25 +00:00
|
|
|
$this->assertEquals( $hashFile, $hashOutput );
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getHash( $input ) {
|
|
|
|
return hash( 'sha256', $input );
|
|
|
|
}
|
|
|
|
}
|