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  __version__ = '$Id: test_serialize.py 1116 2008-03-05 13:52:23Z cthedot $'
      4
      5  import basetest
      6  import cssutils
      7
      8 -class CSSSerializerTestCase(basetest.BaseTestCase):
      9      """
     10      testcases for cssutils.CSSSerializer
     11      """
     12  #    def test_init(self):
     13  #        "CSSSerializer.__init__()"
     14
     15 -    def test_useDefaults(self):
     16          "Preferences.useDefaults()"
     17          cssutils.ser.prefs.useMinified()
     18          cssutils.ser.prefs.useDefaults()
     19          self.assertEqual(cssutils.ser.prefs.defaultAtKeyword, True)
     20          self.assertEqual(cssutils.ser.prefs.defaultPropertyName, True)
     21          self.assertEqual(cssutils.ser.prefs.defaultPropertyPriority, True)
     22          self.assertEqual(cssutils.ser.prefs.importHrefFormat, None)
     23          self.assertEqual(cssutils.ser.prefs.indent, 4 * u' ')
     24          self.assertEqual(cssutils.ser.prefs.keepAllProperties, True)
     25          self.assertEqual(cssutils.ser.prefs.keepComments, True)
     26          self.assertEqual(cssutils.ser.prefs.keepEmptyRules, False)
     27          self.assertEqual(cssutils.ser.prefs.keepUsedNamespaceRulesOnly, 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.selectorCombinatorSpacer, u' ')
     35          self.assertEqual(cssutils.ser.prefs.spacer, u' ')
     36          self.assertEqual(cssutils.ser.prefs.validOnly, False)
     37          css = u'''
     38      /*1*/
     39      @import url(x) tv , print;
     40      @namespace prefix "uri";
     41      @namespace unused "unused";
     42      @media all {}
     43      @media all {
     44          a {}
     45      }
     46      @media   all  {
     47      a { color: red; }
     48          }
     49      @page     { left: 0; }
     50      a {}
     51      prefix|x, a  +  b  >  c  ~  d  ,  b { top : 1px ;
     52          font-family : arial ,'some'
     53          }
     54      '''
     55          parsedcss = u'''/*1*/
     56  @import url(x) tv, print;
     57  @namespace prefix "uri";
     58  @namespace unused "unused";
     59  @media all {
     60      a {
     61          color: red
     62          }
     63      }
     64  @page {
     65      left: 0
     66      }
     67  prefix|x, a + b > c ~ d, b {
     68      top: 1px;
     69      font-family: arial, "some"
     70      }'''
     71          s = cssutils.parseString(css)
     72          self.assertEqual(s.cssText, parsedcss)
     73
     74 -    def test_useMinified(self):
     75          "Preferences.useMinified()"
     76          cssutils.ser.prefs.useDefaults()
     77          cssutils.ser.prefs.useMinified()
     78          self.assertEqual(cssutils.ser.prefs.defaultAtKeyword, True)
     79          self.assertEqual(cssutils.ser.prefs.defaultPropertyName, True)
     80          self.assertEqual(cssutils.ser.prefs.importHrefFormat, 'string')
     81          self.assertEqual(cssutils.ser.prefs.indent, u'')
     82          self.assertEqual(cssutils.ser.prefs.keepAllProperties, True)
     83          self.assertEqual(cssutils.ser.prefs.keepComments, False)
     84          self.assertEqual(cssutils.ser.prefs.keepEmptyRules, False)
     85          self.assertEqual(cssutils.ser.prefs.keepUsedNamespaceRulesOnly, True)
     86          self.assertEqual(cssutils.ser.prefs.lineNumbers, False)
     87          self.assertEqual(cssutils.ser.prefs.lineSeparator, u'')
     88          self.assertEqual(cssutils.ser.prefs.listItemSpacer, u'')
     89          self.assertEqual(cssutils.ser.prefs.omitLastSemicolon, True)
     90          self.assertEqual(cssutils.ser.prefs.paranthesisSpacer, u'')
     91          self.assertEqual(cssutils.ser.prefs.propertyNameSpacer, u'')
     92          self.assertEqual(cssutils.ser.prefs.selectorCombinatorSpacer, u'')
     93          self.assertEqual(cssutils.ser.prefs.spacer, u'')
     94          self.assertEqual(cssutils.ser.prefs.validOnly, False)
     95
     96          css = u'''
     97      /*1*/
     98      @import url(x) tv , print;
     99      @namespace prefix "uri";
    100      @namespace unused "unused";
    101      @media all {}
    102      @media all {
    103          a {}
    104      }
    105      @media all "name" {
    106          a { color: red; }
    107      }
    108      @page:left {
    109      left: 0
    110      }
    111      a {}
    112      prefix|x, a + b > c ~ d , b { top : 1px ;
    113          font-family : arial ,  'some'
    114          }
    115      '''
    116          s = cssutils.parseString(css)
    117          self.assertEqual(s.cssText,
    118              u'''@import"x"tv,print;@namespaceprefix"uri";@mediaall"name"{a{color:red}}@page:left{left:0}prefix|x,a+b>c~d,b{top:1px;font-family:arial,"some"}'''
    119              )
    120
    121 -    def test_defaultAtKeyword(self):
    122          "Preferences.defaultAtKeyword"
    123          cssutils.ser.prefs.useDefaults()
    124
    125          s = cssutils.parseString(u'@im\\port "x";')
    126          self.assertEqual(u'@import "x";', s.cssText)
    127          cssutils.ser.prefs.defaultAtKeyword = True
    128          self.assertEqual(u'@import "x";', s.cssText)
    129          cssutils.ser.prefs.defaultAtKeyword = False
    130          self.assertEqual(u'@im\\port "x";', s.cssText)
    131
    132 -    def test_defaultPropertyName(self):
    133          "Preferences.defaultPropertyName"
    134          cssutils.ser.prefs.useDefaults()
    135          cssutils.ser.prefs.keepAllProperties = False
    136
    137          # does not actually work as once the name is set it is used also
    138          # if used with a backslash in it later...
    139
    140          s = cssutils.parseString(ur'a { c\olor: green; }')
    141          self.assertEqual(u'a {\n    color: green\n    }', s.cssText)
    142          cssutils.ser.prefs.defaultPropertyName = True
    143          self.assertEqual(u'a {\n    color: green\n    }', s.cssText)
    144          cssutils.ser.prefs.defaultPropertyName = False
    145          self.assertEqual(u'a {\n    c\\olor: green\n    }', s.cssText)
    146
    147          s = cssutils.parseString(u'a { color: red; c\olor: green; }')
    148          self.assertEqual(u'a {\n    c\\olor: green\n    }', s.cssText)
    149          cssutils.ser.prefs.defaultPropertyName = False
    150          self.assertEqual(u'a {\n    c\\olor: green\n    }', s.cssText)
    151          cssutils.ser.prefs.defaultPropertyName = True
    152          self.assertEqual(u'a {\n    color: green\n    }', s.cssText)
    153
    154 -    def test_defaultPropertyPriority(self):
    155          "Preferences.defaultPropertyPriority"
    156          cssutils.ser.prefs.useDefaults()
    157          css = u'''a {\n    color: green !IM\\portant\n    }'''
    158          s = cssutils.parseString(css)
    159          self.assertEqual(s.cssText,
    160                           u'''a {\n    color: green !important\n    }''')
    161          cssutils.ser.prefs.defaultPropertyPriority = False
    162          self.assertEqual(s.cssText, css)
    163
    164 -    def test_importHrefFormat(self):
    165          "Preferences.importHrefFormat"
    166          cssutils.ser.prefs.useDefaults()
    167
    168          r0 = cssutils.css.CSSImportRule()
    169          r0.cssText=u'@import url("not");'
    170          r1 = cssutils.css.CSSImportRule()
    171          r1.cssText=u'@import "str";'
    172          self.assertEqual(u'@import url(not);', r0.cssText)
    173          self.assertEqual(u'@import "str";', r1.cssText)
    174
    175          cssutils.ser.prefs.importHrefFormat = 'string'
    176          self.assertEqual(u'@import "not";', r0.cssText)
    177          self.assertEqual(u'@import "str";', r1.cssText)
    178
    179          cssutils.ser.prefs.importHrefFormat = 'uri'
    180          self.assertEqual(u'@import url(not);', r0.cssText)
    181          self.assertEqual(u'@import url(str);', r1.cssText)
    182
    183          cssutils.ser.prefs.importHrefFormat = 'not defined'
    184          self.assertEqual(u'@import url(not);', r0.cssText)
    185          self.assertEqual(u'@import "str";', r1.cssText)
    186
    187 -    def test_indent(self):
    188          "Preferences.ident"
    189          cssutils.ser.prefs.useDefaults()
    190
    191          s = cssutils.parseString(u'a { left: 0 }')
    192          exp4 = u'''a {
    193      left: 0
    194      }'''
    195          exp1 = u'''a {
    196   left: 0
    197   }'''
    198          cssutils.ser.prefs.indent = ' '
    199          self.assertEqual(exp1, s.cssText)
    200          cssutils.ser.prefs.indent = 4* ' '
    201          self.assertEqual(exp4, s.cssText)
    202
    203 -    def test_keepAllProperties(self):
    204          "Preferences.keepAllProperties"
    205          cssutils.ser.prefs.useDefaults()
    206
    207          css = '''a {
    208              color: pink;
    209              color: red;
    210              c\olor: blue;
    211              c\olor: green;
    212              }'''
    213          s = cssutils.parseString(css)
    214          # keep only last
    215          cssutils.ser.prefs.keepAllProperties = False
    216          self.assertEqual(u'a {\n    color: green\n    }', s.cssText)
    217          # keep all
    218          cssutils.ser.prefs.keepAllProperties = True
    219          self.assertEqual(u'a {\n    color: pink;\n    color: red;\n    c\olor: blue;\n    c\olor: green\n    }', s.cssText)
    220
    221 -    def test_keepComments(self):
    222          "Preferences.keepComments"
    223          cssutils.ser.prefs.useDefaults()
    224
    225          s = cssutils.parseString('/*1*/ a { /*2*/ }')
    226          cssutils.ser.prefs.keepComments = False
    227          self.assertEqual('', s.cssText)
    228          cssutils.ser.prefs.keepEmptyRules = True
    229          self.assertEqual('a {}', s.cssText)
    230
    231 -    def test_keepEmptyRules(self):
    232          "Preferences.keepEmptyRules"
    233          # CSSStyleRule
    234          css = u'''a {}
    235  a {
    236      /*1*/
    237      }
    238  a {
    239      color: red
    240      }'''
    241          s = cssutils.parseString(css)
    242          cssutils.ser.prefs.useDefaults()
    243          cssutils.ser.prefs.keepEmptyRules = True
    244          self.assertEqual(css, s.cssText)
    245          cssutils.ser.prefs.keepEmptyRules = False
    246          self.assertEqual(u'a {\n    /*1*/\n    }\na {\n    color: red\n    }',
    247                           s.cssText)
    248          cssutils.ser.prefs.keepComments = False
    249          self.assertEqual(u'a {\n    color: red\n    }',
    250                           s.cssText)
    251
    252          # CSSMediaRule
    253          css = u'''@media tv {
    254      }
    255  @media all {
    256      /*1*/
    257      }
    258  @media print {
    259      a {}
    260      }
    261  @media print {
    262      a {
    263          /*1*/
    264          }
    265      }
    266  @media all {
    267      a {
    268          color: red
    269          }
    270      }'''
    271          s = cssutils.parseString(css)
    272          cssutils.ser.prefs.useDefaults()
    273          cssutils.ser.prefs.keepEmptyRules = True
    274     #     self.assertEqual(css, s.cssText)
    275          cssutils.ser.prefs.keepEmptyRules = False
    276          self.assertEqual('''@media all {
    277      /*1*/
    278      }
    279  @media print {
    280      a {
    281          /*1*/
    282          }
    283      }
    284  @media all {
    285      a {
    286          color: red
    287          }
    288      }''', s.cssText)
    289          cssutils.ser.prefs.keepComments = False
    290          self.assertEqual('''@media all {
    291      a {
    292          color: red
    293          }
    294      }''', s.cssText)
    295
    296 -    def test_keepUsedNamespaceRulesOnly(self):
    297          "Preferences.keepUsedNamespaceRulesOnly"
    298          cssutils.ser.prefs.useDefaults()
    299          tests = {
    300              # default == prefix => both are combined
    301              '@namespace p "u"; @namespace "u"; p|a, a {top: 0}':
    302                  ('@namespace "u";\na, a {\n    top: 0\n    }',
    303                   '@namespace "u";\na, a {\n    top: 0\n    }'),
    304              '@namespace "u"; @namespace p "u"; p|a, a {top: 0}':
    305                  ('@namespace p "u";\np|a, p|a {\n    top: 0\n    }',
    306                   '@namespace p "u";\np|a, p|a {\n    top: 0\n    }'),
    307              # default and prefix
    308              '@namespace p "u"; @namespace "d"; p|a, a {top: 0}':
    309                  ('@namespace p "u";\n@namespace "d";\np|a, a {\n    top: 0\n    }',
    310                   '@namespace p "u";\n@namespace "d";\np|a, a {\n    top: 0\n    }'),
    311              # prefix only
    312              '@namespace p "u"; @namespace "d"; p|a {top: 0}':
    313                  ('@namespace p "u";\n@namespace "d";\np|a {\n    top: 0\n    }',
    314                   '@namespace p "u";\np|a {\n    top: 0\n    }'),
    315              # default only
    316              '@namespace p "u"; @namespace "d"; a {top: 0}':
    317                  ('@namespace p "u";\n@namespace "d";\na {\n    top: 0\n    }',
    318                   '@namespace "d";\na {\n    top: 0\n    }'),
    319              # prefix-ns only
    320              '@namespace p "u"; @namespace d "d"; p|a {top: 0}':
    321                  ('@namespace p "u";\n@namespace d "d";\np|a {\n    top: 0\n    }',
    322                   '@namespace p "u";\np|a {\n    top: 0\n    }'),
    323          }
    324          for test in tests:
    325              s = cssutils.parseString(test)
    326              expwith, expwithout = tests[test]
    327              cssutils.ser.prefs.keepUsedNamespaceRulesOnly = False
    328              self.assertEqual(s.cssText, expwith)
    329              cssutils.ser.prefs.keepUsedNamespaceRulesOnly = True
    330              self.assertEqual(s.cssText, expwithout)
    331
    332 -    def test_lineNumbers(self):
    333          "Preferences.lineNumbers"
    334          cssutils.ser.prefs.useDefaults()
    335
    336          s = cssutils.parseString('a {top: 1; left: 2}')
    337          exp0 = '''a {
    338      top: 1;
    339      left: 2
    340      }'''
    341          exp1 = '''1: a {
    342  2:     top: 1;
    343  3:     left: 2
    344  4:     }'''
    345          self.assertEqual(False, cssutils.ser.prefs.lineNumbers)
    346          self.assertEqual(exp0, s.cssText)
    347          cssutils.ser.prefs.lineNumbers = True
    348          self.assertEqual(True, cssutils.ser.prefs.lineNumbers)
    349          self.assertEqual(exp1, s.cssText)
    350
    351 -    def test_lineSeparator(self):
    352          "Preferences.lineSeparator"
    353          cssutils.ser.prefs.useDefaults()
    354
    355          s = cssutils.parseString('a { x:1;y:2}')
    356          self.assertEqual('a {\n    x: 1;\n    y: 2\n    }', s.cssText)
    357          # cannot be indented as no split possible
    358          cssutils.ser.prefs.lineSeparator = u''
    359          self.assertEqual('a {x: 1;y: 2    }', s.cssText)
    360          # no valid css but should work
    361          cssutils.ser.prefs.lineSeparator = u'XXX'
    362          self.assertEqual('a {XXX    x: 1;XXX    y: 2XXX    }', s.cssText)
    363
    364 -    def test_listItemSpacer(self):
    365          "Preferences.listItemSpacer"
    366          cssutils.ser.prefs.useDefaults()
    367          cssutils.ser.prefs.keepEmptyRules = True
    368
    369          css = '''
    370          @import "x" print, tv;
    371  a, b {}'''
    372          s = cssutils.parseString(css)
    373          self.assertEqual(u'@import "x" print, tv;\na, b {}', s.cssText)
    374          cssutils.ser.prefs.listItemSpacer = u''
    375          self.assertEqual(u'@import "x" print,tv;\na,b {}', s.cssText)
    376
    377 -    def test_omitLastSemicolon(self):
    378          "Preferences.omitLastSemicolon"
    379          cssutils.ser.prefs.useDefaults()
    380
    381          css = 'a { x: 1; y: 2 }'
    382          s = cssutils.parseString(css)
    383          self.assertEqual(u'a {\n    x: 1;\n    y: 2\n    }', s.cssText)
    384          cssutils.ser.prefs.omitLastSemicolon = False
    385          self.assertEqual(u'a {\n    x: 1;\n    y: 2;\n    }', s.cssText)
    386
    387 -    def test_paranthesisSpacer(self):
    388          "Preferences.paranthesisSpacer"
    389          cssutils.ser.prefs.useDefaults()
    390          css = 'a { x: 1; y: 2 }'
    391          s = cssutils.parseString(css)
    392          self.assertEqual(u'a {\n    x: 1;\n    y: 2\n    }', s.cssText)
    393          cssutils.ser.prefs.paranthesisSpacer = u''
    394          self.assertEqual(u'a{\n    x: 1;\n    y: 2\n    }', s.cssText)
    395
    396 -    def test_propertyNameSpacer(self):
    397          "Preferences.propertyNameSpacer"
    398          cssutils.ser.prefs.useDefaults()
    399          css = 'a { x: 1; y: 2 }'
    400          s = cssutils.parseString(css)
    401          self.assertEqual(u'a {\n    x: 1;\n    y: 2\n    }', s.cssText)
    402          cssutils.ser.prefs.propertyNameSpacer = u''
    403          self.assertEqual(u'a {\n    x:1;\n    y:2\n    }', s.cssText)
    404
    405 -    def test_selectorCombinatorSpacer(self):
    406          "Preferences.selectorCombinatorSpacer"
    407          cssutils.ser.prefs.useDefaults()
    408          s = cssutils.css.Selector(selectorText='a+b>c~d  e')
    409          self.assertEqual(u'a + b > c ~ d e', s.selectorText)
    410          cssutils.ser.prefs.selectorCombinatorSpacer = u''
    411          self.assertEqual(u'a+b>c~d e', s.selectorText)
    412
    413 -    def test_validOnly(self):
    414          "Preferences.validOnly"
    415          # check pref
    416          cssutils.ser.prefs.useDefaults()
    417
    418          # Property
    419          p = cssutils.css.Property('color', '1px')
    420          self.assertEqual(p.cssText, 'color: 1px')
    421          p.value = '1px'
    422          cssutils.ser.prefs.validOnly = True
    423          self.assertEqual(p.cssText, '')
    424          cssutils.ser.prefs.validOnly = False
    425          self.assertEqual(p.cssText, 'color: 1px')
    426
    427          # CSSStyleDeclaration has no actual property valid
    428          # but is empty if containing invalid Properties only
    429          s = cssutils.css.CSSStyleDeclaration()
    430          s.setProperty(p)
    431          self.assertEqual(s.cssText, 'color: 1px')
    432          cssutils.ser.prefs.validOnly = True
    433          self.assertEqual(s.cssText, '')
    434
    435 -    def test_CSSStyleSheet(self):
    436          "CSSSerializer.do_CSSStyleSheet"
    437          css = u'/* κουρος */'
    438          sheet = cssutils.parseString(css)
    439          self.assertEqual(css, unicode(sheet.cssText, 'utf-8'))
    440
    441          css = u'@charset "utf-8";\n/* κουρος */'
    442          sheet = cssutils.parseString(css)
    443          self.assertEqual(css, unicode(sheet.cssText, 'utf-8'))
    444          sheet.cssRules[0].encoding = 'ascii'
    445          self.assertEqual('@charset "ascii";\n/* \\3BA \\3BF \\3C5 \\3C1 \\3BF \\3C2  */',
    446                           sheet.cssText)
    447
    448 -    def test_Property(self):
    449          "CSSSerializer.do_Property"
    450          cssutils.ser.prefs.useDefaults()
    451
    452          name="color"
    453          value="red"
    454          priority="!important"
    455
    456          s = cssutils.css.property.Property(
    457              name=name, value=value, priority=priority)
    458          self.assertEqual(u'color: red !important',
    459                      cssutils.ser.do_Property(s))
    460
    461          s = cssutils.css.property.Property(
    462              name=name, value=value)
    463          self.assertEqual(u'color: red',
    464                      cssutils.ser.do_Property(s))
    465
    466 -    def test_escapestring(self):
    467          "CSSSerializer._escapestring"
    468          #'"\a\22\27"'
    469          css = ur'''@import url("ABC\a");
    470  @import "ABC\a";
    471  @import 'ABC\a';
    472  a[href='"\a\22\27"'] {
    473      a: "\a\d\c";
    474      b: "\a \d \c ";
    475      c: "\"";
    476      d: "\22";
    477      e: '\'';
    478      content: '\27';
    479      }'''
    480          exp = ur'''@import url("ABC\a ");
    481  @import "ABC\a ";
    482  @import "ABC\a ";
    483  a[href="\"\a \"'\""] {
    484      a: "\a \d \c ";
    485      b: "\a \d \c ";
    486      c: "\"";
    487      d: "\"";
    488      e: "'";
    489      content: "'"
    490      }'''
    491          sheet = cssutils.parseString(css)
    492          self.assertEqual(sheet.cssText, exp)
    493
    494
    495  if __name__ == '__main__':
    496      import unittest
    497      unittest.main()
    498