fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / storage / test_base.py
blobbaced41435b6d91e81b840ee65d245ce9001e0e8
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 """tests for storage base classes"""
6 from translate.storage import base
7 from py import test
8 import os
10 def test_force_override():
11 """Tests that derived classes are not allowed to call certain functions"""
12 class BaseClass:
13 def test(self):
14 base.force_override(self.test, BaseClass)
15 return True
16 def classtest(cls):
17 base.force_override(cls.classtest, BaseClass)
18 return True
19 classtest = classmethod(classtest)
20 class DerivedClass(BaseClass):
21 pass
22 baseobject = BaseClass()
23 assert baseobject.test()
24 assert baseobject.classtest()
25 derivedobject = DerivedClass()
26 assert test.raises(NotImplementedError, derivedobject.test)
27 assert test.raises(NotImplementedError, derivedobject.classtest)
29 class TestTranslationUnit:
30 """Tests a TranslationUnit.
31 Derived classes can reuse these tests by pointing UnitClass to a derived Unit"""
32 UnitClass = base.TranslationUnit
34 def setup_method(self, method):
35 self.unit = self.UnitClass("Test String")
37 def test_isfuzzy(self):
38 """Test that we can call isfuzzy() on a unit.
40 The default return value for isfuzzy() should be False.
41 """
42 assert not self.unit.isfuzzy()
44 def test_create(self):
45 """tests a simple creation with a source string"""
46 unit = self.unit
47 print 'unit.source:', unit.source
48 assert unit.source == "Test String"
50 def test_eq(self):
51 """tests equality comparison"""
52 unit1 = self.unit
53 unit2 = self.UnitClass("Test String")
54 unit3 = self.UnitClass("Test String")
55 unit4 = self.UnitClass("Blessed String")
56 unit5 = self.UnitClass("Blessed String")
57 unit6 = self.UnitClass("Blessed String")
58 assert unit1 == unit1
59 assert unit1 == unit2
60 assert unit1 != unit4
61 unit1.target = "Stressed Ting"
62 unit2.target = "Stressed Ting"
63 unit5.target = "Stressed Bling"
64 unit6.target = "Stressed Ting"
65 assert unit1 == unit2
66 assert unit1 != unit3
67 assert unit4 != unit5
68 assert unit1 != unit6
70 def test_target(self):
71 unit = self.unit
72 assert not unit.target
73 unit.target = "Stressed Ting"
74 assert unit.target == "Stressed Ting"
75 unit.target = "Stressed Bling"
76 assert unit.target == "Stressed Bling"
77 unit.target = ""
78 assert unit.target == ""
80 def test_escapes(self):
81 """Test all sorts of characters that might go wrong in a quoting and
82 escaping roundtrip."""
83 unit = self.unit
84 specials = ['Fish & chips', 'five < six', 'six > five', 'five &lt; six',
85 'Use &nbsp;', 'Use &amp;nbsp;', 'Use &amp;amp;nbsp;',
86 'A "solution"', "skop 'n bal", '"""', "'''", u'µ',
87 '\n', '\t', '\r', '\r\n', '\\r', '\\', '\\\r']
88 for special in specials:
89 unit.source = special
90 print "unit.source:", repr(unit.source)
91 print "special:", repr(special)
92 assert unit.source == special
94 def test_difficult_escapes(self):
95 """Test difficult characters that might go wrong in a quoting and
96 escaping roundtrip."""
98 unit = self.unit
99 specials = ['\\n', '\\t', '\\"', '\\ ',
100 '\\\n', '\\\t', '\\\\n', '\\\\t', '\\\\r', '\\\\"',
101 '\\r\\n', '\\\\r\\n', '\\r\\\\n', '\\\\n\\\\r']
102 for special in specials:
103 unit.source = special
104 print "unit.source:", repr(unit.source) + '|'
105 print "special:", repr(special) + '|'
106 assert unit.source == special
108 def test_note_sanity(self):
109 """Tests that all subclasses of the base behaves consistently with regards to notes."""
110 unit = self.unit
112 unit.addnote("Test note 1", origin="translator")
113 unit.addnote("Test note 2", origin="translator")
114 unit.addnote("Test note 3", origin="translator")
115 expected_notes = u"Test note 1\nTest note 2\nTest note 3"
116 actual_notes = unit.getnotes(origin="translator")
117 assert actual_notes == expected_notes
119 # Test with no origin.
120 unit.removenotes()
121 assert not unit.getnotes()
122 unit.addnote("Test note 1")
123 unit.addnote("Test note 2")
124 unit.addnote("Test note 3")
125 expected_notes = u"Test note 1\nTest note 2\nTest note 3"
126 actual_notes = unit.getnotes()
127 assert actual_notes == expected_notes
129 class TestTranslationStore:
130 """Tests a TranslationStore.
131 Derived classes can reuse these tests by pointing StoreClass to a derived Store"""
132 StoreClass = base.TranslationStore
134 def setup_method(self, method):
135 """Allocates a unique self.filename for the method, making sure it doesn't exist"""
136 self.filename = "%s_%s.test" % (self.__class__.__name__, method.__name__)
137 if os.path.exists(self.filename):
138 os.remove(self.filename)
140 def teardown_method(self, method):
141 """Makes sure that if self.filename was created by the method, it is cleaned up"""
142 if os.path.exists(self.filename):
143 os.remove(self.filename)
145 def test_create_blank(self):
146 """Tests creating a new blank store"""
147 store = self.StoreClass()
148 assert len(store.units) == 0
150 def test_add(self):
151 """Tests adding a new unit with a source string"""
152 store = self.StoreClass()
153 unit = store.addsourceunit("Test String")
154 print str(unit)
155 print str(store)
156 assert len(store.units) == 1
157 assert unit.source == "Test String"
159 def test_find(self):
160 """Tests searching for a given source string"""
161 store = self.StoreClass()
162 unit1 = store.addsourceunit("Test String")
163 unit2 = store.addsourceunit("Blessed String")
164 assert store.findunit("Test String") == unit1
165 assert store.findunit("Blessed String") == unit2
166 assert store.findunit("Nest String") is None
168 def test_translate(self):
169 """Tests the translate method and non-ascii characters."""
170 store = self.StoreClass()
171 unit = store.addsourceunit("scissor")
172 unit.target = u"skêr"
173 unit = store.addsourceunit(u"Beziér curve")
174 unit.target = u"Beziér-kurwe"
175 assert store.translate("scissor") == u"skêr"
176 assert store.translate(u"Beziér curve") == u"Beziér-kurwe"
178 def reparse(self, store):
179 """converts the store to a string and back to a store again"""
180 storestring = str(store)
181 newstore = self.StoreClass.parsestring(storestring)
182 return newstore
184 def check_equality(self, store1, store2):
185 """asserts that store1 and store2 are the same"""
186 assert len(store1.units) == len(store2.units)
187 for n, store1unit in enumerate(store1.units):
188 store2unit = store2.units[n]
189 match = store1unit == store2unit
190 if not match:
191 print "match failed between elements %d of %d" % (n+1, len(store1.units))
192 print "store1:"
193 print str(store1)
194 print "store2:"
195 print str(store2)
196 print "store1.units[%d].__dict__:" % n, store1unit.__dict__
197 print "store2.units[%d].__dict__:" % n, store2unit.__dict__
198 assert store1unit == store2unit
200 def test_parse(self):
201 """Tests converting to a string and parsing the resulting string"""
202 store = self.StoreClass()
203 unit1 = store.addsourceunit("Test String")
204 unit1.target = "Test String"
205 unit2 = store.addsourceunit("Test String 2")
206 unit2.target = "Test String 2"
207 newstore = self.reparse(store)
208 self.check_equality(store, newstore)
210 def test_files(self):
211 """Tests saving to and loading from files"""
212 store = self.StoreClass()
213 unit1 = store.addsourceunit("Test String")
214 unit1.target = "Test String"
215 unit2 = store.addsourceunit("Test String 2")
216 unit2.target = "Test String 2"
217 store.savefile(self.filename)
218 newstore = self.StoreClass.parsefile(self.filename)
219 self.check_equality(store, newstore)
221 def test_save(self):
222 """Tests that we can save directly back to the original file."""
223 store = self.StoreClass()
224 unit1 = store.addsourceunit("Test String")
225 unit1.target = "Test String"
226 unit2 = store.addsourceunit("Test String 2")
227 unit2.target = "Test String 2"
228 store.savefile(self.filename)
229 store.save()
230 newstore = self.StoreClass.parsefile(self.filename)
231 self.check_equality(store, newstore)
233 def test_markup(self):
234 """Tests that markup survives the roundtrip. Most usefull for xml types."""
235 store = self.StoreClass()
236 unit = store.addsourceunit("<vark@hok.org> %d keer %2$s")
237 assert unit.source == "<vark@hok.org> %d keer %2$s"
238 unit.target = "bla"
239 assert store.translate("<vark@hok.org> %d keer %2$s") == "bla"
241 def test_nonascii(self):
242 store = self.StoreClass()
243 unit = store.addsourceunit(u"Beziér curve")
244 string = u"Beziér-kurwe"
245 unit.target = string.encode("utf-8")
246 answer = store.translate(u"Beziér curve")
247 if isinstance(answer, str):
248 answer = answer.decode("utf-8")
249 assert answer == u"Beziér-kurwe"
250 #Just test that __str__ doesn't raise exception:
251 src = str(store)