| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math\Tests\WikiTexVC\Nodes; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Extension\Math\WikiTexVC\Nodes\TexArray; |
| 7 |
use MediaWiki\Extension\Math\WikiTexVC\Nodes\TexNode; |
| 8 |
use MediaWikiUnitTestCase; |
| 9 |
use RuntimeException; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class TexNodeTest extends MediaWikiUnitTestCase { |
| 15 |
|
| 16 |
public static function provideTexToRender() { |
| 17 |
return [ |
| 18 |
[ [], '' ], |
| 19 |
[ [ '' ], '' ], |
| 20 |
[ [ 'hello', ' ', 'world' ], 'hello world' ], |
| 21 |
[ [ 'hello', new TexNode( ' ' ), new TexNode( new TexNode( 'world' ) ) ], 'hello world' ], |
| 22 |
]; |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public function testRender( array $args, string $expected ) { |
| 29 |
$n = new TexNode( ...$args ); |
| 30 |
$this->assertSame( $expected, $n->render() ); |
| 31 |
} |
| 32 |
|
| 33 |
public function testIntegerArgs() { |
| 34 |
$this->expectException( InvalidArgumentException::class ); |
| 35 |
( new TexNode( 1 ) )->render(); |
| 36 |
throw new RuntimeException( 'Should not accept integers as arguments' ); |
| 37 |
} |
| 38 |
|
| 39 |
public static function provideTexWithoutCurlies() { |
| 40 |
return [ |
| 41 |
[ 'a', '{a}' ], |
| 42 |
[ new TexNode( 'a' ), '{a}' ], |
| 43 |
[ '', '{}' ], |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function testInCurlies( $arg, string $expected ) { |
| 51 |
$n = new TexNode( $arg ); |
| 52 |
$this->assertSame( $expected, $n->inCurlies() ); |
| 53 |
} |
| 54 |
|
| 55 |
public function testExtractIdentifiers() { |
| 56 |
$n = new TexNode( new TexNode( 'a' ) ); |
| 57 |
$this->assertEquals( [ 'a' ], $n->extractIdentifiers(), 'Should extract identifiers' ); |
| 58 |
} |
| 59 |
|
| 60 |
public function testGetters() { |
| 61 |
$n = new TexNode( new TexNode( 'a' ) ); |
| 62 |
$this->assertNotEmpty( $n->getArgs() ); |
| 63 |
} |
| 64 |
|
| 65 |
public function testIdentiferMods() { |
| 66 |
$n = new TexNode( '' ); |
| 67 |
$this->assertEquals( [], $n->getModIdent(), |
| 68 |
'Should contain a method stub for extracting identifier modifications' ); |
| 69 |
} |
| 70 |
|
| 71 |
public function testExtractSubscripts() { |
| 72 |
$n = new TexNode( '' ); |
| 73 |
$this->assertEquals( [], $n->extractSubscripts(), |
| 74 |
'Should contain a method stub for extracting subscripts' ); |
| 75 |
} |
| 76 |
|
| 77 |
public static function provideNegativeMatches() { |
| 78 |
return [ |
| 79 |
[ 'asd', 'sda' ], |
| 80 |
[ [ 'asd', 'ert' ], 'sda' ], |
| 81 |
[ [ 0 => 'not a string key' ], '0' ], |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function testMatchFails( $target, string $str ) { |
| 89 |
$this->assertFalse( TexNode::match( $target, $str ) ); |
| 90 |
} |
| 91 |
|
| 92 |
public static function providePositiveMatches() { |
| 93 |
return [ |
| 94 |
[ '', '' ], |
| 95 |
[ 'asd', 'asd' ], |
| 96 |
[ [ 'ert', 'asd' ], 'asd' ], |
| 97 |
[ [ 'asd' => 'key should match' ], 'asd' ], |
| 98 |
[ '0', '0' ], |
| 99 |
[ [ '0' ], '0' ], |
| 100 |
[ [ [ '0' ] ], '0' ], |
| 101 |
]; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public function testMatchSucceeds( $target, string $str ) { |
| 108 |
$this->assertSame( $str, TexNode::match( $target, $str ) ); |
| 109 |
} |
| 110 |
|
| 111 |
public static function provideTextContainingFunctions() { |
| 112 |
return [ |
| 113 |
[ '', '', false ], |
| 114 |
[ '\\', '\\' ], |
| 115 |
[ 'bad input', 'bad input', false ], |
| 116 |
[ '\\operatorname', '\\mismatch', false ], |
| 117 |
[ '\\operatorname', '\\operatorname' ], |
| 118 |
[ '\\operatorname', '\\operatorname {}' ], |
| 119 |
[ '\\operatorname', '\\operatorname {someword}' ], |
| 120 |
[ '\\operatorname', '\\operatorname {someword}(' ], |
| 121 |
[ '\\operatorname', '\\operatorname {someword}[' ], |
| 122 |
[ '\\operatorname', '\\operatorname {someword}{', false ], |
| 123 |
[ '\\operatorname', '\\operatorname {someword}\\{' ], |
| 124 |
[ '\\operatorname', '\\operatorname {someword} ' ], |
| 125 |
[ '\\operatorname', '\\operatorname {someword} ', false ], |
| 126 |
[ '\\operatorname', '\\operatorname {\\someword}', false ], |
| 127 |
[ '\\operatorname', '\\operatorname{someword}', false ], |
| 128 |
[ |
| 129 |
[ '\\operatorname', '\\nonexistingooperator' ], |
| 130 |
'\\operatorname {someword}', |
| 131 |
'\\operatorname' |
| 132 |
], |
| 133 |
[ '\\mbox', '\\mbox{}', false ], |
| 134 |
[ '\\mbox', '\\mbox{foo}', false ], |
| 135 |
[ '\\mbox', '\\mbox{\\}' ], |
| 136 |
[ '\\mbox', '\\mbox{\\somefunc}' ], |
| 137 |
[ '\\somefunc', '\\mbox{\\somefunc}' ], |
| 138 |
[ '\\mismatch', '\\mbox{\\somefunc}', false ], |
| 139 |
[ '\\somefunc', '\\mbox {\\somefunc}', false ], |
| 140 |
[ '\\color', '\\color' ], |
| 141 |
[ '\\color', '\\color the rest is ignored' ], |
| 142 |
[ '\\pagecolor', '\\pagecolor' ], |
| 143 |
[ '\\definecolor', '\\definecolor' ], |
| 144 |
[ '\\mathbb', '\\mathbb {}', false ], |
| 145 |
[ '\\mathbb', '\\mathbb {A}', false ], |
| 146 |
[ '\\mathbb', '\\mathbb {foo}', false ], |
| 147 |
[ '\\mathbb', '\\mathbb{}', false ], |
| 148 |
[ '\\mathbb', '\\mathbb {.}', false ], |
| 149 |
[ '\\mathbb', '\\mathbb {..........}', false ], |
| 150 |
[ '\\mathbb', '\\mathbb {\\foo}' ], |
| 151 |
[ '\\foo', '\\mathbb {\\foo}' ], |
| 152 |
]; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public function testContainsFunc( $target, string $t, $expected = null ) { |
| 159 |
$this->assertSame( $expected ?? $target, TexNode::texContainsFunc( $target, $t ) ); |
| 160 |
} |
| 161 |
|
| 162 |
public function testIsEmptyString() { |
| 163 |
$this->assertTrue( ( new TexNode( '', '' ) )->isEmpty() ); |
| 164 |
$this->assertFalse( ( new TexNode( '', 'a' ) )->isEmpty() ); |
| 165 |
$this->assertTrue( ( new TexNode() )->isEmpty() ); |
| 166 |
} |
| 167 |
|
| 168 |
public function testIsEmptyObject() { |
| 169 |
$this->assertTrue( ( new TexNode( new TexNode() ) )->isEmpty() ); |
| 170 |
$this->assertFalse( ( new TexNode( new TexNode( 'a' ) ) )->isEmpty() ); |
| 171 |
$this->assertTrue( ( new TexNode( new TexArray() ) )->isEmpty() ); |
| 172 |
$this->assertTrue( ( new TexNode( new TexArray( new TexNode( '' ) ) ) )->isEmpty() ); |
| 173 |
} |
| 174 |
|
| 175 |
} |