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

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

Issue 81: Fix CPO memory leak SVN Base: https://translate.svn.sourceforge.net/svnroot/translate/src/trunk/
Left Patch Set: Added tests Created 1 year, 4 months ago
Right Patch Set: Fix addlocation method Created 1 year, 3 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_context(self): 75 def test_context(self):
76 unit = self.UnitClass("Message") 76 unit = self.UnitClass("Message")
77 assert unit.getcontext() == u"" 77 assert unit.getcontext() == u""
78 78
79 unit.setcontext("context message") 79 unit.setcontext("context message")
80 assert unit.getcontext() == u"context message" 80 assert unit.getcontext() == u"context message"
81 81
82 unit.setcontext("") 82 unit.setcontext("")
83 assert unit.getcontext() == u"" 83 assert unit.getcontext() == u""
84 84
85 def test_typecomment(self): 85 def test_typecomment(self):
86 unit = self.UnitClass("Comment") 86 unit = self.UnitClass("Comment")
87 assert unit.gettypecomments() == [] 87 assert unit.gettypecomments() == []
88 88
89 unit.addtypecomment("c-format") 89 unit.addtypecomment("c-format")
90 assert unit.hastypecomment("c-format") 90 assert unit.hastypecomment("c-format")
91 assert unit.gettypecomments() == ["c-format"] 91 assert unit.gettypecomments() == ["c-format"]
92 92
93 unit.addtypecomment("invalid-format") 93 unit.addtypecomment("invalid-format")
94 assert not unit.hastypecomment("invalid-format") 94 assert not unit.hastypecomment("invalid-format")
dwaynebailey 2008/08/29 13:19:25 Should we not raise an exception if we shouldn't b
95 assert unit.gettypecomments() == ["c-format"] 95 assert unit.gettypecomments() == ["c-format"]
96 96
dwaynebailey 2008/08/29 13:19:25 My guess is that these should go into the common t
97 97
98 class TestCPOFile(test_po.TestPOFile): 98 class TestCPOFile(test_po.TestPOFile):
99 StoreClass = cpo.pofile 99 StoreClass = cpo.pofile
100 def test_msgidcomments(self): 100 def test_msgidcomments(self):
101 """checks that we handle msgid comments""" 101 """checks that we handle msgid comments"""
102 posource = 'msgid "test me"\nmsgstr ""' 102 posource = 'msgid "test me"\nmsgstr ""'
103 pofile = self.poparse(posource) 103 pofile = self.poparse(posource)
104 thepo = pofile.units[0] 104 thepo = pofile.units[0]
105 thepo.msgidcomment = "first comment" 105 thepo.msgidcomment = "first comment"
106 print pofile 106 print pofile
107 print "Blah", thepo.source 107 print "Blah", thepo.source
108 assert thepo.source == "test me" 108 assert thepo.source == "test me"
109 thepo.msgidcomment = "second comment" 109 thepo.msgidcomment = "second comment"
110 assert str(pofile).count("_:") == 1 110 assert str(pofile).count("_:") == 1
111 111
112 # def test_merge_duplicates_msgctxt(self): 112 # def test_merge_duplicates_msgctxt(self):
113 # """checks that merging duplicates works for msgctxt""" 113 # """checks that merging duplicates works for msgctxt"""
114 # posource = '#: source1\nmsgid "test me"\nmsgstr ""\n\n#: source2\nmsgid "test me"\nmsgstr ""\n' 114 # posource = '#: source1\nmsgid "test me"\nmsgstr ""\n\n#: source2\nmsgid "test me"\nmsgstr ""\n'
115 # pofile = self.poparse(posource) 115 # pofile = self.poparse(posource)
116 # assert len(pofile.units) == 2 116 # assert len(pofile.units) == 2
117 # pofile.removeduplicates("msgctxt") 117 # pofile.removeduplicates("msgctxt")
118 # print pofile 118 # print pofile
119 # assert len(pofile.units) == 2 119 # assert len(pofile.units) == 2
120 # assert str(pofile.units[0]).count("source1") == 2 120 # assert str(pofile.units[0]).count("source1") == 2
121 # assert str(pofile.units[1]).count("source2") == 2 121 # assert str(pofile.units[1]).count("source2") == 2
122 122
123 # def test_merge_blanks(self): 123 # def test_merge_blanks(self):
124 # """checks that merging adds msgid_comments to blanks""" 124 # """checks that merging adds msgid_comments to blanks"""
125 # posource = '#: source1\nmsgid ""\nmsgstr ""\n\n#: source2\nmsgid ""\nms gstr ""\n' 125 # posource = '#: source1\nmsgid ""\nmsgstr ""\n\n#: source2\nmsgid ""\nms gstr ""\n'
126 # pofile = self.poparse(posource) 126 # pofile = self.poparse(posource)
127 # assert len(pofile.units) == 2 127 # assert len(pofile.units) == 2
128 # pofile.removeduplicates("merge") 128 # pofile.removeduplicates("merge")
129 # assert len(pofile.units) == 2 129 # assert len(pofile.units) == 2
130 # print pofile.units[0].msgidcomments 130 # print pofile.units[0].msgidcomments
131 # print pofile.units[1].msgidcomments 131 # print pofile.units[1].msgidcomments
132 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == "_: source1\n " 132 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == "_: source1\n "
133 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == "_: source2\n " 133 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == "_: source2\n "
134 134
135 # def test_msgid_comment(self): 135 # def test_msgid_comment(self):
136 # """checks that when adding msgid_comments we place them on a newline""" 136 # """checks that when adding msgid_comments we place them on a newline"""
137 # posource = '#: source0\nmsgid "Same"\nmsgstr ""\n\n#: source1\nmsgid "S ame"\nmsgstr ""\n' 137 # posource = '#: source0\nmsgid "Same"\nmsgstr ""\n\n#: source1\nmsgid "S ame"\nmsgstr ""\n'
138 # pofile = self.poparse(posource) 138 # pofile = self.poparse(posource)
139 # assert len(pofile.units) == 2 139 # assert len(pofile.units) == 2
140 # pofile.removeduplicates("msgid_comment") 140 # pofile.removeduplicates("msgid_comment")
141 # assert len(pofile.units) == 2 141 # assert len(pofile.units) == 2
142 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == "_: source0\n " 142 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == "_: source0\n "
143 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == "_: source1\n " 143 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == "_: source1\n "
144 # # Now lets check for formating 144 # # Now lets check for formating
145 # for i in (0, 1): 145 # for i in (0, 1):
146 # expected = '''#: source%d\nmsgid ""\n"_: source%d\\n"\n"Same"\nmsgstr ""\n''' % (i, i) 146 # expected = '''#: source%d\nmsgid ""\n"_: source%d\\n"\n"Same"\nmsgstr ""\n''' % (i, i)
147 # assert pofile.units[i].__str__() == expected 147 # assert pofile.units[i].__str__() == expected
148 148
149 # def test_keep_blanks(self): 149 # def test_keep_blanks(self):
150 # """checks that keeping keeps blanks and doesn't add msgid_comments""" 150 # """checks that keeping keeps blanks and doesn't add msgid_comments"""
151 # posource = '#: source1\nmsgid ""\nmsgstr ""\n\n#: source2\nmsgid ""\nms gstr ""\n' 151 # posource = '#: source1\nmsgid ""\nmsgstr ""\n\n#: source2\nmsgid ""\nms gstr ""\n'
152 # pofile = self.poparse(posource) 152 # pofile = self.poparse(posource)
153 # assert len(pofile.units) == 2 153 # assert len(pofile.units) == 2
154 # pofile.removeduplicates("keep") 154 # pofile.removeduplicates("keep")
155 # assert len(pofile.units) == 2 155 # assert len(pofile.units) == 2
156 # # check we don't add msgidcomments 156 # # check we don't add msgidcomments
157 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == "" 157 # assert po.unquotefrompo(pofile.units[0].msgidcomments) == ""
158 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == "" 158 # assert po.unquotefrompo(pofile.units[1].msgidcomments) == ""
159 159
160 def test_output_str_unicode(self): 160 def test_output_str_unicode(self):
161 """checks that we can str(pofile) which is in unicode""" 161 """checks that we can str(pofile) which is in unicode"""
162 posource = u'''#: nb\nmsgid "Norwegian Bokm\xe5l"\nmsgstr ""\n''' 162 posource = u'''#: nb\nmsgid "Norwegian Bokm\xe5l"\nmsgstr ""\n'''
163 pofile = self.StoreClass(wStringIO.StringIO(posource.encode("UTF-8")), e ncoding="UTF-8") 163 pofile = self.StoreClass(wStringIO.StringIO(posource.encode("UTF-8")), e ncoding="UTF-8")
164 assert len(pofile.units) == 1 164 assert len(pofile.units) == 1
165 print str(pofile) 165 print str(pofile)
166 thepo = pofile.units[0] 166 thepo = pofile.units[0]
167 # assert str(pofile) == posource.encode("UTF-8") 167 # assert str(pofile) == posource.encode("UTF-8")
168 # extra test: what if we set the msgid to a unicode? this happens in pro p2po etc 168 # extra test: what if we set the msgid to a unicode? this happens in pro p2po etc
169 thepo.source = u"Norwegian Bokm\xe5l" 169 thepo.source = u"Norwegian Bokm\xe5l"
170 # assert str(thepo) == posource.encode("UTF-8") 170 # assert str(thepo) == posource.encode("UTF-8")
171 # Now if we set the msgstr to Unicode 171 # Now if we set the msgstr to Unicode
172 # this is an escaped half character (1/2) 172 # this is an escaped half character (1/2)
173 halfstr = "\xbd ...".decode("latin-1") 173 halfstr = "\xbd ...".decode("latin-1")
174 thepo.target = halfstr 174 thepo.target = halfstr
175 # assert halfstr in str(pofile).decode("UTF-8") 175 # assert halfstr in str(pofile).decode("UTF-8")
176 thepo.target = halfstr.encode("UTF-8") 176 thepo.target = halfstr.encode("UTF-8")
177 # assert halfstr.encode("UTF-8") in str(pofile) 177 # assert halfstr.encode("UTF-8") in str(pofile)
178 178
179 def test_posections(self): 179 def test_posections(self):
180 """checks the content of all the expected sections of a PO message""" 180 """checks the content of all the expected sections of a PO message"""
181 posource = '# other comment\n#. automatic comment\n#: source comment\n#, fuzzy\nmsgid "One"\nmsgstr "Een"\n' 181 posource = '# other comment\n#. automatic comment\n#: source comment\n#, fuzzy\nmsgid "One"\nmsgstr "Een"\n'
182 pofile = self.poparse(posource) 182 pofile = self.poparse(posource)
183 print pofile 183 print pofile
184 assert len(pofile.units) == 1 184 assert len(pofile.units) == 1
185 assert str(pofile) == posource 185 assert str(pofile) == posource
186 186
187 def test_multiline_obsolete(self): 187 def test_multiline_obsolete(self):
188 """Tests for correct output of mulitline obsolete messages""" 188 """Tests for correct output of mulitline obsolete messages"""
189 posource = '#~ msgid ""\n#~ "Old thing\\n"\n#~ "Second old thing"\n#~ ms gstr ""\n#~ "Ou ding\\n"\n#~ "Tweede ou ding"\n' 189 posource = '#~ msgid ""\n#~ "Old thing\\n"\n#~ "Second old thing"\n#~ ms gstr ""\n#~ "Ou ding\\n"\n#~ "Tweede ou ding"\n'
190 pofile = self.poparse(posource) 190 pofile = self.poparse(posource)
191 print "Source:\n%s" % posource 191 print "Source:\n%s" % posource
192 print "Output:\n%s" % str(pofile) 192 print "Output:\n%s" % str(pofile)
193 assert len(pofile.units) == 1 193 assert len(pofile.units) == 1
194 assert pofile.units[0].isobsolete() 194 assert pofile.units[0].isobsolete()
195 assert not pofile.units[0].istranslatable() 195 assert not pofile.units[0].istranslatable()
196 assert str(pofile) == posource 196 assert str(pofile) == posource
197 197
198 def test_unassociated_comments(self): 198 def test_unassociated_comments(self):
199 """tests behaviour of unassociated comments.""" 199 """tests behaviour of unassociated comments."""
200 oldsource = '# old lonesome comment\n\nmsgid "one"\nmsgstr "een"\n' 200 oldsource = '# old lonesome comment\n\nmsgid "one"\nmsgstr "een"\n'
201 oldfile = self.poparse(oldsource) 201 oldfile = self.poparse(oldsource)
202 print "__str__", str(oldfile) 202 print "__str__", str(oldfile)
203 assert len(oldfile.units) == 1 203 assert len(oldfile.units) == 1
204 assert str(oldfile).find("# old lonesome comment\nmsgid") >= 0 204 assert str(oldfile).find("# old lonesome comment\nmsgid") >= 0
205 205
LEFTRIGHT

Powered by Google App Engine
This is Rietveld r159