Debian Codegraph @

extensions/Math/tests/phpunit/unit/WikiTexVC/EnWikiFormulaeTest.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 * Currently WIP functionalities of en-wiki-formulae.js testsuite.
10 * All assertions are currently deactivated, cause high memory load on CI.
11 * These tests can be run locally by enabling the ACTIVE flag.
12 * File download of the json-input can be done by running:
13 * $ cd maintenance && ./downloadMoreTexVCtests.sh
14 * @covers \MediaWiki\Extension\Math\WikiTexVC\Parser
15 *
16 * @group Standalone
17 */
18 class EnWikiFormulaeTest extends MediaWikiUnitTestCase {
19 private const FILEPATH = __DIR__ . '/en-wiki-formulae-good.json';
20 private const REF_FILEPATH = __DIR__ . '/en-wiki-formulae-good-reference.json';
21 private const CHUNK_SIZE = 1000;
22
23 public static function setUpBeforeClass(): void {
24 self::checkFiles();
25 parent::setUpBeforeClass();
26 }
27
28 private static function checkFiles() {
29 if ( !file_exists( self::FILEPATH ) || !file_exists( self::REF_FILEPATH ) ) {
30 self::markTestSkipped( 'Missing test files. Required: ' .
31 self::FILEPATH . ' and ' .
32 self::REF_FILEPATH );
33 }
34 }
35
36 /**
37 * Reads the json file to an object
38 * @param string $filePath file to be read
39 * @return array json with testcases
40 */
41 private static function getJSON( $filePath ): array {
42 self::checkFiles();
43 $file = file_get_contents( $filePath );
44 return json_decode( $file, true );
45 }
46
47 public static function provideTestCases(): \Generator {
48 $group = [];
49 $groupNo = 1;
50 $references = self::getJSON( self::REF_FILEPATH );
51 foreach ( self::getJSON( self::FILEPATH ) as $key => $elem ) {
52 $group[$key] = [ $elem, $references[ $key ] ];
53 if ( count( $group ) >= self::CHUNK_SIZE ) {
54 yield "Group $groupNo" => [ $group ];
55 $groupNo++;
56 $group = [];
57 }
58 }
59 if ( count( $group ) > 0 ) {
60 yield "Group $groupNo" => [ $group ];
61 }
62 }
63
64 /**
65 * @dataProvider provideTestCases
66 */
67 public function testRunCases( $testcase ) {
68 $texVC = new TexVC();
69
70 foreach ( $testcase as $hash => [ $tex, $ref ] ) {
71 try {
72 $result = $texVC->check( $tex, [
73 "debug" => false,
74 "usemathrm" => false,
75 "oldtexvc" => false
76 ] );
77
78 $this->assertEquals( '+', $result["status"],
79 $hash . " failed. Input: " . $tex );
80 if ( preg_match( '/\\\\definecolor \{/m', $ref ) ) {
81 // crop long numbers in color codes from 16 to 14 digits
82 // while this heuristic might produce false positivies in general, it is sufficient
83 // for this dataset
84 $ref = preg_replace( '/(0.\d{14})\d{2}([,\}])/m', '$1$2', $ref );
85 }
86
87 $this->assertEquals( $ref, $result["output"],
88 $hash . " does not match reference." );
89
90 $r1 = $texVC->check( $result["output"] );
91 $this->assertEquals( "+", $r1["status"],
92 "error rechecking output: " . $tex . " -> " . $result["output"] );
93 $mathml = $result["input"]->renderMML();
94 $this->assertStringNotContainsString( 'merror', $mathml,
95 "error rendering MathML: " . $tex . " -> " . $result["output"] );
96
97 } catch ( PhpPegJs\SyntaxError $ex ) {
98 $message = "Syntax error: " . $ex->getMessage() .
99 ' at line ' . $ex->grammarLine . ' column ' .
100 $ex->grammarColumn . ' offset ' . $ex->grammarOffset;
101
102 $this->assertTrue( false, $message );
103 }
104 }
105 }
106 }