Package cssutils :: Package tests :: Module test_cssutils |
[hide private]
[frames] | no frames]

# Source Code for Module cssutils.tests.test_cssutils

      1  # -*- coding: utf-8 -*-
      2  """Testcases for cssutils.css.CSSCharsetRule"""
      3  __version__ = '$Id: test_cssutils.py 1200 2008-03-23 00:18:51Z cthedot $'
      4
      5  import os
      6  import tempfile
      7  import urllib
      8  import xml.dom
      9  import basetest
     10  import cssutils
     11  import codecs
     12
     13 -class CSSutilsTestCase(basetest.BaseTestCase):
     14
     15      exp = u'''@import "import/import2.css";
     16  a {
     17      background-image: url(test/x.gif)
     18      }
     19  /* import.css*/'''
     20
     21 -    def test_parseString(self):
     22          "cssutils.parseString()"
     23          s = cssutils.parseString(self.exp,
     24                                   media='handheld, screen',
     25                                   title='from string')
     26          self.assert_(isinstance(s, cssutils.css.CSSStyleSheet))
     27          self.assertEqual(None, s.href)
     28          self.assertEqual(self.exp, s.cssText)
     29          self.assertEqual(u'utf-8', s.encoding)
     30          self.assertEqual(u'handheld, screen', s.media.mediaText)
     31          self.assertEqual(u'from string', s.title)
     32          self.assertEqual(self.exp, s.cssText)
     33
     34          ir = s.cssRules[0]
     35          self.assertEqual('import/import2.css', ir.href)
     36          irs = ir.styleSheet
     37          self.assertEqual(None, irs)
     38
     39          href = os.path.join(os.path.dirname(__file__),
     40                              '..', '..', '..', 'sheets', 'import.css')
     41          href = 'file:' + urllib.pathname2url(href)
     42          s = cssutils.parseString(self.exp,
     43                                   href=href)
     44          self.assertEqual(href, s.href)
     45
     46          ir = s.cssRules[0]
     47          self.assertEqual('import/import2.css', ir.href)
     48          irs = ir.styleSheet
     49          self.assert_(isinstance(irs, cssutils.css.CSSStyleSheet))
     50          self.assertEqual(u'''@import "../t2.css";
     51  /* sheets/import2.css */''', irs.cssText)
     52
     53 -    def test_parse(self):
     54          "cssutils.parse()"
     55          # name if used with open, href used for @import resolving
     56          name = os.path.join(os.path.dirname(__file__),
     57                              '..', '..', '..', 'sheets', 'import.css')
     58          href = 'file:' + urllib.pathname2url(name)
     59
     60          s = cssutils.parse(name, href=href, media='screen', title='from file')
     61          self.assert_(isinstance(s, cssutils.css.CSSStyleSheet))
     62          self.assert_(s.href.startswith('file:///'))
     63          self.assert_(s.href.endswith('/sheets/import.css'))
     64          self.assertEqual(u'utf-8', s.encoding)
     65          self.assertEqual(u'screen', s.media.mediaText)
     66          self.assertEqual(u'from file', s.title)
     67          self.assertEqual(self.exp, s.cssText)
     68
     69          ir = s.cssRules[0]
     70          self.assertEqual('import/import2.css', ir.href)
     71          irs = ir.styleSheet
     72          self.assert_(isinstance(irs, cssutils.css.CSSStyleSheet))
     73          self.assertEqual(u'''@import "../t2.css";
     74  /* sheets/import2.css */''', irs.cssText)
     75
     76          # name is used for open and setting of href automatically
     77          # test needs to be relative to this test file!
     78          os.chdir(os.path.dirname(__file__))
     79          name = os.path.join('..', '..', '..', 'sheets', 'import.css')
     80
     81          s = cssutils.parse(name, media='screen', title='from file')
     82          self.assert_(isinstance(s, cssutils.css.CSSStyleSheet))
     83          self.assert_(s.href.startswith('file:///'))
     84          self.assert_(s.href.endswith('/sheets/import.css'))
     85          self.assertEqual(u'utf-8', s.encoding)
     86          self.assertEqual(u'screen', s.media.mediaText)
     87          self.assertEqual(u'from file', s.title)
     88          self.assertEqual(self.exp, s.cssText)
     89
     90          ir = s.cssRules[0]
     91          self.assertEqual('import/import2.css', ir.href)
     92          irs = ir.styleSheet
     93          self.assert_(isinstance(irs, cssutils.css.CSSStyleSheet))
     94          self.assertEqual(u'''@import "../t2.css";
     95  /* sheets/import2.css */''', irs.cssText)
     96
     97          # next test
     98          css = u'a:after { content: "羊蹄€\u2020" }'
     99
    100          fd, name = tempfile.mkstemp('_cssutilstest.css')
    101          t = os.fdopen(fd, 'wb')
    102          t.write(css.encode('utf-8'))
    103          t.close()
    104
    105          self.assertRaises(
    106              UnicodeDecodeError, cssutils.parse, name, 'ascii')
    107
    108          # ???
    109          s = cssutils.parse(name, encoding='iso-8859-1')
    110          self.assertEqual(cssutils.css.CSSStyleSheet, type(s))
    111          self.assertEqual(s.cssRules[0].selectorText, 'a:after')
    112
    113          s = cssutils.parse(name, encoding='utf-8')
    114          self.assertEqual(cssutils.css.CSSStyleSheet, type(s))
    115          self.assertEqual(s.cssRules[0].selectorText, 'a:after')
    116
    117          css = u'@charset "iso-8859-1"; a:after { content: "ä" }'
    118          t = codecs.open(name, 'w', 'iso-8859-1')
    119          t.write(css)
    120          t.close()
    121
    122          self.assertRaises(
    123              UnicodeDecodeError, cssutils.parse, name, 'ascii')
    124
    125          s = cssutils.parse(name, encoding='iso-8859-1')
    126          self.assertEqual(cssutils.css.CSSStyleSheet, type(s))
    127          self.assertEqual(s.cssRules[1].selectorText, 'a:after')
    128
    129          self.assertRaises(
    130              UnicodeDecodeError, cssutils.parse, name, 'utf-8')
    131
    132          # clean up
    133          os.remove(name)
    134
    135 -    def test_parseUrl(self):
    136          "cssutils.parseUrl()"
    137          href = os.path.join(os.path.dirname(__file__),
    138                              '..', '..', '..', 'sheets', 'import.css')
    139          href = u'file:' + urllib.pathname2url(href)
    140          #href = 'http://seewhatever.de/sheets/import.css'
    141          s = cssutils.parseUrl(href,
    142                                media='tv, print',
    143                                title='from url')
    144          self.assert_(isinstance(s, cssutils.css.CSSStyleSheet))
    145          self.assertEqual(href, s.href)
    146          self.assertEqual(self.exp, s.cssText)
    147          self.assertEqual(u'utf-8', s.encoding)
    148          self.assertEqual(u'tv, print', s.media.mediaText)
    149          self.assertEqual('from url', s.title)
    150
    151          sr = s.cssRules[1]
    152          img = sr.style.getProperty('background-image').cssValue.getStringValue()
    153          self.assertEqual(img, 'test/x.gif')
    154
    155          ir = s.cssRules[0]
    156          self.assertEqual(u'import/import2.css', ir.href)
    157          irs = ir.styleSheet
    158          self.assertEqual(u'''@import "../t2.css";
    159  /* sheets/import2.css */''', irs.cssText)
    160
    161          ir2 = irs.cssRules[0]
    162          self.assertEqual(u'../t2.css', ir2.href)
    163          irs2 = ir2.styleSheet
    164          self.assertEqual(u'/* t2 */', irs2.cssText)
    165
    166 -    def test_setCSSSerializer(self):
    167          "cssutils.setSerializer() and cssutils.ser"
    168          s = cssutils.parseString('a { left: 0 }')
    169          exp4 = '''a {
    170      left: 0
    171      }'''
    172          exp1 = '''a {
    173   left: 0
    174   }'''
    175          self.assertEqual(exp4, s.cssText)
    176          newser = cssutils.CSSSerializer(cssutils.serialize.Preferences(indent=' '))
    177          cssutils.setSerializer(newser)
    178          self.assertEqual(exp1, s.cssText)
    179          newser = cssutils.CSSSerializer(cssutils.serialize.Preferences(indent='    '))
    180          cssutils.ser = newser
    181          self.assertEqual(exp4, s.cssText)
    182
    183 -    def test_getUrls(self):
    184          "cssutils.getUrls()"
    185          cssutils.ser.prefs.keepAllProperties = True
    186
    187          css='''
    188          @import "im1";
    189          @import url(im2);
    190          @import url( im3 );
    191          @import url( "im4" );
    192          @import url( 'im5' );
    193          a {
    194              background-image: url(c) !important;
    195              background-\image: url(b);
    196              background: url(a) no-repeat !important;
    197              }'''
    198          s = cssutils.parseString(css)
    199          urls = set(cssutils.getUrls(s))
    200          self.assertEqual(urls, set(["im1", "im2", "im3", "im4", "im5",
    201                                      "c", "b", "a"]))
    202
    203          cssutils.ser.prefs.keepAllProperties = False
    204
    205 -    def test_replaceUrls(self):
    206          "cssutils.replaceUrls()"
    207          cssutils.ser.prefs.keepAllProperties = True
    208
    209          css='''
    210          @import "im1";
    211          @import url(im2);
    212          a {
    213              background-image: url(c) !important;
    214              background-\image: url(b);
    215              background: url(a) no-repeat !important;
    216              }'''
    217          s = cssutils.parseString(css)
    218          cssutils.replaceUrls(s, lambda old: "NEW" + old)
    219          self.assertEqual(u'@import "NEWim1";', s.cssRules[0].cssText)
    220          self.assertEqual(u'NEWim2', s.cssRules[1].href)
    221          self.assertEqual(u'''background-image: url(NEWc) !important;
    222  background-\\image: url(NEWb);
    223  background: url(NEWa) no-repeat !important''', s.cssRules[2].style.cssText)
    224
    225          cssutils.ser.prefs.keepAllProperties = False
    226
    227
    228  if __name__ == '__main__':
    229      import unittest
    230      unittest.main()
    231