Debian Codegraph @

extensions/Scribunto/tests/phpunit/Engines/LuaCommon/UstringLibraryTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Scribunto\Tests\Engines\LuaCommon;
4
5 use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaError;
6 use Wikimedia\ScopedCallback;
7
8 /**
9 * @covers \MediaWiki\Extension\Scribunto\Engines\LuaCommon\UstringLibrary
10 */
11 class UstringLibraryTest extends LuaEngineUnitTestBase {
12 /** @inheritDoc */
13 protected static $moduleName = 'UstringLibraryTests';
14
15 /** @var UstringLibraryNormalizationTestProvider|null */
16 private $normalizationDataProvider = null;
17
18 protected function tearDown(): void {
19 if ( $this->normalizationDataProvider ) {
20 $this->normalizationDataProvider->destroy();
21 $this->normalizationDataProvider = null;
22 }
23 parent::tearDown();
24 }
25
26 protected function getTestModules() {
27 return parent::getTestModules() + [
28 'UstringLibraryTests' => __DIR__ . '/UstringLibraryTests.lua',
29 'UstringLibraryNormalizationTests' => __DIR__ . '/UstringLibraryNormalizationTests.lua',
30 ];
31 }
32
33 public function testUstringLibraryNormalizationTestsAvailable() {
34 if ( UstringLibraryNormalizationTestProvider::available( $err ) ) {
35 $this->assertTrue( true );
36 } else {
37 $this->markTestSkipped( $err );
38 }
39 }
40
41 public function provideUstringLibraryNormalizationTests() {
42 if ( !$this->normalizationDataProvider ) {
43 $this->normalizationDataProvider =
44 new UstringLibraryNormalizationTestProvider( $this->getEngine() );
45 }
46 return $this->normalizationDataProvider;
47 }
48
49 /**
50 * @dataProvider provideUstringLibraryNormalizationTests
51 */
52 public function testUstringLibraryNormalizationTests( $name, $c1, $c2, $c3, $c4, $c5 ) {
53 $this->luaTestName = "UstringLibraryNormalization: $name";
54 $dataProvider = $this->provideUstringLibraryNormalizationTests();
55 $expected = [
56 // NFC
57 $c2, $c2, $c2, $c4, $c4,
58 // NFD
59 $c3, $c3, $c3, $c5, $c5,
60 // NFKC
61 $c4, $c4, $c4, $c4, $c4,
62 // NFKD
63 $c5, $c5, $c5, $c5, $c5,
64 ];
65 foreach ( $expected as &$e ) {
66 $chars = array_values( unpack( 'N*', mb_convert_encoding( $e, 'UTF-32BE', 'UTF-8' ) ) );
67 foreach ( $chars as &$c ) {
68 $c = sprintf( "%x", $c );
69 }
70 $e = "$e\t" . implode( "\t", $chars );
71 }
72 $actual = $dataProvider->runNorm( $c1, $c2, $c3, $c4, $c5 );
73 $this->assertSame( $expected, $actual );
74 $this->luaTestName = null;
75 }
76
77 /**
78 * @dataProvider providePCREErrors
79 */
80 public function testPCREErrors( $ini, $args, $error ) {
81 $reset = [];
82 foreach ( $ini as $key => $value ) {
83 $old = ini_set( $key, $value );
84 if ( $old === false ) {
85 $this->markTestSkipped( "Failed to set ini setting $key = $value" );
86 }
87 $reset[] = new ScopedCallback( 'ini_set', [ $key, $old ] );
88 }
89
90 $interpreter = $this->getEngine()->getInterpreter();
91 $func = $interpreter->loadString( 'return mw.ustring.gsub( ... )', 'fortest' );
92 try {
93 $interpreter->callFunction( $func, ...$args );
94 $this->fail( 'Expected exception not thrown' );
95 } catch ( LuaError $e ) {
96 $this->assertSame( $error, $e->getMessage() );
97 }
98 }
99
100 public static function providePCREErrors() {
101 return [
102 [
103 [ 'pcre.backtrack_limit' => 10 ],
104 [ 'zzzzzzzzzzzzzzzzzzzz', '^(.-)[abc]*$', '%1' ],
105 'Lua error: PCRE backtrack limit reached while matching pattern \'^(.-)[abc]*$\'.'
106 ],
107 // @TODO: Figure out patterns that hit other PCRE limits
108 ];
109 }
110 }