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

Side by Side Diff: translate/storage/test_cpo.py

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

Powered by Google App Engine
This is Rietveld r159