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