1
2
3
4 from translate.storage import test_po
5 from translate.storage import pypo
6 from translate.misc.multistring import multistring
7 from translate.misc import wStringIO
8 from py.test import raises
9
11 UnitClass = pypo.pounit
12
14 """Tests that plurals are handled correctly."""
15 unit = self.UnitClass("Cow")
16 unit.msgid_plural = ['"Cows"']
17 assert isinstance(unit.source, multistring)
18 assert unit.source.strings == ["Cow", "Cows"]
19 assert unit.source == "Cow"
20
21 unit.target = ["Koei", "Koeie"]
22 assert isinstance(unit.target, multistring)
23 assert unit.target.strings == ["Koei", "Koeie"]
24 assert unit.target == "Koei"
25
26 unit.target = {0:"Koei", 3:"Koeie"}
27 assert isinstance(unit.target, multistring)
28 assert unit.target.strings == ["Koei", "Koeie"]
29 assert unit.target == "Koei"
30
31 unit.target = [u"Sk\u00ear", u"Sk\u00eare"]
32 assert isinstance(unit.target, multistring)
33 assert unit.target.strings == [u"Sk\u00ear", u"Sk\u00eare"]
34 assert unit.target.strings == [u"Sk\u00ear", u"Sk\u00eare"]
35 assert unit.target == u"Sk\u00ear"
36
38 """checks that reducing the number of plurals supplied works"""
39 unit = self.UnitClass("Tree")
40 unit.msgid_plural = ['"Trees"']
41 assert isinstance(unit.source, multistring)
42 assert unit.source.strings == ["Tree", "Trees"]
43 unit.target = multistring(["Boom", "Bome", "Baie Bome"])
44 assert isinstance(unit.source, multistring)
45 assert unit.target.strings == ["Boom", "Bome", "Baie Bome"]
46 unit.target = multistring(["Boom", "Bome"])
47 assert unit.target.strings == ["Boom", "Bome"]
48 unit.target = "Boom"
49
50
51 assert unit.target.strings == ["Boom", "Bome"]
52 unit.target = "Een Boom"
53 assert unit.target.strings == ["Een Boom"]
54
56 """tests that the generic notes API works"""
57 unit = self.UnitClass("File")
58 unit.addnote("Which meaning of file?")
59 assert str(unit) == '# Which meaning of file?\nmsgid "File"\nmsgstr ""\n'
60 unit.addnote("Verb", origin="programmer")
61 assert str(unit) == '# Which meaning of file?\n#. Verb\nmsgid "File"\nmsgstr ""\n'
62 unit.addnote("Thank you", origin="translator")
63 assert str(unit) == '# Which meaning of file?\n# Thank you\n#. Verb\nmsgid "File"\nmsgstr ""\n'
64
65 assert unit.getnotes("developer") == "Verb"
66 assert unit.getnotes("translator") == "Which meaning of file?\nThank you"
67 assert unit.getnotes() == "Which meaning of file?\nThank you\nVerb"
68 assert raises(ValueError, unit.getnotes, "devteam")
69
76
78 '''tests that we wrap the first line correctly a first line if longer then 71 chars
79 as at 71 chars we should align the text on the left and preceed with with a msgid ""'''
80
81 str_max = "123456789 123456789 123456789 123456789 123456789 123456789 123456789 1"
82 unit = self.UnitClass(str_max)
83 expected = 'msgid "%s"\nmsgstr ""\n' % str_max
84 print expected, str(unit)
85 assert str(unit) == expected
86
87 str_wrap = str_max + '2'
88 unit = self.UnitClass(str_wrap)
89 expected = 'msgid ""\n"%s"\nmsgstr ""\n' % str_wrap
90 print expected, str(unit)
91 assert str(unit) == expected
92
94 """test that we wrap newlines on a real \n"""
95 string = "123456789\n" * 3
96 postring = ('"123456789\\n"\n' * 3)[:-1]
97 unit = self.UnitClass(string)
98 expected = 'msgid ""\n%s\nmsgstr ""\n' % postring
99 print expected, str(unit)
100 assert str(unit) == expected
101
102
103 longstring = ("123456789 " * 10 + "\n") * 3
104 expected = r'''msgid ""
105 "123456789 123456789 123456789 123456789 123456789 123456789 123456789 "
106 "123456789 123456789 123456789 \n"
107 "123456789 123456789 123456789 123456789 123456789 123456789 123456789 "
108 "123456789 123456789 123456789 \n"
109 "123456789 123456789 123456789 123456789 123456789 123456789 123456789 "
110 "123456789 123456789 123456789 \n"
111 msgstr ""
112 '''
113 unit = self.UnitClass(longstring)
114 print expected, str(unit)
115 assert str(unit) == expected
116
118 """test that we wrap all lines on the maximum line length"""
119 string = "1 3 5 7 N " * 11
120 expected = 'msgid ""\n%s\nmsgstr ""\n' % '"1 3 5 7 N 1 3 5 7 N 1 3 5 7 N 1 3 5 7 N 1 3 5 7 N 1 3 5 7 N 1 3 5 7 N 1 3 5 "\n"7 N 1 3 5 7 N 1 3 5 7 N 1 3 5 7 N "'
121 unit = self.UnitClass(string)
122 print "Expected:"
123 print expected
124 print "Actual:"
125 print str(unit)
126 assert str(unit) == expected
127
129 """Test that the spacing of text is done the same as msgcat."""
130 idstring = "Creates a new document using an existing template iiiiiiiiiiiiiiiiiiiiiii or "
131 idstring += "opens a sample document."
132 expected = '''msgid ""
133 "Creates a new document using an existing template iiiiiiiiiiiiiiiiiiiiiii or "
134 "opens a sample document."
135 msgstr ""
136 '''
137 unit = self.UnitClass(idstring)
138 print "Expected:"
139 print expected
140 print "Actual:"
141 print str(unit)
142 assert str(unit) == expected
143
145 StoreClass = pypo.pofile
155
166
178
192
203
205 """checks that we can str(element) which is in unicode"""
206 posource = u'''#: nb\nmsgid "Norwegian Bokm\xe5l"\nmsgstr ""\n'''
207 pofile = self.StoreClass(wStringIO.StringIO(posource.encode("UTF-8")), encoding="UTF-8")
208 assert len(pofile.units) == 1
209 print str(pofile)
210 thepo = pofile.units[0]
211 assert str(thepo) == posource.encode("UTF-8")
212
213 thepo.source = u"Norwegian Bokm\xe5l"
214 assert str(thepo) == posource.encode("UTF-8")
215
216
217 halfstr = "\xbd ...".decode("latin-1")
218 thepo.target = halfstr
219 assert halfstr in str(thepo).decode("UTF-8")
220 thepo.target = halfstr.encode("UTF-8")
221 assert halfstr.encode("UTF-8") in str(thepo)
222
224 """checks the content of all the expected sections of a PO message"""
225 posource = '# other comment\n#. automatic comment\n#: source comment\n#, fuzzy\nmsgid "One"\nmsgstr "Een"\n'
226 pofile = self.poparse(posource)
227 print pofile
228 assert len(pofile.units) == 1
229 assert str(pofile) == posource
230 assert pofile.units[0].othercomments == ["# other comment\n"]
231 assert pofile.units[0].automaticcomments == ["#. automatic comment\n"]
232 assert pofile.units[0].sourcecomments == ["#: source comment\n"]
233 assert pofile.units[0].typecomments == ["#, fuzzy\n"]
234
242