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

# Source Code for Module cssutils.tests.test_serialize

      1  # -*- coding: utf-8 -*-
      2  """testcases for cssutils.CSSSerializer
      3  """
      4  __author__ = '$LastChangedBy: cthedot $'
      5  __date__ = '$LastChangedDate: 2008-01-04 23:26:28 +0100 (Fr, 04 Jan 2008) $'
      6  __version__ = '$LastChangedRevision: 808 $'
      7
      8  import basetest
      9  import cssutils
     10
     11 -class CSSSerializerTestCase(basetest.BaseTestCase):
     12      """
     13      testcases for cssutils.CSSSerializer
     14      """
     15  #    def test_init(self):
     16  #        "CSSSerializer.__init__()"
     17
     18 -    def test_useDefaults(self):
     19          "Preferences.useDefaults()"
     20          cssutils.ser.prefs.useDefaults()
     21          self.assertEqual(cssutils.ser.prefs.defaultAtKeyword, True)
     22          self.assertEqual(cssutils.ser.prefs.defaultPropertyName, True)
     23          self.assertEqual(cssutils.ser.prefs.importHrefFormat, None)
     24          self.assertEqual(cssutils.ser.prefs.indent, 4 * u' ')
     25          self.assertEqual(cssutils.ser.prefs.keepAllProperties, True)
     26          self.assertEqual(cssutils.ser.prefs.keepComments, True)
     27          self.assertEqual(cssutils.ser.prefs.keepEmptyRules, False)
     28          self.assertEqual(cssutils.ser.prefs.lineNumbers, False)
     29          self.assertEqual(cssutils.ser.prefs.lineSeparator, u'\n')
     30          self.assertEqual(cssutils.ser.prefs.listItemSpacer, u' ')
     31          self.assertEqual(cssutils.ser.prefs.omitLastSemicolon, True)
     32          self.assertEqual(cssutils.ser.prefs.paranthesisSpacer, u' ')
     33          self.assertEqual(cssutils.ser.prefs.propertyNameSpacer, u' ')
     34          self.assertEqual(cssutils.ser.prefs.validOnly, False)
     35          self.assertEqual(cssutils.ser.prefs.wellformedOnly, True)
     36          # DEPRECATED
     37          self.assertEqual(cssutils.ser.prefs.removeInvalid, True)
     38
     39 -    def test_useMinified(self):
     40          "Preferences.useMinified()"
     41          cssutils.ser.prefs.useDefaults()
     42          cssutils.ser.prefs.useMinified()
     43          self.assertEqual(cssutils.ser.prefs.defaultAtKeyword, True)
     44          self.assertEqual(cssutils.ser.prefs.defaultPropertyName, True)
     45          self.assertEqual(cssutils.ser.prefs.importHrefFormat, 'string')
     46          self.assertEqual(cssutils.ser.prefs.indent, u'')
     47          self.assertEqual(cssutils.ser.prefs.keepAllProperties, True)
     48          self.assertEqual(cssutils.ser.prefs.keepComments, False)
     49          self.assertEqual(cssutils.ser.prefs.keepEmptyRules, False)
     50          self.assertEqual(cssutils.ser.prefs.lineNumbers, False)
     51          self.assertEqual(cssutils.ser.prefs.lineSeparator, u'')
     52          self.assertEqual(cssutils.ser.prefs.listItemSpacer, u'')
     53          self.assertEqual(cssutils.ser.prefs.omitLastSemicolon, True)
     54          self.assertEqual(cssutils.ser.prefs.paranthesisSpacer, u'')
     55          self.assertEqual(cssutils.ser.prefs.propertyNameSpacer, u'')
     56          self.assertEqual(cssutils.ser.prefs.validOnly, False)
     57          self.assertEqual(cssutils.ser.prefs.wellformedOnly, True)
     58          # DEPRECATED
     59          self.assertEqual(cssutils.ser.prefs.removeInvalid, True)
     60
     61          css = u'''
     62      /*1*/
     63      @import url(x) tv , print;
     64      @media all {}
     65      @media all {
     66          a {}
     67      }
     68      @media all {
     69          a { color: red; }
     70      }
     71      @page {}
     72      a {}
     73      a , b { top : 1px ;
     74          font-family : arial ,  'some'
     75          }
     76      '''
     77          s = cssutils.parseString(css)
     78          self.assertEqual(s.cssText,
     79              u'''@import "x" tv,print;@media all{a{color:red}}a,b{top:1px;font-family:arial,'some'}'''
     80              )
     81
     82 -    def test_defaultAtKeyword(self):
     83          "Preferences.defaultAtKeyword"
     84          cssutils.ser.prefs.useDefaults()
     85
     86          s = cssutils.parseString(u'@im\\port "x";')
     87          self.assertEqual(u'@import "x";', s.cssText)
     88          cssutils.ser.prefs.defaultAtKeyword = True
     89          self.assertEqual(u'@import "x";', s.cssText)
     90          cssutils.ser.prefs.defaultAtKeyword = False
     91          self.assertEqual(u'@im\\port "x";', s.cssText)
     92
     93 -    def test_defaultPropertyName(self):
     94          "Preferences.defaultPropertyName"
     95          cssutils.ser.prefs.useDefaults()
     96          cssutils.ser.prefs.keepAllProperties = False
     97
     98          # does not actually work as once the name is set it is used also
     99          # if used with a backslash in it later...
    100
    101          s = cssutils.parseString(u'a { c\olor: green; }')
    102          self.assertEqual(u'a {\n    color: green\n    }', s.cssText)
    103          cssutils.ser.prefs.defaultPropertyName = True
    104          self.assertEqual(u'a {\n    color: green\n    }', s.cssText)
    105          cssutils.ser.prefs.defaultPropertyName = False
    106          self.assertEqual(u'a {\n    c\\olor: green\n    }', s.cssText)
    107
    108          s = cssutils.parseString(u'a { color: red; c\olor: green; }')
    109          self.assertEqual(u'a {\n    c\\olor: green\n    }', s.cssText)
    110          cssutils.ser.prefs.defaultPropertyName = False
    111          self.assertEqual(u'a {\n    c\\olor: green\n    }', s.cssText)
    112          cssutils.ser.prefs.defaultPropertyName = True
    113          self.assertEqual(u'a {\n    color: green\n    }', s.cssText)
    114
    115 -    def test_importHrefFormat(self):
    116          "Preferences.importHrefFormat"
    117          cssutils.ser.prefs.useDefaults()
    118
    119          r0 = cssutils.css.CSSImportRule(u'not')
    120          r1 = cssutils.css.CSSImportRule(u'str', hreftype="string")
    121          r2 = cssutils.css.CSSImportRule(u'uri', hreftype="uri")
    122          self.assertEqual(u'@import url(not);', r0.cssText)
    123          self.assertEqual(u'@import "str";', r1.cssText)
    124          self.assertEqual(u'@import url(uri);', r2.cssText)
    125
    126          cssutils.ser.prefs.importHrefFormat = 'string'
    127          self.assertEqual(u'@import "not";', r0.cssText)
    128          self.assertEqual(u'@import "str";', r1.cssText)
    129          self.assertEqual(u'@import "uri";', r2.cssText)
    130
    131          cssutils.ser.prefs.importHrefFormat = 'uri'
    132          self.assertEqual(u'@import url(not);', r0.cssText)
    133          self.assertEqual(u'@import url(str);', r1.cssText)
    134          self.assertEqual(u'@import url(uri);', r2.cssText)
    135
    136          cssutils.ser.prefs.importHrefFormat = 'not defined'
    137          self.assertEqual(u'@import url(not);', r0.cssText)
    138          self.assertEqual(u'@import "str";', r1.cssText)
    139          self.assertEqual(u'@import url(uri);', r2.cssText)
    140
    141 -    def test_indent(self):
    142          "Preferences.ident"
    143          cssutils.ser.prefs.useDefaults()
    144
    145          s = cssutils.parseString(u'a { left: 0 }')
    146          exp4 = u'''a {
    147      left: 0
    148      }'''
    149          exp1 = u'''a {
    150   left: 0
    151   }'''
    152          cssutils.ser.prefs.indent = ' '
    153          self.assertEqual(exp1, s.cssText)
    154          cssutils.ser.prefs.indent = 4* ' '
    155          self.assertEqual(exp4, s.cssText)
    156
    157 -    def test_keepAllProperties(self):
    158          "Preferences.keepAllProperties"
    159          cssutils.ser.prefs.useDefaults()
    160
    161          css = '''a {
    162              color: pink;
    163              color: red;
    164              c\olor: blue;
    165              c\olor: green;
    166              }'''
    167          s = cssutils.parseString(css)
    168          # keep only last
    169          cssutils.ser.prefs.keepAllProperties = False
    170          self.assertEqual(u'a {\n    color: green\n    }', s.cssText)
    171          # keep all
    172          cssutils.ser.prefs.keepAllProperties = True
    173          self.assertEqual(u'a {\n    color: pink;\n    color: red;\n    c\olor: blue;\n    c\olor: green\n    }', s.cssText)
    174
    175 -    def test_keepComments(self):
    176          "Preferences.keepComments"
    177          cssutils.ser.prefs.useDefaults()
    178
    179          s = cssutils.parseString('/*1*/ a { /*2*/ }')
    180          cssutils.ser.prefs.keepComments = False
    181          self.assertEqual('', s.cssText)
    182          cssutils.ser.prefs.keepEmptyRules = True
    183          self.assertEqual('a {}', s.cssText)
    184
    185 -    def test_keepEmptyRules(self):
    186          "Preferences.keepEmptyRules"
    187          # CSSStyleRule
    188          css = u'''a {}
    189  a {
    190      /*1*/
    191      }
    192  a {
    193      color: red
    194      }'''
    195          s = cssutils.parseString(css)
    196          cssutils.ser.prefs.useDefaults()
    197          cssutils.ser.prefs.keepEmptyRules = True
    198          self.assertEqual(css, s.cssText)
    199          cssutils.ser.prefs.keepEmptyRules = False
    200          self.assertEqual(u'a {\n    /*1*/\n    }\na {\n    color: red\n    }',
    201                           s.cssText)
    202          cssutils.ser.prefs.keepComments = False
    203          self.assertEqual(u'a {\n    color: red\n    }',
    204                           s.cssText)
    205
    206          # CSSMediaRule
    207          css = u'''@media tv {
    208      }
    209  @media all {
    210      /*1*/
    211      }
    212  @media print {
    213      a {}
    214      }
    215  @media print {
    216      a {
    217          /*1*/
    218          }
    219      }
    220  @media all {
    221      a {
    222          color: red
    223          }
    224      }'''
    225          s = cssutils.parseString(css)
    226          cssutils.ser.prefs.useDefaults()
    227          cssutils.ser.prefs.keepEmptyRules = True
    228     #     self.assertEqual(css, s.cssText)
    229          cssutils.ser.prefs.keepEmptyRules = False
    230          self.assertEqual('''@media all {
    231      /*1*/
    232      }
    233  @media print {
    234      a {
    235          /*1*/
    236          }
    237      }
    238  @media all {
    239      a {
    240          color: red
    241          }
    242      }''', s.cssText)
    243          cssutils.ser.prefs.keepComments = False
    244          self.assertEqual('''@media all {
    245      a {
    246          color: red
    247          }
    248      }''', s.cssText)
    249
    250 -    def test_lineNumbers(self):
    251          "Preferences.lineNumbers"
    252          cssutils.ser.prefs.useDefaults()
    253
    254          s = cssutils.parseString('a {top: 1; left: 2}')
    255          exp0 = '''a {
    256      top: 1;
    257      left: 2
    258      }'''
    259          exp1 = '''1: a {
    260  2:     top: 1;
    261  3:     left: 2
    262  4:     }'''
    263          self.assertEqual(False, cssutils.ser.prefs.lineNumbers)
    264          self.assertEqual(exp0, s.cssText)
    265          cssutils.ser.prefs.lineNumbers = True
    266          self.assertEqual(True, cssutils.ser.prefs.lineNumbers)
    267          self.assertEqual(exp1, s.cssText)
    268
    269 -    def test_lineSeparator(self):
    270          "Preferences.lineSeparator"
    271          cssutils.ser.prefs.useDefaults()
    272
    273          s = cssutils.parseString('a { x:1;y:2}')
    274          self.assertEqual('a {\n    x: 1;\n    y: 2\n    }', s.cssText)
    275          # cannot be indented as no split possible
    276          cssutils.ser.prefs.lineSeparator = u''
    277          self.assertEqual('a {x: 1;y: 2    }', s.cssText)
    278          # no valid css but should work
    279          cssutils.ser.prefs.lineSeparator = u'XXX'
    280          self.assertEqual('a {XXX    x: 1;XXX    y: 2XXX    }', s.cssText)
    281
    282 -    def test_listItemSpacer(self):
    283          "Preferences.listItemSpacer"
    284          cssutils.ser.prefs.useDefaults()
    285          cssutils.ser.prefs.keepEmptyRules = True
    286
    287          css = '''
    288          @import "x" print, tv;
    289  a, b {}'''
    290          s = cssutils.parseString(css)
    291          self.assertEqual(u'@import "x" print, tv;\na, b {}', s.cssText)
    292          cssutils.ser.prefs.listItemSpacer = u''
    293          self.assertEqual(u'@import "x" print,tv;\na,b {}', s.cssText)
    294
    295 -    def test_omitLastSemicolon(self):
    296          "Preferences.omitLastSemicolon"
    297          cssutils.ser.prefs.useDefaults()
    298
    299          css = 'a { x: 1; y: 2 }'
    300          s = cssutils.parseString(css)
    301          self.assertEqual(u'a {\n    x: 1;\n    y: 2\n    }', s.cssText)
    302          cssutils.ser.prefs.omitLastSemicolon = False
    303          self.assertEqual(u'a {\n    x: 1;\n    y: 2;\n    }', s.cssText)
    304
    305 -    def test_paranthesisSpacer(self):
    306          "Preferences.paranthesisSpacer"
    307          cssutils.ser.prefs.useDefaults()
    308          css = 'a { x: 1; y: 2 }'
    309          s = cssutils.parseString(css)
    310          self.assertEqual(u'a {\n    x: 1;\n    y: 2\n    }', s.cssText)
    311          cssutils.ser.prefs.paranthesisSpacer = u''
    312          self.assertEqual(u'a{\n    x: 1;\n    y: 2\n    }', s.cssText)
    313
    314 -    def test_propertyNameSpacer(self):
    315          "Preferences.propertyNameSpacer"
    316          cssutils.ser.prefs.useDefaults()
    317          css = 'a { x: 1; y: 2 }'
    318          s = cssutils.parseString(css)
    319          self.assertEqual(u'a {\n    x: 1;\n    y: 2\n    }', s.cssText)
    320          cssutils.ser.prefs.propertyNameSpacer = u''
    321          self.assertEqual(u'a {\n    x:1;\n    y:2\n    }', s.cssText)
    322
    323 -    def test_CSSStyleSheet(self):
    324          "CSSSerializer.do_CSSStyleSheet"
    325          css = u'/* κουρος */'
    326          sheet = cssutils.parseString(css)
    327          self.assertEqual(css, unicode(sheet.cssText, 'utf-8'))
    328
    329          css = u'@charset "utf-8";\n/* κουρος */'
    330          sheet = cssutils.parseString(css)
    331          self.assertEqual(css, unicode(sheet.cssText, 'utf-8'))
    332          sheet.cssRules[0].encoding = 'ascii'
    333          self.assertEqual('@charset "ascii";\n/* \\3BA \\3BF \\3C5 \\3C1 \\3BF \\3C2  */',
    334                           sheet.cssText)
    335
    336 -    def test_Property(self):
    337          "CSSSerializer.do_Property"
    338          cssutils.ser.prefs.useDefaults()
    339
    340          name="color"
    341          value="red"
    342          priority="!important"
    343
    344          s = cssutils.css.property.Property(
    345              name=name, value=value, priority=priority)
    346          self.assertEqual(u'color: red !important',
    347                      cssutils.ser.do_Property(s))
    348
    349          s = cssutils.css.property.Property(
    350              name=name, value=value)
    351          self.assertEqual(u'color: red',
    352                      cssutils.ser.do_Property(s))
    353
    354 -    def test_escapeSTRINGtype(self):
    355          "CSSSerializer._escapeSTRINGtype"
    356          css = ur'''@import url("ABC\a");
    357  @import "ABC\a";
    358  @import 'ABC\a';
    359  a[href='"\a\22\27"'] {
    360      a: "\a\d\c";
    361      b: "\a \d \c ";
    362      c: "\"";
    363      d: "\22";
    364      e: '\'';
    365      content: '\27';
    366      }'''
    367          exp = ur'''@import url("ABC\a ");
    368  @import "ABC\a ";
    369  @import "ABC\a ";
    370  a[href='"\a "\'"'] {
    371      a: "\a \d \c ";
    372      b: "\a \d \c ";
    373      c: "\"";
    374      d: "\"";
    375      e: '\'';
    376      content: '\''
    377      }'''
    378          sheet = cssutils.parseString(css)
    379          self.assertEqual(sheet.cssText, exp)
    380
    381
    382  if __name__ == '__main__':
    383      import unittest
    384      unittest.main()
    385