From d2bd2f4554a99bde69a1918f776fb5b035c9b69b Mon Sep 17 00:00:00 2001 From: Stegmujo Date: Thu, 20 Oct 2022 19:07:58 +0000 Subject: [PATCH] Add Tests and Downloader for Chem Input Types Bug: T321262 Change-Id: I4e0af8ab0eaa02829dffb7ff5fccc4564ed5c1ba --- .gitignore | 1 + maintenance/downloadMoreTexVCtests.sh | 17 ++++ maintenance/downloadWikipediaEnFormulae.sh | 7 -- .../phpunit/unit/TexVC/ChemRegressionTest.php | 98 +++++++++++++++++++ .../phpunit/unit/TexVC/EnWikiFormulaeTest.php | 2 +- 5 files changed, 117 insertions(+), 8 deletions(-) create mode 100755 maintenance/downloadMoreTexVCtests.sh delete mode 100755 maintenance/downloadWikipediaEnFormulae.sh create mode 100644 tests/phpunit/unit/TexVC/ChemRegressionTest.php diff --git a/.gitignore b/.gitignore index f61f68dda..fd8c40b27 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ vendor composer.lock /tests/selenium/log en-wiki-formulae.json +chem-regression.json # Editors *.kate-swp *~ diff --git a/maintenance/downloadMoreTexVCtests.sh b/maintenance/downloadMoreTexVCtests.sh new file mode 100755 index 000000000..324adffee --- /dev/null +++ b/maintenance/downloadMoreTexVCtests.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# Downloads files which contain input and supposed output for some tests of TexVC within this extension. + +# The tests are deactivated by default. They can be found and activated with a flag in: +# - EnWikiFormulaeTest.php +# - ChemRegressionTest.php + +# Downloads the file containing all english wikipedia formula to the testfolder +FILEPATH=../tests/phpunit/unit/TexVC/en-wiki-formulae.json +URL=https://raw.githubusercontent.com/wikimedia/mediawiki-services-texvcjs/fb56991251b8889b554fc42ef9fe4825bc35d0ed/test/en-wiki-formulae.json +curl $URL -o $FILEPATH + +# Downloads the file containing for chem-regression tests to the testfolder +FILEPATH=../tests/phpunit/unit/TexVC/chem-regression.json +URL=https://raw.githubusercontent.com/wikimedia/mediawiki-services-texvcjs/fb56991251b8889b554fc42ef9fe4825bc35d0ed/test/chem-regression.json +curl $URL -o $FILEPATH diff --git a/maintenance/downloadWikipediaEnFormulae.sh b/maintenance/downloadWikipediaEnFormulae.sh deleted file mode 100755 index 78162c448..000000000 --- a/maintenance/downloadWikipediaEnFormulae.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -# Downloads the file containing all english wikipedia formula to the testfolder -# this is required for running the corresponding tests in EnWikiFormulaeTest.php locally. -# Mind that the tests have to be activated in the php file. -FILEPATH=../tests/phpunit/unit/TexVC/en-wiki-formulae.json -URL=https://raw.githubusercontent.com/wikimedia/mediawiki-services-texvcjs/fb56991251b8889b554fc42ef9fe4825bc35d0ed/test/en-wiki-formulae.json -curl $URL -o $FILEPATH diff --git a/tests/phpunit/unit/TexVC/ChemRegressionTest.php b/tests/phpunit/unit/TexVC/ChemRegressionTest.php new file mode 100644 index 000000000..7e9930021 --- /dev/null +++ b/tests/phpunit/unit/TexVC/ChemRegressionTest.php @@ -0,0 +1,98 @@ +texVC = new TexVC(); + } + + /** + * Reads the json file to an object + * @throws InvalidArgumentException File with testcases does not exists. + * @return array json with testcases + */ + private function getJSON() { + $filePath = __DIR__ . '/' . $this->FILENAME; + if ( !file_exists( $filePath ) ) { + throw new InvalidArgumentException( "No testfile found at specified path: " . $filePath ); + } + $file = file_get_contents( $filePath ); + $json = json_decode( $file, true ); + return $json; + } + + private function mkgroups( $arr, $n ) { + $result = []; + $group = []; + $seen = []; + foreach ( $arr as $elem ) { + if ( array_key_exists( $elem["input"], $seen ) ) { + continue; + } else { + $seen[$elem["input"]] = true; + } + array_push( $group, $elem ); + if ( count( $group ) >= $n ) { + array_push( $result, $group ); + $group = []; + } + } + if ( count( $group ) > 0 ) { + array_push( $result, $group ); + } + return $result; + } + + public function testAllChemRegression() { + if ( !$this->ACTIVE ) { + $this->markTestSkipped( "Chem-Regression test not active and skipped. Can be activated in test-flag." ); + return; + } + + $texVC = new TexVC(); + $groups = $this->mkgroups( $this->getJSON(), $this->CHUNKSIZE ); + foreach ( $groups as $group ) { + foreach ( $group as $testcase ) { + $testHash = $testcase["inputhash"]; + $f = $testcase["input"]; + $type = $testcase["type"]; + try { + $options = [ + "debug" => false, + "usemathrm" => false, + "oldtexvc" => false + ]; + + if ( $type === "chem" ) { + $options["usemhchem"] = true; + } + + $result = $texVC->check( $testcase["input"], $options ); + $this->assertEquals( '+', $result["status"], $testHash . " with input: " . $f ); + } catch ( PhpPegJs\SyntaxError $ex ) { + $message = "Syntax error: " . $ex->getMessage() . + ' at line ' . $ex->grammarLine . ' column ' . + $ex->grammarColumn . ' offset ' . $ex->grammarOffset; + + $this->assertTrue( false, $message ); + } + } + } + } +} diff --git a/tests/phpunit/unit/TexVC/EnWikiFormulaeTest.php b/tests/phpunit/unit/TexVC/EnWikiFormulaeTest.php index d642df5a4..aac11208d 100644 --- a/tests/phpunit/unit/TexVC/EnWikiFormulaeTest.php +++ b/tests/phpunit/unit/TexVC/EnWikiFormulaeTest.php @@ -11,7 +11,7 @@ use MediaWikiUnitTestCase; * All assertions are currently deactivated, cause high memory load on CI. * These tests can be run locally by enabling the ACTIVE flag. * File download of the json-input can be done by running: - * $ cd maintenance && ./downloadWikipediaEnFormulae.sh + * $ cd maintenance && ./downloadMoreTexVCtests.sh * @covers \MediaWiki\Extension\Math\TexVC\Parser * @group Stub */