rob-revisited/ide/tests/MessagesTest.php

136 lines
6 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Ide;
use JumPHP\Ide\Lang\Messages;
use JumPHP\Ide\Workspace\MemoryWorkspace;
use PHPUnit\Framework\TestCase;
/** The texts the PHP side shows, in the interface's language. */
final class MessagesTest extends TestCase
{
private const OTHERS = ['fr', 'es', 'de', 'it', 'pt', 'nl', 'pl', 'ru', 'uk', 'tr', 'ja', 'zh', 'ko'];
/** Technical lines that read the same in some languages (a program's name and its detail). */
private const SAME_AS_ENGLISH = ['ElevenLabs: {detail}', 'Claude: {detail}', 'OpenCode: {detail}', 'OpenAI Images: {detail}', 'Stable Diffusion: {detail}',
'git {name}: {detail}', 'update.json: HTTP {status}', 'download: HTTP {status}', 'claude {command}: {detail}', 'terminal {tty}', 'Status: {status}', 'Community'];
protected function tearDown(): void
{
Messages::reset();
}
public function testEnglishIsTheKeyAndPlaceholdersAreFilled(): void
{
Messages::setLocale('en');
self::assertSame('/a/b already exists', Messages::t('{path} already exists', ['path' => '/a/b']));
self::assertSame('{path} already exists', Messages::t('{path} already exists'));
}
public function testTheLanguageIsChosenByTheInterface(): void
{
Messages::setLocale('fr');
self::assertSame('/a/b existe déjà', Messages::t('{path} already exists', ['path' => '/a/b']));
Messages::setLocale('ja-JP');
self::assertSame('/a/b は既に存在します', Messages::t('{path} already exists', ['path' => '/a/b']));
Messages::setLocale('pt_BR');
self::assertSame('/a/b já existe', Messages::t('{path} already exists', ['path' => '/a/b']));
}
public function testTheCommandIdeLangSetsTheLanguage(): void
{
$ide = new Ide(new MemoryWorkspace([]));
self::assertTrue($ide->has('ide.lang'));
self::assertSame('de', $ide->handle('ide.lang', ['lang' => 'de']));
self::assertSame('Das Hochladen ist fehlgeschlagen (x)', Messages::t('The upload failed ({detail})', ['detail' => 'x']));
self::assertSame('en', $ide->handle('ide.lang', ['lang' => 'xx']));
self::assertSame('The upload failed (x)', Messages::t('The upload failed ({detail})', ['detail' => 'x']));
}
public function testUnknownTextsAndLanguagesFallBackToEnglish(): void
{
Messages::setLocale('fr');
self::assertSame('A text nobody translated 3', Messages::t('A text nobody translated {n}', ['n' => 3]));
Messages::setLocale('klingon');
self::assertSame('en', Messages::locale());
self::assertSame('not a zip', Messages::t('not a zip'));
}
public function testTheEnvironmentIsTheDefault(): void
{
$old = getenv('JUMPHP_LANG');
putenv('JUMPHP_LANG=es_ES.UTF-8');
Messages::reset();
try {
self::assertSame('es', Messages::locale());
} finally {
$old === false ? putenv('JUMPHP_LANG') : putenv("JUMPHP_LANG=$old");
}
}
public function testEveryLiteralOfTheSourcesIsTranslatedInEveryLanguage(): void
{
$keys = self::sourceKeys();
self::assertGreaterThan(100, count($keys));
foreach (self::OTHERS as $lang) {
$dictionary = Messages::dictionary($lang);
foreach ($keys as $key => $where) {
self::assertArrayHasKey($key, $dictionary, "$lang lacks \"$key\" (used in $where)");
}
}
}
public function testTheDictionariesAreClean(): void
{
$english = array_keys(Messages::dictionary('fr'));
foreach (self::OTHERS as $lang) {
$dictionary = Messages::dictionary($lang);
self::assertSame($english, array_keys($dictionary), "$lang does not hold the same texts as fr");
foreach ($dictionary as $key => $value) {
self::assertNotSame('', trim($value), "$lang: empty value for \"$key\"");
self::assertSame(self::placeholders($key), self::placeholders($value), "$lang: placeholders differ for \"$key\"");
$letters = preg_replace('/\{\w+\}/', '', $key);
if (preg_match('/[a-z]{3}/i', (string)$letters) === 1 && !in_array($key, self::SAME_AS_ENGLISH, true)) {
self::assertNotSame($key, $value, "$lang: \"$key\" is still English");
}
}
}
}
public function testNoDictionaryHoldsATextTheSourcesDoNotUse(): void
{
$keys = self::sourceKeys();
$unused = array_diff(array_keys(Messages::dictionary('fr')), array_keys($keys));
// texts of ide/src/Claude and ide/src/Remote may be added to the dictionaries before their source is converted
self::assertSame([], array_values($unused), 'dictionary texts no source uses');
}
/** @return list<string> */
private static function placeholders(string $text): array
{
preg_match_all('/\{\w+\}/', $text, $m);
sort($m[0]);
return $m[0];
}
/** The English literals given to Messages::t() in the PHP sources of the IDE: text => file. @return array<string, string> */
private static function sourceKeys(): array
{
$keys = [];
foreach ([dirname(__DIR__) . '/src', dirname(__DIR__, 2) . '/games/ide/src'] as $dir) {
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS)) as $file) {
if ($file->getExtension() !== 'php' || str_ends_with($file->getPathname(), 'Lang/Messages.php')) {
continue;
}
$code = (string)file_get_contents($file->getPathname());
if (preg_match_all("/Messages::t\\(\\s*'((?:[^'\\\\]|\\\\.)*)'/", $code, $m) === 0) {
continue;
}
foreach ($m[1] as $literal) {
$keys[str_replace(["\\'", '\\\\'], ["'", '\\'], $literal)] = basename($file->getPathname());
}
}
}
return $keys;
}
}