Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(91)

Delta Between Two Patch Sets: translate/storage/test_cpo.py

Issue 62: CPO support for previous messages SVN Base: https://translate.svn.sourceforge.net/svnroot/translate/src/trunk/
Left Patch Set: Fixed minor issues Created 1 year, 4 months ago
Right Patch Set: Updated accessors, tests and added base.py & test_base.py Created 1 year, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: View regular side by side diff
Right: View regular side by side diff
LEFTRIGHT
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 from translate.storage import test_po 4 from translate.storage import test_po
5 from translate.storage import cpo 5 from translate.storage import cpo
6 from translate.misc.multistring import multistring 6 from translate.misc.multistring import multistring
7 from translate.misc import wStringIO 7 from translate.misc import wStringIO
8 from py.test import raises 8 from py.test import raises
9 9
10 class TestCPOUnit(test_po.TestPOUnit): 10 class TestCPOUnit(test_po.TestPOUnit):
11 UnitClass = cpo.pounit 11 UnitClass = cpo.pounit
12 12
13 def test_plurals(self): 13 def test_plurals(self):
14 """Tests that plurals are handled correctly.""" 14 """Tests that plurals are handled correctly."""
15 unit = self.UnitClass("Cow") 15 unit = self.UnitClass("Cow")
16 unit.msgid_plural = ["Cows"] 16 unit.msgid_plural = ["Cows"]
17 assert isinstance(unit.source, multistring) 17 assert isinstance(unit.source, multistring)
18 assert unit.source.strings == ["Cow", "Cows"] 18 assert unit.source.strings == ["Cow", "Cows"]
19 assert unit.source == "Cow" 19 assert unit.source == "Cow"
20 20
21 unit.target = ["Koei", "Koeie"] 21 unit.target = ["Koei", "Koeie"]
22 assert isinstance(unit.target, multistring) 22 assert isinstance(unit.target, multistring)
23 assert unit.target.strings == ["Koei", "Koeie"] 23 assert unit.target.strings == ["Koei", "Koeie"]
24 assert unit.target == "Koei" 24 assert unit.target == "Koei"
25 25
26 unit.target = {0:"Koei", 3:"Koeie"} 26 unit.target = {0:"Koei", 3:"Koeie"}
27 assert isinstance(unit.target, multistring) 27 assert isinstance(unit.target, multistring)
28 assert unit.target.strings == ["Koei", "Koeie"] 28 assert unit.target.strings == ["Koei", "Koeie"]
29 assert unit.target == "Koei" 29 assert unit.target == "Koei"
30 30
31 unit.target = [u"Sk\u00ear", u"Sk\u00eare"] 31 unit.target = [u"Sk\u00ear", u"Sk\u00eare"]
32 assert isinstance(unit.target, multistring) 32 assert isinstance(unit.target, multistring)
33 assert unit.target.strings == [u"Sk\u00ear", u"Sk\u00eare"] 33 assert unit.target.strings == [u"Sk\u00ear", u"Sk\u00eare"]
34 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" 35 assert unit.target == u"Sk\u00ear"
36 36
37 def test_plural_reduction(self): 37 def test_plural_reduction(self):
38 """checks that reducing the number of plurals supplied works""" 38 """checks that reducing the number of plurals supplied works"""
39 unit = self.UnitClass("Tree") 39 unit = self.UnitClass("Tree")
40 unit.msgid_plural = ["Trees"] 40 unit.msgid_plural = ["Trees"]
41 assert isinstance(unit.source, multistring) 41 assert isinstance(unit.source, multistring)
42 assert unit.source.strings == ["Tree", "Trees"] 42 assert unit.source.strings == ["Tree", "Trees"]
43 unit.target = multistring(["Boom", "Bome", "Baie Bome"]) 43 unit.target = multistring(["Boom", "Bome", "Baie Bome"])
44 assert isinstance(unit.source, multistring) 44 assert isinstance(unit.source, multistring)
45 assert unit.target.strings == ["Boom", "Bome", "Baie Bome"] 45 assert unit.target.strings == ["Boom", "Bome", "Baie Bome"]
46 unit.target = multistring(["Boom", "Bome"]) 46 unit.target = multistring(["Boom", "Bome"])
47 assert unit.target.strings == ["Boom", "Bome"] 47 assert unit.target.strings == ["Boom", "Bome"]
48 unit.target = "Boom" 48 unit.target = "Boom"
49 # FIXME: currently assigning the target to the same as the first string won't change anything 49 # FIXME: currently assigning the target to the same as the first string won't change anything
50 # we need to verify that this is the desired behaviour... 50 # we need to verify that this is the desired behaviour...
51 assert unit.target.strings == ["Boom", "Bome"] 51 assert unit.target.strings == ["Boom", "Bome"]
52 unit.target = "Een Boom" 52 unit.target = "Een Boom"
53 assert unit.target.strings == ["Een Boom"] 53 assert unit.target.strings == ["Een Boom"]
54 54
55 def test_notes(self): 55 def test_notes(self):
56 """tests that the generic notes API works""" 56 """tests that the generic notes API works"""
57 unit = self.UnitClass("File") 57 unit = self.UnitClass("File")
58 assert unit.getnotes() == "" 58 assert unit.getnotes() == ""
59 unit.addnote("Which meaning of file?") 59 unit.addnote("Which meaning of file?")
60 assert unit.getnotes("translator") == "Which meaning of file?" 60 assert unit.getnotes("translator") == "Which meaning of file?"
61 assert unit.getnotes("developer") == "" 61 assert unit.getnotes("developer") == ""
62 unit.addnote("Verb", origin="programmer") 62 unit.addnote("Verb", origin="programmer")
63 assert unit.getnotes("developer") == "Verb" 63 assert unit.getnotes("developer") == "Verb"
64 unit.addnote("Thank you", origin="translator") 64 unit.addnote("Thank you", origin="translator")
65 assert unit.getnotes("translator") == "Which meaning of file?\nThank you " 65 assert unit.getnotes("translator") == "Which meaning of file?\nThank you "
66 assert unit.getnotes() == "Which meaning of file?\nThank you\nVerb" 66 assert unit.getnotes() == "Which meaning of file?\nThank you\nVerb"
67 assert raises(ValueError, unit.getnotes, "devteam") 67 assert raises(ValueError, unit.getnotes, "devteam")
68 68
69 def test_notes_withcomments(self): 69 def test_notes_withcomments(self):
70 """tests that when we add notes that look like comments that we treat th em properly""" 70 """tests that when we add notes that look like comments that we treat th em properly"""
71 unit = self.UnitClass("File") 71 unit = self.UnitClass("File")
72 unit.addnote("# Double commented comment") 72 unit.addnote("# Double commented comment")
73 assert unit.getnotes() == "# Double commented comment" 73 assert unit.getnotes() == "# Double commented comment"
74 74
75 def test_previous_source(self): 75 def test_previous_source(self):
76 """tests if we handle previous msgid properly""" 76 """tests if we handle previous msgid properly"""
77 unit = self.UnitClass("Cat") 77 unit = self.UnitClass("Cat")
78 assert unit.prev_source == "" 78 assert unit.prev_source == ""
79 assert isinstance(unit.prev_source, multistring) 79 assert isinstance(unit.prev_source, multistring)
80 80
81 unit.prev_source = "Feline" 81 unit.prev_source = "Feline"
82 assert unit.prev_source == "Feline" 82 assert unit.prev_source == "Feline"
83 assert isinstance(unit.prev_source, multistring) 83 assert isinstance(unit.prev_source, multistring)
84 84
85 unit.prev_source = ["Feline", "Felines"] 85 unit.prev_source = ["Feline", "Felines"]
86 assert unit.prev_source.strings == ["Feline", "Felines"] 86 assert unit.prev_source.strings == ["Feline", "Felines"]
87 assert unit.prev_source == "Feline" 87 assert unit.prev_source == "Feline"
88 assert isinstance(unit.prev_source, multistring) 88 assert isinstance(unit.prev_source, multistring)
89 89
90 unit.prev_source = "" 90 unit.prev_source = ""
91 assert unit.prev_source == "" 91 assert unit.prev_source == ""
92 assert unit.source == "Cat"
93 assert len(unit.prev_source.strings) == 1 92 assert len(unit.prev_source.strings) == 1
94 assert isinstance(unit.prev_source, multistring) 93 assert isinstance(unit.prev_source, multistring)
dwaynebailey 2008/07/17 12:59:27 Surely if we've removed the previous source we sho
georgeyk 2008/07/18 05:38:17 On 2008/07/17 12:59:27, dwaynebailey wrote: > Sure
95
96 unit.source = ""
97 unit.prev_source = "Feline"
98 assert unit.prev_source == ""
99 unit.source = "Cat"
100 94
101 unit.markfuzzy() 95 unit.markfuzzy()
102 unit.prev_source = "Feline" 96 unit.prev_source = "Feline"
103 assert unit.prev_source == "" 97 assert unit.prev_source == ""
104 assert len(unit.prev_source.strings) == 1 98 assert len(unit.prev_source.strings) == 1
105 assert isinstance(unit.prev_source, multistring) 99 assert isinstance(unit.prev_source, multistring)
106 100
107 def test_previous_context(self): 101 def test_previous_context(self):
108 """tests if we handle previous context messages properly""" 102 """tests if we handle previous context messages properly"""
109 unit = self.UnitClass("Cat") 103 unit = self.UnitClass("Cat")
110 assert unit.prev_context == "" 104 assert unit.prev_context == ""
111 105
112 context = "A sort of a cute animal" 106 context = "A sort of a cute animal"
113 unit.prev_context = context 107 unit.prev_context = context
114 assert unit.prev_context == context 108 assert unit.prev_context == context
115 109
116 unit.prev_context = "" 110 unit.prev_context = ""
117 assert unit.prev_context == "" 111 assert unit.prev_context == ""
118 112
119 unit.markfuzzy() 113 unit.markfuzzy()
120 unit.prev_context = "A sort of a cute animal" 114 unit.prev_context = "A sort of a cute animal"
121 assert unit.prev_context == "" 115 assert unit.prev_context == ""
122 116
123 def test_set_as_previous(self): 117 def test_set_as_previous(self):
124 """tests if we set previous messages properly""" 118 """tests if we set previous messages properly"""
125 # This is just a sanity test, for a more complete test look 119 # This is just a sanity test, for a more complete test look
126 # at convert/test_pot2po.py 120 # at convert/test_pot2po.py
127 unit = self.UnitClass(["Test %d", "Tests %d"]) 121 unit = self.UnitClass("Test %d")
122 unit.msgid_plural = ["Tests %d"]
dwaynebailey 2008/07/17 12:59:27 I'd rather see you set this through .source rather
128 unit.target = ["Teste %d", "Testes %d"] 123 unit.target = ["Teste %d", "Testes %d"]
129 unit.set_as_previous() 124 unit.set_as_previous()
130 unit.source = "Test %s" 125 unit.source = "Test %s"
131 test_unit = '''#, fuzzy\n#| msgid "Test %d"\n#| msgid_plural "Tests %d" 126 test_unit = '''#, fuzzy\n#| msgid "Test %d"\n#| msgid_plural "Tests %d"
132 msgid "Test %s"\nmsgstr "Teste %d"\n''' 127 msgid "Test %s"\nmsgstr "Teste %d"\n'''
133 assert str(unit) == test_unit 128 assert str(unit) == test_unit
134 assert unit.isfuzzy() 129 assert unit.isfuzzy()
135 130
136 # without a target, the unit remain the same 131 # without a target, the unit remain the same
137 unit2 = self.UnitClass(["Other", "Others"]) 132 unit2 = self.UnitClass("Other")
133 unit2.msgid_plural = ["Others"]
138 expected = str(unit2) 134 expected = str(unit2)
139 unit2.set_as_previous() 135 unit2.set_as_previous()
140 assert str(unit2) == expected 136 assert str(unit2) == expected
141 137
142 # we can't set a message as previous if the message is marked as fuzzy 138 # we can't set a message as previous if the message is marked as fuzzy
143 unit = self.UnitClass("Pizza") 139 unit = self.UnitClass("Pizza")
144 unit.msgid_plural = ["Pizzas"] 140 unit.msgid_plural = ["Pizzas"]
145 unit.target = ["Pizza", "Pizzas"] 141 unit.target = ["Pizza", "Pizzas"]
146 unit.markfuzzy() 142 unit.markfuzzy()
147 unit.set_as_previous() 143 unit.set_as_previous()
148 assert unit.prev_context == "" 144 assert unit.prev_context == ""
149 assert unit.prev_source == "" 145 assert unit.prev_source == ""
150 146
151 147
152 class TestCPOFile(test_po.TestPOFile): 148 class TestCPOFile(test_po.TestPOFile):
153 StoreClass = cpo.pofile 149 StoreClass = cpo.pofile
154 def test_msgidcomments(self): 150 def test_msgidcomments(self):
155 """checks that we handle msgid comments""" 151 """checks that we handle msgid comments"""
156 posource = 'msgid "test me"\nmsgstr ""' 152 posource = 'msgid "test me"\nmsgstr ""'
157 pofile = self.poparse(posource) 153 pofile = self.poparse(posource)
158 thepo = pofile.units[0] 154 thepo = pofile.units[0]
159 thepo.msgidcomment = "first comment" 155 thepo.msgidcomment = "first comment"
160 print pofile 156 print pofile
161 print "Blah", thepo.source 157 print "Blah", thepo.source
162 assert thepo.source == "test me" 158 assert thepo.source == "test me"
163 thepo.msgidcomment = "second comment" 159 thepo.msgidcomment = "second comment"
164 assert str(pofile).count("_:") == 1 160 assert str(pofile).count("_:") == 1
165 161
166 # def test_merge_duplicates_msgctxt(self): 162 # def test_merge_duplicates_msgctxt(self):
167 # """checks that merging duplicates works for msgctxt""" 163 # """checks that merging duplicates works for msgctxt"""
168 # posource = '#: source1\nmsgid "test me"\nmsgstr ""\n\n#: source2\nmsgid "test me"\nmsgstr ""\n' 164 # posource = '#: source1\nmsgid "test me"\nmsgstr ""\n\n#: source2\nmsgid "test me"\nmsgstr ""\n'
169 # pofile = self.poparse(posource) 165 # pofile = self.poparse(posource)
170 # assert len(pofile.units) == 2 166 # assert len(pofile.units) == 2
171 # pofile.removeduplicates("msgctxt") 167 # pofile.removeduplicates("msgctxt")
172 # print pofile 168 # print pofile
173 # assert len(pofile.units) == 2 169 # assert len(pofile.units) == 2
174 # assert str(pofile.units[0]).count("source1") == 2 170 # assert str(pofile.units[0]).count("source1") == 2
175 # assert str(pofile.units[1]).count("source2") == 2 171 # assert str(pofile.units[1]).count("source2") == 2
176 172
177 # def test_merge_blanks(self): 173 # def test_merge_blanks(self):
178 # """checks that merging adds msgid_comments to blanks""" 174 # """checks that merging adds msgid_comments to blanks"""
179 # posource = '#: source1\nmsgid ""\nmsgstr ""\n\n#: source2\nmsgid ""\nms gstr ""\n' 175 # posource = '#: source1\nmsgid ""\nmsgstr ""\n\n#: source2\nmsgid ""\nms gstr ""\n'
180 # pofile = self.poparse(posource) 176 # pofile = self.poparse(posource)
181 # assert len(pofile.units) == 2 177 # assert len(pofile.units) == 2
182 # pofile.removeduplicates("merge") 178 # pofile.removeduplicates("merge")
183 # assert len(pofile.units) == 2 179 # assert len(pofile.units) == 2
184 # print pofile.units[0].msgidcomments 180 # print pofile.units[0].msgidcomments
185 # print pofile.units[1].msgidcomments 181 # print pofile.units[1].msgidcomments
186 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == "_: source1\n " 182 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == "_: source1\n "
187 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == "_: source2\n " 183 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == "_: source2\n "
188 184
189 # def test_msgid_comment(self): 185 # def test_msgid_comment(self):
190 # """checks that when adding msgid_comments we place them on a newline""" 186 # """checks that when adding msgid_comments we place them on a newline"""
191 # posource = '#: source0\nmsgid "Same"\nmsgstr ""\n\n#: source1\nmsgid "S ame"\nmsgstr ""\n' 187 # posource = '#: source0\nmsgid "Same"\nmsgstr ""\n\n#: source1\nmsgid "S ame"\nmsgstr ""\n'
192 # pofile = self.poparse(posource) 188 # pofile = self.poparse(posource)
193 # assert len(pofile.units) == 2 189 # assert len(pofile.units) == 2
194 # pofile.removeduplicates("msgid_comment") 190 # pofile.removeduplicates("msgid_comment")
195 # assert len(pofile.units) == 2 191 # assert len(pofile.units) == 2
196 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == "_: source0\n " 192 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == "_: source0\n "
197 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == "_: source1\n " 193 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == "_: source1\n "
198 # # Now lets check for formating 194 # # Now lets check for formating
199 # for i in (0, 1): 195 # for i in (0, 1):
200 # expected = '''#: source%d\nmsgid ""\n"_: source%d\\n"\n"Same"\nmsgstr ""\n''' % (i, i) 196 # expected = '''#: source%d\nmsgid ""\n"_: source%d\\n"\n"Same"\nmsgstr ""\n''' % (i, i)
201 # assert pofile.units[i].__str__() == expected 197 # assert pofile.units[i].__str__() == expected
202 198
203 # def test_keep_blanks(self): 199 # def test_keep_blanks(self):
204 # """checks that keeping keeps blanks and doesn't add msgid_comments""" 200 # """checks that keeping keeps blanks and doesn't add msgid_comments"""
205 # posource = '#: source1\nmsgid ""\nmsgstr ""\n\n#: source2\nmsgid ""\nms gstr ""\n' 201 # posource = '#: source1\nmsgid ""\nmsgstr ""\n\n#: source2\nmsgid ""\nms gstr ""\n'
206 # pofile = self.poparse(posource) 202 # pofile = self.poparse(posource)
207 # assert len(pofile.units) == 2 203 # assert len(pofile.units) == 2
208 # pofile.removeduplicates("keep") 204 # pofile.removeduplicates("keep")
209 # assert len(pofile.units) == 2 205 # assert len(pofile.units) == 2
210 # # check we don't add msgidcomments 206 # # check we don't add msgidcomments
211 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == "" 207 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == ""
212 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == "" 208 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == ""
213 209
214 def test_output_str_unicode(self): 210 def test_output_str_unicode(self):
215 """checks that we can str(pofile) which is in unicode""" 211 """checks that we can str(pofile) which is in unicode"""
216 posource = u'''#: nb\nmsgid "Norwegian Bokm\xe5l"\nmsgstr ""\n''' 212 posource = u'''#: nb\nmsgid "Norwegian Bokm\xe5l"\nmsgstr ""\n'''
217 pofile = self.StoreClass(wStringIO.StringIO(posource.encode("UTF-8")), e ncoding="UTF-8") 213 pofile = self.StoreClass(wStringIO.StringIO(posource.encode("UTF-8")), e ncoding="UTF-8")
218 assert len(pofile.units) == 1 214 assert len(pofile.units) == 1
219 print str(pofile) 215 print str(pofile)
220 thepo = pofile.units[0] 216 thepo = pofile.units[0]
221 # assert str(pofile) == posource.encode("UTF-8") 217 # assert str(pofile) == posource.encode("UTF-8")
222 # extra test: what if we set the msgid to a unicode? this happens in pro p2po etc 218 # extra test: what if we set the msgid to a unicode? this happens in pro p2po etc
223 thepo.source = u"Norwegian Bokm\xe5l" 219 thepo.source = u"Norwegian Bokm\xe5l"
224 # assert str(thepo) == posource.encode("UTF-8") 220 # assert str(thepo) == posource.encode("UTF-8")
225 # Now if we set the msgstr to Unicode 221 # Now if we set the msgstr to Unicode
226 # this is an escaped half character (1/2) 222 # this is an escaped half character (1/2)
227 halfstr = "\xbd ...".decode("latin-1") 223 halfstr = "\xbd ...".decode("latin-1")
228 thepo.target = halfstr 224 thepo.target = halfstr
229 # assert halfstr in str(pofile).decode("UTF-8") 225 # assert halfstr in str(pofile).decode("UTF-8")
230 thepo.target = halfstr.encode("UTF-8") 226 thepo.target = halfstr.encode("UTF-8")
231 # assert halfstr.encode("UTF-8") in str(pofile) 227 # assert halfstr.encode("UTF-8") in str(pofile)
232 228
233 def test_posections(self): 229 def test_posections(self):
234 """checks the content of all the expected sections of a PO message""" 230 """checks the content of all the expected sections of a PO message"""
235 posource = '# other comment\n#. automatic comment\n#: source comment\n#, fuzzy\nmsgid "One"\nmsgstr "Een"\n' 231 posource = '# other comment\n#. automatic comment\n#: source comment\n#, fuzzy\nmsgid "One"\nmsgstr "Een"\n'
236 pofile = self.poparse(posource) 232 pofile = self.poparse(posource)
237 print pofile 233 print pofile
238 assert len(pofile.units) == 1 234 assert len(pofile.units) == 1
239 assert str(pofile) == posource 235 assert str(pofile) == posource
240 236
241 def test_multiline_obsolete(self): 237 def test_multiline_obsolete(self):
242 """Tests for correct output of mulitline obsolete messages""" 238 """Tests for correct output of mulitline obsolete messages"""
243 posource = '#~ msgid ""\n#~ "Old thing\\n"\n#~ "Second old thing"\n#~ ms gstr ""\n#~ "Ou ding\\n"\n#~ "Tweede ou ding"\n' 239 posource = '#~ msgid ""\n#~ "Old thing\\n"\n#~ "Second old thing"\n#~ ms gstr ""\n#~ "Ou ding\\n"\n#~ "Tweede ou ding"\n'
244 pofile = self.poparse(posource) 240 pofile = self.poparse(posource)
245 print "Source:\n%s" % posource 241 print "Source:\n%s" % posource
246 print "Output:\n%s" % str(pofile) 242 print "Output:\n%s" % str(pofile)
247 assert len(pofile.units) == 1 243 assert len(pofile.units) == 1
248 assert pofile.units[0].isobsolete() 244 assert pofile.units[0].isobsolete()
249 assert not pofile.units[0].istranslatable() 245 assert not pofile.units[0].istranslatable()
250 assert str(pofile) == posource 246 assert str(pofile) == posource
251 247
252 def test_unassociated_comments(self): 248 def test_unassociated_comments(self):
253 """tests behaviour of unassociated comments.""" 249 """tests behaviour of unassociated comments."""
254 oldsource = '# old lonesome comment\n\nmsgid "one"\nmsgstr "een"\n' 250 oldsource = '# old lonesome comment\n\nmsgid "one"\nmsgstr "een"\n'
255 oldfile = self.poparse(oldsource) 251 oldfile = self.poparse(oldsource)
256 print "__str__", str(oldfile) 252 print "__str__", str(oldfile)
257 assert len(oldfile.units) == 1 253 assert len(oldfile.units) == 1
258 assert str(oldfile).find("# old lonesome comment\nmsgid") >= 0 254 assert str(oldfile).find("# old lonesome comment\nmsgid") >= 0
259 255
LEFTRIGHT

Powered by Google App Engine
This is Rietveld r159