| 1 |
<?php |
| 2 |
|
| 3 |
namespace Test\Parsoid\Language; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use Wikimedia\Bcp47Code\Bcp47CodeValue; |
| 7 |
use Wikimedia\LangConv\ReplacementMachine; |
| 8 |
use Wikimedia\Parsoid\Language\LanguageConverter; |
| 9 |
use Wikimedia\Parsoid\Mocks\MockEnv; |
| 10 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 11 |
|
| 12 |
class ZhTest extends TestCase { |
| 13 |
|
| 14 |
private const CODES = [ |
| 15 |
"zh-cn", "zh-sg", "zh-my", "zh-hans", "zh-tw", "zh-hk", "zh-mo", "zh-hant" |
| 16 |
]; |
| 17 |
|
| 18 |
// phpcs:disable Generic.Files.LineLength.TooLong |
| 19 |
private const TEST_CASES = [ |
| 20 |
[ |
| 21 |
'title' => 'Plain hant -> hans', |
| 22 |
'output' => [ |
| 23 |
'zh' => "㑯", |
| 24 |
'zh-hans' => "㑔", |
| 25 |
'zh-hant' => "㑯", |
| 26 |
'zh-cn' => "㑔", |
| 27 |
'zh-hk' => "㑯", |
| 28 |
'zh-mo' => "㑯", |
| 29 |
'zh-my' => "㑔", |
| 30 |
'zh-sg' => "㑔", |
| 31 |
'zh-tw' => "㑯" |
| 32 |
], |
| 33 |
'input' => "㑯" |
| 34 |
], |
| 35 |
[ |
| 36 |
'title' => 'Plain hans -> hant', |
| 37 |
'output' => [ |
| 38 |
'zh' => "㐷", |
| 39 |
'zh-hans' => "㐷", |
| 40 |
'zh-hant' => "傌", |
| 41 |
'zh-cn' => "㐷", |
| 42 |
'zh-hk' => "傌", |
| 43 |
'zh-mo' => "傌", |
| 44 |
'zh-my' => "㐷", |
| 45 |
'zh-sg' => "㐷", |
| 46 |
'zh-tw' => "傌" |
| 47 |
], |
| 48 |
'input' => "㐷" |
| 49 |
], |
| 50 |
[ |
| 51 |
'title' => 'zh-cn specific', |
| 52 |
'output' => [ |
| 53 |
'zh' => "仲介", |
| 54 |
'zh-hans' => "仲介", |
| 55 |
'zh-hant' => "仲介", |
| 56 |
'zh-cn' => "中介", |
| 57 |
'zh-hk' => "仲介", |
| 58 |
'zh-mo' => "仲介", |
| 59 |
'zh-my' => "中介", |
| 60 |
'zh-sg' => "中介", |
| 61 |
'zh-tw' => "仲介" |
| 62 |
], |
| 63 |
'input' => "仲介" |
| 64 |
], |
| 65 |
[ |
| 66 |
'title' => 'zh-hk specific', |
| 67 |
'output' => [ |
| 68 |
'zh' => "中文里", |
| 69 |
'zh-hans' => "中文里", |
| 70 |
'zh-hant' => "中文裡", |
| 71 |
'zh-cn' => "中文里", |
| 72 |
'zh-hk' => "中文裏", |
| 73 |
'zh-mo' => "中文裏", |
| 74 |
'zh-my' => "中文里", |
| 75 |
'zh-sg' => "中文里", |
| 76 |
'zh-tw' => "中文裡" |
| 77 |
], |
| 78 |
'input' => "中文里" |
| 79 |
], |
| 80 |
[ |
| 81 |
'title' => 'zh-tw specific', |
| 82 |
'output' => [ |
| 83 |
'zh' => "甲肝", |
| 84 |
'zh-hans' => "甲肝", |
| 85 |
'zh-hant' => "甲肝", |
| 86 |
'zh-cn' => "甲肝", |
| 87 |
'zh-hk' => "甲肝", |
| 88 |
'zh-mo' => "甲肝", |
| 89 |
'zh-my' => "甲肝", |
| 90 |
'zh-sg' => "甲肝", |
| 91 |
'zh-tw' => "A肝" |
| 92 |
], |
| 93 |
'input' => "甲肝" |
| 94 |
], |
| 95 |
[ |
| 96 |
'title' => 'zh-tw overrides zh-hant', |
| 97 |
'output' => [ |
| 98 |
'zh' => "账", |
| 99 |
'zh-hans' => "账", |
| 100 |
'zh-hant' => "賬", |
| 101 |
'zh-cn' => "账", |
| 102 |
'zh-hk' => "賬", |
| 103 |
'zh-mo' => "賬", |
| 104 |
'zh-my' => "账", |
| 105 |
'zh-sg' => "账", |
| 106 |
'zh-tw' => "帳" |
| 107 |
], |
| 108 |
'input' => "账" |
| 109 |
], |
| 110 |
[ |
| 111 |
'title' => 'zh-hk overrides zh-hant', |
| 112 |
'output' => [ |
| 113 |
'zh' => "一地里", |
| 114 |
'zh-hans' => "一地里", |
| 115 |
'zh-hant' => "一地裡", |
| 116 |
'zh-cn' => "一地里", |
| 117 |
'zh-hk' => "一地裏", |
| 118 |
'zh-mo' => "一地裏", |
| 119 |
'zh-my' => "一地里", |
| 120 |
'zh-sg' => "一地里", |
| 121 |
'zh-tw' => "一地裡" |
| 122 |
], |
| 123 |
'input' => "一地里" |
| 124 |
] |
| 125 |
]; |
| 126 |
|
| 127 |
/** @var ReplacementMachine */ |
| 128 |
private static $machine; |
| 129 |
|
| 130 |
public static function setUpBeforeClass(): void { |
| 131 |
$lang = LanguageConverter::loadLanguage( |
| 132 |
new MockEnv( [] ), |
| 133 |
new Bcp47CodeValue( 'zh' ) |
| 134 |
); |
| 135 |
self::$machine = $lang->getConverter()->getMachine(); |
| 136 |
} |
| 137 |
|
| 138 |
public static function tearDownAfterClass(): void { |
| 139 |
self::$machine = null; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* @covers \Wikimedia\LangConv\FST |
| 144 |
* @dataProvider provideZh |
| 145 |
*/ |
| 146 |
public function testZh( string $title, array $output, string $input, ?string $invertCode ) { |
| 147 |
foreach ( self::CODES as $variantCode ) { |
| 148 |
if ( !array_key_exists( $variantCode, $output ) ) { |
| 149 |
continue; |
| 150 |
} |
| 151 |
|
| 152 |
$doc = DOMCompat::newDocument( true ); |
| 153 |
$out = self::$machine->convert( |
| 154 |
$doc, $input, $variantCode, |
| 155 |
$invertCode ?? $this->getInvertCode( $variantCode ) |
| 156 |
); |
| 157 |
$expected = $output[$variantCode]; |
| 158 |
$this->assertEquals( $expected, $out->textContent ); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
public function provideZh() { |
| 163 |
return array_map( static function ( $item ) { |
| 164 |
return [ |
| 165 |
$item['title'], |
| 166 |
$item['output'], |
| 167 |
$item['input'], |
| 168 |
$item['code'] ?? null |
| 169 |
]; |
| 170 |
}, self::TEST_CASES ); |
| 171 |
} |
| 172 |
|
| 173 |
private function getInvertCode( $variantCode ) { |
| 174 |
return (bool)preg_match( '/^zh-(cn|sg|my|hans)$/', $variantCode ) ? "zh-hant" : "zh-hans"; |
| 175 |
} |
| 176 |
|
| 177 |
} |