test/unittest_uilib.py
brancholdstable
changeset 7074 e4580e5f0703
parent 6685 eeedb3575d25
child 7568 c5ee33fb6a3b
equal deleted inserted replaced
6749:48f468f33704 7074:e4580e5f0703
    14 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    14 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    15 # details.
    15 # details.
    16 #
    16 #
    17 # You should have received a copy of the GNU Lesser General Public License along
    17 # You should have received a copy of the GNU Lesser General Public License along
    18 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    19 """unittests for cubicweb.uilib
    19 """unittests for cubicweb.uilib"""
    20 
       
    21 """
       
    22 
    20 
    23 __docformat__ = "restructuredtext en"
    21 __docformat__ = "restructuredtext en"
    24 
    22 
       
    23 
       
    24 import pkg_resources
    25 from logilab.common.testlib import TestCase, unittest_main
    25 from logilab.common.testlib import TestCase, unittest_main
    26 from logilab.common.tree import Node
    26 from unittest2 import skipIf
    27 
    27 
    28 from cubicweb import uilib
    28 from cubicweb import uilib
       
    29 
       
    30 lxml_version = pkg_resources.get_distribution('lxml').version.split('.')
       
    31 
    29 
    32 
    30 class UILIBTC(TestCase):
    33 class UILIBTC(TestCase):
    31 
    34 
    32     def test_remove_tags(self):
    35     def test_remove_tags(self):
    33         """make sure remove_tags remove all tags"""
    36         """make sure remove_tags remove all tags"""
    92                 ]
    95                 ]
    93         for text, expected in data:
    96         for text, expected in data:
    94             got = uilib.text_cut(text, 30)
    97             got = uilib.text_cut(text, 30)
    95             self.assertEqual(got, expected)
    98             self.assertEqual(got, expected)
    96 
    99 
       
   100     def test_soup2xhtml_0(self):
       
   101         self.assertEqual(uilib.soup2xhtml('hop\r\nhop', 'ascii'),
       
   102                           'hop\nhop')
       
   103 
    97     def test_soup2xhtml_1_1(self):
   104     def test_soup2xhtml_1_1(self):
       
   105         self.assertEqual(uilib.soup2xhtml('hop', 'ascii'),
       
   106                           'hop')
       
   107         self.assertEqual(uilib.soup2xhtml('hop<div>', 'ascii'),
       
   108                           'hop<div/>')
    98         self.assertEqual(uilib.soup2xhtml('hop <div>', 'ascii'),
   109         self.assertEqual(uilib.soup2xhtml('hop <div>', 'ascii'),
    99                           'hop <div/>')
   110                           'hop <div/>')
   100         self.assertEqual(uilib.soup2xhtml('<div> hop', 'ascii'),
   111         self.assertEqual(uilib.soup2xhtml('<div> hop', 'ascii'),
   101                           '<div> hop</div>')
   112                           '<div> hop</div>')
   102         self.assertEqual(uilib.soup2xhtml('hop <div> hop', 'ascii'),
   113         self.assertEqual(uilib.soup2xhtml('hop <div> hop', 'ascii'),
   116         self.assertEqual(uilib.soup2xhtml('<body> hop', 'ascii'),
   127         self.assertEqual(uilib.soup2xhtml('<body> hop', 'ascii'),
   117                           ' hop')
   128                           ' hop')
   118         self.assertEqual(uilib.soup2xhtml('hop <body> hop', 'ascii'),
   129         self.assertEqual(uilib.soup2xhtml('hop <body> hop', 'ascii'),
   119                           'hop  hop')
   130                           'hop  hop')
   120 
   131 
   121     def test_soup2xhtml_2_2(self):
   132     def test_soup2xhtml_2_2a(self):
   122         self.assertEqual(uilib.soup2xhtml('hop </body>', 'ascii'),
   133         self.assertEqual(uilib.soup2xhtml('hop </body>', 'ascii'),
   123                           'hop ')
   134                           'hop ')
   124         self.assertEqual(uilib.soup2xhtml('</body> hop', 'ascii'),
   135         self.assertEqual(uilib.soup2xhtml('</body> hop', 'ascii'),
   125                           ' hop')
   136                           ' hop')
       
   137 
       
   138     @skipIf(lxml_version < ['2', '2'], 'expected behaviour on recent version of lxml only')
       
   139     def test_soup2xhtml_2_2b(self):
   126         self.assertEqual(uilib.soup2xhtml('hop </body> hop', 'ascii'),
   140         self.assertEqual(uilib.soup2xhtml('hop </body> hop', 'ascii'),
   127                           'hop  hop')
   141                           'hop  hop')
   128 
   142 
   129     def test_soup2xhtml_3_1(self):
   143     def test_soup2xhtml_3_1(self):
   130         self.assertEqual(uilib.soup2xhtml('hop <html>', 'ascii'),
   144         self.assertEqual(uilib.soup2xhtml('hop <html>', 'ascii'),
   140         self.assertEqual(uilib.soup2xhtml('</html> hop', 'ascii'),
   154         self.assertEqual(uilib.soup2xhtml('</html> hop', 'ascii'),
   141                           ' hop')
   155                           ' hop')
   142         self.assertEqual(uilib.soup2xhtml('hop </html> hop', 'ascii'),
   156         self.assertEqual(uilib.soup2xhtml('hop </html> hop', 'ascii'),
   143                           'hop  hop')
   157                           'hop  hop')
   144 
   158 
       
   159     def test_soup2xhtml_3_3(self):
       
   160         self.assertEqual(uilib.soup2xhtml('<script>test</script> hop ', 'ascii'),
       
   161                           ' hop ')
       
   162 
   145     def test_js(self):
   163     def test_js(self):
   146         self.assertEqual(str(uilib.js.pouet(1, "2")),
   164         self.assertEqual(str(uilib.js.pouet(1, "2")),
   147                           'pouet(1,"2")')
   165                           'pouet(1,"2")')
   148         self.assertEqual(str(uilib.js.cw.pouet(1, "2")),
   166         self.assertEqual(str(uilib.js.cw.pouet(1, "2")),
   149                           'cw.pouet(1,"2")')
   167                           'cw.pouet(1,"2")')
   150         self.assertEqual(str(uilib.js.cw.pouet(1, "2").pouet(None)),
   168         self.assertEqual(str(uilib.js.cw.pouet(1, "2").pouet(None)),
   151                           'cw.pouet(1,"2").pouet(null)')
   169                           'cw.pouet(1,"2").pouet(null)')
   152 
   170 
       
   171     def test_embedded_css(self):
       
   172         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>"""
       
   173         expected = 'voir le ticket <p class="MsoNormal">text</p>'
       
   174         self.assertMultiLineEqual(uilib.soup2xhtml(incoming, 'ascii'), expected)
       
   175 
       
   176     def test_unknown_namespace(self):
       
   177         incoming = '''<table cellspacing="0" cellpadding="0" width="81" border="0" x:str="" style="width: 61pt; border-collapse: collapse">
       
   178 <colgroup><col width="81" style="width: 61pt; mso-width-source: userset; mso-width-alt: 2962"/></colgroup>
       
   179 <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>
       
   180 </table>'''
       
   181         expected = '''<table cellspacing="0" cellpadding="0" width="81" border="0">\
       
   182 <colgroup><col width="81"/></colgroup>\
       
   183 <tbody><tr height="17"><td width="81" height="17">XXXXXXX</td></tr></tbody>\
       
   184 </table>'''
       
   185         self.assertMultiLineEqual(uilib.soup2xhtml(incoming, 'ascii'), expected)
       
   186 
       
   187 
   153 if __name__ == '__main__':
   188 if __name__ == '__main__':
   154     unittest_main()
   189     unittest_main()
   155 
   190