| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math\Tests\WikiTexVC\Nodes; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Extension\Math\WikiTexVC\Nodes\DQ; |
| 7 |
use MediaWiki\Extension\Math\WikiTexVC\Nodes\Fun1nb; |
| 8 |
use MediaWiki\Extension\Math\WikiTexVC\Nodes\Literal; |
| 9 |
use MediaWiki\Extension\Math\WikiTexVC\Nodes\TexArray; |
| 10 |
use MediaWiki\Extension\Math\WikiTexVC\Nodes\TexNode; |
| 11 |
use MediaWikiUnitTestCase; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class TexArrayTest extends MediaWikiUnitTestCase { |
| 17 |
|
| 18 |
public function testIsTexNode() { |
| 19 |
$this->assertTrue( new TexArray() instanceof TexNode, |
| 20 |
'Should create an instance of TexNode' ); |
| 21 |
} |
| 22 |
|
| 23 |
public function testConcatOutput() { |
| 24 |
$ta = new TexArray( new Literal( 'a' ), new Literal( 'b' ) ); |
| 25 |
$this->assertEquals( 'ab', $ta->render(), 'Should concatenate its input' ); |
| 26 |
} |
| 27 |
|
| 28 |
public function testExtractCurlies() { |
| 29 |
$n = new TexNode( new TexArray( new Literal( 'a' ) ) ); |
| 30 |
$this->assertEquals( '{a}', $n->inCurlies(), |
| 31 |
'Should create exactly one pair of curlies' ); |
| 32 |
} |
| 33 |
|
| 34 |
public function testExtractIdentifiers() { |
| 35 |
$n = new TexArray( new Literal( 'd' ) ); |
| 36 |
$this->assertEquals( [ 'd' ], $n->extractIdentifiers(), |
| 37 |
'Should extract identifiers' ); |
| 38 |
} |
| 39 |
|
| 40 |
public function testExtractIdentifiersFromArg() { |
| 41 |
$n = new TexArray(); |
| 42 |
$this->assertEquals( [ 'd' ], |
| 43 |
$n->extractIdentifiers( [ new Literal( 'd' ) ] ), |
| 44 |
'Should extract identifiers from the argument' ); |
| 45 |
} |
| 46 |
|
| 47 |
public function testExtractSplitIdentifiers() { |
| 48 |
$n = new TexArray( new Literal( 'a' ), new Literal( '\'' ) ); |
| 49 |
$this->assertEquals( [ 'a\'' ], $n->extractIdentifiers(), |
| 50 |
'Should extract split identifiers' ); |
| 51 |
} |
| 52 |
|
| 53 |
public function testNotConfuseIntegralsIdentifiers() { |
| 54 |
$n = new TexArray( new Literal( 'd' ), new Literal( '\\int' ) ); |
| 55 |
$this->assertEquals( [ 'd' ], $n->extractIdentifiers(), |
| 56 |
'Should not confuse integrals and identifiers' ); |
| 57 |
} |
| 58 |
|
| 59 |
public function testNotConfuseIntegralD() { |
| 60 |
$n = new TexArray( new Literal( '\\int' ), new Literal( 'd' ) ); |
| 61 |
$this->assertEquals( [], $n->extractIdentifiers(), 'Should not confuse integral d with d identifier' ); |
| 62 |
} |
| 63 |
|
| 64 |
public function testNotConfuseUprightIntegralD() { |
| 65 |
$n = new TexArray( new Literal( '\\int' ), new Literal( '\\mathrm{d}' ) ); |
| 66 |
$this->assertEquals( [], $n->extractIdentifiers(), |
| 67 |
'Should not confuse upright integral d with d identifier' ); |
| 68 |
} |
| 69 |
|
| 70 |
public function testExtractIdentifierMods() { |
| 71 |
$n = new TexArray( new TexNode( '' ) ); |
| 72 |
$this->assertEquals( [], $n->getModIdent(), 'Should extract identifier modifications' ); |
| 73 |
} |
| 74 |
|
| 75 |
public function testExtractSubscripts() { |
| 76 |
$n = new TexArray( new TexNode( '' ) ); |
| 77 |
$this->assertEquals( [], $n->extractSubscripts(), 'Should extract subscripts' ); |
| 78 |
} |
| 79 |
|
| 80 |
public function testUnshift() { |
| 81 |
$n = new TexArray( new TexNode( '' ) ); |
| 82 |
$n->unshift( 'test1', 'test2' ); |
| 83 |
$this->assertEquals( 3, $n->getLength(), 'Should unshift elements' ); |
| 84 |
} |
| 85 |
|
| 86 |
public function testGenerator() { |
| 87 |
$ta = new TexArray( new Literal( 'a' ), new Literal( 'b' ) ); |
| 88 |
foreach ( $ta as $item ) { |
| 89 |
$this->assertInstanceOf( |
| 90 |
'MediaWiki\Extension\Math\WikiTexVC\Nodes\Literal', |
| 91 |
$item, |
| 92 |
'Should iterate over the elements' ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
public function testOffsetExists() { |
| 97 |
$ta = new TexArray( new Literal( 'a' ), new Literal( 'b' ) ); |
| 98 |
$this->assertTrue( isset( $ta[0] ) ); |
| 99 |
$this->assertFalse( isset( $ta[2] ) ); |
| 100 |
} |
| 101 |
|
| 102 |
public function testOffsetGet() { |
| 103 |
$ta = new TexArray( new Literal( 'a' ), new Literal( 'b' ) ); |
| 104 |
$this->assertEquals( 'a', $ta[0]->render() ); |
| 105 |
$this->assertNull( $ta[100] ); |
| 106 |
} |
| 107 |
|
| 108 |
public function testOffsetUnset() { |
| 109 |
$ta = new TexArray( new Literal( 'a' ), new Literal( 'b' ) ); |
| 110 |
unset( $ta[0] ); |
| 111 |
$this->assertNull( $ta[0] ); |
| 112 |
} |
| 113 |
|
| 114 |
public function testOffsetSet() { |
| 115 |
$ta = new TexArray(); |
| 116 |
$ta[0] = new Literal( 'a' ); |
| 117 |
$this->assertEquals( 'a', $ta[0]->render() ); |
| 118 |
} |
| 119 |
|
| 120 |
public function testOffsetSetInvalid() { |
| 121 |
$this->expectException( InvalidArgumentException::class ); |
| 122 |
$ta = new TexArray(); |
| 123 |
$ta[0] = 'a'; |
| 124 |
} |
| 125 |
|
| 126 |
public function testSquashLiterals() { |
| 127 |
$ta = new TexArray( new Literal( 'a' ), new Literal( 'b' ) ); |
| 128 |
$state = [ 'squashLiterals' => true ]; |
| 129 |
$res = $ta->renderMML( [], $state ); |
| 130 |
$this->assertEquals( '<mi>ab</mi>', $res ); |
| 131 |
} |
| 132 |
|
| 133 |
public function testSquashLiteralsMacro() { |
| 134 |
$ta = new TexArray( new Literal( 'a' ), new Literal( '\gamma' ) ); |
| 135 |
$state = [ 'squashLiterals' => true ]; |
| 136 |
$res = $ta->renderMML( [], $state ); |
| 137 |
$this->assertEquals( '<mi>a</mi><mi>γ</mi>', $res ); |
| 138 |
} |
| 139 |
|
| 140 |
public function testSumInLimits() { |
| 141 |
$ta = new TexArray(); |
| 142 |
$sum = new Literal( '\sum' ); |
| 143 |
$res = $ta->checkForLimits( $sum, new DQ( new Literal( '\limits' ), new Literal( 'n' ) ) ); |
| 144 |
$this->assertTrue( $res[1] ); |
| 145 |
$this->assertEquals( $sum, $res[0] ); |
| 146 |
} |
| 147 |
|
| 148 |
public function testCustomOpInLimits() { |
| 149 |
$ta = new TexArray(); |
| 150 |
$custom = new Fun1nb( '\operatorname', new TexArray( new Literal( 'S' ) ) ); |
| 151 |
$res = $ta->checkForLimits( $custom, new DQ( new Literal( '\limits' ), new Literal( 'n' ) ) ); |
| 152 |
$this->assertTrue( $res[1] ); |
| 153 |
$this->assertEquals( $custom, $res[0] ); |
| 154 |
} |
| 155 |
|
| 156 |
public function testRenderADeriv() { |
| 157 |
$n = new TexArray( new Literal( 'A' ) ); |
| 158 |
$state = [ 'deriv' => 1 ]; |
| 159 |
$mml = $n->renderMML( [], $state ); |
| 160 |
$this->assertStringContainsString( '′</mo>', $mml ); |
| 161 |
} |
| 162 |
} |