| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math\WikiTexVC\Mhchem; |
| 4 |
|
| 5 |
use MediaWikiUnitTestCase; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
final class MhchemBasicTest extends MediaWikiUnitTestCase { |
| 14 |
|
| 15 |
public function testZk() { |
| 16 |
|
| 17 |
$input = "\ce{{Zk_{c} e^2 m_{e}}}"; |
| 18 |
$mhchemParser = new MhchemParser(); |
| 19 |
$out = $mhchemParser->toTex( $input, "ce" ); |
| 20 |
$this->assertEquals( "{{\mathrm{Zk_{c}~e^2 m_{e}}}}", $out ); |
| 21 |
} |
| 22 |
|
| 23 |
public function testTextPattern() { |
| 24 |
|
| 25 |
$input = "K^\\text{b, 2}"; |
| 26 |
$mhchemParser = new MhchemParser(); |
| 27 |
$out = $mhchemParser->toTex( $input, "ce" ); |
| 28 |
$this->assertEquals( "{\mathrm{K}{\\vphantom{A}}^{\\text{b, 2}}}", $out ); |
| 29 |
} |
| 30 |
|
| 31 |
public function testFormulaDollarPattern() { |
| 32 |
$input = "\ce{H_{n + 2}}"; |
| 33 |
$mhchemParser = new MhchemParser(); |
| 34 |
$out = $mhchemParser->toTex( $input, "ce" ); |
| 35 |
$this->assertEquals( "{\mathrm{H}{\\vphantom{A}}_{\smash[t]{n + 2}}}", $out ); |
| 36 |
} |
| 37 |
|
| 38 |
public function testPhantom() { |
| 39 |
$input = "\ce{-C-O-}"; |
| 40 |
$mhchemParser = new MhchemParser(); |
| 41 |
$out = $mhchemParser->toTex( $input, "ce" ); |
| 42 |
$this->assertEquals( "{{-}\mathrm{C}{-}\mathrm{O}{\\vphantom{A}}^{-}}", $out ); |
| 43 |
} |
| 44 |
|
| 45 |
public function testH20withPU() { |
| 46 |
$input = "H2O (\pu{1g})"; |
| 47 |
$mhchemParser = new MhchemParser(); |
| 48 |
$out = $mhchemParser->toTex( $input, "ce" ); |
| 49 |
$this->assertEquals( "{\mathrm{H}{\\vphantom{A}}_{\smash[t]{2}}\mathrm{O}~({1~\mathrm{g}})}", $out ); |
| 50 |
} |
| 51 |
|
| 52 |
public function testPUt1010() { |
| 53 |
$input = "10^-10 m"; |
| 54 |
$mhchemParser = new MhchemParser(); |
| 55 |
$out = $mhchemParser->toTex( $input, "pu" ); |
| 56 |
$this->assertEquals( "{10^{-10}~\mathrm{m}}", $out ); |
| 57 |
} |
| 58 |
|
| 59 |
public function testPUtimes() { |
| 60 |
$input = "7.8 \\times 10^-10 m"; |
| 61 |
$mhchemParser = new MhchemParser(); |
| 62 |
$out = $mhchemParser->toTex( $input, "pu" ); |
| 63 |
$this->assertEquals( "{7.8\\times 10^{-10}~\mathrm{m}}", $out ); |
| 64 |
} |
| 65 |
|
| 66 |
public function testPUE10() { |
| 67 |
$input = "E10"; |
| 68 |
$mhchemParser = new MhchemParser(); |
| 69 |
$out = $mhchemParser->toTex( $input, "pu" ); |
| 70 |
$this->assertEquals( "{10^{10}}", $out ); |
| 71 |
} |
| 72 |
|
| 73 |
public function testXParenthesis() { |
| 74 |
$input = "\${x}$"; |
| 75 |
$mhchemParser = new MhchemParser(); |
| 76 |
$out = $mhchemParser->toTex( $input, "ce" ); |
| 77 |
$this->assertEquals( "{x }", $out ); |
| 78 |
} |
| 79 |
|
| 80 |
public function testH2OAmount() { |
| 81 |
$input = "\$n$/2 H2O"; |
| 82 |
$mhchemParser = new MhchemParser(); |
| 83 |
$out = $mhchemParser->toTex( $input, "ce" ); |
| 84 |
$this->assertEquals( |
| 85 |
"{\mathchoice{\\textstyle\\frac{n}{2}}{\\frac{n}{2}}{\\frac{n}{2}}{\\frac{n}{2}}\,\mathrm{H}" . |
| 86 |
"{\\vphantom{A}}_{\smash[t]{2}}\mathrm{O}}", $out ); |
| 87 |
} |
| 88 |
|
| 89 |
public function testH1() { |
| 90 |
$input = "H^°"; |
| 91 |
$mhchemParser = new MhchemParser(); |
| 92 |
$out = $mhchemParser->toTex( $input, "ce" ); |
| 93 |
$this->assertEquals( "{\mathrm{H}{\\vphantom{A}}^{°}}", $out ); |
| 94 |
} |
| 95 |
|
| 96 |
public function testDollar1() { |
| 97 |
$input = "A $\pm$ B"; |
| 98 |
$mhchemParser = new MhchemParser(); |
| 99 |
$out = $mhchemParser->toTex( $input, "ce" ); |
| 100 |
$this->assertEquals( "{\mathrm{A} {}\pm{} \mathrm{B}}", $out ); |
| 101 |
} |
| 102 |
|
| 103 |
public function testDollar2() { |
| 104 |
$input = "1/2\$n\$ H2O"; |
| 105 |
$mhchemParser = new MhchemParser(); |
| 106 |
$out = $mhchemParser->toTex( $input, "ce" ); |
| 107 |
$this->assertEquals( |
| 108 |
"{\mathchoice{\\textstyle\\frac{1}{2}}{\\frac{1}{2}}{\\frac{1}{2}}" . |
| 109 |
"{\\frac{1}{2}}n \,\mathrm{H}{\\vphantom{A}}_{\smash[t]{2}}\\mathrm{O}}", $out ); |
| 110 |
} |
| 111 |
|
| 112 |
public function testApattern() { |
| 113 |
$input = "^0_-1n-"; |
| 114 |
$pattern = "/^\^([0-9]+|[^\\_])/"; |
| 115 |
|
| 116 |
$matches = []; |
| 117 |
$match = preg_match( $pattern, $input, $matches ); |
| 118 |
|
| 119 |
$mhchemPatterns = new MhchemPatterns(); |
| 120 |
$matchesR = $mhchemPatterns->match( "^a", $input ); |
| 121 |
|
| 122 |
$this->assertSame( 1, $match ); |
| 123 |
$this->assertSame( "0", $matchesR["match_"] ); |
| 124 |
$this->assertEquals( "_-1n-", $matchesR["remainder"] ); |
| 125 |
} |
| 126 |
|
| 127 |
public function testLettersPattern() { |
| 128 |
$input = "mu-Cl"; |
| 129 |
$pattern = '/^(?:[a-zA-Z\x{03B1}-\x{03C9}\x{0391}-\x{03A9}?@]|(?:\\\\(?:alpha|beta|gamma|delta|epsilon|zeta|' . |
| 130 |
'eta|theta|iota|kappa|lambda|mu|nu|xi|omicron|pi|rho|sigma|tau|upsilon|phi|chi|psi|omega|Gamma' . |
| 131 |
'|Delta|Theta|Lambda|Xi|Pi|Sigma|Upsilon|Phi|Psi|Omega)(?:\s+|\{\}|(?![a-zA-Z]))))+/u'; |
| 132 |
$matches = []; |
| 133 |
$match = preg_match( $pattern, $input, $matches ); |
| 134 |
$this->assertTrue( $match == 1 ); |
| 135 |
} |
| 136 |
|
| 137 |
public function testStateOfAggregationPattern() { |
| 138 |
$input = "(\\ca\$c\$)"; |
| 139 |
$match = preg_match( '/^(?:\((?:\\\\ca\s?)?\$[amothc]\$\))/', $input ); |
| 140 |
$this->assertTrue( $match == 1 ); |
| 141 |
} |
| 142 |
|
| 143 |
public function testCelsiusPattern() { |
| 144 |
$input = "°C"; |
| 145 |
$output = preg_replace( "/\x{00B0}C|\^oC|\^{o}C/u", "{}^{\\circ}C", $input ); |
| 146 |
$target = "{}^{\circ}C"; |
| 147 |
$this->assertEquals( $target, $output ); |
| 148 |
} |
| 149 |
|
| 150 |
public function testPatternsMatchObsGroups() { |
| 151 |
$mhchemPatterns = new MhchemPatterns(); |
| 152 |
$a = $mhchemPatterns->findObserveGroups( "(aq)", "", |
| 153 |
new MhchemRegExp( '/^\\([a-z]{1,3}(?=[\\),])/' ), ")", "" ); |
| 154 |
$target = [ |
| 155 |
"match_" => "(aq)", |
| 156 |
"remainder" => "" |
| 157 |
]; |
| 158 |
$this->assertEquals( $target, $a ); |
| 159 |
} |
| 160 |
|
| 161 |
public function testIssetJS() { |
| 162 |
$this->assertFalse( MhchemUtil::issetJS( "" ) ); |
| 163 |
$this->assertFalse( MhchemUtil::issetJS( null ) ); |
| 164 |
$this->assertFalse( MhchemUtil::issetJS( false ) ); |
| 165 |
$this->assertFalse( MhchemUtil::issetJS( 0 ) ); |
| 166 |
|
| 167 |
|
| 168 |
$this->assertTrue( MhchemUtil::issetJS( new \stdClass( "" ) ) ); |
| 169 |
$this->assertTrue( MhchemUtil::issetJS( "abc" ) ); |
| 170 |
$this->assertTrue( MhchemUtil::issetJS( "false" ) ); |
| 171 |
$this->assertTrue( MhchemUtil::issetJS( [] ) ); |
| 172 |
$this->assertTrue( MhchemUtil::issetJS( "0" ) ); |
| 173 |
$this->assertTrue( MhchemUtil::issetJS( 123 ) ); |
| 174 |
|
| 175 |
|
| 176 |
$test1 = [ "b" => 123 ]; |
| 177 |
$this->assertFalse( MhchemUtil::issetJS( $test1["a"] ?? null ) ); |
| 178 |
$this->assertTrue( MhchemUtil::issetJS( $test1["b"] ) ); |
| 179 |
} |
| 180 |
|
| 181 |
public function testTransitionsInitTex() { |
| 182 |
$empty = [ |
| 183 |
"pattern" => "empty", |
| 184 |
"task" => [ |
| 185 |
"action_" => [ |
| 186 |
[ "type_" => "copy" ] |
| 187 |
], |
| 188 |
"stateArray" => [ |
| 189 |
"0" |
| 190 |
] |
| 191 |
], |
| 192 |
]; |
| 193 |
|
| 194 |
$ce = [ |
| 195 |
"pattern" => "\\ce{(...)}", |
| 196 |
"task" => [ |
| 197 |
"action_" => [ |
| 198 |
[ "type_" => "write", "option" => "{" ], |
| 199 |
[ "type_" => "ce" ], |
| 200 |
[ "type_" => "write", "option" => "}" ], |
| 201 |
], |
| 202 |
"stateArray" => [ |
| 203 |
"0" |
| 204 |
] |
| 205 |
], |
| 206 |
]; |
| 207 |
|
| 208 |
$mhchemParser = new MhchemParser(); |
| 209 |
$mhchemStateMachines = new MhchemStateMachines( $mhchemParser ); |
| 210 |
$transitions = $mhchemStateMachines->stateMachines["tex"]["transitions"]; |
| 211 |
|
| 212 |
|
| 213 |
$emptyGen = $transitions[0][0]; |
| 214 |
$ceGen = $transitions[0][1]; |
| 215 |
$this->assertEquals( $empty, $emptyGen ); |
| 216 |
$this->assertEquals( $ce, $ceGen ); |
| 217 |
} |
| 218 |
|
| 219 |
public function testTransitionsInitCe() { |
| 220 |
$mhchemParser = new MhchemParser(); |
| 221 |
$mhchemStateMachines = new MhchemStateMachines( $mhchemParser ); |
| 222 |
$transitions = $mhchemStateMachines->stateMachines["ce"]["transitions"]; |
| 223 |
|
| 224 |
$this->assertCount( 23, $transitions ); |
| 225 |
$this->assertCount( 61, $transitions["0"] ); |
| 226 |
$this->assertCount( 50, $transitions["qD"] ); |
| 227 |
} |
| 228 |
|
| 229 |
public function testMHPatternsMatch() { |
| 230 |
$mhchemPatterns = new MhchemPatterns(); |
| 231 |
$pattern = "\ce{(...)}"; |
| 232 |
$matches = $mhchemPatterns->match( $pattern, "\ce{CO2 + C -> 2 CO}" ); |
| 233 |
$this->assertEquals( "CO2 + C -> 2 CO", $matches["match_"] ); |
| 234 |
$this->assertSame( "", $matches["remainder"] ); |
| 235 |
} |
| 236 |
|
| 237 |
public function testMHPatternsMatch2() { |
| 238 |
$mhchemPatterns = new MhchemPatterns(); |
| 239 |
$matches = $mhchemPatterns->match( 'letters', "C" ); |
| 240 |
$this->assertNotNull( $matches ); |
| 241 |
} |
| 242 |
|
| 243 |
public function testMMHPatternsMatch3() { |
| 244 |
$mhchemPatterns = new MhchemPatterns(); |
| 245 |
$matches2 = $mhchemPatterns->match( '->', "C" ); |
| 246 |
$this->assertNull( $matches2 ); |
| 247 |
} |
| 248 |
|
| 249 |
public function testMHChemParserGo1() { |
| 250 |
$target = [ |
| 251 |
"type_" => "chemfive", |
| 252 |
"a" => [], |
| 253 |
"b" => [], |
| 254 |
"p" => [], |
| 255 |
"o" => [ |
| 256 |
[ |
| 257 |
"type_" => "rm", |
| 258 |
"p1" => "C" |
| 259 |
] |
| 260 |
], |
| 261 |
"q" => [], |
| 262 |
"d" => [] |
| 263 |
]; |
| 264 |
|
| 265 |
$mhchemParser = new MhchemParser(); |
| 266 |
$output = $mhchemParser->go( "C", "ce" ); |
| 267 |
|
| 268 |
$this->assertEquals( $target, $output[0] ); |
| 269 |
} |
| 270 |
|
| 271 |
public function testMHChemParserGo2() { |
| 272 |
$target = [ |
| 273 |
"type_" => "chemfive", |
| 274 |
"a" => [], |
| 275 |
"b" => [], |
| 276 |
"p" => [], |
| 277 |
"o" => [ |
| 278 |
[ |
| 279 |
"type_" => "rm", |
| 280 |
"p1" => "CO" |
| 281 |
] |
| 282 |
], |
| 283 |
"q" => [ |
| 284 |
"2" |
| 285 |
], |
| 286 |
"d" => [] |
| 287 |
]; |
| 288 |
|
| 289 |
$mhchemParser = new MhchemParser(); |
| 290 |
$output = $mhchemParser->go( "CO2", "ce" ); |
| 291 |
|
| 292 |
$this->assertEquals( $target, $output[0] ); |
| 293 |
} |
| 294 |
|
| 295 |
public function testMHChemParserGo3() { |
| 296 |
$target = [ |
| 297 |
[ |
| 298 |
"type_" => "arrow", |
| 299 |
"r" => "->", |
| 300 |
"rd" => [], |
| 301 |
"rq" => [] |
| 302 |
] |
| 303 |
]; |
| 304 |
|
| 305 |
$mhchemParser = new MhchemParser(); |
| 306 |
$output = $mhchemParser->go( "->", "ce" ); |
| 307 |
|
| 308 |
$this->assertEquals( $target, $output ); |
| 309 |
} |
| 310 |
|
| 311 |
public function testMHChemParserGo4() { |
| 312 |
$target = [ |
| 313 |
[ |
| 314 |
"type_" => "chemfive", |
| 315 |
"a" => [], |
| 316 |
"b" => [], |
| 317 |
"p" => [], |
| 318 |
"o" => [ |
| 319 |
"+" |
| 320 |
], |
| 321 |
"q" => [], |
| 322 |
"d" => [] |
| 323 |
] |
| 324 |
]; |
| 325 |
|
| 326 |
$mhchemParser = new MhchemParser(); |
| 327 |
$output = $mhchemParser->go( "+", "ce" ); |
| 328 |
|
| 329 |
$this->assertEquals( $target, $output ); |
| 330 |
} |
| 331 |
|
| 332 |
public function testMHChemParserGo5() { |
| 333 |
|
| 334 |
$mhchemParser = new MhchemParser(); |
| 335 |
$output = $mhchemParser->go( "CO2 + ", "ce" ); |
| 336 |
$this->assertCount( 2, $output ); |
| 337 |
$this->assertEquals( "chemfive", $output[0]["type_"] ); |
| 338 |
$this->assertEquals( "operator", $output[1]["type_"] ); |
| 339 |
} |
| 340 |
|
| 341 |
public function testMHChemParserGo6() { |
| 342 |
$mhchemParser = new MhchemParser(); |
| 343 |
$output = $mhchemParser->go( "CO2 + C -> 2 CO", "ce" ); |
| 344 |
$this->assertCount( 5, $output ); |
| 345 |
$this->assertEquals( "chemfive", $output[0]["type_"] ); |
| 346 |
$this->assertEquals( "operator", $output[1]["type_"] ); |
| 347 |
$this->assertEquals( "chemfive", $output[2]["type_"] ); |
| 348 |
$this->assertEquals( "arrow", $output[3]["type_"] ); |
| 349 |
$this->assertEquals( 2, $output[4]["a"][0] ); |
| 350 |
$this->assertEquals( "CO", $output[4]["o"][0]["p1"] ); |
| 351 |
} |
| 352 |
|
| 353 |
public function testMHChemTexify() { |
| 354 |
$mhchemParser = new MhchemParser(); |
| 355 |
$output = $mhchemParser->toTex( "CO2", "ce" ); |
| 356 |
$this->assertEquals( "{\\mathrm{CO}{\\vphantom{A}}_{\\smash[t]{2}}}", $output ); |
| 357 |
} |
| 358 |
|
| 359 |
public function testConcatArray1() { |
| 360 |
$a = []; |
| 361 |
$b = [ "{" ]; |
| 362 |
$target = [ "{" ]; |
| 363 |
MhchemUtil::concatArray( $a, $b ); |
| 364 |
$this->assertEquals( $target, $a ); |
| 365 |
} |
| 366 |
|
| 367 |
public function testConcatArray2() { |
| 368 |
$a = []; |
| 369 |
|
| 370 |
$b = [ "type_" => "rm", "p1" => "C" ]; |
| 371 |
$target = [ $b ]; |
| 372 |
MhchemUtil::concatArray( $a, $b ); |
| 373 |
$this->assertEquals( $target, $a ); |
| 374 |
} |
| 375 |
|
| 376 |
public function testConcatArray3() { |
| 377 |
$a = [ "{" ]; |
| 378 |
$b = [ "type_" => "chemfive" ]; |
| 379 |
|
| 380 |
$target = [ $a[0], $b ]; |
| 381 |
MhchemUtil::concatArray( $a, $b ); |
| 382 |
$this->assertEquals( $target, $a ); |
| 383 |
} |
| 384 |
|
| 385 |
} |