contentdb/app/tests/unit/test_translation.py

62 lines
1.7 KiB
Python
Raw Normal View History

2024-02-25 17:32:54 +01:00
import os
2024-02-25 22:13:35 +01:00
import pytest
2024-02-25 17:32:54 +01:00
from app.tasks.minetestcheck.translation import parse_tr
def test_parses_tr():
dirname = os.path.dirname(__file__)
2024-02-25 22:13:35 +01:00
filepath = os.path.join(dirname, "foo.bar.fr.tr")
out = parse_tr(filepath)
assert out.language == "fr"
assert out.textdomain == "foo.bar"
assert len(out.entries) == 5
assert out.entries["Hello, World!"] == "Bonjour, Monde!"
assert out.entries["Hello @1!"] == "@1, salut!"
assert out.entries["Cats = cool"] == "Chats = cool"
assert out.entries["A \n newline"] == "Une \nnouvelle ligne"
assert out.entries["Maybe @\n@=@"] == "Peut être @\n@=@"
def test_parses_tr_infers_textdomain():
dirname = os.path.dirname(__file__)
filepath = os.path.join(dirname, "no_textdomain_comment.fr.tr")
2024-02-25 17:32:54 +01:00
out = parse_tr(filepath)
assert out.language == "fr"
2024-02-25 22:13:35 +01:00
assert out.textdomain == "no_textdomain_comment"
2024-02-25 17:32:54 +01:00
assert len(out.entries) == 1
assert out.entries["Hello, World!"] == "Bonjour, Monde!"
2024-02-25 22:13:35 +01:00
def test_parses_tr_error_on_textdomain_mismatch():
dirname = os.path.dirname(__file__)
filepath = os.path.join(dirname, "textdomain_mismatch.fr.tr")
with pytest.raises(SyntaxError) as e:
parse_tr(filepath)
assert str(e.value) == "Line 1: The filename's textdomain (textdomain_mismatch) should match the comment (foobar)"
def test_parses_tr_error_on_missing_eq():
dirname = os.path.dirname(__file__)
filepath = os.path.join(dirname, "err_missing_eq.fr.tr")
with pytest.raises(SyntaxError) as e:
parse_tr(filepath)
assert str(e.value) == "Line 4: Missing = in line"
def test_parses_tr_error_on_bad_escape():
dirname = os.path.dirname(__file__)
filepath = os.path.join(dirname, "bad_escape.fr.tr")
with pytest.raises(SyntaxError) as e:
parse_tr(filepath)
assert str(e.value) == "Line 1: Unknown escape character: x"