From b2e1b70e15958f6dc838d39077a91bc604680c39 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Wed, 7 Jan 2015 11:12:09 -0800 Subject: [PATCH] Turn Utf8Test into a PHPUnit test case Change-Id: I7e709f475a37a592fb3052f367894ebce32310f3 --- includes/libs/normal/.gitignore | 1 - includes/libs/normal/Makefile | 8 +- includes/libs/normal/Utf8Test.php | 156 ------------------ tests/phpunit/data/normal/UTF-8-test.txt | Bin 0 -> 20334 bytes .../phpunit/includes/libs/normal/Utf8Test.php | 122 ++++++++++++++ 5 files changed, 123 insertions(+), 164 deletions(-) delete mode 100644 includes/libs/normal/Utf8Test.php create mode 100644 tests/phpunit/data/normal/UTF-8-test.txt create mode 100644 tests/phpunit/includes/libs/normal/Utf8Test.php diff --git a/includes/libs/normal/.gitignore b/includes/libs/normal/.gitignore index 0dea457973..5af0afa7fa 100644 --- a/includes/libs/normal/.gitignore +++ b/includes/libs/normal/.gitignore @@ -2,6 +2,5 @@ /DerivedNormalizationProps.txt /NormalizationCorrections.txt /NormalizationTest.txt -/UTF-8-test.txt /UnicodeData.txt /testdata diff --git a/includes/libs/normal/Makefile b/includes/libs/normal/Makefile index 76cb68bab0..b00154df65 100644 --- a/includes/libs/normal/Makefile +++ b/includes/libs/normal/Makefile @@ -21,12 +21,9 @@ all : UtfNormalData.inc UtfNormalData.inc : UtfNormalGenerate.php UtfNormalUtil.php UnicodeData.txt CompositionExclusions.txt NormalizationCorrections.txt DerivedNormalizationProps.txt $(PHP) UtfNormalGenerate.php -test : testutf8 UtfNormalTest.php UtfNormalData.inc NormalizationTest.txt +test : UtfNormalTest.php UtfNormalData.inc NormalizationTest.txt $(PHP) UtfNormalTest.php -testutf8 : Utf8Test.php UTF-8-test.txt - $(PHP) Utf8Test.php - bench : UtfNormalData.inc testdata/washington.txt testdata/berlin.txt testdata/tokyo.txt testdata/young.txt testdata/bulgakov.txt $(PHP) UtfNormalBench.php @@ -59,9 +56,6 @@ DerivedNormalizationProps.txt : UnicodeData.txt : $(FETCH) $(BASE)/UnicodeData.txt -UTF-8-test.txt : - $(FETCH) http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt - testdata/berlin.txt : mkdir -p testdata && wget -U MediaWiki/test -O testdata/berlin.txt "http://de.wikipedia.org/w/index.php?title=Berlin&oldid=2775712&action=raw" diff --git a/includes/libs/normal/Utf8Test.php b/includes/libs/normal/Utf8Test.php deleted file mode 100644 index f4acc1eb56..0000000000 --- a/includes/libs/normal/Utf8Test.php +++ /dev/null @@ -1,156 +0,0 @@ - - * https://www.mediawiki.org/ - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/gpl.html - * - * @file - * @ingroup UtfNormal - */ - -/** */ - -if ( PHP_SAPI != 'cli' ) { - die( "Run me from the command line please.\n" ); -} - -require_once 'UtfNormalDefines.php'; -require_once 'UtfNormalUtil.php'; -require_once 'UtfNormal.php'; -mb_internal_encoding( "utf-8" ); - -$verbose = false; -#$verbose = true; - -$in = fopen( "UTF-8-test.txt", "rt" ); -if ( !$in ) { - print "Couldn't open UTF-8-test.txt -- can't run tests.\n"; - print "If necessary, manually download this file. It can be obtained at\n"; - print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt\n"; - exit( -1 ); -} - -$columns = 0; -while ( false !== ( $line = fgets( $in ) ) ) { - $matches = array(); - if ( preg_match( '/^(Here come the tests:\s*)\|$/', $line, $matches ) ) { - $columns = strpos( $line, '|' ); - break; - } -} - -if ( !$columns ) { - print "Something seems to be wrong; couldn't extract line length.\n"; - print "Check that UTF-8-test.txt was downloaded correctly from\n"; - print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt\n"; - exit( -1 ); -} - -# print "$columns\n"; - -$ignore = array( - # These two lines actually seem to be corrupt - '2.1.1', '2.2.1' ); - -$exceptions = array( - # Tests that should mark invalid characters due to using long - # sequences beyond what is now considered legal. - '2.1.5', '2.1.6', '2.2.4', '2.2.5', '2.2.6', '2.3.5', - - # Literal 0xffff, which is illegal - '2.2.3' ); - -$longTests = array( - # These tests span multiple lines - '3.1.9', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5', - '3.4' ); - -# These tests are not in proper subsections -$sectionTests = array( '3.4' ); - -$section = null; -$test = ''; -$failed = 0; -$success = 0; -$total = 0; -while ( false !== ( $line = fgets( $in ) ) ) { - $matches = array(); - if ( preg_match( '/^(\d+)\s+(.*?)\s*\|/', $line, $matches ) ) { - $section = $matches[1]; - print $line; - continue; - } - if ( preg_match( '/^(\d+\.\d+\.\d+)\s*/', $line, $matches ) ) { - $test = $matches[1]; - - if ( in_array( $test, $ignore ) ) { - continue; - } - if ( in_array( $test, $longTests ) ) { - $line = fgets( $in ); - - // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed - for ( $line = fgets( $in ); !preg_match( '/^\s+\|/', $line ); $line = fgets( $in ) ) { - // @codingStandardsIgnoreEnd - - testLine( $test, $line, $total, $success, $failed, $columns, $exceptions, $verbose ); - } - } else { - testLine( $test, $line, $total, $success, $failed, $columns, $exceptions, $verbose ); - } - } -} - -if ( $failed ) { - echo "\nFailed $failed tests.\n"; - echo "UTF-8 DECODER TEST FAILED\n"; - exit ( -1 ); -} - -echo "UTF-8 DECODER TEST SUCCESS!\n"; -exit ( 0 ); - -function testLine( $test, $line, &$total, &$success, &$failed, $columns, $exceptions, $verbose ) { - $stripped = $line; - UtfNormal::quickisNFCVerify( $stripped ); - - $same = ( $line == $stripped ); - $len = mb_strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) ); - if ( $len == 0 ) { - $len = strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) ); - } - - $ok = $same ^ ( $test >= 3 ); - - $ok ^= in_array( $test, $exceptions ); - - $ok &= ( $columns == $len ); - - $total++; - if ( $ok ) { - $success++; - } else { - $failed++; - } - - if ( $verbose || !$ok ) { - print str_replace( "\n", "$len\n", $stripped ); - } -} diff --git a/tests/phpunit/data/normal/UTF-8-test.txt b/tests/phpunit/data/normal/UTF-8-test.txt new file mode 100644 index 0000000000000000000000000000000000000000..abd16f7253bf7254ad1ae04cb5bee8d8c47e407a GIT binary patch literal 20334 zcmdU1X>%Ocb-l=P@_l?QkaQ+KqMb=kWH&#~HSJo~$&h?IO#*?-m zc%Ubgju-S~81=~ARry~yhNh5G^057n(4B1 z-Jl)CU9Yt)Q7lo~@#2A>c;bht2ToHz+%uR?;yu;(!lswhMM+X>Kw223()HXhl{Auu zCoaH4V!C)(1yz@8sk9={!DoAbZN`2Bgt*~QdTyNh&3@ow^nTimx?SUnX>wt*17rPAz z)GE5+b=(6;DHb=Cgmm-*H)9tx`CZcBhe_(WATM%qC1CJyU)%=BH>6_>9koDo+V zLQs$lfz)-uJLDAvP==4s@nApfCw(`-P`!Q%VP8G$d9hE)gDPo5PL>ZR?SRy7gyGtL zoTMwMPwDl+c&u6;Glm2YL9lou@LaqSg&uTj?4lP(5Cu4-)I2{fgff(VLK{#CQ{{D9 zq|vJcu)PF24`6-G=m6dVA%rbA9?}L{K3PgiHa2NV;wCcif`C6WiwuSWR^fTA%#?Gp zqik-3KEK1mEzjviFjCkwg^)nkK}$j?9(L4hLW|&qrl@wFVP*6`)WpXvfwh*ek+1J39Sl`y({PyUv>iuXYRwo~Fm!U$wRSqC&$svCdeo15fXJl* zDAN*bHU{fXI|Ba=#F8QyxBDb2@b=tbJhqjLR|9PLK+X9_%WELc>HrMiEx+An zd`GTfTxa@1{9y!DbA#~0HbP7kBCx!TkqH*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 index 0000000000..86ae08ec83 --- /dev/null +++ b/tests/phpunit/includes/libs/normal/Utf8Test.php @@ -0,0 +1,122 @@ + + * https://www.mediawiki.org/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + * @ingroup UtfNormal + */ + +class Utf8Test extends PHPUnit_Framework_TestCase { + public static function provideLines() { + global $IP; + $in = fopen( "$IP/tests/phpunit/data/normal/UTF-8-test.txt", "rt" ); + + $columns = 0; + while ( false !== ( $line = fgets( $in ) ) ) { + $matches = array(); + if ( preg_match( '/^(Here come the tests:\s*)\|$/', $line, $matches ) ) { + $columns = strpos( $line, '|' ); + break; + } + } + + if ( !$columns ) { + print "Something seems to be wrong; couldn't extract line length.\n"; + print "Check that UTF-8-test.txt was downloaded correctly from\n"; + print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt\n"; + exit( -1 ); + } + + + $ignore = array( + # These two lines actually seem to be corrupt + '2.1.1', '2.2.1' ); + + $exceptions = array( + # Tests that should mark invalid characters due to using long + # sequences beyond what is now considered legal. + '2.1.5', '2.1.6', '2.2.4', '2.2.5', '2.2.6', '2.3.5', + + # Literal 0xffff, which is illegal + '2.2.3' ); + + $longTests = array( + # These tests span multiple lines + '3.1.9', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5', + '3.4' ); + + $testCases = array(); + + $section = null; + while ( false !== ( $line = fgets( $in ) ) ) { + $matches = array(); + if ( preg_match( '/^(\d+)\s+(.*?)\s*\|/', $line, $matches ) ) { + continue; + } + if ( preg_match( '/^(\d+\.\d+\.\d+)\s*/', $line, $matches ) ) { + $test = $matches[1]; + + if ( in_array( $test, $ignore ) ) { + continue; + } + if ( in_array( $test, $longTests ) ) { + fgets( $in ); + + // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed + for ( $line = fgets( $in ); !preg_match( '/^\s+\|/', $line ); $line = fgets( $in ) ) { + // @codingStandardsIgnoreEnd + + $testCases[] = array( $test, $line, $columns, $exceptions ); + } + } else { + $testCases[] = array( $test, $line, $columns, $exceptions ); + } + } + } + + return $testCases; + } + + + /** + * @dataProvider provideLines + */ + function testLine( $test, $line, $columns, $exceptions ) { + $stripped = $line; + UtfNormal::quickisNFCVerify( $stripped ); + + $same = ( $line == $stripped ); + $len = mb_strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) ); + if ( $len == 0 ) { + $len = strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) ); + } + + $ok = $same ^ ( $test >= 3 ); + + $ok ^= in_array( $test, $exceptions ); + + $ok &= ( $columns == $len ); + + $this->assertEquals( 1, $ok ); + } + +} -- 2.20.1