2012-12-11 02:53:43 +00:00
|
|
|
<?php
|
|
|
|
|
2022-07-30 22:43:58 +00:00
|
|
|
namespace MediaWiki\Extension\Scribunto\Tests\Engines\LuaCommon;
|
|
|
|
|
2022-08-03 12:31:15 +00:00
|
|
|
use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaError;
|
2017-08-21 21:06:34 +00:00
|
|
|
use Wikimedia\ScopedCallback;
|
|
|
|
|
2018-05-27 00:50:43 +00:00
|
|
|
/**
|
2022-08-03 12:31:15 +00:00
|
|
|
* @covers \MediaWiki\Extension\Scribunto\Engines\LuaCommon\UstringLibrary
|
2018-05-27 00:50:43 +00:00
|
|
|
*/
|
2022-08-03 12:31:15 +00:00
|
|
|
class UstringLibraryTest extends LuaEngineUnitTestBase {
|
2021-01-23 01:33:17 +00:00
|
|
|
/** @inheritDoc */
|
2012-12-11 02:53:43 +00:00
|
|
|
protected static $moduleName = 'UstringLibraryTests';
|
|
|
|
|
2021-01-23 01:33:17 +00:00
|
|
|
/** @var UstringLibraryNormalizationTestProvider|null */
|
2012-12-11 02:53:43 +00:00
|
|
|
private $normalizationDataProvider = null;
|
|
|
|
|
2021-07-23 10:49:13 +00:00
|
|
|
protected function tearDown(): void {
|
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 = [
|
2022-07-30 18:46:09 +00:00
|
|
|
// NFC
|
|
|
|
$c2, $c2, $c2, $c4, $c4,
|
|
|
|
// NFD
|
|
|
|
$c3, $c3, $c3, $c5, $c5,
|
|
|
|
// NFKC
|
|
|
|
$c4, $c4, $c4, $c4, $c4,
|
|
|
|
// NFKD
|
|
|
|
$c5, $c5, $c5, $c5, $c5,
|
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 );
|
|
|
|
}
|
2018-02-15 13:57:18 +00:00
|
|
|
$e = "$e\t" . implode( "\t", $chars );
|
2012-12-11 02:53:43 +00:00
|
|
|
}
|
|
|
|
$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 {
|
2018-06-08 07:56:03 +00:00
|
|
|
$interpreter->callFunction( $func, ...$args );
|
2016-03-24 14:08:29 +00:00
|
|
|
$this->fail( 'Expected exception not thrown' );
|
2022-08-03 12:31:15 +00:00
|
|
|
} catch ( LuaError $e ) {
|
2016-03-24 14:08:29 +00:00
|
|
|
$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
|
|
|
}
|