Back to index

plone3  3.1.7
testPTS.py
Go to the documentation of this file.
00001 #
00002 # PTS test
00003 #
00004 
00005 import os
00006 import unittest
00007 from Testing import ZopeTestCase
00008 
00009 ZopeTestCase.installProduct('PlacelessTranslationService')
00010 
00011 from Products.CMFPlone.utils import versionTupleFromString
00012 
00013 from Products.PlacelessTranslationService import pts_globals
00014 from Products.PlacelessTranslationService import PlacelessTranslationService as PTS
00015 
00016 from Globals import package_home
00017 PACKAGE_HOME = package_home(pts_globals)
00018 
00019 def getFSVersionTuple():
00020     """Reads version.txt and returns version tuple"""
00021     vfile = "%s/version.txt" % PACKAGE_HOME
00022     v_str = open(vfile, 'r').read().lower()
00023     return versionTupleFromString(v_str)
00024 
00025 
00026 class TestPTS(ZopeTestCase.ZopeTestCase):
00027 
00028     def afterSetUp(self):
00029         self.service = self.app.Control_Panel.TranslationService
00030         self.dir1 = os.path.join(os.path.dirname(__file__), 'i18nsample')
00031         self.dir2 = os.path.join(os.path.dirname(__file__), 'i18nsample2')
00032         self.mo_file = os.path.join(self.dir1, 'fr', 'LC_MESSAGES', 'plone.mo')
00033         self.mo_file2 = os.path.join(self.dir2, 'fr', 'LC_MESSAGES', 'plone.mo')
00034 
00035     def tearDown(self):
00036         for f in (self.mo_file, self.mo_file2):
00037             if os.path.exists(f):
00038                 os.remove(f)
00039 
00040     def testClassVersion(self):
00041         clv = PTS._class_version
00042         fsv = getFSVersionTuple()
00043         for i in range(3):
00044             self.assertEquals(clv[i], fsv[i],
00045                               'class version (%s) does not match filesystem version (%s)' % (clv, fsv))
00046 
00047     def testInterpolate(self):
00048         # empty mapping
00049         text = 'ascii'
00050         self.assertEquals(self.service.interpolate(text, []), text)
00051 
00052         text = 'ascii-with-funky-chars\xe2'
00053         self.assertEquals(self.service.interpolate(text, []), text)
00054 
00055         text = u'unicode-with-ascii-only'
00056         self.assertEquals(self.service.interpolate(text, []), text)
00057 
00058         text = u'unicode\xe2'
00059         self.assertEquals(self.service.interpolate(text, []), text)
00060 
00061         # text is ascii
00062         text = '${ascii}'
00063         mapping = {u'ascii' : 'ascii'}
00064         expected = 'ascii'
00065         self.assertEquals(self.service.interpolate(text, mapping), expected)
00066 
00067         text = '${ascii}'
00068         mapping = {u'ascii' : 'ascii-with-funky-chars\xe2'}
00069         expected = 'ascii-with-funky-chars\xe2'
00070         self.assertEquals(self.service.interpolate(text, mapping), expected)
00071 
00072         text = '${ascii}'
00073         mapping = {u'ascii' : u'unicode-with-ascii-only'}
00074         expected = u'unicode-with-ascii-only'
00075         self.assertEquals(self.service.interpolate(text, mapping), expected)
00076 
00077         text = '${ascii}'
00078         mapping = {u'ascii' : u'unicode\xe2'}
00079         expected = u'unicode\xe2'
00080         self.assertEquals(self.service.interpolate(text, mapping), expected)
00081 
00082         text = '${ascii}'
00083         mapping = {'ascii' : 1}
00084         expected = '1'
00085         self.assertEquals(self.service.interpolate(text, mapping), expected)
00086 
00087         # text is ascii with funky chars
00088         text = '${ascii-with-funky-chars}\xc2'
00089         mapping = {u'ascii-with-funky-chars' : 'ascii'}
00090         expected = 'ascii\xc2'
00091         self.assertEquals(self.service.interpolate(text, mapping), expected)
00092 
00093         text = '${ascii-with-funky-chars}\xc2'
00094         mapping = {u'ascii-with-funky-chars' : 'ascii-with-funky-chars\xe2'}
00095         expected = 'ascii-with-funky-chars\xe2\xc2'
00096         self.assertEquals(self.service.interpolate(text, mapping), expected)
00097 
00098         text = '${ascii-with-funky-chars}\xc2'
00099         mapping = {u'ascii-with-funky-chars' : u'unicode-with-ascii-only'}
00100         expected = '${ascii-with-funky-chars}\xc2'
00101         self.assertEquals(self.service.interpolate(text, mapping), expected)
00102 
00103         text = '${ascii-with-funky-chars}\xc2'
00104         mapping = {u'ascii-with-funky-chars' : u'unicode\xe2'}
00105         expected = '${ascii-with-funky-chars}\xc2'
00106         self.assertEquals(self.service.interpolate(text, mapping), expected)
00107 
00108         text = '${ascii-with-funky-chars}\xc2'
00109         mapping = {'ascii-with-funky-chars' : 1}
00110         expected = '1\xc2'
00111         self.assertEquals(self.service.interpolate(text, mapping), expected)
00112 
00113         # text is unicode with only ascii chars
00114         text = u'${unicode-with-ascii-only}'
00115         mapping = {u'unicode-with-ascii-only' : 'ascii'}
00116         expected = 'ascii'
00117         self.assertEquals(self.service.interpolate(text, mapping), expected)
00118 
00119         text = u'${unicode-with-ascii-only}'
00120         mapping = {u'unicode-with-ascii-only' : 'ascii-with-funky-chars\xe2'}
00121         expected = u'${unicode-with-ascii-only}'
00122         self.assertEquals(self.service.interpolate(text, mapping), expected)
00123 
00124         text = u'${unicode-with-ascii-only}'
00125         mapping = {u'unicode-with-ascii-only' : u'unicode-with-ascii-only'}
00126         expected = u'unicode-with-ascii-only'
00127         self.assertEquals(self.service.interpolate(text, mapping), expected)
00128 
00129         text = u'${unicode-with-ascii-only}'
00130         mapping = {u'unicode-with-ascii-only' : u'unicode\xe2'}
00131         expected = u'unicode\xe2'
00132         self.assertEquals(self.service.interpolate(text, mapping), expected)
00133 
00134         text = u'${unicode-with-ascii-only}'
00135         mapping = {u'unicode-with-ascii-only' : 1}
00136         expected = u'1'
00137         self.assertEquals(self.service.interpolate(text, mapping), expected)
00138 
00139         # text is real unicode
00140         text = u'${unicode}\xc2'
00141         mapping = {u'unicode' : 'ascii'}
00142         expected = u'ascii\xc2'
00143         self.assertEquals(self.service.interpolate(text, mapping), expected)
00144 
00145         text = u'${unicode}\xc2'
00146         mapping = {u'unicode' : 'ascii-with-funky-chars\xe2'}
00147         expected = u'${unicode}\xc2'
00148         self.assertEquals(self.service.interpolate(text, mapping), expected)
00149 
00150         text = u'${unicode}\xc2'
00151         mapping = {u'unicode' : u'unicode-with-ascii-only'}
00152         expected = u'unicode-with-ascii-only\xc2'
00153         self.assertEquals(self.service.interpolate(text, mapping), expected)
00154 
00155         text = u'${unicode}\xc2'
00156         mapping = {u'unicode' : u'unicode\xe2'}
00157         expected = u'unicode\xe2\xc2'
00158         self.assertEquals(self.service.interpolate(text, mapping), expected)
00159 
00160         text = u'${unicode}\xc2'
00161         mapping = {'unicode' : 1}
00162         expected = u'1\xc2'
00163         self.assertEquals(self.service.interpolate(text, mapping), expected)
00164 
00165 def test_suite():
00166     from unittest import TestSuite, makeSuite
00167     suite = TestSuite()
00168     suite.addTest(makeSuite(TestPTS))
00169     return suite
00170 
00171 if __name__ == '__main__':
00172     unittest.main(defaultTest="test_suite")