From d68c302b71477fbfcbe2501f857439ce69b12e5b Mon Sep 17 00:00:00 2001 From: physikerwelt Date: Thu, 21 Nov 2024 22:30:15 +0100 Subject: [PATCH] Add tests for BaseMappings Change-Id: I017815230c2709ef5933256ba430b5284022637f --- src/WikiTexVC/MMLmappings/BaseMappings.php | 10 +- .../MMLmappings/BaseMappingsTest.php | 100 ++++++++++++++++++ .../{ => Util}/MMLParsingUtilTest.php | 0 3 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 tests/phpunit/unit/WikiTexVC/MMLmappings/BaseMappingsTest.php rename tests/phpunit/unit/WikiTexVC/MMLmappings/{ => Util}/MMLParsingUtilTest.php (100%) diff --git a/src/WikiTexVC/MMLmappings/BaseMappings.php b/src/WikiTexVC/MMLmappings/BaseMappings.php index 1bed0e638..5d17d2e85 100644 --- a/src/WikiTexVC/MMLmappings/BaseMappings.php +++ b/src/WikiTexVC/MMLmappings/BaseMappings.php @@ -778,19 +778,15 @@ class BaseMappings { // Just an empty private constructor, for singleton pattern } - public static function removeInstance() { - self::$instance = null; + public static function getAll(): array { + return self::ALL; } - public static function getInstance() { + public static function getInstance(): BaseMappings { self::$instance ??= new BaseMappings(); return self::$instance; } - public static function getEntryFromList( $keylist, $key ) { - return self::ALL[$keylist][$key] ?? null; - } - public static function getOperatorByKey( $key ) { if ( $key === '-' ) { return MMLutil::uc2xNotation( '\u2212' ); // added this additionally for running all tests diff --git a/tests/phpunit/unit/WikiTexVC/MMLmappings/BaseMappingsTest.php b/tests/phpunit/unit/WikiTexVC/MMLmappings/BaseMappingsTest.php new file mode 100644 index 000000000..3f5c037dc --- /dev/null +++ b/tests/phpunit/unit/WikiTexVC/MMLmappings/BaseMappingsTest.php @@ -0,0 +1,100 @@ +assertIsArray( $all ); + $this->assertNotEmpty( $all ); + } + + public function provideTestCases(): array { + // the second argument is an array of known problems which should be removed in the future + return [ + 'macros' => [ 'macros', [ 'SetSize', 'Overunderset', 'Root', 'MoveRoot', 'LeftRight', 'MoveLeftRight', + 'rule', 'Rule', 'Nonscript', 'BuildRel', 'FBox', 'FrameBox', 'Strut', 'Cr', 'HFill', 'BeginEnd', + 'HandleLabel', 'HandleRef', 'HandleNoTag', 'MmlToken' ] + ], + 'cancel' => [ 'cancel' ], + 'mhchem' => [ 'mhchem' ], + 'custom' => [ 'custom', [ 'Insert' ] ], + 'special' => [ 'special', [ 'open', 'close', 'superscript', 'subscript', 'space', 'prime', 'comment', + 'entry', 'hash' ] ], // only tilde exists + ]; + } + + /** + * @dataProvider provideTestCases + */ + public function testValidMethods( $setName, $knownProblems = [] ) { + foreach ( BaseMappings::getAll()[$setName] as $symbol => $payload ) { + $methodName = is_array( $payload ) ? $payload[0] : $payload; + if ( in_array( $methodName, $knownProblems ) ) { + continue; + } + $this->assertTrue( method_exists( BaseParsing::class, $methodName ), + 'Method ' . $methodName . ' for symbol ' . $symbol . ' does not exist in BaseParsing' ); + + } + } + + public function testGetCancelByKey() { + $this->assertEquals( 'updiagonalstrike', BaseMappings::getCancelByKey( '\\cancel' )[1] ); + } + + public function testGetOperatorByKey() { + $this->assertEquals( '√', BaseMappings::getOperatorByKey( '\\surd' )[0] ); + $this->assertEquals( '−', BaseMappings::getOperatorByKey( '-' ) ); + } + + public function testGetCustomByKey() { + $this->assertEquals( '\u222E', BaseMappings::getCustomByKey( '\\oint' )[1] ); + } + + public function testGetDelimiterByKey() { + $this->assertEquals( '(', BaseMappings::getDelimiterByKey( '(' )[0] ); + } + + public function testGetMacroByKey() { + $this->assertEquals( 'D', BaseMappings::getMacroByKey( '\\displaystyle' )[1] ); + } + + public function testGetSpecialByKey() { + $this->assertEquals( 'tilde', BaseMappings::getSpecialByKey( '~' )[1] ); + $this->assertNull( BaseMappings::getSpecialByKey( '_' ) ); + } + + public function testGetColorByKey() { + $this->assertEquals( '#ED1B23', BaseMappings::getColorByKey( 'red' )[0] ); + } + + public function testGetInstance() { + $this->assertInstanceOf( BaseMappings::class, BaseMappings::getInstance() ); + } + + public function testGetNullaryMacro() { + $this->assertEquals( 'Å', BaseMappings::getNullaryMacro( 'AA' )[0] ); + } + + public function testGetCharacterByKey() { + $this->assertEquals( '\u0393', BaseMappings::getCharacterByKey( '\\Gamma' ) ); + } + + public function testGetMhChemByKey() { + $this->assertEquals( 'ce', BaseMappings::getMhChemByKey( '\\ce' )[1] ); + } + + public function testGetIdentifierByKey() { + $this->assertEquals( 'α', BaseMappings::getIdentifierByKey( '\\alpha' )[0] ); + } + +} diff --git a/tests/phpunit/unit/WikiTexVC/MMLmappings/MMLParsingUtilTest.php b/tests/phpunit/unit/WikiTexVC/MMLmappings/Util/MMLParsingUtilTest.php similarity index 100% rename from tests/phpunit/unit/WikiTexVC/MMLmappings/MMLParsingUtilTest.php rename to tests/phpunit/unit/WikiTexVC/MMLmappings/Util/MMLParsingUtilTest.php