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