| 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 EnTest extends TestCase { |
| 13 |
|
| 14 |
private const CODES = [ 'en', 'en-x-piglatin' ]; |
| 15 |
|
| 16 |
// phpcs:disable Generic.Files.LineLength.TooLong |
| 17 |
private const TEST_CASES = [ |
| 18 |
[ |
| 19 |
'title' => 'Converting to Pig Latin', |
| 20 |
'output' => [ |
| 21 |
'en' => "123 Pigpen pig latin of 123 don't stop believing in yourself queen JavaScript NASA", |
| 22 |
'en-x-piglatin' => "123 Igpenpay igpay atinlay ofway 123 on'tday opstay elievingbay inway ourselfyay eenquay JavaScript NASA" |
| 23 |
], |
| 24 |
'input' => "123 Pigpen pig latin of 123 don't stop believing in yourself queen JavaScript NASA", |
| 25 |
'code' => 'en' |
| 26 |
], |
| 27 |
[ |
| 28 |
'title' => 'Converting from Pig Latin', |
| 29 |
'output' => [ |
| 30 |
'en' => "123 Pigpen pig latin of 123 don't tops believing in yourself queen avaScriptJay ASANAY", |
| 31 |
'en-x-piglatin' => "123 Igpenpayway igpayway atinlayway ofwayway 123 on'tdayway opstayway elievingbayway inwayway ourselfyayway eenquayway avaScriptJay ASANAY" |
| 32 |
], |
| 33 |
'input' => "123 Igpenpay igpay atinlay ofway 123 on'tday opstay elievingbay inway ourselfyay eenquay avaScriptJay ASANAY", |
| 34 |
// XXX: this is currently treated as just a guess, so it doesn't |
| 35 |
// prevent pig latin from being double-encoded. |
| 36 |
'code' => 'en-x-piglatin' |
| 37 |
] |
| 38 |
]; |
| 39 |
|
| 40 |
/** @var ReplacementMachine */ |
| 41 |
private static $machine; |
| 42 |
|
| 43 |
public static function setUpBeforeClass(): void { |
| 44 |
$lang = LanguageConverter::loadLanguage( |
| 45 |
new MockEnv( [] ), |
| 46 |
new Bcp47CodeValue( 'en' ) |
| 47 |
); |
| 48 |
self::$machine = $lang->getConverter()->getMachine(); |
| 49 |
} |
| 50 |
|
| 51 |
public static function tearDownAfterClass(): void { |
| 52 |
self::$machine = null; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @covers \Wikimedia\LangConv\FST |
| 57 |
* @dataProvider provideEn |
| 58 |
*/ |
| 59 |
public function testEn( string $title, array $output, string $input, ?string $invertCode ) { |
| 60 |
foreach ( self::CODES as $variantCode ) { |
| 61 |
if ( !array_key_exists( $variantCode, $output ) ) { |
| 62 |
continue; |
| 63 |
} |
| 64 |
|
| 65 |
$doc = DOMCompat::newDocument( true ); |
| 66 |
$out = self::$machine->convert( |
| 67 |
$doc, $input, $variantCode, |
| 68 |
$invertCode ?? $this->getInvertCode( $variantCode ) |
| 69 |
); |
| 70 |
$expected = $output[$variantCode]; |
| 71 |
$this->assertEquals( $expected, $out->textContent ); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
public function provideEn() { |
| 76 |
return array_map( static function ( $item ) { |
| 77 |
return [ |
| 78 |
$item['title'], |
| 79 |
$item['output'], |
| 80 |
$item['input'], |
| 81 |
$item['code'] ?? null |
| 82 |
]; |
| 83 |
}, self::TEST_CASES ); |
| 84 |
} |
| 85 |
|
| 86 |
private function getInvertCode( $variantCode ) { |
| 87 |
return $variantCode === "en" ? "en-x-piglatin" : "en"; |
| 88 |
} |
| 89 |
} |