Fix textdomain validation in .tr parser

This commit is contained in:
rubenwardy
2024-02-26 01:18:38 +00:00
parent 40d572d645
commit 966023be17
5 changed files with 18 additions and 10 deletions

View File

@@ -1 +1,2 @@
# textdomain: bad_args
Some @1 args @5=Some @1 args @5

View File

@@ -1 +1,2 @@
# textdomain: bad_escape
Bad @x escape = Bad @x escape

View File

@@ -20,15 +20,14 @@ def test_parses_tr():
assert out.entries["Maybe @\n@=@"] == "Peut être @\n@=@"
def test_parses_tr_infers_textdomain():
def test_parses_tr_error_on_missing_textdomain():
dirname = os.path.dirname(__file__)
filepath = os.path.join(dirname, "no_textdomain_comment.fr.tr")
out = parse_tr(filepath)
assert out.language == "fr"
assert out.textdomain == "no_textdomain_comment"
assert len(out.entries) == 1
assert out.entries["Hello, World!"] == "Bonjour, Monde!"
with pytest.raises(SyntaxError) as e:
parse_tr(filepath)
assert str(e.value) == "Missing `# textdomain: no_textdomain_comment` at the top of the file"
def test_parses_tr_error_on_textdomain_mismatch():
@@ -58,7 +57,7 @@ def test_parses_tr_error_on_bad_escape():
with pytest.raises(SyntaxError) as e:
parse_tr(filepath)
assert str(e.value) == "Line 1: Unknown escape character: x"
assert str(e.value) == "Line 2: Unknown escape character: x"
def test_parses_tr_error_on_bad_args():
@@ -68,7 +67,7 @@ def test_parses_tr_error_on_bad_args():
with pytest.raises(SyntaxError) as e:
parse_tr(filepath)
assert "Line 1: Arguments out of order in source, found @5 and expected @2." in str(e.value)
assert "Line 2: Arguments out of order in source, found @5 and expected @2." in str(e.value)
def test_parses_tr_error_on_unknown_arg():
@@ -78,4 +77,4 @@ def test_parses_tr_error_on_unknown_arg():
with pytest.raises(SyntaxError) as e:
parse_tr(filepath)
assert str(e.value) == "Line 1: Unknown argument @2 in translated string"
assert str(e.value) == "Line 2: Unknown argument @2 in translated string"

View File

@@ -1 +1,2 @@
# textdomain: unknown_arg
Some @1 arg=Some @2 arg