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

Side by Side Diff: translate/storage/test_xliff.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 2
3 from translate.storage import xliff 3 from translate.storage import xliff
4 from translate.storage import test_base 4 from translate.storage import test_base
5 5
6 class TestXLIFFUnit(test_base.TestTranslationUnit): 6 class TestXLIFFUnit(test_base.TestTranslationUnit):
7 UnitClass = xliff.xliffunit 7 UnitClass = xliff.xliffunit
8 8
9 def test_isfuzzy(self): 9 def test_isfuzzy(self):
10 """The default behaviour for XLIFF is different, so we adapt the test 10 """The default behaviour for XLIFF is different, so we adapt the test
11 from test_base.py""" 11 from test_base.py"""
12 assert self.unit.isfuzzy() 12 assert self.unit.isfuzzy()
13 13
14 def test_markreview(self): 14 def test_markreview(self):
15 """Tests if we can mark the unit to need review.""" 15 """Tests if we can mark the unit to need review."""
16 unit = self.unit 16 unit = self.unit
17 # We have to explicitly set the target to nothing, otherwise xliff 17 # We have to explicitly set the target to nothing, otherwise xliff
18 # tests will fail. 18 # tests will fail.
19 # Can we make it default behavior for the UnitClass? 19 # Can we make it default behavior for the UnitClass?
20 unit.target = "" 20 unit.target = ""
21 21
22 unit.addnote("Test note 1", origin="translator") 22 unit.addnote("Test note 1", origin="translator")
23 unit.addnote("Test note 2", origin="translator") 23 unit.addnote("Test note 2", origin="translator")
24 original_notes = unit.getnotes(origin="translator") 24 original_notes = unit.getnotes(origin="translator")
25 25
26 assert not unit.isreview() 26 assert not unit.isreview()
27 unit.markreviewneeded() 27 unit.markreviewneeded()
28 assert unit.isreview() 28 assert unit.isreview()
29 unit.markreviewneeded(False) 29 unit.markreviewneeded(False)
30 assert not unit.isreview() 30 assert not unit.isreview()
31 assert unit.getnotes(origin="translator") == original_notes 31 assert unit.getnotes(origin="translator") == original_notes
32 unit.markreviewneeded(explanation="Double check spelling.") 32 unit.markreviewneeded(explanation="Double check spelling.")
33 assert unit.isreview() 33 assert unit.isreview()
34 notes = unit.getnotes(origin="translator") 34 notes = unit.getnotes(origin="translator")
35 assert notes.count("Double check spelling.") == 1 35 assert notes.count("Double check spelling.") == 1
36 36
37 def test_errors(self): 37 def test_errors(self):
38 """Tests that we can add and retrieve error messages for a unit.""" 38 """Tests that we can add and retrieve error messages for a unit."""
39 unit = self.unit 39 unit = self.unit
40 40
41 assert len(unit.geterrors()) == 0 41 assert len(unit.geterrors()) == 0
42 unit.adderror(errorname='test1', errortext='Test error message 1.') 42 unit.adderror(errorname='test1', errortext='Test error message 1.')
43 unit.adderror(errorname='test2', errortext='Test error message 2.') 43 unit.adderror(errorname='test2', errortext='Test error message 2.')
44 unit.adderror(errorname='test3', errortext='Test error message 3.') 44 unit.adderror(errorname='test3', errortext='Test error message 3.')
45 assert len(unit.geterrors()) == 3 45 assert len(unit.geterrors()) == 3
46 assert unit.geterrors()['test1'] == 'Test error message 1.' 46 assert unit.geterrors()['test1'] == 'Test error message 1.'
47 assert unit.geterrors()['test2'] == 'Test error message 2.' 47 assert unit.geterrors()['test2'] == 'Test error message 2.'
48 assert unit.geterrors()['test3'] == 'Test error message 3.' 48 assert unit.geterrors()['test3'] == 'Test error message 3.'
49 unit.adderror(errorname='test1', errortext='New error 1.') 49 unit.adderror(errorname='test1', errortext='New error 1.')
50 assert unit.geterrors()['test1'] == 'New error 1.' 50 assert unit.geterrors()['test1'] == 'New error 1.'
51
52 def test_contextgroup(self):
53 """tests context groups """
54 unit = self.UnitClass("Group")
55 unit.createcontextgroup('my-group', [('group-type', 'my message')],
56 purpose="test context group")
57 groups = unit.getcontextgroups('my-group')
58 assert len(groups) == 1
59
60 # group = [(type, message)]
61 group = groups[0]
62 print group
63 assert group[0][0] == 'group-type'
64 assert group[0][1] == 'my message'
65
66 unit.delcontextgroup('my-group')
67 groups = unit.getcontextgroups('my-group')
68 assert groups == []
69
51 70
52 class TestXLIFFfile(test_base.TestTranslationStore): 71 class TestXLIFFfile(test_base.TestTranslationStore):
53 StoreClass = xliff.xlifffile 72 StoreClass = xliff.xlifffile
54 skeleton = '''<?xml version="1.0" encoding="utf-8"?> 73 skeleton = '''<?xml version="1.0" encoding="utf-8"?>
55 <xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1"> 74 <xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1">
56 <file original="doc.txt" source-language="en-US"> 75 <file original="doc.txt" source-language="en-US">
57 <body> 76 <body>
58 %s 77 %s
59 </body> 78 </body>
60 </file> 79 </file>
61 </xliff>''' 80 </xliff>'''
62 81
63 def test_basic(self): 82 def test_basic(self):
64 xlifffile = xliff.xlifffile() 83 xlifffile = xliff.xlifffile()
65 assert xlifffile.units == [] 84 assert xlifffile.units == []
66 xlifffile.addsourceunit("Bla") 85 xlifffile.addsourceunit("Bla")
67 assert len(xlifffile.units) == 1 86 assert len(xlifffile.units) == 1
68 newfile = xliff.xlifffile.parsestring(str(xlifffile)) 87 newfile = xliff.xlifffile.parsestring(str(xlifffile))
69 print str(xlifffile) 88 print str(xlifffile)
70 assert len(newfile.units) == 1 89 assert len(newfile.units) == 1
71 assert newfile.units[0].source == "Bla" 90 assert newfile.units[0].source == "Bla"
72 assert newfile.findunit("Bla").source == "Bla" 91 assert newfile.findunit("Bla").source == "Bla"
73 assert newfile.findunit("dit") is None 92 assert newfile.findunit("dit") is None
74 93
75 def test_source(self): 94 def test_source(self):
76 xlifffile = xliff.xlifffile() 95 xlifffile = xliff.xlifffile()
77 xliffunit = xlifffile.addsourceunit("Concept") 96 xliffunit = xlifffile.addsourceunit("Concept")
78 xliffunit.source = "Term" 97 xliffunit.source = "Term"
79 newfile = xliff.xlifffile.parsestring(str(xlifffile)) 98 newfile = xliff.xlifffile.parsestring(str(xlifffile))
80 print str(xlifffile) 99 print str(xlifffile)
81 assert newfile.findunit("Concept") is None 100 assert newfile.findunit("Concept") is None
82 assert newfile.findunit("Term") is not None 101 assert newfile.findunit("Term") is not None
83 102
84 def test_target(self): 103 def test_target(self):
85 xlifffile = xliff.xlifffile() 104 xlifffile = xliff.xlifffile()
86 xliffunit = xlifffile.addsourceunit("Concept") 105 xliffunit = xlifffile.addsourceunit("Concept")
87 xliffunit.target = "Konsep" 106 xliffunit.target = "Konsep"
88 newfile = xliff.xlifffile.parsestring(str(xlifffile)) 107 newfile = xliff.xlifffile.parsestring(str(xlifffile))
89 print str(xlifffile) 108 print str(xlifffile)
90 assert newfile.findunit("Concept").target == "Konsep" 109 assert newfile.findunit("Concept").target == "Konsep"
91 110
92 def test_sourcelanguage(self): 111 def test_sourcelanguage(self):
93 xlifffile = xliff.xlifffile(sourcelanguage="xh") 112 xlifffile = xliff.xlifffile(sourcelanguage="xh")
94 xmltext = str(xlifffile) 113 xmltext = str(xlifffile)
95 print xmltext 114 print xmltext
96 assert xmltext.find('source-language="xh"')> 0 115 assert xmltext.find('source-language="xh"')> 0
97 #TODO: test that it also works for new files. 116 #TODO: test that it also works for new files.
98 117
99 def test_targetlanguage(self): 118 def test_targetlanguage(self):
100 xlifffile = xliff.xlifffile(sourcelanguage="zu", targetlanguage="af") 119 xlifffile = xliff.xlifffile(sourcelanguage="zu", targetlanguage="af")
(...skipping 60 matching lines...) Show 10 above Show 10 below
161 def test_fuzzy(self): 180 def test_fuzzy(self):
162 xlifffile = xliff.xlifffile() 181 xlifffile = xliff.xlifffile()
163 unit = xlifffile.addsourceunit("Concept") 182 unit = xlifffile.addsourceunit("Concept")
164 unit.markfuzzy() 183 unit.markfuzzy()
165 assert unit.isfuzzy() 184 assert unit.isfuzzy()
166 unit.target = "Konsep" 185 unit.target = "Konsep"
167 assert unit.isfuzzy() 186 assert unit.isfuzzy()
168 unit.markfuzzy() 187 unit.markfuzzy()
169 assert unit.isfuzzy() 188 assert unit.isfuzzy()
170 unit.markfuzzy(False) 189 unit.markfuzzy(False)
171 assert not unit.isfuzzy() 190 assert not unit.isfuzzy()
172 unit.markfuzzy(True) 191 unit.markfuzzy(True)
173 assert unit.isfuzzy() 192 assert unit.isfuzzy()
174 193
175 #If there is no target, we can't really indicate fuzzyness, so we set 194 #If there is no target, we can't really indicate fuzzyness, so we set
176 #approved to "no". If we want isfuzzy() to reflect that, the line can 195 #approved to "no". If we want isfuzzy() to reflect that, the line can
177 #be uncommented 196 #be uncommented
178 unit.target = None 197 unit.target = None
179 assert unit.target is None 198 assert unit.target is None
180 print unit 199 print unit
181 unit.markfuzzy(True) 200 unit.markfuzzy(True)
182 assert 'approved="no"' in str(unit) 201 assert 'approved="no"' in str(unit)
183 #assert unit.isfuzzy() 202 #assert unit.isfuzzy()
184 203
185 def test_parsing(self): 204 def test_parsing(self):
186 xlfsource = self.skeleton \ 205 xlfsource = self.skeleton \
187 % '''<trans-unit id="1" xml:space="preserve"> 206 % '''<trans-unit id="1" xml:space="preserve">
188 <source>File</source> 207 <source>File</source>
189 <target/> 208 <target/>
190 </trans-unit>''' 209 </trans-unit>'''
191 xlifffile = xliff.xlifffile.parsestring(xlfsource) 210 xlifffile = xliff.xlifffile.parsestring(xlfsource)
192 assert xlifffile.units[0].istranslatable() 211 assert xlifffile.units[0].istranslatable()
193 212
194 xlfsource = self.skeleton \ 213 xlfsource = self.skeleton \
195 % '''<trans-unit id="1" xml:space="preserve" translate="no"> 214 % '''<trans-unit id="1" xml:space="preserve" translate="no">
196 <source>File</source> 215 <source>File</source>
197 <target/> 216 <target/>
198 </trans-unit>''' 217 </trans-unit>'''
199 xlifffile = xliff.xlifffile.parsestring(xlfsource) 218 xlifffile = xliff.xlifffile.parsestring(xlfsource)
200 assert not xlifffile.units[0].istranslatable() 219 assert not xlifffile.units[0].istranslatable()
201 220
202 xlfsource = self.skeleton \ 221 xlfsource = self.skeleton \
203 % '''<trans-unit id="1" xml:space="preserve" translate="yes"> 222 % '''<trans-unit id="1" xml:space="preserve" translate="yes">
204 <source>File</source> 223 <source>File</source>
205 <target/> 224 <target/>
206 </trans-unit>''' 225 </trans-unit>'''
207 xlifffile = xliff.xlifffile.parsestring(xlfsource) 226 xlifffile = xliff.xlifffile.parsestring(xlfsource)
208 assert xlifffile.units[0].istranslatable() 227 assert xlifffile.units[0].istranslatable()
209 228
210 229
OLDNEW

Powered by Google App Engine
This is Rietveld r159