mediawiki-extensions-Math/tests/phpunit/MathInputCheckRestbaseTest.php
Thiemo Kreuz b189280467 Make all @covers tags absolute, and fix some
Some mentioned a "__constructor", which should be "__construct".

The leading backslash is not strictly needed by all tools. But some
need it. I learned it's best practice to always have it.

Change-Id: I22fe3a3b4828c02ab5a457aba9af5af1c72567ea
2019-11-26 11:01:23 +01:00

97 lines
3.1 KiB
PHP

<?php
/**
* @group Math
*
* @license GPL-2.0-or-later
*/
class MathInputCheckRestbaseTest extends MediaWikiTestCase {
protected static $hasRestbase;
/** @var MathInputCheckRestbase */
protected $BadObject;
/** @var MathInputCheckRestbase */
protected $GoodObject;
public static function setUpBeforeClass() : void {
$rbi = new MathRestbaseInterface();
self::$hasRestbase = $rbi->checkBackend( true );
}
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp() : void {
parent::setUp();
if ( !self::$hasRestbase ) {
$this->markTestSkipped( "Can not connect to Restbase Math interface." );
}
$this->BadObject = new MathInputCheckRestbase( '\newcommand{\text{do evil things}}' );
$this->GoodObject = new MathInputCheckRestbase( '\sin\left(\frac12x\right)' );
}
/**
* @covers \MathInputCheckRestbase::getError
*/
public function testGetError() {
$this->assertNull( $this->GoodObject->getError() );
$this->assertNull( $this->BadObject->getError() );
$this->BadObject->isValid();
$this->GoodObject->isValid();
$this->assertNull( $this->GoodObject->getError() );
$expectedMessage = wfMessage(
'math_unknown_function', '\newcommand'
)->inContentLanguage()->escaped();
$this->assertContains( $expectedMessage, $this->BadObject->getError() );
}
/**
* @covers \MathInputCheckRestbase::getError
*/
public function testErrorSyntax() {
$o = new MathInputCheckRestbase( '\left(' );
$this->assertFalse( $o->isValid() );
$expectedMessage = wfMessage( 'math_syntax_error' )->inContentLanguage()->escaped();
$this->assertContains( $expectedMessage, $o->getError() );
}
/**
* @covers \MathInputCheckRestbase::getError
*/
public function testErrorLexing() {
$o = new MathInputCheckRestbase( "\x61\xCC\x81" );
$this->assertFalse( $o->isValid() );
// Lexical errors are no longer supported. The new error message
// Expected "-", "[", "\\\\",
// "\\\\begin", "\\\\begin{", "]", "^", "_", "{", [ \\t\\n\\r], [%$], [().], [,:;?!\\\'],
// [-+*=], [0-9], [><~], [\\/|] or [a-zA-Z] but "\\u0301" found.
// is more expressive anyhow.
$expectedMessage = wfMessage( 'math_syntax_error' )->inContentLanguage()->escaped();
$this->assertContains( $expectedMessage, $o->getError() );
}
/**
* @covers \MathInputCheckRestbase::isValid
*/
public function testIsValid() {
$this->assertFalse( $this->BadObject->isValid() );
$this->assertTrue( $this->GoodObject->isValid() );
}
/**
* @covers \MathInputCheckRestbase::getValidTex
*/
public function testGetValidTex() {
$this->assertNull( $this->GoodObject->getValidTex() );
$this->assertNull( $this->BadObject->getValidTex() );
$this->BadObject->isValid();
$this->GoodObject->isValid();
$this->assertNull( $this->BadObject->getValidTex() );
// Note that texvcjs has slightly diverged from texvc and enforces brackets for function
// arguments. Also the double space between frac and the arg has ben reduce to a single space.
$this->assertEquals( $this->GoodObject->getValidTex(), '\\sin \\left({\\frac {1}{2}}x\\right)' );
}
}