| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math\Tests\WikiTexVC\Nodes; |
| 4 |
|
| 5 |
use ArgumentCountError; |
| 6 |
use MediaWiki\Extension\Math\WikiTexVC\Nodes\DQ; |
| 7 |
use MediaWiki\Extension\Math\WikiTexVC\Nodes\FQ; |
| 8 |
use MediaWiki\Extension\Math\WikiTexVC\Nodes\Fun1; |
| 9 |
use MediaWiki\Extension\Math\WikiTexVC\Nodes\Literal; |
| 10 |
use MediaWiki\Extension\Math\WikiTexVC\Nodes\TexArray; |
| 11 |
use MediaWikiUnitTestCase; |
| 12 |
use RuntimeException; |
| 13 |
use TypeError; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class Fun1Test extends MediaWikiUnitTestCase { |
| 19 |
|
| 20 |
public function testEmptyFun1() { |
| 21 |
$this->expectException( ArgumentCountError::class ); |
| 22 |
new Fun1(); |
| 23 |
throw new ArgumentCountError( 'Should not create an empty fun1' ); |
| 24 |
} |
| 25 |
|
| 26 |
public function testOneArgumentFun1() { |
| 27 |
$this->expectException( ArgumentCountError::class ); |
| 28 |
new Fun1( '\\f' ); |
| 29 |
throw new ArgumentCountError( 'Should not create a fun1 with one argument' ); |
| 30 |
} |
| 31 |
|
| 32 |
public function testIncorrectTypeFun1() { |
| 33 |
$this->expectException( TypeError::class ); |
| 34 |
new Fun1( '\\f', 'x' ); |
| 35 |
throw new RuntimeException( 'Should not create a fun1 with incorrect type' ); |
| 36 |
} |
| 37 |
|
| 38 |
public function testBasicFunctionFun1() { |
| 39 |
$f = new Fun1( '\\f', new Literal( 'a' ) ); |
| 40 |
$this->assertEquals( '{\\f {a}}', $f->render(), |
| 41 |
'Should create a basic function' ); |
| 42 |
} |
| 43 |
|
| 44 |
public function testCurliesFun1() { |
| 45 |
$f = new Fun1( '\\f', new Literal( 'a' ) ); |
| 46 |
$this->assertEquals( '{\\f {a}}', $f->inCurlies(), |
| 47 |
'Should create exactly one set of curlies' ); |
| 48 |
} |
| 49 |
|
| 50 |
public function testExtractIdentifiersFun1() { |
| 51 |
$f = new Fun1( '\\mathbf', new Literal( 'B' ) ); |
| 52 |
$this->assertEquals( [ '\\mathbf{B}' ], $f->extractIdentifiers(), |
| 53 |
'Should extract identifiers' ); |
| 54 |
} |
| 55 |
|
| 56 |
public function testExtractExtendedLiteralsFun1() { |
| 57 |
$f = new Fun1( '\\mathbf', new Literal( '\\infty' ) ); |
| 58 |
$this->assertEquals( [], $f->extractIdentifiers(), |
| 59 |
'Should not extract extended literals as identifiers.' ); |
| 60 |
} |
| 61 |
|
| 62 |
public function testGetters() { |
| 63 |
$f = new Fun1( '\\mathbf', new Literal( '\\infty' ) ); |
| 64 |
$this->assertNotEmpty( $f->getFname() ); |
| 65 |
$this->assertNotEmpty( $f->getArg() ); |
| 66 |
} |
| 67 |
|
| 68 |
public function testExtractPhantomIdentifiers() { |
| 69 |
$f = new Fun1( '\\hphantom', new Literal( 'A' ) ); |
| 70 |
$this->assertEquals( [], $f->extractIdentifiers(), |
| 71 |
'Should not extract phantom identifiers.' ); |
| 72 |
} |
| 73 |
|
| 74 |
public function testIgnoreUnknownFunctions() { |
| 75 |
$f = new Fun1( '\\unknown', new Literal( 'A' ) ); |
| 76 |
$this->assertEquals( [ 'A' ], $f->extractIdentifiers(), |
| 77 |
'Should ignore unknown functions.' ); |
| 78 |
} |
| 79 |
|
| 80 |
public function testExtractIdentifierMods() { |
| 81 |
$f = new Fun1( '\\mathbf', new Literal( 'B' ) ); |
| 82 |
$this->assertEquals( [ '\\mathbf{B}' ], $f->getModIdent(), |
| 83 |
'Should extract identifier modifications.' ); |
| 84 |
} |
| 85 |
|
| 86 |
public function testExtractSubscripts() { |
| 87 |
$f = new Fun1( '\\mathbf', new Literal( 'B' ) ); |
| 88 |
$this->assertEquals( [ '\\mathbf{B}' ], |
| 89 |
$f->extractSubscripts(), 'Should extract subscripts.' ); |
| 90 |
} |
| 91 |
|
| 92 |
public function testExtractSubscriptsExtendedLits() { |
| 93 |
$f = new Fun1( '\\mathbf', new Literal( '\\infty' ) ); |
| 94 |
$this->assertEquals( [ '\\mathbf{\\infty}' ], $f->extractSubscripts(), |
| 95 |
'Should extract subscripts for extended literals.' ); |
| 96 |
} |
| 97 |
|
| 98 |
public function testExtractSubscriptsEmptyMods() { |
| 99 |
$f = new Fun1( '\\mathbf', new Literal( '' ) ); |
| 100 |
$this->assertEquals( [], $f->extractSubscripts(), |
| 101 |
'Should not extract subscripts for empty mods.' ); |
| 102 |
} |
| 103 |
|
| 104 |
public function testMathRmLiteral() { |
| 105 |
$f = new Fun1( '\\mathrm', new Literal( 'b' ) ); |
| 106 |
$this->assertStringContainsString( 'mathvariant="normal"', $f->renderMML() ); |
| 107 |
} |
| 108 |
|
| 109 |
public function testMathRmCurly() { |
| 110 |
$f = new Fun1( '\\mathrm', TexArray::newCurly( |
| 111 |
new Literal( 'a' ), |
| 112 |
new Literal( 'b' ), |
| 113 |
) ); |
| 114 |
$rendering = $f->renderMML(); |
| 115 |
preg_match_all( '/mathvariant="normal"/', $rendering, $matches ); |
| 116 |
$this->assertCount( 2, $matches[0] ); |
| 117 |
} |
| 118 |
|
| 119 |
public function testMathRmDq() { |
| 120 |
$f = new Fun1( '\\mathrm', TexArray::newCurly( |
| 121 |
new DQ( new Literal( 'a' ), new Literal( 'b' ) ) |
| 122 |
) ); |
| 123 |
$rendering = $f->renderMML(); |
| 124 |
preg_match_all( '/mathvariant="normal"/', $rendering, $matches ); |
| 125 |
$this->assertCount( 2, $matches[0] ); |
| 126 |
} |
| 127 |
|
| 128 |
public function testMathRmFq() { |
| 129 |
$f = new Fun1( '\\mathrm', TexArray::newCurly( |
| 130 |
new FQ( new Literal( 'a' ), new Literal( 'b' ), new Literal( 'c' ) ) |
| 131 |
) ); |
| 132 |
$rendering = $f->renderMML(); |
| 133 |
preg_match_all( '/mathvariant="normal"/', $rendering, $matches ); |
| 134 |
$this->assertCount( 2, $matches[0] ); |
| 135 |
} |
| 136 |
} |