2012-12-11 02:53:43 +00:00
|
|
|
<?php
|
|
|
|
|
2017-08-21 21:06:34 +00:00
|
|
|
use Wikimedia\ScopedCallback;
|
|
|
|
|
2016-05-17 14:52:05 +00:00
|
|
|
// @codingStandardsIgnoreLine Squiz.Classes.ValidClassName.NotCamelCaps
|
2012-12-11 02:53:43 +00:00
|
|
|
class Scribunto_LuaUstringLibraryTests extends Scribunto_LuaEngineTestBase {
|
|
|
|
protected static $moduleName = 'UstringLibraryTests';
|
|
|
|
|
|
|
|
private $normalizationDataProvider = null;
|
|
|
|
|
2014-11-12 11:21:38 +00:00
|
|
|
protected function tearDown() {
|
2012-12-11 02:53:43 +00:00
|
|
|
if ( $this->normalizationDataProvider ) {
|
|
|
|
$this->normalizationDataProvider->destroy();
|
|
|
|
$this->normalizationDataProvider = null;
|
|
|
|
}
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:21:38 +00:00
|
|
|
protected function getTestModules() {
|
2017-06-15 17:19:00 +00:00
|
|
|
return parent::getTestModules() + [
|
2012-12-11 02:53:43 +00:00
|
|
|
'UstringLibraryTests' => __DIR__ . '/UstringLibraryTests.lua',
|
|
|
|
'UstringLibraryNormalizationTests' => __DIR__ . '/UstringLibraryNormalizationTests.lua',
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2012-12-11 02:53:43 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
public function testUstringLibraryNormalizationTestsAvailable() {
|
2012-12-11 02:53:43 +00:00
|
|
|
if ( UstringLibraryNormalizationTestProvider::available( $err ) ) {
|
|
|
|
$this->assertTrue( true );
|
|
|
|
} else {
|
|
|
|
$this->markTestSkipped( $err );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
public function provideUstringLibraryNormalizationTests() {
|
2012-12-11 02:53:43 +00:00
|
|
|
if ( !$this->normalizationDataProvider ) {
|
2016-05-17 14:52:05 +00:00
|
|
|
$this->normalizationDataProvider =
|
|
|
|
new UstringLibraryNormalizationTestProvider( $this->getEngine() );
|
2012-12-11 02:53:43 +00:00
|
|
|
}
|
|
|
|
return $this->normalizationDataProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideUstringLibraryNormalizationTests
|
|
|
|
*/
|
2014-11-12 11:43:44 +00:00
|
|
|
public function testUstringLibraryNormalizationTests( $name, $c1, $c2, $c3, $c4, $c5 ) {
|
2012-12-11 02:53:43 +00:00
|
|
|
$this->luaTestName = "UstringLibraryNormalization: $name";
|
|
|
|
$dataProvider = $this->provideUstringLibraryNormalizationTests();
|
2017-06-15 17:19:00 +00:00
|
|
|
$expected = [
|
2016-04-01 10:54:42 +00:00
|
|
|
$c2, $c2, $c2, $c4, $c4, // NFC
|
|
|
|
$c3, $c3, $c3, $c5, $c5, // NFD
|
|
|
|
$c4, $c4, $c4, $c4, $c4, // NFKC
|
|
|
|
$c5, $c5, $c5, $c5, $c5, // NFKD
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2012-12-11 02:53:43 +00:00
|
|
|
foreach ( $expected as &$e ) {
|
|
|
|
$chars = array_values( unpack( 'N*', mb_convert_encoding( $e, 'UTF-32BE', 'UTF-8' ) ) );
|
|
|
|
foreach ( $chars as &$c ) {
|
|
|
|
$c = sprintf( "%x", $c );
|
|
|
|
}
|
|
|
|
$e = "$e\t" . join( "\t", $chars );
|
|
|
|
}
|
|
|
|
$actual = $dataProvider->runNorm( $c1, $c2, $c3, $c4, $c5 );
|
|
|
|
$this->assertSame( $expected, $actual );
|
|
|
|
$this->luaTestName = null;
|
|
|
|
}
|
2016-03-24 14:08:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providePCREErrors
|
|
|
|
*/
|
|
|
|
public function testPCREErrors( $ini, $args, $error ) {
|
2017-06-15 17:19:00 +00:00
|
|
|
$reset = [];
|
2016-03-24 14:08:29 +00:00
|
|
|
foreach ( $ini as $key => $value ) {
|
|
|
|
$old = ini_set( $key, $value );
|
|
|
|
if ( $old === false ) {
|
|
|
|
$this->markTestSkipped( "Failed to set ini setting $key = $value" );
|
|
|
|
}
|
2017-06-15 17:19:00 +00:00
|
|
|
$reset[] = new ScopedCallback( 'ini_set', [ $key, $old ] );
|
2016-03-24 14:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$interpreter = $this->getEngine()->getInterpreter();
|
|
|
|
$func = $interpreter->loadString( 'return mw.ustring.gsub( ... )', 'fortest' );
|
|
|
|
try {
|
|
|
|
call_user_func_array(
|
2017-06-15 17:19:00 +00:00
|
|
|
[ $interpreter, 'callFunction' ],
|
|
|
|
array_merge( [ $func ], $args )
|
2016-03-24 14:08:29 +00:00
|
|
|
);
|
|
|
|
$this->fail( 'Expected exception not thrown' );
|
|
|
|
} catch ( Scribunto_LuaError $e ) {
|
|
|
|
$this->assertSame( $error, $e->getMessage() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function providePCREErrors() {
|
2017-06-15 17:19:00 +00:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
[ 'pcre.backtrack_limit' => 10 ],
|
|
|
|
[ 'zzzzzzzzzzzzzzzzzzzz', '^(.-)[abc]*$', '%1' ],
|
2016-03-24 14:08:29 +00:00
|
|
|
'Lua error: PCRE backtrack limit reached while matching pattern \'^(.-)[abc]*$\'.'
|
2017-06-15 17:19:00 +00:00
|
|
|
],
|
2016-03-24 14:08:29 +00:00
|
|
|
// @TODO: Figure out patterns that hit other PCRE limits
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2016-03-24 14:08:29 +00:00
|
|
|
}
|
2012-12-11 02:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class UstringLibraryNormalizationTestProvider extends Scribunto_LuaDataProvider {
|
|
|
|
protected $file = null;
|
|
|
|
protected $current = null;
|
2017-06-15 17:19:00 +00:00
|
|
|
protected static $static = [
|
2012-12-11 02:53:43 +00:00
|
|
|
'1E0A 0323;1E0C 0307;0044 0323 0307;1E0C 0307;0044 0323 0307;',
|
|
|
|
false
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2012-12-11 02:53:43 +00:00
|
|
|
|
|
|
|
public static function available( &$message = null ) {
|
|
|
|
if ( is_readable( __DIR__ . '/NormalizationTest.txt' ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$message = wordwrap( 'Download the Unicode Normalization Test Suite from ' .
|
|
|
|
'http://unicode.org/Public/6.0.0/ucd/NormalizationTest.txt and save as ' .
|
|
|
|
__DIR__ . '/NormalizationTest.txt to run normalization tests. Note that ' .
|
|
|
|
'running these tests takes quite some time.' );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct( $engine ) {
|
|
|
|
parent::__construct( $engine, 'UstringLibraryNormalizationTests' );
|
2017-07-24 11:18:59 +00:00
|
|
|
if ( self::available() ) {
|
2012-12-11 02:53:43 +00:00
|
|
|
$this->file = fopen( __DIR__ . '/NormalizationTest.txt', 'r' );
|
|
|
|
}
|
|
|
|
$this->rewind();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function destory() {
|
|
|
|
if ( $this->file ) {
|
|
|
|
fclose( $this->file );
|
|
|
|
$this->file = null;
|
|
|
|
}
|
|
|
|
parent::destory();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rewind() {
|
|
|
|
if ( $this->file ) {
|
2015-06-26 16:37:34 +00:00
|
|
|
rewind( $this->file );
|
2012-12-11 02:53:43 +00:00
|
|
|
}
|
|
|
|
$this->key = 0;
|
|
|
|
$this->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function valid() {
|
|
|
|
if ( $this->file ) {
|
2015-06-26 16:37:34 +00:00
|
|
|
$v = !feof( $this->file );
|
2012-12-11 02:53:43 +00:00
|
|
|
} else {
|
2015-06-26 16:37:34 +00:00
|
|
|
$v = $this->key < count( self::$static );
|
2012-12-11 02:53:43 +00:00
|
|
|
}
|
|
|
|
return $v;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function current() {
|
|
|
|
return $this->current;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function next() {
|
2017-06-15 17:19:00 +00:00
|
|
|
$this->current = [ null, null, null, null, null, null ];
|
2015-06-26 16:37:34 +00:00
|
|
|
while ( $this->valid() ) {
|
2012-12-11 02:53:43 +00:00
|
|
|
if ( $this->file ) {
|
|
|
|
$line = fgets( $this->file );
|
|
|
|
} else {
|
|
|
|
$line = self::$static[$this->key];
|
|
|
|
}
|
|
|
|
$this->key++;
|
|
|
|
if ( preg_match( '/^((?:[0-9A-F ]+;){5})/', $line, $m ) ) {
|
|
|
|
$line = rtrim( $m[1], ';' );
|
2017-06-15 17:19:00 +00:00
|
|
|
$ret = [ $line ];
|
2012-12-11 02:53:43 +00:00
|
|
|
foreach ( explode( ';', $line ) as $field ) {
|
2017-06-15 17:19:00 +00:00
|
|
|
$args = [ 'N*' ];
|
2012-12-11 02:53:43 +00:00
|
|
|
foreach ( explode( ' ', $field ) as $char ) {
|
|
|
|
$args[] = hexdec( $char );
|
|
|
|
}
|
|
|
|
$s = call_user_func_array( 'pack', $args );
|
|
|
|
$s = mb_convert_encoding( $s, 'UTF-8', 'UTF-32BE' );
|
|
|
|
$ret[] = $s;
|
|
|
|
}
|
|
|
|
$this->current = $ret;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function runNorm( $c1, $c2, $c3, $c4, $c5 ) {
|
|
|
|
return $this->engine->getInterpreter()->callFunction( $this->exports['run'],
|
|
|
|
$c1, $c2, $c3, $c4, $c5
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|