Improve test coverage for nodes

* Use merror instead of plain strings for not implemented functions

Change-Id: If20559e05f3b0c8921a28c623102b8f4788a3c94
This commit is contained in:
Moritz Schubotz (physikerwelt) 2022-12-28 16:05:59 +01:00 committed by Physikerwelt
parent 1a30bbe04f
commit b9e297c4ec
16 changed files with 57 additions and 90 deletions

View file

@ -4,12 +4,14 @@ namespace MediaWiki\Extension\Math\TexVC\MMLmappings;
use ArgumentCountError;
use MediaWiki\Extension\Math\TexVC\MMLmappings\TexConstants\Variants;
use MediaWiki\Extension\Math\TexVC\MMLmappings\Util\MMLutil;
use MediaWiki\Extension\Math\TexVC\MMLnodes\MMLmerror;
use MediaWiki\Extension\Math\TexVC\MMLnodes\MMLmi;
use MediaWiki\Extension\Math\TexVC\MMLnodes\MMLmo;
use MediaWiki\Extension\Math\TexVC\MMLnodes\MMLmrow;
use MediaWiki\Extension\Math\TexVC\MMLnodes\MMLmspace;
use MediaWiki\Extension\Math\TexVC\MMLnodes\MMLmstyle;
use MediaWiki\Extension\Math\TexVC\MMLnodes\MMLmtext;
use MediaWiki\Extension\Math\TexVC\Nodes\TexNode;
/**
* This contains the basic parsing methods for tex elements, which get invoked
@ -21,18 +23,18 @@ use MediaWiki\Extension\Math\TexVC\MMLnodes\MMLmtext;
*/
class BaseMethods {
public function checkAndParse( $input, $node, $passedArgs, $operatorContent ) {
public static function checkAndParse( $input, $passedArgs, $operatorContent, TexNode $node ) {
if ( !is_string( $input ) ) {
// just discard these elements, sometimes empty TexArray
return null;
}
$input = trim( $input );
$input = trim( $input );
if ( str_starts_with( $input, "\\" ) ) {
$input = substr( $input, 1 );
$input = substr( $input, 1 );
}
// Checking for a named parsing function
$resFct = BaseMappings::getMacroByKey( $input );
$resFct = BaseMappings::getMacroByKey( $input );
if ( $resFct == null ) {
$resFct = AMSMappings::getMacroByKey( $input );
if ( $resFct == null ) {
@ -61,11 +63,13 @@ class BaseMethods {
// Passing resolved function as param without first id
if ( count( $resFct ) > 1 ) {
$shifted = array_shift( $resFct );
return BaseParsing::{$shifted}( $node, $passedArgs, $operatorContent, $input, ...$resFct );
} else {
return BaseParsing::{$resFct[0]}( $node, $passedArgs, $operatorContent, $input );
return BaseParsing::{$resFct[0]}( $node, $passedArgs, $operatorContent, $input );
}
} catch ( \Exception $exception ) {
}
catch ( \Exception $exception ) {
return null;
}
}
@ -253,4 +257,10 @@ class BaseMethods {
}
}
}
public static function generateMMLError( $msg ): string {
return ( new MMLmerror() )->encapsulate(
( new MMLmtext() )->encapsulate( $msg )
);
}
}

View file

@ -4,8 +4,6 @@ declare( strict_types = 1 );
namespace MediaWiki\Extension\Math\TexVC\Nodes;
use MediaWiki\Extension\Math\TexVC\MMLmappings\BaseMethods;
class Big extends TexNode {
/** @var string */
@ -41,14 +39,8 @@ class Big extends TexNode {
return '{' . $this->fname . ' ' . $this->arg . '}';
}
public function renderMML( $arguments = [] ) {
$bm = new BaseMethods();
$res = $bm->checkAndParse( $this->fname, $this, $arguments, null );
if ( $res ) {
return $res;
} else {
return "Not Implemented Big for: " . $this->getArgs()[0];
}
public function renderMML( $arguments = [] ): string {
return $this->parseToMML( $this->fname, $arguments, null );
}
public function extractIdentifiers( $args = null ) {

View file

@ -4,8 +4,6 @@ declare( strict_types = 1 );
namespace MediaWiki\Extension\Math\TexVC\Nodes;
use MediaWiki\Extension\Math\TexVC\MMLmappings\BaseMethods;
class Box extends TexNode {
/** @var string */
@ -42,13 +40,7 @@ class Box extends TexNode {
}
public function renderMML( $arguments = [] ) {
$bm = new BaseMethods();
$res = $bm->checkAndParse( $this->getArgs()[0], $this, $arguments, null );
if ( $res ) {
return $res;
} else {
return "Not Implemented Box for: " . $this->getArgs()[0];
}
return $this->parseToMML( $this->getArgs()[0], $arguments, null );
/**
* $mrow = new MMLmrow();
* $mtext = new MMLmtext();

View file

@ -39,9 +39,7 @@ class DQ extends TexNode {
}
public function renderMML( $arguments = [] ) {
// Check if there is a specific parsing in BaseMethods
$bm = new BaseMethods();
$res = $bm->checkAndParse( $this->base->getArgs()[0], $this, $arguments, null );
$res = BaseMethods::checkAndParse( $this->base->getArgs()[0], $arguments, null, $this );
if ( $res ) {
return $res;
} else {

View file

@ -4,8 +4,6 @@ declare( strict_types = 1 );
namespace MediaWiki\Extension\Math\TexVC\Nodes;
use MediaWiki\Extension\Math\TexVC\MMLmappings\BaseMethods;
class Declh extends TexNode {
/** @var string */
@ -42,13 +40,7 @@ class Declh extends TexNode {
}
public function renderMML( $arguments = [] ) {
$bm = new BaseMethods();
$res = $bm->checkAndParse( $this->fname, $this, $arguments, null );
if ( $res ) {
return $res;
} else {
return "not implemented yet Declh parsing";
}
return $this->parseToMML( $this->fname, $arguments, null );
}
public function extractIdentifiers( $args = null ) {

View file

@ -61,12 +61,7 @@ class FQ extends TexNode {
// Not sure if this case is necessary ..
if ( is_string( $this->getArgs()[0] ) ) {
$res = $bm->checkAndParse( $this->getArgs()[0], $this, $arguments, null );
if ( $res ) {
return $res;
} else {
return "Not Implemented FQ for: " . $this->getArgs()[0];
}
return $this->parseToMML( $this->getArgs()[0], $arguments, null );
}
$melement = new MMLmsubsup();

View file

@ -4,7 +4,6 @@ declare( strict_types = 1 );
namespace MediaWiki\Extension\Math\TexVC\Nodes;
use MediaWiki\Extension\Math\TexVC\MMLmappings\BaseMethods;
use MediaWiki\Extension\Math\TexVC\MMLnodes\MMLmo;
use MediaWiki\Extension\Math\TexVC\MMLnodes\MMLmover;
use MediaWiki\Extension\Math\TexVC\MMLnodes\MMLmrow;
@ -49,13 +48,7 @@ class Fun1 extends TexNode {
}
public function renderMML( $arguments = [] ) {
$bm = new BaseMethods();
$res = $bm->checkAndParse( $this->fname, $this, $arguments, null );
if ( $res ) {
return $res;
} else {
return "Not Implemented Fun1 for: " . $this->fname;
}
return $this->parseToMML( $this->fname, $arguments, null );
}
public function createMover( $inner, $moArgs = [] ): string {

View file

@ -4,8 +4,6 @@ declare( strict_types = 1 );
namespace MediaWiki\Extension\Math\TexVC\Nodes;
use MediaWiki\Extension\Math\TexVC\MMLmappings\BaseMethods;
class Fun1nb extends Fun1 {
public function inCurlies() {
@ -16,14 +14,8 @@ class Fun1nb extends Fun1 {
return $this->fname . ' ' . $this->arg->inCurlies() . ' ';
}
public function renderMML( $arguments = [] ) {
$bm = new BaseMethods();
$res = $bm->checkAndParse( $this->fname, $this, $arguments, null );
if ( $res ) {
return $res;
} else {
return "Not Implemented Fun1nb for: " . $this->fname;
}
public function renderMML( $arguments = [] ): string {
return $this->parseToMML( $this->fname, $arguments, null );
// This is very preliminary and should most probably be synced with the mappings from time to time
// this might move to Fun1.php
/**

View file

@ -4,8 +4,6 @@ declare( strict_types = 1 );
namespace MediaWiki\Extension\Math\TexVC\Nodes;
use MediaWiki\Extension\Math\TexVC\MMLmappings\BaseMethods;
class Fun2 extends TexNode {
/** @var string */
@ -51,14 +49,8 @@ class Fun2 extends TexNode {
return '{' . $this->fname . ' ' . $this->arg1->inCurlies() . $this->arg2->inCurlies() . '}';
}
public function renderMML( $arguments = [] ) {
$bm = new BaseMethods();
$res = $bm->checkAndParse( $this->fname, $this, $arguments, null );
if ( $res ) {
return $res;
} else {
return "NotImplemented Fun2 for: " . $this->fname;
}
public function renderMML( $arguments = [] ): string {
return $this->parseToMML( $this->fname, $arguments, null );
}
public function extractIdentifiers( $args = null ) {

View file

@ -4,8 +4,6 @@ declare( strict_types = 1 );
namespace MediaWiki\Extension\Math\TexVC\Nodes;
use MediaWiki\Extension\Math\TexVC\MMLmappings\BaseMethods;
class Infix extends TexNode {
/** @var string */
@ -54,13 +52,7 @@ class Infix extends TexNode {
}
public function renderMML( $arguments = [] ) {
$bm = new BaseMethods();
$res = $bm->checkAndParse( $this->op, $this, $arguments, null );
if ( $res ) {
return $res;
} else {
return "Not Implemented Infix for: " . $this->getArgs()[0];
}
return $this->parseToMML( $this->op, $arguments, null );
}
public function extractIdentifiers( $args = null ) {

View file

@ -76,7 +76,7 @@ class Literal extends TexNode {
}
// Sieve for Makros
$ret = $bm->checkAndParse( $input, $this, $arguments, $operatorContent );
$ret = BaseMethods::checkAndParse( $input, $arguments, $operatorContent, $this );
if ( $ret ) {
return $ret;
}

View file

@ -5,7 +5,6 @@ declare( strict_types = 1 );
namespace MediaWiki\Extension\Math\TexVC\Nodes;
use InvalidArgumentException;
use MediaWiki\Extension\Math\TexVC\MMLmappings\BaseMethods;
class Matrix extends TexNode {
@ -63,14 +62,8 @@ class Matrix extends TexNode {
return '{\\begin{' . $this->top . '}' . $this->renderMatrix( $this->mainarg ) . '\\end{' . $this->top . '}}';
}
public function renderMML( $arguments = [] ) {
$bm = new BaseMethods();
$res = $bm->checkAndParse( $this->getTop(), $this, $arguments, null );
if ( $res ) {
return $res;
} else {
return "Not Implemented Matrix for: " . $this->getArgs()[0];
}
public function renderMML( $arguments = [] ): string {
return $this->parseToMML( $this->getTop(), $arguments, null );
}
private function renderMatrix( $matrix ) {

View file

@ -5,6 +5,7 @@ declare( strict_types = 1 );
namespace MediaWiki\Extension\Math\TexVC\Nodes;
use InvalidArgumentException;
use MediaWiki\Extension\Math\TexVC\MMLmappings\BaseMethods;
class TexNode {
@ -24,6 +25,16 @@ class TexNode {
$this->args = $args;
}
protected function parseToMML( $input, $passedArgs, $operatorContent ): string {
$parsed = BaseMethods::checkAndParse( $input, $passedArgs, $operatorContent, $this );
if ( $parsed ) {
return $parsed;
}
$name = strtoupper( get_class() );
return BaseMethods::generateMMLError( "Not implemented $name for $input" );
}
/**
* @return TexNode[]|string[]
*/

View file

@ -52,4 +52,9 @@ class BigTest extends MediaWikiUnitTestCase {
$big = new Big( '\\big', 'a' );
$this->assertEquals( '{\\big a}', $big->inCurlies(), 'Should create exactly one set of curlies' );
}
public function testRenderMML() {
$big = new Big( '\\big', 'a' );
$this->assertStringContainsString( '</mrow>', $big->renderMML(), 'Should render to MathML' );
}
}

View file

@ -52,4 +52,9 @@ class BoxTest extends MediaWikiUnitTestCase {
$box = new Box( '\\hbox', 'a' );
$this->assertEquals( '{\\hbox{a}}', $box->inCurlies(), 'Should create exactly one set of curlies' );
}
public function testRenderMML() {
$box = new Box( '\\hbox', 'a' );
$this->assertStringContainsString( '</mtext>', $box->renderMML(), 'Render MathML as text.' );
}
}

View file

@ -86,4 +86,9 @@ class DeclhTest extends MediaWikiUnitTestCase {
"Should extract subscripts for {$mod} font modification" );
}
}
public function testRenderMML() {
$f = new Declh( '\\bf', new TexArray( new Literal( 'a' ) ) );
$this->assertStringContainsString( 'mathvariant="bold"', $f->renderMML(), 'MathML should render bold' );
}
}