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

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: Updated accessors, tests and added base.py & test_base.py Created 1 year, 4 months ago
Right Patch Set: 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)
80 79
81 unit.prev_source = "Feline" 80 unit.prev_source = "Feline"
82 assert unit.prev_source == "Feline" 81 assert unit.prev_source == "Feline"
83 assert isinstance(unit.prev_source, multistring)
84
85 unit.prev_source = ["Feline", "Felines"]
86 assert unit.prev_source.strings == ["Feline", "Felines"]
87 assert unit.prev_source == "Feline"
88 assert isinstance(unit.prev_source, multistring)
89 82
90 unit.prev_source = "" 83 unit.prev_source = ""
91 assert unit.prev_source == "" 84 assert unit.prev_source == ""
92 assert len(unit.prev_source.strings) == 1
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
94
95 unit.markfuzzy()
96 unit.prev_source = "Feline"
97 assert unit.prev_source == ""
98 assert len(unit.prev_source.strings) == 1
99 assert isinstance(unit.prev_source, multistring)
100 85
101 def test_previous_context(self): 86 def test_previous_context(self):
102 """tests if we handle previous context messages properly""" 87 """tests if we handle previous context messages properly"""
103 unit = self.UnitClass("Cat") 88 unit = self.UnitClass("Cat")
104 assert unit.prev_context == "" 89 assert unit.prev_context == ""
105 90
106 context = "A sort of a cute animal" 91 context = "A sort of a cute animal"
107 unit.prev_context = context 92 unit.prev_context = context
108 assert unit.prev_context == context 93 assert unit.prev_context == context
109 94
110 unit.prev_context = "" 95 unit.prev_context = ""
111 assert unit.prev_context == "" 96 assert unit.prev_context == ""
112 97
113 unit.markfuzzy() 98 def test_previous_plural(self):
114 unit.prev_context = "A sort of a cute animal" 99 """tests if we handle previous plural properly"""
115 assert unit.prev_context == "" 100 unit = self.UnitClass("Cat")
101 assert unit.prev_plural == ""
102
103 unit.prev_plural = "Cats"
104 assert unit.prev_plural == "Cats"
105
106 unit.prev_plural = ""
107 assert unit.prev_plural == ""
116 108
117 def test_set_as_previous(self): 109 def test_set_as_previous(self):
118 """tests if we set previous messages properly""" 110 """tests if we set previous messages properly"""
119 # This is just a sanity test, for a more complete test look 111 # This is just a sanity test, for a more complete test look
120 # at convert/test_pot2po.py 112 # at convert/test_pot2po.py
121 unit = self.UnitClass("Test %d") 113 unit = self.UnitClass("Test %d")
122 unit.msgid_plural = ["Tests %d"] 114 unit.msgid_plural = ["Tests %d"]
dwaynebailey 2008/07/17 12:59:27 I'd rather see you set this through .source rather
123 unit.target = ["Teste %d", "Testes %d"] 115 unit.target = ["Teste %d", "Testes %d"]
124 unit.set_as_previous() 116 unit.set_as_previous()
125 unit.source = "Test %s" 117 unit.source = "Test %s"
126 test_unit = '''#, fuzzy\n#| msgid "Test %d"\n#| msgid_plural "Tests %d" 118 test_unit = '''#, fuzzy\n#| msgid "Test %d"\n#| msgid_plural "Tests %d"
127 msgid "Test %s"\nmsgstr "Teste %d"\n''' 119 msgid "Test %s"\nmsgstr "Teste %d"\n'''
128 assert str(unit) == test_unit 120 assert str(unit) == test_unit
129 assert unit.isfuzzy() 121 assert unit.isfuzzy()
130 122
131 # without a target, the unit remain the same 123 # without a target, the unit remain the same
132 unit2 = self.UnitClass("Other") 124 unit2 = self.UnitClass("Other")
133 unit2.msgid_plural = ["Others"] 125 unit2.msgid_plural = ["Others"]
134 expected = str(unit2) 126 expected = str(unit2)
135 unit2.set_as_previous() 127 unit2.set_as_previous()
136 assert str(unit2) == expected 128 assert str(unit2) == expected
137
138 # we can't set a message as previous if the message is marked as fuzzy
139 unit = self.UnitClass("Pizza")
140 unit.msgid_plural = ["Pizzas"]
141 unit.target = ["Pizza", "Pizzas"]
142 unit.markfuzzy()
143 unit.set_as_previous()
144 assert unit.prev_context == ""
145 assert unit.prev_source == ""
146 129
147 130
148 class TestCPOFile(test_po.TestPOFile): 131 class TestCPOFile(test_po.TestPOFile):
149 StoreClass = cpo.pofile 132 StoreClass = cpo.pofile
150 def test_msgidcomments(self): 133 def test_msgidcomments(self):
151 """checks that we handle msgid comments""" 134 """checks that we handle msgid comments"""
152 posource = 'msgid "test me"\nmsgstr ""' 135 posource = 'msgid "test me"\nmsgstr ""'
153 pofile = self.poparse(posource) 136 pofile = self.poparse(posource)
154 thepo = pofile.units[0] 137 thepo = pofile.units[0]
155 thepo.msgidcomment = "first comment" 138 thepo.msgidcomment = "first comment"
156 print pofile 139 print pofile
157 print "Blah", thepo.source 140 print "Blah", thepo.source
158 assert thepo.source == "test me" 141 assert thepo.source == "test me"
159 thepo.msgidcomment = "second comment" 142 thepo.msgidcomment = "second comment"
160 assert str(pofile).count("_:") == 1 143 assert str(pofile).count("_:") == 1
161 144
162 # def test_merge_duplicates_msgctxt(self): 145 # def test_merge_duplicates_msgctxt(self):
163 # """checks that merging duplicates works for msgctxt""" 146 # """checks that merging duplicates works for msgctxt"""
164 # posource = '#: source1\nmsgid "test me"\nmsgstr ""\n\n#: source2\nmsgid "test me"\nmsgstr ""\n' 147 # posource = '#: source1\nmsgid "test me"\nmsgstr ""\n\n#: source2\nmsgid "test me"\nmsgstr ""\n'
165 # pofile = self.poparse(posource) 148 # pofile = self.poparse(posource)
166 # assert len(pofile.units) == 2 149 # assert len(pofile.units) == 2
167 # pofile.removeduplicates("msgctxt") 150 # pofile.removeduplicates("msgctxt")
168 # print pofile 151 # print pofile
169 # assert len(pofile.units) == 2 152 # assert len(pofile.units) == 2
170 # assert str(pofile.units[0]).count("source1") == 2 153 # assert str(pofile.units[0]).count("source1") == 2
171 # assert str(pofile.units[1]).count("source2") == 2 154 # assert str(pofile.units[1]).count("source2") == 2
172 155
173 # def test_merge_blanks(self): 156 # def test_merge_blanks(self):
174 # """checks that merging adds msgid_comments to blanks""" 157 # """checks that merging adds msgid_comments to blanks"""
175 # posource = '#: source1\nmsgid ""\nmsgstr ""\n\n#: source2\nmsgid ""\nms gstr ""\n' 158 # posource = '#: source1\nmsgid ""\nmsgstr ""\n\n#: source2\nmsgid ""\nms gstr ""\n'
176 # pofile = self.poparse(posource) 159 # pofile = self.poparse(posource)
177 # assert len(pofile.units) == 2 160 # assert len(pofile.units) == 2
178 # pofile.removeduplicates("merge") 161 # pofile.removeduplicates("merge")
179 # assert len(pofile.units) == 2 162 # assert len(pofile.units) == 2
180 # print pofile.units[0].msgidcomments 163 # print pofile.units[0].msgidcomments
181 # print pofile.units[1].msgidcomments 164 # print pofile.units[1].msgidcomments
182 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == "_: source1\n " 165 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == "_: source1\n "
183 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == "_: source2\n " 166 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == "_: source2\n "
184 167
185 # def test_msgid_comment(self): 168 # def test_msgid_comment(self):
186 # """checks that when adding msgid_comments we place them on a newline""" 169 # """checks that when adding msgid_comments we place them on a newline"""
187 # posource = '#: source0\nmsgid "Same"\nmsgstr ""\n\n#: source1\nmsgid "S ame"\nmsgstr ""\n' 170 # posource = '#: source0\nmsgid "Same"\nmsgstr ""\n\n#: source1\nmsgid "S ame"\nmsgstr ""\n'
188 # pofile = self.poparse(posource) 171 # pofile = self.poparse(posource)
189 # assert len(pofile.units) == 2 172 # assert len(pofile.units) == 2
190 # pofile.removeduplicates("msgid_comment") 173 # pofile.removeduplicates("msgid_comment")
191 # assert len(pofile.units) == 2 174 # assert len(pofile.units) == 2
192 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == "_: source0\n " 175 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == "_: source0\n "
193 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == "_: source1\n " 176 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == "_: source1\n "
194 # # Now lets check for formating 177 # # Now lets check for formating
195 # for i in (0, 1): 178 # for i in (0, 1):
196 # expected = '''#: source%d\nmsgid ""\n"_: source%d\\n"\n"Same"\nmsgstr ""\n''' % (i, i) 179 # expected = '''#: source%d\nmsgid ""\n"_: source%d\\n"\n"Same"\nmsgstr ""\n''' % (i, i)
197 # assert pofile.units[i].__str__() == expected 180 # assert pofile.units[i].__str__() == expected
198 181
199 # def test_keep_blanks(self): 182 # def test_keep_blanks(self):
200 # """checks that keeping keeps blanks and doesn't add msgid_comments""" 183 # """checks that keeping keeps blanks and doesn't add msgid_comments"""
201 # posource = '#: source1\nmsgid ""\nmsgstr ""\n\n#: source2\nmsgid ""\nms gstr ""\n' 184 # posource = '#: source1\nmsgid ""\nmsgstr ""\n\n#: source2\nmsgid ""\nms gstr ""\n'
202 # pofile = self.poparse(posource) 185 # pofile = self.poparse(posource)
203 # assert len(pofile.units) == 2 186 # assert len(pofile.units) == 2
204 # pofile.removeduplicates("keep") 187 # pofile.removeduplicates("keep")
205 # assert len(pofile.units) == 2 188 # assert len(pofile.units) == 2
206 # # check we don't add msgidcomments 189 # # check we don't add msgidcomments
207 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == "" 190 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == ""
208 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == "" 191 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == ""
209 192
210 def test_output_str_unicode(self): 193 def test_output_str_unicode(self):
211 """checks that we can str(pofile) which is in unicode""" 194 """checks that we can str(pofile) which is in unicode"""
212 posource = u'''#: nb\nmsgid "Norwegian Bokm\xe5l"\nmsgstr ""\n''' 195 posource = u'''#: nb\nmsgid "Norwegian Bokm\xe5l"\nmsgstr ""\n'''
213 pofile = self.StoreClass(wStringIO.StringIO(posource.encode("UTF-8")), e ncoding="UTF-8") 196 pofile = self.StoreClass(wStringIO.StringIO(posource.encode("UTF-8")), e ncoding="UTF-8")
214 assert len(pofile.units) == 1 197 assert len(pofile.units) == 1
215 print str(pofile) 198 print str(pofile)
216 thepo = pofile.units[0] 199 thepo = pofile.units[0]
217 # assert str(pofile) == posource.encode("UTF-8") 200 # assert str(pofile) == posource.encode("UTF-8")
218 # extra test: what if we set the msgid to a unicode? this happens in pro p2po etc 201 # extra test: what if we set the msgid to a unicode? this happens in pro p2po etc
219 thepo.source = u"Norwegian Bokm\xe5l" 202 thepo.source = u"Norwegian Bokm\xe5l"
220 # assert str(thepo) == posource.encode("UTF-8") 203 # assert str(thepo) == posource.encode("UTF-8")
221 # Now if we set the msgstr to Unicode 204 # Now if we set the msgstr to Unicode
222 # this is an escaped half character (1/2) 205 # this is an escaped half character (1/2)
223 halfstr = "\xbd ...".decode("latin-1") 206 halfstr = "\xbd ...".decode("latin-1")
224 thepo.target = halfstr 207 thepo.target = halfstr
225 # assert halfstr in str(pofile).decode("UTF-8") 208 # assert halfstr in str(pofile).decode("UTF-8")
226 thepo.target = halfstr.encode("UTF-8") 209 thepo.target = halfstr.encode("UTF-8")
227 # assert halfstr.encode("UTF-8") in str(pofile) 210 # assert halfstr.encode("UTF-8") in str(pofile)
228 211
229 def test_posections(self): 212 def test_posections(self):
230 """checks the content of all the expected sections of a PO message""" 213 """checks the content of all the expected sections of a PO message"""
231 posource = '# other comment\n#. automatic comment\n#: source comment\n#, fuzzy\nmsgid "One"\nmsgstr "Een"\n' 214 posource = '# other comment\n#. automatic comment\n#: source comment\n#, fuzzy\nmsgid "One"\nmsgstr "Een"\n'
232 pofile = self.poparse(posource) 215 pofile = self.poparse(posource)
233 print pofile 216 print pofile
234 assert len(pofile.units) == 1 217 assert len(pofile.units) == 1
235 assert str(pofile) == posource 218 assert str(pofile) == posource
236 219
237 def test_multiline_obsolete(self): 220 def test_multiline_obsolete(self):
238 """Tests for correct output of mulitline obsolete messages""" 221 """Tests for correct output of mulitline obsolete messages"""
239 posource = '#~ msgid ""\n#~ "Old thing\\n"\n#~ "Second old thing"\n#~ ms gstr ""\n#~ "Ou ding\\n"\n#~ "Tweede ou ding"\n' 222 posource = '#~ msgid ""\n#~ "Old thing\\n"\n#~ "Second old thing"\n#~ ms gstr ""\n#~ "Ou ding\\n"\n#~ "Tweede ou ding"\n'
240 pofile = self.poparse(posource) 223 pofile = self.poparse(posource)
241 print "Source:\n%s" % posource 224 print "Source:\n%s" % posource
242 print "Output:\n%s" % str(pofile) 225 print "Output:\n%s" % str(pofile)
243 assert len(pofile.units) == 1 226 assert len(pofile.units) == 1
244 assert pofile.units[0].isobsolete() 227 assert pofile.units[0].isobsolete()
245 assert not pofile.units[0].istranslatable() 228 assert not pofile.units[0].istranslatable()
246 assert str(pofile) == posource 229 assert str(pofile) == posource
247 230
248 def test_unassociated_comments(self): 231 def test_unassociated_comments(self):
249 """tests behaviour of unassociated comments.""" 232 """tests behaviour of unassociated comments."""
250 oldsource = '# old lonesome comment\n\nmsgid "one"\nmsgstr "een"\n' 233 oldsource = '# old lonesome comment\n\nmsgid "one"\nmsgstr "een"\n'
251 oldfile = self.poparse(oldsource) 234 oldfile = self.poparse(oldsource)
252 print "__str__", str(oldfile) 235 print "__str__", str(oldfile)
253 assert len(oldfile.units) == 1 236 assert len(oldfile.units) == 1
254 assert str(oldfile).find("# old lonesome comment\nmsgid") >= 0 237 assert str(oldfile).find("# old lonesome comment\nmsgid") >= 0
255 238
LEFTRIGHT

Powered by Google App Engine
This is Rietveld r159