test/unittest_utils.py
changeset 7277 acd7f0e9f276
parent 6415 b0b0f097a72d
child 7569 02c338197322
equal deleted inserted replaced
7276:f9a68136eb87 7277:acd7f0e9f276
    20 import re
    20 import re
    21 import decimal
    21 import decimal
    22 import datetime
    22 import datetime
    23 
    23 
    24 from logilab.common.testlib import TestCase, unittest_main
    24 from logilab.common.testlib import TestCase, unittest_main
    25 
    25 from cubicweb.devtools.testlib import CubicWebTC
    26 from cubicweb.utils import make_uid, UStringIO, SizeConstrainedList, RepeatList
    26 from cubicweb.utils import make_uid, UStringIO, SizeConstrainedList, RepeatList, HTMLHead
    27 from cubicweb.entity import Entity
    27 from cubicweb.entity import Entity
    28 
    28 
    29 try:
    29 try:
    30     from cubicweb.utils import CubicWebJsonEncoder, json
    30     from cubicweb.utils import CubicWebJsonEncoder, json
    31 except ImportError:
    31 except ImportError:
   153                           [{'pouet': 'hop', 'eid': 2}])
   153                           [{'pouet': 'hop', 'eid': 2}])
   154 
   154 
   155     def test_encoding_unknown_stuff(self):
   155     def test_encoding_unknown_stuff(self):
   156         self.assertEqual(self.encode(TestCase), 'null')
   156         self.assertEqual(self.encode(TestCase), 'null')
   157 
   157 
       
   158 class HTMLHeadTC(CubicWebTC):
       
   159     def test_concat_urls(self):
       
   160         base_url = u'http://test.fr/data/'
       
   161         head = HTMLHead(base_url)
       
   162         urls = [base_url + u'bob1.js',
       
   163                 base_url + u'bob2.js',
       
   164                 base_url + u'bob3.js']
       
   165         result = head.concat_urls(urls)
       
   166         expected = u'http://test.fr/data/??bob1.js,bob2.js,bob3.js'
       
   167         self.assertEqual(result, expected)
       
   168 
       
   169     def test_group_urls(self):
       
   170         base_url = u'http://test.fr/data/'
       
   171         head = HTMLHead(base_url)
       
   172         urls_spec = [(base_url + u'bob0.js', None),
       
   173                      (base_url + u'bob1.js', None),
       
   174                      (u'http://ext.com/bob2.js', None),
       
   175                      (u'http://ext.com/bob3.js', None),
       
   176                      (base_url + u'bob4.css', 'all'),
       
   177                      (base_url + u'bob5.css', 'all'),
       
   178                      (base_url + u'bob6.css', 'print'),
       
   179                      (base_url + u'bob7.css', 'print'),
       
   180                      (base_url + u'bob8.css', ('all', u'[if IE 8]')),
       
   181                      (base_url + u'bob9.css', ('print', u'[if IE 8]'))
       
   182                      ]
       
   183         result = head.group_urls(urls_spec)
       
   184         expected = [(base_url + u'??bob0.js,bob1.js', None),
       
   185                     (u'http://ext.com/bob2.js', None),
       
   186                     (u'http://ext.com/bob3.js', None),
       
   187                     (base_url + u'??bob4.css,bob5.css', 'all'),
       
   188                     (base_url + u'??bob6.css,bob7.css', 'print'),
       
   189                     (base_url + u'bob8.css', ('all', u'[if IE 8]')),
       
   190                     (base_url + u'bob9.css', ('print', u'[if IE 8]'))
       
   191                     ]
       
   192         self.assertEqual(list(result), expected)
       
   193 
       
   194     def test_getvalue_with_concat(self):
       
   195         base_url = u'http://test.fr/data/'
       
   196         head = HTMLHead(base_url)
       
   197         head.add_js(base_url + u'bob0.js')
       
   198         head.add_js(base_url + u'bob1.js')
       
   199         head.add_js(u'http://ext.com/bob2.js')
       
   200         head.add_js(u'http://ext.com/bob3.js')
       
   201         head.add_css(base_url + u'bob4.css')
       
   202         head.add_css(base_url + u'bob5.css')
       
   203         head.add_css(base_url + u'bob6.css', 'print')
       
   204         head.add_css(base_url + u'bob7.css', 'print')
       
   205         head.add_ie_css(base_url + u'bob8.css')
       
   206         head.add_ie_css(base_url + u'bob9.css', 'print', u'[if lt IE 7]')
       
   207         result = head.getvalue()
       
   208         expected = u"""<head>
       
   209 <link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/??bob4.css,bob5.css"/>
       
   210 <link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/??bob6.css,bob7.css"/>
       
   211 <!--[if lt IE 8]>
       
   212 <link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob8.css"/>
       
   213 <!--[if lt IE 7]>
       
   214 <link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/bob9.css"/>
       
   215 <![endif]--> 
       
   216 <script type="text/javascript" src="http://test.fr/data/??bob0.js,bob1.js"></script>
       
   217 <script type="text/javascript" src="http://ext.com/bob2.js"></script>
       
   218 <script type="text/javascript" src="http://ext.com/bob3.js"></script>
       
   219 </head>
       
   220 """
       
   221         self.assertEqual(result, expected)
       
   222 
       
   223     def test_getvalue_without_concat(self):
       
   224         base_url = u'http://test.fr/data/'
       
   225         head = HTMLHead()
       
   226         head.add_js(base_url + u'bob0.js')
       
   227         head.add_js(base_url + u'bob1.js')
       
   228         head.add_js(u'http://ext.com/bob2.js')
       
   229         head.add_js(u'http://ext.com/bob3.js')
       
   230         head.add_css(base_url + u'bob4.css')
       
   231         head.add_css(base_url + u'bob5.css')
       
   232         head.add_css(base_url + u'bob6.css', 'print')
       
   233         head.add_css(base_url + u'bob7.css', 'print')
       
   234         head.add_ie_css(base_url + u'bob8.css')
       
   235         head.add_ie_css(base_url + u'bob9.css', 'print', u'[if lt IE 7]')
       
   236         result = head.getvalue()
       
   237         expected = u"""<head>
       
   238 <link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob4.css"/>
       
   239 <link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob5.css"/>
       
   240 <link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/bob6.css"/>
       
   241 <link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/bob7.css"/>
       
   242 <!--[if lt IE 8]>
       
   243 <link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob8.css"/>
       
   244 <!--[if lt IE 7]>
       
   245 <link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/bob9.css"/>
       
   246 <![endif]--> 
       
   247 <script type="text/javascript" src="http://test.fr/data/bob0.js"></script>
       
   248 <script type="text/javascript" src="http://test.fr/data/bob1.js"></script>
       
   249 <script type="text/javascript" src="http://ext.com/bob2.js"></script>
       
   250 <script type="text/javascript" src="http://ext.com/bob3.js"></script>
       
   251 </head>
       
   252 """
       
   253         self.assertEqual(result, expected)
   158 
   254 
   159 if __name__ == '__main__':
   255 if __name__ == '__main__':
   160     unittest_main()
   256     unittest_main()