Debian Codegraph @

extensions/Math/tests/phpunit/unit/WikiTexVC/ChemRegressionTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Math\Tests\WikiTexVC;
4
5 use MediaWiki\Extension\Math\WikiTexVC\TexVC;
6 use MediaWikiUnitTestCase;
7
8 /**
9 * @covers \MediaWiki\Extension\Math\WikiTexVC\TexVC
10 * @covers \MediaWiki\Extension\Math\WikiTexVC\Parser
11 *
12 * @group Standalone
13 */
14 class ChemRegressionTest extends MediaWikiUnitTestCase {
15 /** @var TexVC */
16 private $texVC;
17 private const CHUNK_SIZE = 100;
18
19 private const FILEPATH = __DIR__ . '/chem-regression.json';
20
21 public static function setUpBeforeClass(): void {
22 if ( !file_exists( self::FILEPATH ) ) {
23 self::markTestSkipped( 'No test file found at specified path: ' . self::FILEPATH );
24 }
25 parent::setUpBeforeClass();
26 }
27
28 protected function setUp(): void {
29 parent::setUp();
30 $this->texVC = new TexVC();
31 }
32
33 /**
34 * Reads the json file to an object
35 * @return array json with testcases
36 */
37 private function getJSON(): array {
38 $file = file_get_contents( self::FILEPATH );
39 return json_decode( $file, true );
40 }
41
42 private function mkgroups( $arr, $n ) {
43 $result = [];
44 $group = [];
45 $seen = [];
46 foreach ( $arr as $elem ) {
47 if ( array_key_exists( $elem["input"], $seen ) ) {
48 continue;
49 } else {
50 $seen[$elem["input"]] = true;
51 }
52 array_push( $group, $elem );
53 if ( count( $group ) >= $n ) {
54 array_push( $result, $group );
55 $group = [];
56 }
57 }
58 if ( count( $group ) > 0 ) {
59 array_push( $result, $group );
60 }
61 return $result;
62 }
63
64 public function testAllChemRegression() {
65 $texVC = new TexVC();
66 $groups = $this->mkgroups( $this->getJSON(), self::CHUNK_SIZE );
67 foreach ( $groups as $group ) {
68 foreach ( $group as $testcase ) {
69 $testHash = $testcase["inputhash"];
70 $f = $testcase["input"];
71 $type = $testcase["type"];
72 try {
73 $options = [
74 "debug" => false,
75 "usemathrm" => false,
76 "oldtexvc" => false
77 ];
78
79 if ( $type === "chem" ) {
80 $options["usemhchem"] = true;
81 }
82
83 $result = $texVC->check( $testcase["input"], $options );
84 $this->assertEquals( '+', $result["status"], $testHash . " with input: " . $f );
85 } catch ( PhpPegJs\SyntaxError $ex ) {
86 $message = "Syntax error: " . $ex->getMessage() .
87 ' at line ' . $ex->grammarLine . ' column ' .
88 $ex->grammarColumn . ' offset ' . $ex->grammarOffset;
89
90 $this->assertTrue( false, $message );
91 }
92 }
93 }
94 }
95 }