common/test/unittest_uilib.py
branchstable
changeset 4556 43c14e0e8972
parent 4555 8968c50818db
parent 4553 23201259ffeb
child 4560 23e0632df615
equal deleted inserted replaced
4555:8968c50818db 4556:43c14e0e8972
     1 # -*- coding: utf-8 -*-
       
     2 """unittests for cubicweb.common.uilib
       
     3 
       
     4 :organization: Logilab
       
     5 :copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
       
     6 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     7 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
       
     8 """
       
     9 
       
    10 __docformat__ = "restructuredtext en"
       
    11 
       
    12 from logilab.common.testlib import TestCase, unittest_main
       
    13 from logilab.common.tree import Node
       
    14 
       
    15 from cubicweb.common import uilib
       
    16 
       
    17 class UILIBTC(TestCase):
       
    18 
       
    19     def test_remove_tags(self):
       
    20         """make sure remove_tags remove all tags"""
       
    21         data = [
       
    22             ('<h1>Hello</h1>', 'Hello'),
       
    23             ('<h1>Hello <a href="foo/bar"><b>s</b>pam</a></h1>', 'Hello spam'),
       
    24             ('<br>Hello<img src="doh.png"/>', 'Hello'),
       
    25             ('<p></p>', ''),
       
    26             ]
       
    27         for text, expected in data:
       
    28             got = uilib.remove_html_tags(text)
       
    29             self.assertEquals(got, expected)
       
    30 
       
    31     def test_fallback_safe_cut(self):
       
    32         self.assertEquals(uilib.fallback_safe_cut(u'ab <a href="hello">cd</a>', 4), u'ab c...')
       
    33         self.assertEquals(uilib.fallback_safe_cut(u'ab <a href="hello">cd</a>', 5), u'ab <a href="hello">cd</a>')
       
    34         self.assertEquals(uilib.fallback_safe_cut(u'ab <a href="hello">&amp;d</a>', 4), u'ab &amp;...')
       
    35         self.assertEquals(uilib.fallback_safe_cut(u'ab <a href="hello">&amp;d</a> ef', 5), u'ab &amp;d...')
       
    36         self.assertEquals(uilib.fallback_safe_cut(u'ab <a href="hello">&igrave;d</a>', 4), u'ab ì...')
       
    37         self.assertEquals(uilib.fallback_safe_cut(u'&amp; <a href="hello">&amp;d</a> ef', 4), u'&amp; &amp;d...')
       
    38 
       
    39     def test_lxml_safe_cut(self):
       
    40         self.assertEquals(uilib.safe_cut(u'aaa<div>aaad</div> ef', 4), u'<p>aaa</p><div>a...</div>')
       
    41         self.assertEquals(uilib.safe_cut(u'aaa<div>aaad</div> ef', 7), u'<p>aaa</p><div>aaad</div>...')
       
    42         self.assertEquals(uilib.safe_cut(u'aaa<div>aaad</div>', 7), u'<p>aaa</p><div>aaad</div>')
       
    43         # Missing ellipsis due to space management but we don't care
       
    44         self.assertEquals(uilib.safe_cut(u'ab <a href="hello">&amp;d</a>', 4), u'<p>ab <a href="hello">&amp;...</a></p>')
       
    45 
       
    46     def test_cut(self):
       
    47         """tests uilib.cut() behaviour"""
       
    48         data = [
       
    49             ('hello', 'hello'),
       
    50             ('hello world', 'hello wo...'),
       
    51             ("hell<b>O'</b> world", "hell<b>O..."),
       
    52             ]
       
    53         for text, expected in data:
       
    54             got = uilib.cut(text, 8)
       
    55             self.assertEquals(got, expected)
       
    56 
       
    57     def test_text_cut(self):
       
    58         """tests uilib.text_cut() behaviour with no text"""
       
    59         data = [('',''),
       
    60                 ("""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
       
    61 tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
       
    62 quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
       
    63 consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
       
    64 cillum dolore eu fugiat nulla pariatur.""",
       
    65                  "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod \
       
    66 tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, \
       
    67 quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo \
       
    68 consequat."),
       
    69                 ("""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
       
    70 tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam,
       
    71 quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
       
    72 consequat Duis aute irure dolor in reprehenderit in voluptate velit esse
       
    73 cillum dolore eu fugiat nulla pariatur Excepteur sint occaecat cupidatat non
       
    74 proident, sunt in culpa qui officia deserunt mollit anim id est laborum
       
    75 """,
       
    76                  "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod \
       
    77 tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam, \
       
    78 quis nostrud exercitation ullamco laboris nisi"),
       
    79                 ]
       
    80         for text, expected in data:
       
    81             got = uilib.text_cut(text, 30)
       
    82             self.assertEquals(got, expected)
       
    83 
       
    84 if __name__ == '__main__':
       
    85     unittest_main()
       
    86