| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math\Tests\WikiTexVC\Nodes; |
| 4 |
|
| 5 |
use ArgumentCountError; |
| 6 |
use InvalidArgumentException; |
| 7 |
use MediaWiki\Extension\Math\WikiTexVC\Nodes\Literal; |
| 8 |
use MediaWiki\Extension\Math\WikiTexVC\Nodes\Matrix; |
| 9 |
use MediaWiki\Extension\Math\WikiTexVC\Nodes\TexArray; |
| 10 |
use MediaWikiUnitTestCase; |
| 11 |
use TypeError; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class MatrixTest extends MediaWikiUnitTestCase { |
| 17 |
|
| 18 |
private $sampleMatrix; |
| 19 |
|
| 20 |
protected function setUp(): void { |
| 21 |
parent::setUp(); |
| 22 |
$this->sampleMatrix = new Matrix( 'align', |
| 23 |
new TexArray( new TexArray( new Literal( 'a' ) ) ) ); |
| 24 |
} |
| 25 |
|
| 26 |
public function testEmptyMatrix() { |
| 27 |
$this->expectException( ArgumentCountError::class ); |
| 28 |
new Matrix(); |
| 29 |
throw new TypeError( 'Should not create an empty matrix' ); |
| 30 |
} |
| 31 |
|
| 32 |
public function testNestedArguments() { |
| 33 |
$this->expectException( InvalidArgumentException::class ); |
| 34 |
new Matrix( |
| 35 |
'align', |
| 36 |
new TexArray( |
| 37 |
new Literal( 'a' ) ) ); |
| 38 |
throw new TypeError( 'Nested arguments have to be type of TexArray' ); |
| 39 |
} |
| 40 |
|
| 41 |
public function testInstanceOfTexArray() { |
| 42 |
$this->assertEquals( 'MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\TexArray', |
| 43 |
get_parent_class( $this->sampleMatrix ), |
| 44 |
'Should create an instance of TexArray' ); |
| 45 |
} |
| 46 |
|
| 47 |
public function testGetters() { |
| 48 |
$this->assertNotEmpty( $this->sampleMatrix->getTop() ); |
| 49 |
$this->assertNotEmpty( $this->sampleMatrix->getMainarg() ); |
| 50 |
} |
| 51 |
|
| 52 |
public function testRenderMatrix() { |
| 53 |
$this->assertEquals( '{\\begin{align}a\\end{align}}', $this->sampleMatrix->render() ); |
| 54 |
} |
| 55 |
|
| 56 |
public function testExtractCurlies() { |
| 57 |
$this->assertEquals( '{\\begin{align}a\\end{align}}', $this->sampleMatrix->inCurlies(), |
| 58 |
'Should not create extra curlies' ); |
| 59 |
} |
| 60 |
|
| 61 |
public function testExtractIdentifiers() { |
| 62 |
$this->assertEquals( [ 'a' ], $this->sampleMatrix->extractIdentifiers(), |
| 63 |
'Should extract identifiers' ); |
| 64 |
} |
| 65 |
|
| 66 |
public function testCreateFromMatrix() { |
| 67 |
$matrix = new Matrix( 'align', |
| 68 |
new TexArray( new TexArray( new Literal( 'a' ) ) ) ); |
| 69 |
$newMatrix = new Matrix( 'align', $matrix ); |
| 70 |
$this->assertEquals( $matrix, $newMatrix, |
| 71 |
'Should create a matrix from a matrix' ); |
| 72 |
} |
| 73 |
|
| 74 |
public function testContainsTop() { |
| 75 |
$matrix = new Matrix( 'top', |
| 76 |
new TexArray( new TexArray( new Literal( 'a' ) ) ) ); |
| 77 |
$this->assertTrue( $matrix->containsFunc( '\\begin{top}' ), |
| 78 |
'Should create top attribute' ); |
| 79 |
} |
| 80 |
|
| 81 |
public function testContainsArgs() { |
| 82 |
$matrix = new Matrix( 'top', |
| 83 |
new TexArray( new TexArray( new Literal( '\\sin' ) ) ) ); |
| 84 |
$this->assertTrue( $matrix->containsFunc( '\\sin' ), |
| 85 |
'Should contain inner elements' ); |
| 86 |
} |
| 87 |
|
| 88 |
public function testRenderMML() { |
| 89 |
$matrix = new Matrix( 'matrix', |
| 90 |
new TexArray( new TexArray( new Literal( '\\sin' ) ) ) ); |
| 91 |
$this->assertStringContainsString( 'mtable', $matrix->renderMML(), |
| 92 |
'Should render a matrix' ); |
| 93 |
} |
| 94 |
|
| 95 |
public function testTop() { |
| 96 |
$this->sampleMatrix->setTop( 'abc' ); |
| 97 |
$this->assertEquals( 'abc', $this->sampleMatrix->getTop() ); |
| 98 |
} |
| 99 |
|
| 100 |
public function testColSpec() { |
| 101 |
$this->sampleMatrix->setColumnSpecs( TexArray::newCurly( new Literal( '2' ) ) ); |
| 102 |
$this->assertSame( '2', $this->sampleMatrix->getRenderedColumnSpecs() ); |
| 103 |
} |
| 104 |
|
| 105 |
public function testAdvColSpec() { |
| 106 |
$this->sampleMatrix->setColumnSpecs( TexArray::newCurly( new Literal( '{r|l}' ) ) ); |
| 107 |
$this->assertSame( 'r|l', $this->sampleMatrix->getRenderedColumnSpecs() ); |
| 108 |
$this->assertEquals( 'right left ', $this->sampleMatrix->getAlignInfo() ); |
| 109 |
$this->assertEquals( [ 1 => true ], $this->sampleMatrix->getBoarder() ); |
| 110 |
} |
| 111 |
|
| 112 |
public function testGetLines() { |
| 113 |
$real = $this->sampleMatrix->getLines(); |
| 114 |
$this->assertNotEmpty( $real ); |
| 115 |
$this->assertFalse( $real[0] ); |
| 116 |
} |
| 117 |
|
| 118 |
public function testLinesBottom() { |
| 119 |
$matrix = new Matrix( 'matrix', |
| 120 |
new TexArray( new TexArray( new Literal( 'a' ) ), |
| 121 |
new TexArray( new TexArray( new Literal( '\\hline ' ) ) ) ) ); |
| 122 |
$real = $matrix->getLines(); |
| 123 |
$this->assertNotEmpty( $real ); |
| 124 |
$this->assertFalse( $real[0] ); |
| 125 |
$this->assertTrue( $real[1] ); |
| 126 |
$this->assertCount( 2, $real ); |
| 127 |
} |
| 128 |
|
| 129 |
public function testLinesTop() { |
| 130 |
$matrix = new Matrix( 'matrix', |
| 131 |
new TexArray( new TexArray( new TexArray( new Literal( '\\hline ' ), new Literal( 'a' |
| 132 |
) ) ) ) ); |
| 133 |
$real = $matrix->getLines(); |
| 134 |
$this->assertNotEmpty( $real ); |
| 135 |
$this->assertTrue( $real[0] ); |
| 136 |
$this->assertCount( 1, $real ); |
| 137 |
} |
| 138 |
|
| 139 |
public function testLinesLast() { |
| 140 |
$matrix = new Matrix( 'matrix', |
| 141 |
new TexArray( new TexArray( new Literal( 'a' ) ), |
| 142 |
new TexArray( new TexArray( new Literal( '\\hline ' ), new Literal( 'a' |
| 143 |
) ) ) ) ); |
| 144 |
$real = $matrix->getLines(); |
| 145 |
$this->assertNotEmpty( $real ); |
| 146 |
$this->assertFalse( $real[0] ); |
| 147 |
$this->assertTrue( $real[1] ); |
| 148 |
$this->assertCount( 2, $real ); |
| 149 |
} |
| 150 |
} |