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

# Source Code for Module cssutils.tests.test_serialize

      1  """testcases for cssutils.CSSSerializer
      2  """
      3  __author__ = '$LastChangedBy: cthedot $'
      4  __date__ = '$LastChangedDate: 2007-08-26 14:32:27 +0200 (So, 26 Aug 2007) $'
      5  __version__ = '$LastChangedRevision: 276 $'
      6
      7  import basetest
      8  import cssutils
      9
     10 -class CSSSerializerTestCase(basetest.BaseTestCase):
     11      """
     12      testcases for cssutils.CSSSerializer
     13      """
     14  ##    def test_init(self):
     15  ##        "CSSSerializer.__init__()"
     16
     17 -    def _resetprefs(self):
     18          cssutils.ser.prefs = cssutils.serialize.Preferences()
     19
     20 -    def test_preferences(self):
     21          "Preferences"
     22          self._resetprefs()
     23          self.assertEqual(cssutils.ser.prefs.defaultAtKeyword, True)
     24          self.assertEqual(cssutils.ser.prefs.defaultPropertyName, True)
     25          self.assertEqual(cssutils.ser.prefs.importHrefFormat, None)
     26          self.assertEqual(cssutils.ser.prefs.indent, 4 * u' ')
     27          self.assertEqual(cssutils.ser.prefs.keepAllProperties, False)
     28          self.assertEqual(cssutils.ser.prefs.keepComments, True)
     29          self.assertEqual(cssutils.ser.prefs.lineNumbers, False)
     30          self.assertEqual(cssutils.ser.prefs.lineSeparator, u'\n')
     31          self.assertEqual(cssutils.ser.prefs.omitLastSemicolon, True)
     32          self.assertEqual(cssutils.ser.prefs.removeInvalid, True)
     33
     34 -    def test_defaultAtKeyword(self):
     35          "CSSSerializer Preferences.defaultAtKeyword"
     36          s = cssutils.parseString(u'@im\port "x";')
     37          self.assertEqual(u'@import "x";', s.cssText)
     38          cssutils.ser.prefs.defaultAtKeyword = True
     39          self.assertEqual(u'@import "x";', s.cssText)
     40          cssutils.ser.prefs.defaultAtKeyword = False
     41          self.assertEqual(u'@im\\port "x";', s.cssText)
     42
     43          self._resetprefs()
     44
     45 -    def test_defaultPropertyName(self):
     46          "CSSSerializer Preferences.defaultPropertyName"
     47          # does not actually work as once the name is set it is used also if used with
     48          # a backslash in it later...
     49
     50          s = cssutils.parseString(u'a { c\olor: green; }')
     51          self.assertEqual(u'a {\n    color: green\n    }', s.cssText)
     52          cssutils.ser.prefs.defaultPropertyName = True
     53          self.assertEqual(u'a {\n    color: green\n    }', s.cssText)
     54          cssutils.ser.prefs.defaultPropertyName = False
     55          self.assertEqual(u'a {\n    c\\olor: green\n    }', s.cssText)
     56
     57          s = cssutils.parseString(u'a { color: red; c\olor: green; }')
     58          self.assertEqual(u'a {\n    c\\olor: green\n    }', s.cssText)
     59          cssutils.ser.prefs.defaultPropertyName = False
     60          self.assertEqual(u'a {\n    c\\olor: green\n    }', s.cssText)
     61          cssutils.ser.prefs.defaultPropertyName = True
     62          self.assertEqual(u'a {\n    color: green\n    }', s.cssText)
     63
     64          self._resetprefs()
     65
     66 -    def test_importHrefFormat(self):
     67          "CSSSerializer Preferences.importHrefFormat"
     68          r0 = cssutils.css.CSSImportRule(u'not')
     69          r1 = cssutils.css.CSSImportRule(u'str', hreftype="string")
     70          r2 = cssutils.css.CSSImportRule(u'uri', hreftype="uri")
     71          self.assertEqual(u'@import url(not);', r0.cssText)
     72          self.assertEqual(u'@import "str";', r1.cssText)
     73          self.assertEqual(u'@import url(uri);', r2.cssText)
     74
     75          cssutils.ser.prefs.importHrefFormat = 'string'
     76          self.assertEqual(u'@import "not";', r0.cssText)
     77          self.assertEqual(u'@import "str";', r1.cssText)
     78          self.assertEqual(u'@import "uri";', r2.cssText)
     79
     80          cssutils.ser.prefs.importHrefFormat = 'uri'
     81          self.assertEqual(u'@import url(not);', r0.cssText)
     82          self.assertEqual(u'@import url(str);', r1.cssText)
     83          self.assertEqual(u'@import url(uri);', r2.cssText)
     84
     85          cssutils.ser.prefs.importHrefFormat = 'not defined'
     86          self.assertEqual(u'@import url(not);', r0.cssText)
     87          self.assertEqual(u'@import "str";', r1.cssText)
     88          self.assertEqual(u'@import url(uri);', r2.cssText)
     89
     90          self._resetprefs()
     91
     92 -    def test_indent(self):
     93          "CSSSerializer Preferences.ident"
     94          s = cssutils.parseString(u'a { left: 0 }')
     95          exp4 = u'''a {
     96      left: 0
     97      }'''
     98          exp1 = u'''a {
     99   left: 0
    100   }'''
    101          self.assertEqual(exp4, s.cssText)
    102          cssutils.ser.prefs.indent = ' '
    103          self.assertEqual(exp1, s.cssText)
    104          cssutils.ser.prefs.indent = '    '
    105          self.assertEqual(exp4, s.cssText)
    106
    107          self._resetprefs()
    108
    109 -    def test_lineNumbers(self):
    110          "CSSSerializer Preferences.lineNumbers"
    111          s = cssutils.parseString('a {top: 1; left: 2}')
    112          exp0 = '''a {
    113      top: 1;
    114      left: 2
    115      }'''
    116          exp1 = '''1: a {
    117  2:     top: 1;
    118  3:     left: 2
    119  4:     }'''
    120          self.assertEqual(False, cssutils.ser.prefs.lineNumbers)
    121          self.assertEqual(exp0, s.cssText)
    122          cssutils.ser.prefs.lineNumbers = True
    123          self.assertEqual(True, cssutils.ser.prefs.lineNumbers)
    124          self.assertEqual(exp1, s.cssText)
    125
    126          self._resetprefs()
    127
    128 -    def test_lineSeparator(self):
    129          "CSSSerializer Preferences.lineSeparator"
    130          s = cssutils.parseString('a { x:1;y:2}')
    131          self.assertEqual('a {\n    x: 1;\n    y: 2\n    }', s.cssText)
    132          # cannot be indented as no split possible
    133          cssutils.ser.prefs.lineSeparator = u''
    134          self.assertEqual('a {x: 1;y: 2    }', s.cssText)
    135          # no valid css but should work
    136          cssutils.ser.prefs.lineSeparator = u'XXX'
    137          self.assertEqual('a {XXX    x: 1;XXX    y: 2XXX    }', s.cssText)
    138
    139          self._resetprefs()
    140
    141 -    def test_keepComments(self):
    142          "CSSSerializer Preferences.keepComments"
    143          s = cssutils.parseString('/*1*/ a { /*2*/ }')
    144          cssutils.ser.prefs.keepComments = False
    145          self.assertEqual('a {}', s.cssText)
    146
    147          self._resetprefs()
    148
    149 -    def test_keepAllProperties(self):
    150          "CSSSerializer Preferences.keepAllProperties"
    151          css = '''a {
    152              color: pink;
    153              color: red;
    154              c\olor: blue;
    155              c\olor: green;
    156              }'''
    157          s = cssutils.parseString(css)
    158          # keep only last
    159          self.assertEqual(u'a {\n    color: green\n    }', s.cssText)
    160          # keep all
    161          cssutils.ser.prefs.keepAllProperties = True
    162          self.assertEqual(u'a {\n    color: pink;\n    color: red;\n    c\olor: blue;\n    c\olor: green\n    }', s.cssText)
    163
    164          self._resetprefs()
    165
    166 -    def test__Property(self):
    167          "CSSSerializer.do_css_Property"
    168          name="color"
    169          value="red"
    170          priority="!important"
    171
    172          s = cssutils.css.property._Property(
    173              name=name, value=value, priority=priority)
    174          self.assertEqual(u'color: red !important;',
    175                      cssutils.ser.do_css_Property(s))
    176          self.assertEqual(u'color: red !important',
    177                      cssutils.ser.do_css_Property(s, omitSemicolon=True))
    178
    179          s = cssutils.css.property._Property(
    180              name=name, value=value)
    181          self.assertEqual(u'color: red;',
    182                      cssutils.ser.do_css_Property(s))
    183          self.assertEqual(u'color: red',
    184                      cssutils.ser.do_css_Property(s, omitSemicolon=True))
    185
    186
    187  if __name__ == '__main__':
    188      import unittest
    189      unittest.main()
    190