2 # -*- coding: utf-8 -*-
4 """tests for storage base classes"""
6 from translate
.storage
import base
10 def test_force_override():
11 """Tests that derived classes are not allowed to call certain functions"""
14 base
.force_override(self
.test
, BaseClass
)
17 base
.force_override(cls
.classtest
, BaseClass
)
19 classtest
= classmethod(classtest
)
20 class DerivedClass(BaseClass
):
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.
42 assert not self
.unit
.isfuzzy()
44 def test_create(self
):
45 """tests a simple creation with a source string"""
47 print 'unit.source:', unit
.source
48 assert unit
.source
== "Test String"
51 """tests equality comparison"""
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")
61 unit1
.target
= "Stressed Ting"
62 unit2
.target
= "Stressed Ting"
63 unit5
.target
= "Stressed Bling"
64 unit6
.target
= "Stressed Ting"
70 def test_target(self
):
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"
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."""
84 specials
= ['Fish & chips', 'five < six', 'six > five', 'five < six',
85 'Use ', 'Use &nbsp;', 'Use &amp;nbsp;',
86 'A "solution"', "skop 'n bal", '"""', "'''", u
'µ',
87 '\n', '\t', '\r', '\r\n', '\\r', '\\', '\\\r']
88 for special
in specials
:
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."""
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."""
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.
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
151 """Tests adding a new unit with a source string"""
152 store
= self
.StoreClass()
153 unit
= store
.addsourceunit("Test String")
156 assert len(store
.units
) == 1
157 assert unit
.source
== "Test String"
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
)
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
191 print "match failed between elements %d of %d" % (n
+1, len(store1
.units
))
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
)
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
)
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"
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: