1   
  2   
  3   
  4  """tests for storage base classes""" 
  5   
  6  from translate.storage import base 
  7  from py import test 
  8  import os 
  9   
 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) 
 28   
 30      """Tests a TranslationUnit. 
 31      Derived classes can reuse these tests by pointing UnitClass to a derived Unit""" 
 32      UnitClass = base.TranslationUnit 
 33   
 35          self.unit = self.UnitClass("Test String") 
  36   
 38          """Test that we can call isfuzzy() on a unit. 
 39           
 40          The default return value for isfuzzy() should be False. 
 41          """ 
 42          assert not self.unit.isfuzzy() 
  43   
 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" 
  49   
 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 
  69   
 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 == "" 
  79   
 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 < 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: 
 89              unit.source = special 
 90              print "unit.source:", repr(unit.source) 
 91              print "special:", repr(special) 
 92              assert unit.source == special 
  93   
 95          """Test difficult characters that might go wrong in a quoting and  
 96          escaping roundtrip.""" 
 97   
 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 
 107   
109          """Tests that all subclasses of the base behaves consistently with regards to notes.""" 
110          unit = self.unit 
111   
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 
118   
119           
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 
  128   
130      """Tests a TranslationStore. 
131      Derived classes can reuse these tests by pointing StoreClass to a derived Store""" 
132      StoreClass = base.TranslationStore 
133   
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) 
 139   
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) 
 144   
146          """Tests creating a new blank store""" 
147          store = self.StoreClass() 
148          assert len(store.units) == 0 
 149   
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" 
 158   
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 
 167   
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" 
 177   
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 
 183   
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 
 199   
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) 
 209   
220   
232   
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" 
 240   
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           
251          src = str(store) 
  252