MediaWiki REL1_34
UstringLibraryTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\ScopedCallback;
4
9 protected static $moduleName = 'UstringLibraryTests';
10
12
13 protected function tearDown() {
14 if ( $this->normalizationDataProvider ) {
15 $this->normalizationDataProvider->destroy();
16 $this->normalizationDataProvider = null;
17 }
18 parent::tearDown();
19 }
20
21 protected function getTestModules() {
22 return parent::getTestModules() + [
23 'UstringLibraryTests' => __DIR__ . '/UstringLibraryTests.lua',
24 'UstringLibraryNormalizationTests' => __DIR__ . '/UstringLibraryNormalizationTests.lua',
25 ];
26 }
27
30 $this->assertTrue( true );
31 } else {
32 $this->markTestSkipped( $err );
33 }
34 }
35
37 if ( !$this->normalizationDataProvider ) {
38 $this->normalizationDataProvider =
40 }
42 }
43
47 public function testUstringLibraryNormalizationTests( $name, $c1, $c2, $c3, $c4, $c5 ) {
48 $this->luaTestName = "UstringLibraryNormalization: $name";
49 $dataProvider = $this->provideUstringLibraryNormalizationTests();
50 $expected = [
51 $c2, $c2, $c2, $c4, $c4, // NFC
52 $c3, $c3, $c3, $c5, $c5, // NFD
53 $c4, $c4, $c4, $c4, $c4, // NFKC
54 $c5, $c5, $c5, $c5, $c5, // NFKD
55 ];
56 foreach ( $expected as &$e ) {
57 $chars = array_values( unpack( 'N*', mb_convert_encoding( $e, 'UTF-32BE', 'UTF-8' ) ) );
58 foreach ( $chars as &$c ) {
59 $c = sprintf( "%x", $c );
60 }
61 $e = "$e\t" . implode( "\t", $chars );
62 }
63 $actual = $dataProvider->runNorm( $c1, $c2, $c3, $c4, $c5 );
64 $this->assertSame( $expected, $actual );
65 $this->luaTestName = null;
66 }
67
71 public function testPCREErrors( $ini, $args, $error ) {
72 $reset = [];
73 foreach ( $ini as $key => $value ) {
74 $old = ini_set( $key, $value );
75 if ( $old === false ) {
76 $this->markTestSkipped( "Failed to set ini setting $key = $value" );
77 }
78 $reset[] = new ScopedCallback( 'ini_set', [ $key, $old ] );
79 }
80
81 $interpreter = $this->getEngine()->getInterpreter();
82 $func = $interpreter->loadString( 'return mw.ustring.gsub( ... )', 'fortest' );
83 try {
84 $interpreter->callFunction( $func, ...$args );
85 $this->fail( 'Expected exception not thrown' );
86 } catch ( Scribunto_LuaError $e ) {
87 $this->assertSame( $error, $e->getMessage() );
88 }
89 }
90
91 public static function providePCREErrors() {
92 return [
93 [
94 [ 'pcre.backtrack_limit' => 10 ],
95 [ 'zzzzzzzzzzzzzzzzzzzz', '^(.-)[abc]*$', '%1' ],
96 'Lua error: PCRE backtrack limit reached while matching pattern \'^(.-)[abc]*$\'.'
97 ],
98 // @TODO: Figure out patterns that hit other PCRE limits
99 ];
100 }
101}
102
104 protected $file = null;
105 protected $current = null;
106 protected static $static = [
107 '1E0A 0323;1E0C 0307;0044 0323 0307;1E0C 0307;0044 0323 0307;',
108 false
109 ];
110
111 public static function available( &$message = null ) {
112 if ( is_readable( __DIR__ . '/NormalizationTest.txt' ) ) {
113 return true;
114 }
115 $message = wordwrap( 'Download the Unicode Normalization Test Suite from ' .
116 'http://unicode.org/Public/6.0.0/ucd/NormalizationTest.txt and save as ' .
117 __DIR__ . '/NormalizationTest.txt to run normalization tests. Note that ' .
118 'running these tests takes quite some time.' );
119 return false;
120 }
121
122 public function __construct( $engine ) {
123 parent::__construct( $engine, 'UstringLibraryNormalizationTests' );
124 if ( self::available() ) {
125 $this->file = fopen( __DIR__ . '/NormalizationTest.txt', 'r' );
126 }
127 $this->rewind();
128 }
129
130 public function destory() {
131 if ( $this->file ) {
132 fclose( $this->file );
133 $this->file = null;
134 }
135 parent::destory();
136 }
137
138 public function rewind() {
139 if ( $this->file ) {
140 rewind( $this->file );
141 }
142 $this->key = 0;
143 $this->next();
144 }
145
146 public function valid() {
147 if ( $this->file ) {
148 $v = !feof( $this->file );
149 } else {
150 $v = $this->key < count( self::$static );
151 }
152 return $v;
153 }
154
155 public function current() {
156 return $this->current;
157 }
158
159 public function next() {
160 $this->current = [ null, null, null, null, null, null ];
161 while ( $this->valid() ) {
162 if ( $this->file ) {
163 $line = fgets( $this->file );
164 } else {
165 $line = self::$static[$this->key];
166 }
167 $this->key++;
168 if ( preg_match( '/^((?:[0-9A-F ]+;){5})/', $line, $m ) ) {
169 $line = rtrim( $m[1], ';' );
170 $ret = [ $line ];
171 foreach ( explode( ';', $line ) as $field ) {
172 $args = [];
173 foreach ( explode( ' ', $field ) as $char ) {
174 $args[] = hexdec( $char );
175 }
176 $s = pack( 'N*', ...$args );
177 $s = mb_convert_encoding( $s, 'UTF-8', 'UTF-32BE' );
178 $ret[] = $s;
179 }
180 $this->current = $ret;
181 return;
182 }
183 }
184 }
185
186 public function runNorm( $c1, $c2, $c3, $c4, $c5 ) {
187 return $this->engine->getInterpreter()->callFunction( $this->exports['run'],
188 $c1, $c2, $c3, $c4, $c5
189 );
190 }
191}
$line
Definition cdb.php:59
if( $line===false) $args
Definition cdb.php:64
This is the subclass for Lua library tests.
@covers Scribunto_LuaUstringLibrary
testPCREErrors( $ini, $args, $error)
@dataProvider providePCREErrors
testUstringLibraryNormalizationTests( $name, $c1, $c2, $c3, $c4, $c5)
@dataProvider provideUstringLibraryNormalizationTests
if(PHP_SAPI !=='cli' &&PHP_SAPI !=='phpdbg' $chars)