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