test/unittest_uilib.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 08 Mar 2010 09:05:17 +0100
branchstable
changeset 4828 3e173d598cad
parent 4252 6c4f109c2b03
child 5421 8167de96c523
permissions -rw-r--r--
[cleanup] fti_* method already try/except exception, no need for additional catch here
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
4023
eae23c40627a drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3002
diff changeset
     2
"""unittests for cubicweb.uilib
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1646
diff changeset
     3
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1646
diff changeset
     4
:organization: Logilab
4212
ab6573088b4a update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3002
diff changeset
     5
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1646
diff changeset
     6
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1646
diff changeset
     7
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1646
diff changeset
     8
"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
from logilab.common.testlib import TestCase, unittest_main
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
from logilab.common.tree import Node
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    14
4023
eae23c40627a drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3002
diff changeset
    15
from cubicweb import uilib
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    16
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
class UILIBTC(TestCase):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
    def test_remove_tags(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
        """make sure remove_tags remove all tags"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
        data = [
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
            ('<h1>Hello</h1>', 'Hello'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
            ('<h1>Hello <a href="foo/bar"><b>s</b>pam</a></h1>', 'Hello spam'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
            ('<br>Hello<img src="doh.png"/>', 'Hello'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
            ('<p></p>', ''),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
            ]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
        for text, expected in data:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
            got = uilib.remove_html_tags(text)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
            self.assertEquals(got, expected)
1646
88182252fd11 fix tests, cleanup
sylvain.thenault@logilab.fr
parents: 362
diff changeset
    30
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    31
    def test_fallback_safe_cut(self):
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    32
        self.assertEquals(uilib.fallback_safe_cut(u'ab <a href="hello">cd</a>', 4), u'ab c...')
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    33
        self.assertEquals(uilib.fallback_safe_cut(u'ab <a href="hello">cd</a>', 5), u'ab <a href="hello">cd</a>')
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    34
        self.assertEquals(uilib.fallback_safe_cut(u'ab <a href="hello">&amp;d</a>', 4), u'ab &amp;...')
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    35
        self.assertEquals(uilib.fallback_safe_cut(u'ab <a href="hello">&amp;d</a> ef', 5), u'ab &amp;d...')
362
a6a319f000c3 use mtconverter's html_unescape rather than saxutils' escape to deal with any html entity
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 350
diff changeset
    36
        self.assertEquals(uilib.fallback_safe_cut(u'ab <a href="hello">&igrave;d</a>', 4), u'ab ì...')
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    37
        self.assertEquals(uilib.fallback_safe_cut(u'&amp; <a href="hello">&amp;d</a> ef', 4), u'&amp; &amp;d...')
1646
88182252fd11 fix tests, cleanup
sylvain.thenault@logilab.fr
parents: 362
diff changeset
    38
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    39
    def test_lxml_safe_cut(self):
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    40
        self.assertEquals(uilib.safe_cut(u'aaa<div>aaad</div> ef', 4), u'<p>aaa</p><div>a...</div>')
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    41
        self.assertEquals(uilib.safe_cut(u'aaa<div>aaad</div> ef', 7), u'<p>aaa</p><div>aaad</div>...')
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    42
        self.assertEquals(uilib.safe_cut(u'aaa<div>aaad</div>', 7), u'<p>aaa</p><div>aaad</div>')
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    43
        # Missing ellipsis due to space management but we don't care
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    44
        self.assertEquals(uilib.safe_cut(u'ab <a href="hello">&amp;d</a>', 4), u'<p>ab <a href="hello">&amp;...</a></p>')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
    def test_cut(self):
160
cf9ab83b1cf3 adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
    47
        """tests uilib.cut() behaviour"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
        data = [
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
            ('hello', 'hello'),
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    50
            ('hello world', 'hello wo...'),
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    51
            ("hell<b>O'</b> world", "hell<b>O..."),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
            ]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
        for text, expected in data:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
            got = uilib.cut(text, 8)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
            self.assertEquals(got, expected)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    57
    def test_text_cut(self):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
        """tests uilib.text_cut() behaviour with no text"""
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    59
        data = [('',''),
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    60
                ("""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    64
cillum dolore eu fugiat nulla pariatur.""",
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    65
                 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod \
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    66
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, \
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    67
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo \
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    68
consequat."),
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    69
                ("""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
consequat Duis aute irure dolor in reprehenderit in voluptate velit esse
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
cillum dolore eu fugiat nulla pariatur Excepteur sint occaecat cupidatat non
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
proident, sunt in culpa qui officia deserunt mollit anim id est laborum
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    75
""",
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    76
                 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod \
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    77
tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam, \
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    78
quis nostrud exercitation ullamco laboris nisi"),
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    79
                ]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
        for text, expected in data:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
            got = uilib.text_cut(text, 30)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
            self.assertEquals(got, expected)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
    unittest_main()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86