You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
meshcore-client/tools/l10n/test_check_arb.py

263 lines
10 KiB

"""Tests for check_arb.py (#1323): one deliberately broken string per rule."""
import io
import json
import os
import sys
import tempfile
import unittest
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import check_arb # noqa: E402
REPO = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
ENGLISH = {
"@@locale": "en",
"greeting": "Hello {name}",
"@greeting": {"placeholders": {"name": {"type": "String"}}},
"hops": "{count} {count, plural, =1{hop} other{hops}}",
"minutes": "{minutes} min",
"@minutes": {"placeholders": {"minutes": {"type": "int"}}},
"pronoun": "{gender, select, male{He} female{She} other{They}}",
"percent": "{percent}%",
"chat": "Chat",
"title": "Offband MeshCore",
"untranslated": "Delete All Paths",
}
GERMAN = {
"@@locale": "de",
"greeting": "Hallo {name}",
"hops": "{count} {count, plural, =1{Hop} other{Hops}}",
"minutes": "{minutes} Min.",
"pronoun": "{gender, select, male{Er} female{Sie} other{Sie}}",
"percent": "{percent}%",
"chat": "Unterhaltung",
"title": "Offband MeshCore",
"untranslated": "Alle Pfade löschen",
}
CONFIG = {
"fail_on_missing": False,
"allow_identical": {"title": "*"},
"pending_identical": {},
}
class CheckArbTest(unittest.TestCase):
def run_check(self, german=None, config=None, raw_german=None, english=None, l10n_yaml=None):
with tempfile.TemporaryDirectory() as tmp:
l10n_dir = os.path.join(tmp, "lib", "l10n")
os.makedirs(l10n_dir)
if l10n_yaml is not None:
with open(os.path.join(tmp, "l10n.yaml"), "w", encoding="utf-8") as fh:
fh.write(l10n_yaml)
with open(os.path.join(l10n_dir, "app_en.arb"), "w", encoding="utf-8") as fh:
json.dump(english or ENGLISH, fh, ensure_ascii=False)
with open(os.path.join(l10n_dir, "app_de.arb"), "w", encoding="utf-8") as fh:
fh.write(raw_german if raw_german is not None else json.dumps(german or GERMAN, ensure_ascii=False))
config_path = os.path.join(tmp, "config.json")
with open(config_path, "w", encoding="utf-8") as fh:
json.dump(config or CONFIG, fh)
out = io.StringIO()
code = check_arb.run(l10n_dir, config_path, out)
return code, out.getvalue()
def assert_fails(self, german, expected, config=None):
code, out = self.run_check(german, config)
self.assertEqual(code, 1, out)
self.assertIn(expected, out)
def with_german(self, **changes):
german = dict(GERMAN)
for key, value in changes.items():
if value is None:
german.pop(key)
else:
german[key] = value
return german
def test_a_correct_translation_passes(self):
code, out = self.run_check()
self.assertEqual(code, 0, out)
self.assertIn("check_arb: OK", out)
def test_dropped_placeholder_fails(self):
self.assert_fails(self.with_german(greeting="Hallo"), "'greeting': drops {name}")
def test_renamed_placeholder_fails(self):
code, out = self.run_check(self.with_german(greeting="Hallo {nmae}"))
self.assertEqual(code, 1, out)
self.assertIn("'greeting': drops {name}", out)
self.assertIn("'greeting': adds {nmae}", out)
def test_added_placeholder_fails(self):
self.assert_fails(self.with_german(greeting="Hallo {name} {extra}"), "'greeting': adds {extra}")
def test_plural_without_other_fails(self):
self.assert_fails(
self.with_german(hops="{count} {count, plural, =1{Hop}}"),
"'{count, plural' has no 'other' case",
)
def test_unknown_plural_category_fails(self):
self.assert_fails(
self.with_german(hops="{count} {count, plural, =1{Hop} several{Hops} other{Hops}}"),
"unknown cases: several",
)
def test_unclosed_brace_fails(self):
self.assert_fails(self.with_german(greeting="Hallo {name"), "'greeting': does not parse")
def test_stray_closing_brace_fails(self):
self.assert_fails(self.with_german(greeting="Hallo {name}}"), "'greeting': does not parse")
def test_unbalanced_plural_fails(self):
self.assert_fails(
self.with_german(hops="{count} {count, plural, =1{Hop other{Hops}}"),
"'hops': does not parse",
)
def test_renamed_selector_variable_fails(self):
self.assert_fails(
self.with_german(hops="{count} {anzahl, plural, =1{Hop} other{Hops}}"),
"'hops': adds {anzahl}",
)
def test_select_without_other_fails(self):
self.assert_fails(
self.with_german(pronoun="{gender, select, male{Er} female{Sie}}"),
"'{gender, select' has no 'other' case",
)
def test_repeated_case_fails(self):
self.assert_fails(
self.with_german(hops="{count} {count, plural, one{Hop} one{Sprung} other{Hops}}"),
"repeats cases: one",
)
def test_plural_over_a_text_placeholder_fails(self):
self.assert_fails(
self.with_german(greeting="Hallo {name, plural, one{du} other{ihr}}"),
"'{name, plural' counts a placeholder English does not declare as a number",
)
def test_plural_over_a_number_placeholder_passes(self):
code, out = self.run_check(
self.with_german(minutes="{minutes} {minutes, plural, one{Minute} other{Minuten}}")
)
self.assertEqual(code, 0, out)
def test_malformed_english_fails(self):
english = dict(ENGLISH, greeting="Hello {name")
code, out = self.run_check(english=english)
self.assertEqual(code, 1, out)
self.assertIn("FAIL app_en.arb: 'greeting' does not parse", out)
def test_escaping_turned_on_fails(self):
code, out = self.run_check(l10n_yaml="arb-dir: lib/l10n\nuse-escaping: true\n")
self.assertEqual(code, 1, out)
self.assertIn("use-escaping is on", out)
def test_escaping_turned_on_with_a_quoted_value_fails(self):
code, out = self.run_check(l10n_yaml='use-escaping: "true"\n')
self.assertEqual(code, 1, out)
self.assertIn("use-escaping is on", out)
def test_english_value_that_is_not_a_string_fails(self):
english = dict(ENGLISH, chat=123)
code, out = self.run_check(english=english)
self.assertEqual(code, 1, out)
self.assertIn("FAIL app_en.arb: 'chat' is not a string", out)
def test_escaping_left_off_passes(self):
code, out = self.run_check(l10n_yaml="arb-dir: lib/l10n\nuse-escaping: false\n")
self.assertEqual(code, 0, out)
def test_dropping_a_plural_block_but_keeping_the_count_passes(self):
code, out = self.run_check(self.with_german(hops="{count} Hops"))
self.assertEqual(code, 0, out)
def test_empty_translation_fails(self):
self.assert_fails(self.with_german(chat=""), "'chat': empty translation")
def test_identical_to_english_fails(self):
self.assert_fails(self.with_german(chat="Chat"), "'chat' is identical to English")
def test_identical_apart_from_spacing_fails(self):
self.assert_fails(self.with_german(chat=" Chat "), "'chat' is identical to English")
def test_a_change_of_case_alone_is_a_translation(self):
english = dict(ENGLISH, minutes="{minutes} min.")
code, out = self.run_check(self.with_german(minutes="{minutes} Min."), english=english)
self.assertEqual(code, 0, out)
def test_identical_to_english_is_allowed_when_allowlisted(self):
config = dict(CONFIG, allow_identical={"title": "*", "chat": ["de"]})
code, out = self.run_check(self.with_german(chat="Chat"), config)
self.assertEqual(code, 0, out)
def test_allowlist_for_another_locale_does_not_cover_this_one(self):
config = dict(CONFIG, allow_identical={"title": "*", "chat": ["fr"]})
self.assert_fails(self.with_german(chat="Chat"), "'chat' is identical to English", config)
def test_identical_to_english_is_allowed_while_pending(self):
config = dict(CONFIG, pending_identical={"de": ["untranslated"]})
code, out = self.run_check(self.with_german(untranslated="Delete All Paths"), config)
self.assertEqual(code, 0, out)
def test_pending_entry_that_is_now_translated_fails(self):
config = dict(CONFIG, pending_identical={"de": ["untranslated"]})
self.assert_fails(GERMAN, "'untranslated' is listed in pending_identical", config)
def test_text_with_no_words_may_match_english(self):
code, out = self.run_check(self.with_german(percent="{percent}%"))
self.assertEqual(code, 0, out)
def test_key_missing_from_english_fails(self):
self.assert_fails(self.with_german(gone="Weg"), "'gone' is not in app_en.arb")
def test_metadata_for_a_key_missing_from_english_fails(self):
self.assert_fails(self.with_german(**{"@gone": {}}), "'@gone' is metadata")
def test_missing_key_is_reported_not_failed(self):
code, out = self.run_check(self.with_german(chat=None))
self.assertEqual(code, 0, out)
self.assertIn("note app_de.arb: 1 keys not translated yet", out)
def test_missing_key_fails_once_fail_on_missing_is_set(self):
config = dict(CONFIG, fail_on_missing=True)
self.assert_fails(self.with_german(chat=None), "1 keys missing: chat", config)
def test_duplicate_key_fails(self):
raw = json.dumps(GERMAN, ensure_ascii=False)[:-1] + ', "chat": "Plaudern"}'
code, out = self.run_check(raw_german=raw)
self.assertEqual(code, 1, out)
self.assertIn("duplicate key 'chat'", out)
def test_locale_that_does_not_match_the_file_name_fails(self):
self.assert_fails(self.with_german(**{"@@locale": "fr"}), "@@locale is 'fr'")
class RepositoryTest(unittest.TestCase):
def test_the_repository_passes(self):
out = io.StringIO()
code = check_arb.run(
os.path.join(REPO, "lib", "l10n"),
os.path.join(REPO, "tools", "l10n", "l10n_check.json"),
out,
)
self.assertEqual(code, 0, out.getvalue())
def test_every_allowlisted_key_exists(self):
template, _ = check_arb.load_arb(os.path.join(REPO, "lib", "l10n", "app_en.arb"))
config = check_arb.load_config(os.path.join(REPO, "tools", "l10n", "l10n_check.json"))
stale = [k for k in config["allow_identical"] if k not in template]
self.assertEqual(stale, [])
if __name__ == "__main__":
unittest.main()

Powered by TurnKey Linux.