Back to index

plone3  3.1.7
test_normalizer.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     Normalizer tests.
00004 """
00005 
00006 import unittest
00007 
00008 import plone.i18n.normalizer
00009 from plone.i18n.normalizer.interfaces import IIDNormalizer
00010 from plone.i18n.normalizer.interfaces import IFileNameNormalizer
00011 from plone.i18n.normalizer.interfaces import IURLNormalizer
00012 
00013 import zope.component
00014 from zope.component import queryUtility
00015 from zope.component.testing import setUp, tearDown
00016 from zope.configuration.xmlconfig import XMLConfig
00017 from zope.testing import doctest
00018 from zope.testing.doctestunit import DocTestSuite
00019 
00020 
00021 def configurationSetUp(self):
00022     setUp()
00023     XMLConfig('meta.zcml', zope.component)()
00024     XMLConfig('configure.zcml', plone.i18n.normalizer)()
00025 
00026 
00027 def testIDNormalizer():
00028     """
00029       >>> util = queryUtility(IIDNormalizer)
00030       >>> util
00031       <plone.i18n.normalizer.IDNormalizer object at ...>
00032 
00033       >>> util.normalize(u'simpleandsafe')
00034       'simpleandsafe'
00035 
00036       >>> util.normalize(u' Whitespace and capital Letters  ')
00037       'whitespace-and-capital-letters'
00038 
00039       >>> util.normalize(u">here's another!")
00040       'heres-another'
00041 
00042       >>> util.normalize(u">>>here'!--s yet another!!!")
00043       'here-s-yet-another'
00044 
00045       >>> util.normalize(unicode("umläut.doc", 'utf-8'))
00046       'umlaeut.doc'
00047 
00048       >>> from plone.i18n.normalizer import MAX_LENGTH
00049       
00050       >>> testString = u"thissentenceiswaytolongtobecroppedwithoutcuttedbythenormalizemethodbecauseithasnoplacetocrop"
00051       >>> util.normalize(testString)
00052       'thissentenceiswaytolongtobecroppedwithoutcuttedbyt'
00053       >>> len(util.normalize(testString)) == MAX_LENGTH
00054       True
00055 
00056       >>> testString = u"thissentenceisacropped-by-the-normalize-method-because-it-has-many-places-to-crop"
00057       >>> util.normalize(testString)
00058       'thissentenceisacropped-by-the-normalize-method'
00059       >>> len(util.normalize(testString)) <= MAX_LENGTH
00060       True
00061 
00062       >>> testString = u"this-sentence-is-way-to-long-but-can-be-cropped-by-the-normalize-method-because-it-has-many-places-to-crop"
00063       >>> util.normalize(testString)
00064       'this-sentence-is-way-to-long-but-can-be-cropped-by'
00065       >>> len(util.normalize(testString)) <= MAX_LENGTH
00066       True
00067 
00068       >>> util.normalize(unicode("rest `n` peace", 'utf-8'))
00069       'rest-n-peace'
00070 
00071       >>> util.normalize(u"short-hello-version", max_length=10)
00072       'short'
00073 
00074       >>> util.normalize(u"short-hello-version", max_length=15)
00075       'short-hello'
00076     """
00077 
00078 
00079 def testLocaleAwareIDNormalizer():
00080     """
00081       >>> util = queryUtility(IIDNormalizer)
00082       >>> util
00083       <plone.i18n.normalizer.IDNormalizer object at ...>
00084 
00085     Register the German file name normalizer as an id normalizer as well, to
00086     test the locale-aware id normalization logic:
00087 
00088       >>> de_util = queryUtility(IFileNameNormalizer, name='de')
00089       >>> sm = zope.component.getGlobalSiteManager()
00090       >>> sm.registerUtility(de_util, IIDNormalizer, name='de')
00091 
00092       >>> util.normalize(u'simpleandsafe', locale='de')
00093       'simpleandsafe'
00094 
00095       >>> util.normalize(unicode('text with umläut', 'utf-8'), locale='de')
00096       'text-with-umlaeut'
00097 
00098     Make sure we get the de normalizer as there's no special one for de_DE
00099     registered.
00100        
00101       >>> util.normalize(unicode('text with umläut', 'utf-8'), locale='de_DE')
00102       'text-with-umlaeut'
00103 
00104       >>> util.normalize(u'simpleandsafe', locale='pt_BR')
00105       'simpleandsafe'
00106 
00107       >>> util.normalize(u'simpleandsafe', locale='sr@Latn')
00108       'simpleandsafe'
00109 
00110       >>> util.normalize(u"short-hello-version", locale='de_DE', max_length=10)
00111       'short'
00112 
00113       >>> util.normalize(u"short-hello-version", locale='de_DE', max_length=15)
00114       'short-hello'
00115     """
00116 
00117 
00118 def testFileNameNormalizer():
00119     """
00120       >>> util = queryUtility(IFileNameNormalizer)
00121       >>> util
00122       <plone.i18n.normalizer.FileNameNormalizer object at ...>
00123       
00124       >>> util.normalize(u'simpleandsafe')
00125       'simpleandsafe'
00126 
00127       >>> util.normalize(u' Whitespace and capital Letters  ')
00128       'Whitespace and capital Letters'
00129 
00130       >>> util.normalize(u">here's another!")
00131       'heres another'
00132 
00133       >>> util.normalize(u">>>here'!--s yet another!!!")
00134       'here-s yet another'
00135 
00136       >>> util.normalize(u"{[(me too)]}")
00137       'me too'
00138 
00139       >>> util.normalize("pseudo_filename,pot,#1.doc")
00140       'pseudo_filename-pot-#1.doc'
00141 
00142       >>> util.normalize(unicode("umläut.doc", 'utf-8'))
00143       'umlaeut.doc'
00144 
00145       >>> len(util.normalize(u'aa' * 2000))
00146       1023
00147 
00148       >>> util.normalize(unicode("rest `n` peace", 'utf-8'))
00149       'rest -n- peace'
00150 
00151       >>> util.normalize(u"short-hello-version", max_length=10)
00152       'short'
00153     """
00154 
00155 
00156 def testLocaleAwareFileNameNormalizer():
00157     """
00158       >>> util = queryUtility(IFileNameNormalizer)
00159       >>> util
00160       <plone.i18n.normalizer.FileNameNormalizer object at ...>
00161 
00162       >>> util.normalize(u'simpleandsafe', locale='de')
00163       'simpleandsafe'
00164 
00165       >>> util.normalize(unicode('text with umläut', 'utf-8'), locale='de')
00166       'text with umlaeut'
00167 
00168     Make sure we get the de normalizer as there's no special one for de_DE
00169     registered.
00170        
00171       >>> util.normalize(unicode('text with umläut', 'utf-8'), locale='de_DE')
00172       'text with umlaeut'
00173 
00174       >>> util.normalize(u'simpleandsafe', locale='pt_BR')
00175       'simpleandsafe'
00176 
00177       >>> util.normalize(u'simpleandsafe', locale='sr@Latn')
00178       'simpleandsafe'
00179 
00180       >>> util.normalize(u"short-hello-version", locale='de_DE', max_length=10)
00181       'short'
00182     """
00183 
00184 
00185 def testURLNormalizer():
00186     """
00187       >>> util = queryUtility(IURLNormalizer)
00188       >>> util
00189       <plone.i18n.normalizer.URLNormalizer object at ...>
00190       
00191       >>> util.normalize(u'simpleandsafe')
00192       'simpleandsafe'
00193 
00194       >>> util.normalize(u' Whitespace and capital Letters  ')
00195       'whitespace-and-capital-letters'
00196 
00197       >>> util.normalize(u">here's another!")
00198       'heres-another'
00199 
00200       >>> util.normalize(u">>>here'!--s yet another!!!")
00201       'here-s-yet-another'
00202 
00203       >>> util.normalize(u"Doe, Joe")
00204       'doe-joe'
00205 
00206       >>> util.normalize(unicode("umläut.doc", 'utf-8'))
00207       'umlaeut.doc'
00208 
00209       >>> util.normalize('quote "this"!')
00210       'quote-this'
00211 
00212       >>> util.normalize("quote 'this'!")
00213       'quote-this'
00214 
00215       >>> util.normalize("I'm not a FILE.txt")
00216       'im-not-a-file.txt'
00217 
00218       >>> util.normalize("I'm a big file.TXT")
00219       'im-a-big-file.txt'
00220 
00221       >>> util.normalize(unicode("rest `n` peace", 'utf-8'))
00222       'rest-n-peace'
00223 
00224       >>> len(util.normalize(u'aa' * 2000))
00225       255
00226 
00227       >>> util.normalize(u"short-hello-version", max_length=10)
00228       'short'
00229     """
00230 
00231 
00232 def testLocaleAwareURLNormalizer():
00233     """
00234       >>> util = queryUtility(IURLNormalizer)
00235       >>> util
00236       <plone.i18n.normalizer.URLNormalizer object at ...>
00237 
00238       >>> util.normalize(u'simpleandsafe', locale='de')
00239       'simpleandsafe'
00240 
00241       >>> util.normalize(unicode('text with umläut', 'utf-8'), locale='de')
00242       'text-with-umlaeut'
00243 
00244     Make sure we get the de normalizer as there's no special one for de_DE
00245     registered.
00246        
00247       >>> util.normalize(unicode('text with umläut', 'utf-8'), locale='de_DE')
00248       'text-with-umlaeut'
00249 
00250       >>> util.normalize(u'simpleandsafe', locale='pt_BR')
00251       'simpleandsafe'
00252 
00253       >>> util.normalize(u'simpleandsafe', locale='sr@Latn')
00254       'simpleandsafe'
00255 
00256       >>> util.normalize(u"short-hello-version", locale='de_DE', max_length=10)
00257       'short'
00258     """
00259 
00260 def test_suite():
00261     return unittest.TestSuite((
00262         DocTestSuite('plone.i18n.normalizer'),
00263         DocTestSuite('plone.i18n.normalizer.base'),
00264         DocTestSuite('plone.i18n.normalizer.de'),
00265         DocTestSuite('plone.i18n.normalizer.el'),
00266         DocTestSuite('plone.i18n.normalizer.fr'),
00267         DocTestSuite('plone.i18n.normalizer.pl'),
00268         DocTestSuite('plone.i18n.normalizer.ru'),
00269         DocTestSuite('plone.i18n.normalizer.tr'),
00270         DocTestSuite(setUp=configurationSetUp,
00271                      tearDown=tearDown,
00272                      optionflags=doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE),
00273         ))
00274 
00275 if __name__ == '__main__':
00276     unittest.main(defaultTest="test_suite")