Debian Codegraph @

tests/messages/test_pofile.py

raw
1 #
2 # Copyright (C) 2007-2011 Edgewall Software, 2013-2025 the Babel team
3 # All rights reserved.
4 #
5 # This software is licensed as described in the file LICENSE, which
6 # you should have received as part of this distribution. The terms
7 # are also available at https://github.com/python-babel/babel/blob/master/LICENSE.
8 #
9 # This software consists of voluntary contributions made by many
10 # individuals. For the exact contribution history, see the revision
11 # history and logs, available at https://github.com/python-babel/babel/commits/master/.
12
13 from io import BytesIO, StringIO
14
15 import pytest
16
17 from babel.core import Locale
18 from babel.messages import pofile
19 from babel.messages.catalog import Catalog
20 from babel.messages.pofile import _enclose_filename_if_necessary, _extract_locations
21
22
23 def test_enclosed_filenames_in_location_comment():
24 catalog = Catalog()
25 catalog.add("foo", lineno=2, locations=[("main 1.py", 1)], string="")
26 catalog.add("bar", lineno=6, locations=[("other.py", 2)], string="")
27 catalog.add("baz", lineno=10, locations=[("main 1.py", 3), ("other.py", 4)], string="")
28 buf = BytesIO()
29 pofile.write_po(buf, catalog, omit_header=True, include_lineno=True)
30 buf.seek(0)
31 catalog2 = pofile.read_po(buf)
32 assert True is catalog.is_identical(catalog2)
33
34
35 def test_unescape():
36 escaped = '"Say:\\n \\"hello, world!\\"\\n"'
37 unescaped = 'Say:\n "hello, world!"\n'
38 assert unescaped != escaped
39 assert unescaped == pofile.unescape(escaped)
40
41
42 def test_unescape_of_quoted_newline():
43 # regression test for #198
44 assert pofile.unescape(r'"\\n"') == '\\n'
45
46
47 def test_denormalize_on_msgstr_without_empty_first_line():
48 # handle irregular multi-line msgstr (no "" as first line)
49 # gracefully (#171)
50 msgstr = '"multi-line\\n"\n" translation"'
51 expected_denormalized = 'multi-line\n translation'
52
53 assert expected_denormalized == pofile.denormalize(msgstr)
54 assert expected_denormalized == pofile.denormalize(f'""\n{msgstr}')
55
56
57 @pytest.mark.parametrize(("line", "locations"), [
58 ("\u2068file1.po\u2069", ["file1.po"]),
59 ("file1.po \u2068file 2.po\u2069 file3.po", ["file1.po", "file 2.po", "file3.po"]),
60 ("file1.po:1 \u2068file 2.po\u2069:2 file3.po:3", ["file1.po:1", "file 2.po:2", "file3.po:3"]),
61 ("\u2068file1.po\u2069:1 \u2068file\t2.po\u2069:2 file3.po:3",
62 ["file1.po:1", "file\t2.po:2", "file3.po:3"]),
63 ("file1.po file2.po", ["file1.po", "file2.po"]),
64 ("file1.po \u2068\u2069 file2.po", ["file1.po", "file2.po"]),
65 ])
66 def test_extract_locations_valid_location_comment(line, locations):
67 assert locations == _extract_locations(line)
68
69
70 @pytest.mark.parametrize(("line",), [
71 ("\u2068file 1.po",),
72 ("file 1.po\u2069",),
73 ("\u2069file 1.po\u2068",),
74 ("\u2068file 1.po:1 \u2068file 2.po\u2069:2",),
75 ("\u2068file 1.po\u2069:1 file 2.po\u2069:2",),
76 ])
77 def test_extract_locations_invalid_location_comment(line):
78 with pytest.raises(ValueError):
79 _extract_locations(line)
80
81
82 @pytest.mark.parametrize(("filename",), [
83 ("file.po",),
84 ("file_a.po",),
85 ("file-a.po",),
86 ("file\n.po",),
87 ("\u2068file.po\u2069",),
88 ("\u2068file a.po\u2069",),
89 ])
90 def test_enclose_filename_if_necessary_no_change(filename):
91 assert filename == _enclose_filename_if_necessary(filename)
92
93
94 @pytest.mark.parametrize(("filename",), [
95 ("file a.po",),
96 ("file\ta.po",),
97 ])
98 def test_enclose_filename_if_necessary_enclosed(filename):
99 assert "\u2068" + filename + "\u2069" == _enclose_filename_if_necessary(filename)
100
101
102 def test_unknown_language_roundtrip():
103 buf = StringIO(r'''
104 msgid ""
105 msgstr ""
106 "Language: sr_SP\n"''')
107 catalog = pofile.read_po(buf)
108 assert catalog.locale_identifier == 'sr_SP'
109 assert not catalog.locale
110 buf = BytesIO()
111 pofile.write_po(buf, catalog)
112 assert 'sr_SP' in buf.getvalue().decode()
113
114
115 def test_unknown_language_write():
116 catalog = Catalog(locale='sr_SP')
117 assert catalog.locale_identifier == 'sr_SP'
118 assert not catalog.locale
119 buf = BytesIO()
120 pofile.write_po(buf, catalog)
121 assert 'sr_SP' in buf.getvalue().decode()
122
123
124 def test_iterable_of_strings():
125 """
126 Test we can parse from an iterable of strings.
127 """
128 catalog = pofile.read_po(['msgid "foo"', 'msgstr "Voh"'], locale="en_US")
129 assert catalog.locale == Locale("en", "US")
130 assert catalog.get("foo").string == "Voh"
131
132
133 @pytest.mark.parametrize("order", [1, -1])
134 def test_iterable_of_mismatching_strings(order):
135 # Mixing and matching byteses and strs in the same read_po call is not allowed.
136 with pytest.raises(Exception): # noqa: B017 (will raise either TypeError or AttributeError)
137 pofile.read_po(['msgid "foo"', b'msgstr "Voh"'][::order])
138
139
140 def test_issue_1087():
141 buf = StringIO(r'''
142 msgid ""
143 msgstr ""
144 "Language: \n"
145 ''')
146 assert pofile.read_po(buf).locale is None
147
148
149 @pytest.mark.parametrize("case", ['msgid "foo"', 'msgid "foo"\nmsgid_plural "foos"'])
150 @pytest.mark.parametrize("abort_invalid", [False, True])
151 def test_issue_1134(case: str, abort_invalid: bool):
152 buf = StringIO(case)
153
154 if abort_invalid:
155 # Catalog not created, aborted with PoFileError
156 with pytest.raises(pofile.PoFileError, match="missing msgstr for msgid 'foo' on 0"):
157 pofile.read_po(buf, abort_invalid=True)
158 else:
159 # Catalog is created with warning, no abort
160 output = pofile.read_po(buf)
161 assert len(output) == 1
162 assert output["foo"].string in ((''), ('', ''))