| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math\Tests\WikiTexVC; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Math\WikiTexVC\SyntaxError; |
| 6 |
use MediaWiki\Extension\Math\WikiTexVC\TexVC; |
| 7 |
use MediaWikiUnitTestCase; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class ApiTest extends MediaWikiUnitTestCase { |
| 16 |
|
| 17 |
|
| 18 |
private $texVC; |
| 19 |
|
| 20 |
protected function setUp(): void { |
| 21 |
parent::setUp(); |
| 22 |
$this->texVC = new TexVC(); |
| 23 |
} |
| 24 |
|
| 25 |
public function provideTestCases() { |
| 26 |
$testCases = [ |
| 27 |
[ |
| 28 |
'in' => '\\newcommand{\\text{do evil things}}', |
| 29 |
'status' => 'F', |
| 30 |
'details' => '\\newcommand' |
| 31 |
], |
| 32 |
[ |
| 33 |
'in' => '\\sin\\left(\\frac12x\\right)', |
| 34 |
'output' => '\\sin \\left({\\frac {1}{2}}x\\right)' |
| 35 |
], |
| 36 |
[ |
| 37 |
'in' => '\\reals', |
| 38 |
'output' => '\mathbb {R} ', |
| 39 |
'ams_required' => true |
| 40 |
], |
| 41 |
[ |
| 42 |
'in' => '\\lbrack', |
| 43 |
'output' => '\\lbrack ' |
| 44 |
], |
| 45 |
[ |
| 46 |
'in' => '\\figureEightIntegral', |
| 47 |
'status' => 'F', |
| 48 |
'details' => '\\figureEightIntegral' |
| 49 |
], |
| 50 |
[ |
| 51 |
'in' => '\diamondsuit ' |
| 52 |
], |
| 53 |
[ |
| 54 |
'in' => '\\sinh x' |
| 55 |
], |
| 56 |
[ |
| 57 |
'in' => '\\begin{foo}\\end{foo}', |
| 58 |
'status' => 'F', |
| 59 |
'details' => '\\begin{foo}' |
| 60 |
], |
| 61 |
[ |
| 62 |
'in' => '\\hasOwnProperty', |
| 63 |
'status' => 'F', |
| 64 |
'details' => '\\hasOwnProperty' |
| 65 |
], |
| 66 |
[ |
| 67 |
'in' => '\\hline', |
| 68 |
'status' => 'S' |
| 69 |
], |
| 70 |
[ |
| 71 |
'in' => '\\begin{array}{c}\\hline a \\\\ \\hline\\hline b \\end{array}', |
| 72 |
'output' => '{\\begin{array}{c}\\hline a\\\\\\hline \\hline b\\end{array}}' |
| 73 |
], |
| 74 |
[ |
| 75 |
'in' => '\\Diamond ', |
| 76 |
'ams_required' => true |
| 77 |
], |
| 78 |
[ |
| 79 |
'in' => '{\\begin{matrix}a\\ b\\end{matrix}}', |
| 80 |
'ams_required' => true |
| 81 |
], |
| 82 |
[ |
| 83 |
'in' => '{\\cancel {x}}', |
| 84 |
'cancel_required' => true |
| 85 |
], |
| 86 |
[ |
| 87 |
'in' => '\\color {red}', |
| 88 |
'color_required' => true |
| 89 |
], |
| 90 |
[ |
| 91 |
'in' => '\\euro', |
| 92 |
'output' => '\\mbox{\\euro} ', |
| 93 |
'euro_required' => true |
| 94 |
], |
| 95 |
[ |
| 96 |
'in' => '\\coppa', |
| 97 |
'output' => '\\mbox{\\coppa} ', |
| 98 |
'teubner_required' => true |
| 99 |
], |
| 100 |
[ |
| 101 |
'in' => '{\\color [rgb]{1,0,0}{\\mbox{This text is red.}}}', |
| 102 |
'color_required' => true |
| 103 |
], |
| 104 |
[ |
| 105 |
'in' => '{\\color[rgb]{1.5,0,0}{\\mbox{This text is bright red.}}}', |
| 106 |
'status' => 'S' |
| 107 |
], |
| 108 |
[ |
| 109 |
'in' => '{\\color [RGB]{51,0,0}{\\mbox{This text is dim red.}}}', |
| 110 |
'output' => '{\\color [rgb]{0.2,0,0}{\\mbox{This text is dim red.}}}', |
| 111 |
'color_required' => true |
| 112 |
], |
| 113 |
[ |
| 114 |
'in' => '{\\color[RGB]{256,0,0}{\\mbox{This text is bright red.}}}', |
| 115 |
'status' => 'S' |
| 116 |
], |
| 117 |
[ |
| 118 |
'in' => '\\ce{ H2O }', |
| 119 |
'mhchem_required' => true, |
| 120 |
'status' => 'C' |
| 121 |
], |
| 122 |
[ |
| 123 |
'in' => '\\ce{[Zn(OH)4]^2-}', |
| 124 |
'mhchem_required' => true, |
| 125 |
'status' => 'C' |
| 126 |
], |
| 127 |
[ |
| 128 |
'in' => '{\\begin{aligned}a\\\\b\\end{aligned}}', |
| 129 |
'ams_required' => true, |
| 130 |
], |
| 131 |
[ |
| 132 |
'in' => '{\\begin{aligned}a\\\\[6pt] b\\end{aligned}}', |
| 133 |
'output' => '{\\begin{aligned}a\\\\[6pt]b\\end{aligned}}', |
| 134 |
'ams_required' => true, |
| 135 |
], |
| 136 |
[ |
| 137 |
'in' => '{\\begin{aligned}a\\\\[-3.4mm] b\\end{aligned}}', |
| 138 |
'output' => '{\\begin{aligned}a\\\\[-3.4mm]b\\end{aligned}}', |
| 139 |
'ams_required' => true, |
| 140 |
], |
| 141 |
[ |
| 142 |
'in' => '{\\begin{aligned}a\\\\[8mu] b\\end{aligned}}', |
| 143 |
'output' => '{\\begin{aligned}a\\\\[8mu]b\\end{aligned}}', |
| 144 |
'ams_required' => true, |
| 145 |
], |
| 146 |
[ |
| 147 |
'in' => '{\\begin{aligned}a\\\\[-2.563pt]b\\\\c\\\\[2em]d\\end{aligned}}', |
| 148 |
'ams_required' => true, |
| 149 |
], |
| 150 |
]; |
| 151 |
foreach ( $testCases as $case ) { |
| 152 |
yield $case['in'] => [ $case ]; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public function testDefinedCases( array $case ) { |
| 160 |
$result = $this->texVC->check( $case['in'], [ 'report_required' => true ] ); |
| 161 |
$resultStatus = $result['status'] ?? ''; |
| 162 |
$caseStatus = $case['status'] ?? '+'; |
| 163 |
$this->assertSame( $caseStatus, $resultStatus, 'Status incorrect' ); |
| 164 |
if ( $resultStatus === '+' ) { |
| 165 |
$this->assertSame( $case['output'] ?? $case['in'], $result['output'] ?? '', 'Output incorrect' ); |
| 166 |
foreach ( array_keys( $result ) as $f ) { |
| 167 |
if ( preg_match( '/_required/', $f ) ) { |
| 168 |
$this->assertSame( array_key_exists( $f, $case ), $result[$f], |
| 169 |
'A required field does not match in result ' . $f ); |
| 170 |
} |
| 171 |
$this->assertCount( 0, $result['warnings'], 'No warnings expected here.' ); |
| 172 |
} |
| 173 |
} |
| 174 |
if ( $resultStatus === 'F' ) { |
| 175 |
$this->assertSame( $case['details'], $result['details'] ?? '', 'Details incorrect' ); |
| 176 |
$this->assertCount( 0, $result['warnings'], 'No warnings expected here.' ); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
public function testSuccess1() { |
| 181 |
$message = 'should return success (1)'; |
| 182 |
$result = $this->texVC->check( '\\sin(x)+{}{}\\cos(x)^2 newcommand' ); |
| 183 |
$this->assertEquals( '+', $result['status'], $message ); |
| 184 |
$this->assertEquals( '\\sin(x)+{}{}\\cos(x)^{2}newcommand', $result['output'], $message ); |
| 185 |
} |
| 186 |
|
| 187 |
public function testSuccess2() { |
| 188 |
$message = 'should return success (2)'; |
| 189 |
$result = $this->texVC->check( 'y=x+2' ); |
| 190 |
$this->assertEquals( '+', $result['status'], $message ); |
| 191 |
$this->assertEquals( 'y=x+2', $result['output'], $message ); |
| 192 |
} |
| 193 |
|
| 194 |
public function testReport1() { |
| 195 |
$message = 'should report undefined functions (1)'; |
| 196 |
$result = $this->texVC->check( '\\foo' ); |
| 197 |
$this->assertEquals( 'F', $result['status'], $message ); |
| 198 |
$this->assertEquals( '\\foo', $result['details'], $message ); |
| 199 |
} |
| 200 |
|
| 201 |
public function testReport2() { |
| 202 |
$message = 'should report undefined functions (2)'; |
| 203 |
$result = $this->texVC->check( '\\write18' ); |
| 204 |
$this->assertEquals( 'F', $result['status'], $message ); |
| 205 |
$this->assertEquals( '\\write', $result['details'], $message ); |
| 206 |
} |
| 207 |
|
| 208 |
public function testUndefined() { |
| 209 |
$message = 'should report undefined parser errors'; |
| 210 |
$result = $this->texVC->check( '^' ); |
| 211 |
$this->assertEquals( 'S', $result['status'], $message ); |
| 212 |
} |
| 213 |
|
| 214 |
public function testDebugException() { |
| 215 |
|
| 216 |
$this->expectException( SyntaxError::class ); |
| 217 |
$this->texVC->check( '^', [ 'debug' => true ] ); |
| 218 |
} |
| 219 |
|
| 220 |
public function testParsedInput() { |
| 221 |
$message = 'should accept parsed input'; |
| 222 |
$parsed = $this->texVC->parse( 'y=x+2' ); |
| 223 |
$result = $this->texVC->check( $parsed ); |
| 224 |
$this->assertEquals( '+', $result['status'], $message ); |
| 225 |
$this->assertEquals( 'y=x+2', $result['output'], $message ); |
| 226 |
} |
| 227 |
|
| 228 |
public function testRetryParsing() { |
| 229 |
$message = 'should retry parsing if oldmhchem is not set'; |
| 230 |
$result = $this->texVC->check( '\\ce {A\\;+\\;B\\;->\\;C}', [ 'usemhchem' => true ] ); |
| 231 |
$this->assertEquals( '+', $result['status'], $message ); |
| 232 |
$this->assertEquals( 'mhchem-deprecation', $result['warnings'][0]['type'], $message ); |
| 233 |
$this->assertEquals( 'S', $result['warnings'][0]['details']['status'], $message ); |
| 234 |
} |
| 235 |
|
| 236 |
public function testDeprecationWarning() { |
| 237 |
|
| 238 |
$message = 'should show a deprecation warning for \\and'; |
| 239 |
$result = $this->texVC->check( '\\and' ); |
| 240 |
$this->assertEquals( '+', $result['status'], $message ); |
| 241 |
$this->assertEquals( 'texvc-deprecation', $result['warnings'][0]['type'], $message ); |
| 242 |
$this->assertEquals( 'S', $result['warnings'][0]['details']['status'], $message ); |
| 243 |
} |
| 244 |
|
| 245 |
public function testDeprecationWarning2() { |
| 246 |
$message = 'should not show a deprecation warning for \\land'; |
| 247 |
$result = $this->texVC->check( '\\land' ); |
| 248 |
$this->assertEquals( '+', $result['status'], $message ); |
| 249 |
$this->assertCount( 0, $result['warnings'], $message ); |
| 250 |
} |
| 251 |
|
| 252 |
public function testNoRetry() { |
| 253 |
$message = 'should not retry parsing if oldmhchem is set'; |
| 254 |
$result = $this->texVC->check( '\\ce {A\\;+\\;B\\;->\\notvalidcommand}', |
| 255 |
[ 'usemhchem' => true, 'oldmhchem' => true ] ); |
| 256 |
$this->assertEquals( 'F', $result['status'], $message ); |
| 257 |
} |
| 258 |
|
| 259 |
public function testSquareDq() { |
| 260 |
$result = $this->texVC->check( ']_x', |
| 261 |
[ 'usemhchem' => true, 'oldmhchem' => true ] ); |
| 262 |
$this->assertEquals( ']_{x}', $result['output'] ); |
| 263 |
} |
| 264 |
|
| 265 |
public function testSquareFq() { |
| 266 |
$result = $this->texVC->check( ']_x^2', |
| 267 |
[ 'usemhchem' => true, 'oldmhchem' => true ] ); |
| 268 |
$this->assertEquals( ']_{x}^{2}', $result['output'] ); |
| 269 |
} |
| 270 |
|
| 271 |
public function mhchemtexifiedTest() { |
| 272 |
$result = $this->texVC->check( '\\longleftrightarrows', |
| 273 |
[ 'usemhchemtexified' => true ] ); |
| 274 |
$this->assertEquals( '\\longleftrightarrows', $result['output'] ); |
| 275 |
} |
| 276 |
|
| 277 |
public function mhchemtexifiedTestFail() { |
| 278 |
$result = $this->texVC->check( '\\longleftrightarrows' ); |
| 279 |
$this->assertEquals( 'C', $result['status'] ); |
| 280 |
$this->assertFalse( $result['success'] ); |
| 281 |
} |
| 282 |
} |