fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / storage / test_xliff.py
blobff848d90087f3ec582a5fe42e6cddc8b1381f02a
1 #!/usr/bin/env python
3 from translate.storage import xliff
4 from translate.storage import test_base
6 class TestXLIFFUnit(test_base.TestTranslationUnit):
7 UnitClass = xliff.xliffunit
9 def test_isfuzzy(self):
10 """The default behaviour for XLIFF is different, so we adapt the test
11 from test_base.py"""
12 assert self.unit.isfuzzy()
14 def test_markreview(self):
15 """Tests if we can mark the unit to need review."""
16 unit = self.unit
17 # We have to explicitly set the target to nothing, otherwise xliff
18 # tests will fail.
19 # Can we make it default behavior for the UnitClass?
20 unit.target = ""
22 unit.addnote("Test note 1", origin="translator")
23 unit.addnote("Test note 2", origin="translator")
24 original_notes = unit.getnotes(origin="translator")
26 assert not unit.isreview()
27 unit.markreviewneeded()
28 assert unit.isreview()
29 unit.markreviewneeded(False)
30 assert not unit.isreview()
31 assert unit.getnotes(origin="translator") == original_notes
32 unit.markreviewneeded(explanation="Double check spelling.")
33 assert unit.isreview()
34 notes = unit.getnotes(origin="translator")
35 assert notes.count("Double check spelling.") == 1
37 def test_errors(self):
38 """Tests that we can add and retrieve error messages for a unit."""
39 unit = self.unit
41 assert len(unit.geterrors()) == 0
42 unit.adderror(errorname='test1', errortext='Test error message 1.')
43 unit.adderror(errorname='test2', errortext='Test error message 2.')
44 unit.adderror(errorname='test3', errortext='Test error message 3.')
45 assert len(unit.geterrors()) == 3
46 assert unit.geterrors()['test1'] == 'Test error message 1.'
47 assert unit.geterrors()['test2'] == 'Test error message 2.'
48 assert unit.geterrors()['test3'] == 'Test error message 3.'
49 unit.adderror(errorname='test1', errortext='New error 1.')
50 assert unit.geterrors()['test1'] == 'New error 1.'
52 class TestXLIFFfile(test_base.TestTranslationStore):
53 StoreClass = xliff.xlifffile
54 skeleton = '''<?xml version="1.0" encoding="utf-8"?>
55 <xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1">
56 <file original="doc.txt" source-language="en-US">
57 <body>
59 </body>
60 </file>
61 </xliff>'''
63 def test_basic(self):
64 xlifffile = xliff.xlifffile()
65 assert xlifffile.units == []
66 xlifffile.addsourceunit("Bla")
67 assert len(xlifffile.units) == 1
68 newfile = xliff.xlifffile.parsestring(str(xlifffile))
69 print str(xlifffile)
70 assert len(newfile.units) == 1
71 assert newfile.units[0].source == "Bla"
72 assert newfile.findunit("Bla").source == "Bla"
73 assert newfile.findunit("dit") is None
75 def test_source(self):
76 xlifffile = xliff.xlifffile()
77 xliffunit = xlifffile.addsourceunit("Concept")
78 xliffunit.source = "Term"
79 newfile = xliff.xlifffile.parsestring(str(xlifffile))
80 print str(xlifffile)
81 assert newfile.findunit("Concept") is None
82 assert newfile.findunit("Term") is not None
84 def test_target(self):
85 xlifffile = xliff.xlifffile()
86 xliffunit = xlifffile.addsourceunit("Concept")
87 xliffunit.target = "Konsep"
88 newfile = xliff.xlifffile.parsestring(str(xlifffile))
89 print str(xlifffile)
90 assert newfile.findunit("Concept").target == "Konsep"
92 def test_sourcelanguage(self):
93 xlifffile = xliff.xlifffile(sourcelanguage="xh")
94 xmltext = str(xlifffile)
95 print xmltext
96 assert xmltext.find('source-language="xh"')> 0
97 #TODO: test that it also works for new files.
99 def test_targetlanguage(self):
100 xlifffile = xliff.xlifffile(sourcelanguage="zu", targetlanguage="af")
101 xmltext = str(xlifffile)
102 print xmltext
103 assert xmltext.find('source-language="zu"')> 0
104 assert xmltext.find('target-language="af"')> 0
106 def test_notes(self):
107 xlifffile = xliff.xlifffile()
108 unit = xlifffile.addsourceunit("Concept")
109 unit.addnote("Please buy bread")
110 assert unit.getnotes() == "Please buy bread"
111 notenodes = unit.xmlelement.findall(".//%s" % unit.namespaced("note"))
112 assert len(notenodes) == 1
114 unit.addnote("Please buy milk", origin="Mom")
115 notenodes = unit.xmlelement.findall(".//%s" % unit.namespaced("note"))
116 assert len(notenodes) == 2
117 assert not "from" in notenodes[0].attrib
118 assert notenodes[1].get("from") == "Mom"
119 assert unit.getnotes(origin="Mom") == "Please buy milk"
121 unit.addnote("Don't forget the beer", origin="Dad")
122 notenodes = unit.xmlelement.findall(".//%s" % unit.namespaced("note"))
123 assert len(notenodes) == 3
124 assert notenodes[1].get("from") == "Mom"
125 assert notenodes[2].get("from") == "Dad"
126 assert unit.getnotes(origin="Dad") == "Don't forget the beer"
128 assert not unit.getnotes(origin="Bob") == "Please buy bread\nPlease buy milk\nDon't forget the beer"
129 assert not notenodes[2].get("from") == "Mom"
130 assert not "from" in notenodes[0].attrib
131 assert unit.getnotes() == "Please buy bread\nPlease buy milk\nDon't forget the beer"
132 assert unit.correctorigin(notenodes[2], "ad") == True
133 assert unit.correctorigin(notenodes[2], "om") == False
135 def test_alttrans(self):
136 """Test xliff <alt-trans> accessors"""
137 xlifffile = xliff.xlifffile()
138 unit = xlifffile.addsourceunit("Testing")
140 unit.addalttrans("ginmi")
141 unit.addalttrans("shikenki")
142 alternatives = unit.getalttrans()
143 assert alternatives[0].source == "Testing"
144 assert alternatives[0].target == "ginmi"
145 assert alternatives[1].target == "shikenki"
147 unit.addalttrans("Tasting", origin="bob", lang="eng")
148 alternatives = unit.getalttrans()
149 assert alternatives[2].target == "Tasting"
151 alternatives = unit.getalttrans(origin="bob")
152 assert alternatives[0].target == "Tasting"
154 unit.delalttrans(alternatives[0])
155 assert len(unit.getalttrans(origin="bob")) == 0
156 alternatives = unit.getalttrans()
157 assert len(alternatives) == 2
158 assert alternatives[0].target == "ginmi"
159 assert alternatives[1].target == "shikenki"
161 def test_fuzzy(self):
162 xlifffile = xliff.xlifffile()
163 unit = xlifffile.addsourceunit("Concept")
164 unit.markfuzzy()
165 assert unit.isfuzzy()
166 unit.target = "Konsep"
167 assert unit.isfuzzy()
168 unit.markfuzzy()
169 assert unit.isfuzzy()
170 unit.markfuzzy(False)
171 assert not unit.isfuzzy()
172 unit.markfuzzy(True)
173 assert unit.isfuzzy()
175 #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
177 #be uncommented
178 unit.target = None
179 assert unit.target is None
180 print unit
181 unit.markfuzzy(True)
182 assert 'approved="no"' in str(unit)
183 #assert unit.isfuzzy()
185 def test_parsing(self):
186 xlfsource = self.skeleton \
187 % '''<trans-unit id="1" xml:space="preserve">
188 <source>File</source>
189 <target/>
190 </trans-unit>'''
191 xlifffile = xliff.xlifffile.parsestring(xlfsource)
192 assert xlifffile.units[0].istranslatable()
194 xlfsource = self.skeleton \
195 % '''<trans-unit id="1" xml:space="preserve" translate="no">
196 <source>File</source>
197 <target/>
198 </trans-unit>'''
199 xlifffile = xliff.xlifffile.parsestring(xlfsource)
200 assert not xlifffile.units[0].istranslatable()
202 xlfsource = self.skeleton \
203 % '''<trans-unit id="1" xml:space="preserve" translate="yes">
204 <source>File</source>
205 <target/>
206 </trans-unit>'''
207 xlifffile = xliff.xlifffile.parsestring(xlfsource)
208 assert xlifffile.units[0].istranslatable()