Debian Codegraph @

vendor/wikimedia/parsoid/tests/phpunit/Parsoid/Language/SrTest.php

raw
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 SrTest extends TestCase {
13
14 private const CODES = [ "sr-ec", "sr-el" ];
15
16 // phpcs:disable Generic.Files.LineLength.TooLong
17 private const TEST_CASES = [
18 [
19 'title' => 'A simple conversion of Latin to Cyrillic',
20 'output' => [
21 'sr-ec' => "абвг"
22 ],
23 'input' => 'abvg'
24 ],
25 /*
26 [
27 'title' => 'Same as above, but assert that -{}-s must be removed and not converted',
28 'output' => [
29 // XXX: we don't support embedded -{}- markup in mocha tests;
30 // use parserTests for that
31 // 'sr-ec' => 'ljабnjвгdž'
32 ],
33 'input' => "<span typeof=\"mw:LanguageVariant\" data-mw-variant='{\"disabled\":{\"t\":\"lj\"}}'></span>аб<span typeof=\"mw:LanguageVariant\" data-mw-variant='{\"disabled\":{\"t\":\"nj\"}}'></span>вг<span typeof=\"mw:LanguageVariant\" data-mw-variant='{\"disabled\":{\"t\":\"dž\"}}'></span>"
34 ]
35 */
36 ];
37
38 /** @var ReplacementMachine */
39 private static $machine;
40
41 public static function setUpBeforeClass(): void {
42 $lang = LanguageConverter::loadLanguage(
43 new MockEnv( [] ),
44 new Bcp47CodeValue( 'sr' )
45 );
46 self::$machine = $lang->getConverter()->getMachine();
47 }
48
49 public static function tearDownAfterClass(): void {
50 self::$machine = null;
51 }
52
53 /**
54 * @covers \Wikimedia\LangConv\FST
55 * @dataProvider provideSr
56 */
57 public function testSr( string $title, array $output, string $input, ?string $invertCode ) {
58 foreach ( self::CODES as $variantCode ) {
59 if ( !array_key_exists( $variantCode, $output ) ) {
60 continue;
61 }
62
63 $doc = DOMCompat::newDocument( true );
64 $out = self::$machine->convert(
65 $doc, $input, $variantCode,
66 $invertCode ?? $this->getInvertCode( $variantCode )
67 );
68 $expected = $output[$variantCode];
69 $this->assertEquals( $expected, $out->textContent );
70 }
71 }
72
73 public function provideSr() {
74 return array_map( static function ( $item ) {
75 return [
76 $item['title'],
77 $item['output'],
78 $item['input'],
79 $item['code'] ?? null
80 ];
81 }, self::TEST_CASES );
82 }
83
84 private function getInvertCode( $variantCode ) {
85 return $variantCode === "sr-ec" ? "sr-el" : "sr-ec";
86 }
87
88 }