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