cubicweb/test/unittest_uilib.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 30 Sep 2016 18:25:08 +0200
changeset 11767 432f87a63057
parent 11078 de4367ef4e5e
child 11909 244cd7f407b8
permissions -rw-r--r--
flake8 and all * update some copyright * drop most __docformat__ declaration * fix some flake8 warnings / errors
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 -*-
8909
f46b017db2d9 [test] fix skipIf import, currently failing if unittest2 is not there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7568
diff changeset
     2
# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     3
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     4
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     5
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     6
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     7
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     8
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     9
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    10
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    11
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    12
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    13
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    14
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    15
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    16
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    17
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    18
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
6153
ade53a90ddf1 remove useless import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
    19
"""unittests for cubicweb.uilib"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
11767
432f87a63057 flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11078
diff changeset
    21
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
6685
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
    23
11078
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
    24
import doctest
6685
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
    25
import pkg_resources
7568
c5ee33fb6a3b [test] run and fix utils and uilib doctests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6685
diff changeset
    26
8909
f46b017db2d9 [test] fix skipIf import, currently failing if unittest2 is not there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7568
diff changeset
    27
try:
f46b017db2d9 [test] fix skipIf import, currently failing if unittest2 is not there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7568
diff changeset
    28
    from unittest import skipIf
f46b017db2d9 [test] fix skipIf import, currently failing if unittest2 is not there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7568
diff changeset
    29
except ImportError:
f46b017db2d9 [test] fix skipIf import, currently failing if unittest2 is not there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7568
diff changeset
    30
    from unittest2 import skipIf
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
11078
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
    32
from logilab.common.testlib import TestCase, unittest_main
7568
c5ee33fb6a3b [test] run and fix utils and uilib doctests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6685
diff changeset
    33
10332
da1cb2b12fe1 [uilib] `uilib.js` helper now honors explicit JSString (closes #4959538)
Alain Leufroy <alain@leufroy.fr>
parents: 10116
diff changeset
    34
from cubicweb import uilib, utils as cwutils
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
6685
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
    36
lxml_version = pkg_resources.get_distribution('lxml').version.split('.')
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
    37
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
class UILIBTC(TestCase):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
    def test_remove_tags(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
        """make sure remove_tags remove all tags"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
        data = [
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
            ('<h1>Hello</h1>', 'Hello'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
            ('<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
    45
            ('<br>Hello<img src="doh.png"/>', 'Hello'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
            ('<p></p>', ''),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
            ]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
        for text, expected in data:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
            got = uilib.remove_html_tags(text)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
    50
            self.assertEqual(got, expected)
1646
88182252fd11 fix tests, cleanup
sylvain.thenault@logilab.fr
parents: 362
diff changeset
    51
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    52
    def test_fallback_safe_cut(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
    53
        self.assertEqual(uilib.fallback_safe_cut(u'ab <a href="hello">cd</a>', 4), u'ab c...')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
    54
        self.assertEqual(uilib.fallback_safe_cut(u'ab <a href="hello">cd</a>', 5), u'ab <a href="hello">cd</a>')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
    55
        self.assertEqual(uilib.fallback_safe_cut(u'ab <a href="hello">&amp;d</a>', 4), u'ab &amp;...')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
    56
        self.assertEqual(uilib.fallback_safe_cut(u'ab <a href="hello">&amp;d</a> ef', 5), u'ab &amp;d...')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
    57
        self.assertEqual(uilib.fallback_safe_cut(u'ab <a href="hello">&igrave;d</a>', 4), u'ab ì...')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
    58
        self.assertEqual(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
    59
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    60
    def test_lxml_safe_cut(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
    61
        self.assertEqual(uilib.safe_cut(u'aaa<div>aaad</div> ef', 4), u'<p>aaa</p><div>a...</div>')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
    62
        self.assertEqual(uilib.safe_cut(u'aaa<div>aaad</div> ef', 7), u'<p>aaa</p><div>aaad</div>...')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
    63
        self.assertEqual(uilib.safe_cut(u'aaa<div>aaad</div>', 7), u'<p>aaa</p><div>aaad</div>')
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    64
        # Missing ellipsis due to space management but we don't care
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
    65
        self.assertEqual(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
    66
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
    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
    68
        """tests uilib.cut() behaviour"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
        data = [
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
            ('hello', 'hello'),
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    71
            ('hello world', 'hello wo...'),
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    72
            ("hell<b>O'</b> world", "hell<b>O..."),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
            ]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
        for text, expected in data:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
            got = uilib.cut(text, 8)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
    76
            self.assertEqual(got, expected)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    78
    def test_text_cut(self):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
        """tests uilib.text_cut() behaviour with no text"""
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    80
        data = [('',''),
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    81
                ("""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
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
    83
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
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
    85
cillum dolore eu fugiat nulla pariatur.""",
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    86
                 "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
    87
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
    88
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
    89
consequat."),
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    90
                ("""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
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
    92
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
consequat Duis aute irure dolor in reprehenderit in voluptate velit esse
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
cillum dolore eu fugiat nulla pariatur Excepteur sint occaecat cupidatat non
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
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
    96
""",
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
    97
                 "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
    98
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
    99
quis nostrud exercitation ullamco laboris nisi"),
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 343
diff changeset
   100
                ]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        for text, expected in data:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
            got = uilib.text_cut(text, 30)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   103
            self.assertEqual(got, expected)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
6685
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   105
    def test_soup2xhtml_0(self):
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   106
        self.assertEqual(uilib.soup2xhtml('hop\r\nhop', 'ascii'),
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   107
                          'hop\nhop')
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   108
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   109
    def test_soup2xhtml_1_1(self):
6685
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   110
        self.assertEqual(uilib.soup2xhtml('hop', 'ascii'),
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   111
                          'hop')
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   112
        self.assertEqual(uilib.soup2xhtml('hop<div>', 'ascii'),
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   113
                          'hop<div/>')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   114
        self.assertEqual(uilib.soup2xhtml('hop <div>', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   115
                          'hop <div/>')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   116
        self.assertEqual(uilib.soup2xhtml('<div> hop', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   117
                          '<div> hop</div>')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   118
        self.assertEqual(uilib.soup2xhtml('hop <div> hop', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   119
                          'hop <div> hop</div>')
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   120
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   121
    def test_soup2xhtml_1_2(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   122
        self.assertEqual(uilib.soup2xhtml('hop </div>', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   123
                          'hop ')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   124
        self.assertEqual(uilib.soup2xhtml('</div> hop', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   125
                          '<div/> hop')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   126
        self.assertEqual(uilib.soup2xhtml('hop </div> hop', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   127
                          '<div>hop </div> hop')
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   128
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   129
    def test_soup2xhtml_2_1(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   130
        self.assertEqual(uilib.soup2xhtml('hop <body>', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   131
                          'hop ')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   132
        self.assertEqual(uilib.soup2xhtml('<body> hop', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   133
                          ' hop')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   134
        self.assertEqual(uilib.soup2xhtml('hop <body> hop', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   135
                          'hop  hop')
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   136
6685
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   137
    def test_soup2xhtml_2_2a(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   138
        self.assertEqual(uilib.soup2xhtml('hop </body>', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   139
                          'hop ')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   140
        self.assertEqual(uilib.soup2xhtml('</body> hop', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   141
                          ' hop')
6685
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   142
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   143
    @skipIf(lxml_version < ['2', '2'], 'expected behaviour on recent version of lxml only')
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   144
    def test_soup2xhtml_2_2b(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   145
        self.assertEqual(uilib.soup2xhtml('hop </body> hop', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   146
                          'hop  hop')
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   147
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   148
    def test_soup2xhtml_3_1(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   149
        self.assertEqual(uilib.soup2xhtml('hop <html>', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   150
                          'hop ')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   151
        self.assertEqual(uilib.soup2xhtml('<html> hop', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   152
                          ' hop')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   153
        self.assertEqual(uilib.soup2xhtml('hop <html> hop', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   154
                          'hop  hop')
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   155
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   156
    def test_soup2xhtml_3_2(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   157
        self.assertEqual(uilib.soup2xhtml('hop </html>', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   158
                          'hop ')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   159
        self.assertEqual(uilib.soup2xhtml('</html> hop', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   160
                          ' hop')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   161
        self.assertEqual(uilib.soup2xhtml('hop </html> hop', 'ascii'),
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   162
                          'hop  hop')
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   163
6685
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   164
    def test_soup2xhtml_3_3(self):
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   165
        self.assertEqual(uilib.soup2xhtml('<script>test</script> hop ', 'ascii'),
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   166
                          ' hop ')
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   167
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   168
    def test_js(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   169
        self.assertEqual(str(uilib.js.pouet(1, "2")),
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   170
                          'pouet(1,"2")')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   171
        self.assertEqual(str(uilib.js.cw.pouet(1, "2")),
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   172
                          'cw.pouet(1,"2")')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   173
        self.assertEqual(str(uilib.js.cw.pouet(1, "2").pouet(None)),
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   174
                          'cw.pouet(1,"2").pouet(null)')
10332
da1cb2b12fe1 [uilib] `uilib.js` helper now honors explicit JSString (closes #4959538)
Alain Leufroy <alain@leufroy.fr>
parents: 10116
diff changeset
   175
        self.assertEqual(str(uilib.js.cw.pouet(1, cwutils.JSString("$")).pouet(None)),
da1cb2b12fe1 [uilib] `uilib.js` helper now honors explicit JSString (closes #4959538)
Alain Leufroy <alain@leufroy.fr>
parents: 10116
diff changeset
   176
                         'cw.pouet(1,$).pouet(null)')
da1cb2b12fe1 [uilib] `uilib.js` helper now honors explicit JSString (closes #4959538)
Alain Leufroy <alain@leufroy.fr>
parents: 10116
diff changeset
   177
        self.assertEqual(str(uilib.js.cw.pouet(1, {'callback': cwutils.JSString("cw.cb")}).pouet(None)),
da1cb2b12fe1 [uilib] `uilib.js` helper now honors explicit JSString (closes #4959538)
Alain Leufroy <alain@leufroy.fr>
parents: 10116
diff changeset
   178
                         'cw.pouet(1,{callback: cw.cb}).pouet(null)')
da1cb2b12fe1 [uilib] `uilib.js` helper now honors explicit JSString (closes #4959538)
Alain Leufroy <alain@leufroy.fr>
parents: 10116
diff changeset
   179
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   180
6685
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   181
    def test_embedded_css(self):
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   182
        incoming = u"""voir le ticket <style type="text/css">@font-face { font-family: "Cambria"; }p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 10pt; font-size: 12pt; font-family: "Times New Roman"; }a:link, span.MsoHyperlink { color: blue; text-decoration: underline; }a:visited, span.MsoHyperlinkFollowed { color: purple; text-decoration: underline; }div.Section1 { page: Section1; }</style></p><p class="MsoNormal">text</p>"""
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   183
        expected = 'voir le ticket <p class="MsoNormal">text</p>'
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   184
        self.assertMultiLineEqual(uilib.soup2xhtml(incoming, 'ascii'), expected)
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   185
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   186
    def test_unknown_namespace(self):
10116
4e9738f5c798 [test] fixes to make tests pass with recent lxml
David Douard <david.douard@logilab.fr>
parents: 8909
diff changeset
   187
        incoming = '''<table cellspacing="0" cellpadding="0" width="81" border="0" x:str="" style="width: 61pt; border-collapse: collapse">\
4e9738f5c798 [test] fixes to make tests pass with recent lxml
David Douard <david.douard@logilab.fr>
parents: 8909
diff changeset
   188
<colgroup><col width="81" style="width: 61pt; mso-width-source: userset; mso-width-alt: 2962"/></colgroup>\
4e9738f5c798 [test] fixes to make tests pass with recent lxml
David Douard <david.douard@logilab.fr>
parents: 8909
diff changeset
   189
<tbody><tr height="17" style="height: 12.75pt"><td width="81" height="17" style="border-right: #e0dfe3; border-top: #e0dfe3; border-left: #e0dfe3; width: 61pt; border-bottom: #e0dfe3; height: 12.75pt; background-color: transparent"><font size="2">XXXXXXX</font></td></tr></tbody>\
6685
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   190
</table>'''
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   191
        expected = '''<table cellspacing="0" cellpadding="0" width="81" border="0">\
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   192
<colgroup><col width="81"/></colgroup>\
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   193
<tbody><tr height="17"><td width="81" height="17">XXXXXXX</td></tr></tbody>\
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   194
</table>'''
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   195
        self.assertMultiLineEqual(uilib.soup2xhtml(incoming, 'ascii'), expected)
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   196
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6366
diff changeset
   197
11078
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   198
def load_tests(loader, tests, ignore):
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   199
    import cubicweb.utils
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   200
    tests.addTests(doctest.DocTestSuite(uilib))
de4367ef4e5e [test] Use stdlib doctest module instead of logilab-common class
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   201
    return tests
7568
c5ee33fb6a3b [test] run and fix utils and uilib doctests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6685
diff changeset
   202
c5ee33fb6a3b [test] run and fix utils and uilib doctests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6685
diff changeset
   203
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
    unittest_main()