Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Scribunto\Tests\Engines\LuaCommon;
4
5 use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaEngine;
6
7 class UstringLibraryNormalizationTestProvider extends LuaDataProvider {
8 /** @var resource|null */
9 protected $file = null;
10 /** @var string[]|null */
11 protected $current = null;
12 /** @var array */
13 protected static $static = [
14 '1E0A 0323;1E0C 0307;0044 0323 0307;1E0C 0307;0044 0323 0307;',
15 false
16 ];
17
18 /**
19 * @param string|null &$message Message to report when skipping
20 * @return bool Whether the test can be run
21 */
22 public static function available( &$message = null ) {
23 if ( is_readable( __DIR__ . '/NormalizationTest.txt' ) ) {
24 return true;
25 }
26 $message = wordwrap( 'Download the Unicode Normalization Test Suite from ' .
27 'http://unicode.org/Public/8.0.0/ucd/NormalizationTest.txt and save as ' .
28 __DIR__ . '/NormalizationTest.txt to run normalization tests. Note that ' .
29 'running these tests takes quite some time.' );
30 return false;
31 }
32
33 /**
34 * @param LuaEngine $engine
35 */
36 public function __construct( $engine ) {
37 parent::__construct( $engine, 'UstringLibraryNormalizationTests' );
38 if ( self::available() ) {
39 $this->file = fopen( __DIR__ . '/NormalizationTest.txt', 'r' );
40 }
41 $this->rewind();
42 }
43
44 public function destroy() {
45 if ( $this->file ) {
46 fclose( $this->file );
47 $this->file = null;
48 }
49 parent::destroy();
50 }
51
52 public function rewind(): void {
53 if ( $this->file ) {
54 rewind( $this->file );
55 }
56 $this->key = 0;
57 $this->next();
58 }
59
60 public function valid(): bool {
61 if ( $this->file ) {
62 $v = !feof( $this->file );
63 } else {
64 $v = $this->key < count( self::$static );
65 }
66 return $v;
67 }
68
69 #[\ReturnTypeWillChange]
70 public function current() {
71 return $this->current;
72 }
73
74 public function next(): void {
75 $this->current = [ null, null, null, null, null, null ];
76 while ( $this->valid() ) {
77 if ( $this->file ) {
78 $line = fgets( $this->file );
79 } else {
80 $line = self::$static[$this->key];
81 }
82 $this->key++;
83 if ( preg_match( '/^((?:[0-9A-F ]+;){5})/', $line, $m ) ) {
84 $line = rtrim( $m[1], ';' );
85 $ret = [ $line ];
86 foreach ( explode( ';', $line ) as $field ) {
87 $args = [];
88 foreach ( explode( ' ', $field ) as $char ) {
89 $args[] = hexdec( $char );
90 }
91 $s = pack( 'N*', ...$args );
92 $s = mb_convert_encoding( $s, 'UTF-8', 'UTF-32BE' );
93 $ret[] = $s;
94 }
95 $this->current = $ret;
96 return;
97 }
98 }
99 }
100
101 /**
102 * Run the normalization test
103 * @param string $c1 Column 1 from NormalizationTest.txt
104 * @param string $c2 Column 2 from NormalizationTest.txt
105 * @param string $c3 Column 3 from NormalizationTest.txt
106 * @param string $c4 Column 4 from NormalizationTest.txt
107 * @param string $c5 Column 5 from NormalizationTest.txt
108 * @return array
109 */
110 public function runNorm( $c1, $c2, $c3, $c4, $c5 ) {
111 return $this->engine->getInterpreter()->callFunction( $this->exports['run'],
112 $c1, $c2, $c3, $c4, $c5
113 );
114 }
115 }