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

# Source Code for Module cssutils.tests.test_cssmediarule

      1  """Testcases for cssutils.css.CSSMediaRule"""
      2  __version__ = '$Id: test_cssmediarule.py 1116 2008-03-05 13:52:23Z cthedot $'
      3
      4  import xml.dom
      5  import test_cssrule
      6  import cssutils
      7
      8 -class CSSMediaRuleTestCase(test_cssrule.CSSRuleTestCase):
      9
     10 -    def setUp(self):
     11          super(CSSMediaRuleTestCase, self).setUp()
     12          self.r = cssutils.css.CSSMediaRule()
     13          self.rRO = cssutils.css.CSSMediaRule(readonly=True)
     14          self.r_type = cssutils.css.CSSMediaRule.MEDIA_RULE
     15          self.r_typeString = 'MEDIA_RULE'
     16          # for tests
     17          self.stylerule = cssutils.css.CSSStyleRule()
     18          self.stylerule.cssText = u'a {}'
     19
     20 -    def test_init(self):
     21          "CSSMediaRule.__init__()"
     22          super(CSSMediaRuleTestCase, self).test_init()
     23
     24          r = cssutils.css.CSSMediaRule()
     25          self.assertEqual(cssutils.css.CSSRuleList, type(r.cssRules))
     26          self.assertEqual([], r.cssRules)
     27          self.assertEqual(u'', r.cssText)
     28          self.assertEqual(cssutils.stylesheets.MediaList, type(r.media))
     29          self.assertEqual('all', r.media.mediaText)
     30          self.assertEqual(None, r.name)
     31
     32          r = cssutils.css.CSSMediaRule(mediaText='print', name='name')
     33          self.assertEqual(cssutils.css.CSSRuleList, type(r.cssRules))
     34          self.assertEqual([], r.cssRules)
     35          self.assertEqual(u'', r.cssText)
     36          self.assertEqual(cssutils.stylesheets.MediaList, type(r.media))
     37          self.assertEqual('print', r.media.mediaText)
     38          self.assertEqual('name', r.name)
     39
     40 -    def test_iter(self):
     41          "CSSMediaRule.__iter__()"
     42          m = cssutils.css.CSSMediaRule()
     43          m.cssText = '''@media all { /*1*/a { left: 0} b{ top:0} }'''
     44          types = [cssutils.css.CSSRule.COMMENT,
     45                   cssutils.css.CSSRule.STYLE_RULE,
     46                   cssutils.css.CSSRule.STYLE_RULE]
     47          for i, rule in enumerate(m):
     48              self.assertEqual(rule, m.cssRules[i])
     49              self.assertEqual(rule.type, types[i])
     50              self.assertEqual(rule.parentRule, m)
     51
     52 -    def test_cssRules(self):
     53          "CSSMediaRule.cssRules"
     54          r = cssutils.css.CSSMediaRule()
     55          self.assertEqual([], r.cssRules)
     56          sr = cssutils.css.CSSStyleRule()
     57          r.cssRules.append(sr)
     58          self.assertEqual([sr], r.cssRules)
     59          ir = cssutils.css.CSSImportRule()
     60          self.assertRaises(xml.dom.HierarchyRequestErr, r.cssRules.append, ir)
     61
     62 -    def test_cssText(self):
     63          "CSSMediaRule.cssText"
     64          tests = {
     65              u'''@mediaall"name"{}''': u'',
     66              u'''@media all "name"{}''': u'',
     67              u'''@media all {}''': u'',
     68              u'''@mediaall{}''': u'',
     69              u'''@media/*x*/all{}''': u'',
     70              u'''@media all { a{ x: 1} }''': u'''@media all {\n    a {\n        x: 1\n        }\n    }''',
     71              u'''@media all "name" { a{ x: 1} }''': u'''@media all "name" {\n    a {\n        x: 1\n        }\n    }''',
     72              u'''@MEDIA all { a{x:1} }''': u'''@media all {\n    a {\n        x: 1\n        }\n    }''',
     73              u'''@\\media all { a{x:1} }''': u'''@media all {\n    a {\n        x: 1\n        }\n    }''',
     74              u'''@media all {@x some;a{color: red;}b{color: green;}}''':
     75                  u'''@media all {
     76      @x some;
     77      a {
     78          color: red
     79          }
     80      b {
     81          color: green
     82          }
     83      }''',
     84              u'@media all { @x{}}': u'@media all {\n    @x {\n    }\n    }',
     85              u'@media all "n" /**/ { @x{}}':
     86                  u'@media all "n" /**/ {\n    @x {\n    }\n    }',
     87              u'@mediaall"n"/**/{@x{}}':
     88                  u'@media all "n" /**/ {\n    @x {\n    }\n    }',
     89              # comments
     90              u'@media/*1*//*2*/all/*3*//*4*/{/*5*/a{x:1}}':
     91                  u'@media /*1*/ /*2*/ all /*3*/ /*4*/ {\n    /*5*/\n    a {\n        x: 1\n        }\n    }',
     92              u'@media  /*1*/  /*2*/  all  /*3*/  /*4*/  {  /*5*/  a{ x: 1} }':
     93                  u'@media /*1*/ /*2*/ all /*3*/ /*4*/ {\n    /*5*/\n    a {\n        x: 1\n        }\n    }',
     94              # WS
     95              u'@media\n\t\f all\n\t\f {\n\t\f a{ x: 1}\n\t\f }':
     96                  u'@media all {\n    a {\n        x: 1\n        }\n    }',
     97              }
     98          self.do_equal_p(tests)
     99          self.do_equal_r(tests)
    100
    101          tests = {
    102              u'@media {}': xml.dom.SyntaxErr,
    103              u'@media;': xml.dom.SyntaxErr,
    104              u'@media/*only comment*/{}': xml.dom.SyntaxErr,
    105              u'@media all;': xml.dom.SyntaxErr,
    106              u'@media all "n";': xml.dom.SyntaxErr,
    107              u'@media all; @x{}': xml.dom.SyntaxErr,
    108              u'@media { a{ x: 1} }': xml.dom.SyntaxErr,
    109              u'@media "name" { a{ x: 1} }': xml.dom.SyntaxErr,
    110              u'@media "name" all { a{ x: 1} }': xml.dom.SyntaxErr,
    111              u'@media all { @charset "x"; a{}}': xml.dom.HierarchyRequestErr,
    112              u'@media all { @import "x"; a{}}': xml.dom.HierarchyRequestErr,
    113              u'@media all { @media all {} }': xml.dom.HierarchyRequestErr,
    114              u'@media all { , }': xml.dom.SyntaxErr,
    115              u'@media all {}EXTRA': xml.dom.SyntaxErr,
    116              }
    117          self.do_raise_p(tests)
    118          self.do_raise_r(tests)
    119
    120          tests = {
    121              # extra stuff
    122              '@media all { x{} } a{}': xml.dom.SyntaxErr,
    123              }
    124          self.do_raise_r(tests)
    125
    126          m = cssutils.css.CSSMediaRule()
    127          m.cssText = u'''@media all {@x; /*1*/a{color: red;}}'''
    128          for r in m.cssRules:
    129              self.assertEqual(m, r.parentRule)
    130              self.assertEqual(m.parentStyleSheet, r.parentStyleSheet)
    131
    132          cssutils.ser.prefs.useDefaults()
    133
    134 -    def test_media(self):
    135          "CSSMediaRule.media"
    136          # see CSSImportRule.media
    137
    138          # setting not allowed
    139          self.assertRaises(AttributeError,
    140                            self.r.__setattr__, 'media', None)
    141          self.assertRaises(AttributeError,
    142                            self.r.__setattr__, 'media', 0)
    143
    144          # set mediaText instead
    145          self.r.media.mediaText = 'print'
    146          self.r.insertRule(self.stylerule)
    147          self.assertEqual(u'', self.r.cssText)
    148          cssutils.ser.prefs.keepEmptyRules = True
    149          self.assertEqual(u'@media print {\n    a {}\n    }', self.r.cssText)
    150          cssutils.ser.prefs.useDefaults()
    151
    152 -    def test_name(self):
    153          "CSSMediaRule.name"
    154          r = cssutils.css.CSSMediaRule()
    155          r.cssText = '@media all "\\n\\"ame" {a{left: 0}}'
    156
    157          self.assertEqual('\\n"ame', r.name)
    158          r.name = "n"
    159          self.assertEqual('n', r.name)
    160          self.assertEqual(u'@media all "n" {\n    a {\n        left: 0\n        }\n    }',
    161                           r.cssText)
    162          r.name = '"'
    163          self.assertEqual('"', r.name)
    164          self.assertEqual(u'@media all "\\"" {\n    a {\n        left: 0\n        }\n    }',
    165                           r.cssText)
    166
    167          r.name = ''
    168          self.assertEqual('', r.name)
    169          self.assertEqual(u'@media all {\n    a {\n        left: 0\n        }\n    }',
    170                           r.cssText)
    171
    172          r.name = None
    173          self.assertEqual(None, r.name)
    174          self.assertEqual(u'@media all {\n    a {\n        left: 0\n        }\n    }',
    175                           r.cssText)
    176
    177 -    def test_deleteRule(self):
    178          "CSSMediaRule.deleteRule"
    179          # see CSSStyleSheet.deleteRule
    180          m = cssutils.css.CSSMediaRule()
    181          m.cssText = u'''@media all {
    182              @a;
    183              /* x */
    184              @b;
    185              @c;
    186              @d;
    187          }'''
    188          self.assertEqual(5, m.cssRules.length)
    189          self.assertRaises(xml.dom.IndexSizeErr, m.deleteRule, 5)
    190
    191          # end -1
    192          # check parentRule
    193          r = m.cssRules[-1]
    194          self.assertEqual(m, r.parentRule)
    195          m.deleteRule(-1)
    196          self.assertEqual(None, r.parentRule)
    197
    198          self.assertEqual(4, m.cssRules.length)
    199          self.assertEqual(
    200              u'@media all {\n    @a;\n    /* x */\n    @b;\n    @c;\n    }', m.cssText)
    201          # beginning
    202          m.deleteRule(0)
    203          self.assertEqual(3, m.cssRules.length)
    204          self.assertEqual(u'@media all {\n    /* x */\n    @b;\n    @c;\n    }', m.cssText)
    205          # middle
    206          m.deleteRule(1)
    207          self.assertEqual(2, m.cssRules.length)
    208          self.assertEqual(u'@media all {\n    /* x */\n    @c;\n    }', m.cssText)
    209          # end
    210          m.deleteRule(1)
    211          self.assertEqual(1, m.cssRules.length)
    212          self.assertEqual(u'@media all {\n    /* x */\n    }', m.cssText)
    213
    214 -    def test_add(self):
    215          "CSSMediaRule.add()"
    216          # see CSSStyleSheet.add
    217          r = cssutils.css.CSSMediaRule()
    218          stylerule1 = cssutils.css.CSSStyleRule()
    219          stylerule2 = cssutils.css.CSSStyleRule()
    220          r.add(stylerule1)
    221          r.add(stylerule2)
    222          self.assertEqual(r.cssRules[0], stylerule1)
    223          self.assertEqual(r.cssRules[1], stylerule2)
    224
    225 -    def test_insertRule(self):
    226          "CSSMediaRule.insertRule"
    227          # see CSSStyleSheet.insertRule
    228          r = cssutils.css.CSSMediaRule()
    229          charsetrule = cssutils.css.CSSCharsetRule('ascii')
    230          importrule = cssutils.css.CSSImportRule('x')
    231          namespacerule = cssutils.css.CSSNamespaceRule()
    232          pagerule = cssutils.css.CSSPageRule()
    233          mediarule = cssutils.css.CSSMediaRule()
    234          unknownrule = cssutils.css.CSSUnknownRule('@x;')
    235          stylerule = cssutils.css.CSSStyleRule('a')
    236          stylerule.cssText = u'a { x: 1}'
    237          comment1 = cssutils.css.CSSComment(u'/*1*/')
    238          comment2 = cssutils.css.CSSComment(u'/*2*/')
    239
    240          # hierarchy
    241          self.assertRaises(xml.dom.HierarchyRequestErr,
    242                            r.insertRule, charsetrule, 0)
    243          self.assertRaises(xml.dom.HierarchyRequestErr,
    244                            r.insertRule, importrule, 0)
    245          self.assertRaises(xml.dom.HierarchyRequestErr,
    246                            r.insertRule, namespacerule, 0)
    247          self.assertRaises(xml.dom.HierarchyRequestErr,
    248                            r.insertRule, pagerule, 0)
    249          self.assertRaises(xml.dom.HierarchyRequestErr,
    250                            r.insertRule, mediarule, 0)
    251
    252          # start insert
    253          r.insertRule(stylerule, 0)
    254          self.assertEqual(r, stylerule.parentRule)
    255          self.assertEqual(r.parentStyleSheet, stylerule.parentStyleSheet)
    256          # before
    257          r.insertRule(comment1, 0)
    258          self.assertEqual(r, comment1.parentRule)
    259          self.assertEqual(r.parentStyleSheet, stylerule.parentStyleSheet)
    260          # end explicit
    261          r.insertRule(unknownrule, 2)
    262          self.assertEqual(r, unknownrule.parentRule)
    263          self.assertEqual(r.parentStyleSheet, stylerule.parentStyleSheet)
    264          # end implicit
    265          r.insertRule(comment2)
    266          self.assertEqual(r, comment2.parentRule)
    267          self.assertEqual(r.parentStyleSheet, stylerule.parentStyleSheet)
    268          self.assertEqual(
    269              '@media all {\n    /*1*/\n    a {\n        x: 1\n        }\n    @x;\n    /*2*/\n    }',
    270              r.cssText)
    271
    272          # index
    273          self.assertRaises(xml.dom.IndexSizeErr,
    274                    r.insertRule, stylerule, -1)
    275          self.assertRaises(xml.dom.IndexSizeErr,
    276                    r.insertRule, stylerule, r.cssRules.length + 1)
    277
    278 -    def test_InvalidModificationErr(self):
    279          "CSSMediaRule.cssText InvalidModificationErr"
    280          self._test_InvalidModificationErr(u'@media')
    281
    282 -    def test_incomplete(self):
    283          "CSSMediaRule (incomplete)"
    284          tests = {
    285              u'@media all { @unknown;': # no }
    286                  u'@media all {\n    @unknown;\n    }',
    287              u'@media all { a {x:1}': # no }
    288                  u'@media all {\n    a {\n        x: 1\n        }\n    }',
    289          }
    290          self.do_equal_p(tests) # parse
    291
    292 -    def test_reprANDstr(self):
    293          "CSSMediaRule.__repr__(), .__str__()"
    294          mediaText='tv, print'
    295
    296          s = cssutils.css.CSSMediaRule(mediaText=mediaText)
    297
    298          self.assert_(mediaText in str(s))
    299
    300          s2 = eval(repr(s))
    301          self.assert_(isinstance(s2, s.__class__))
    302          self.assert_(mediaText == s2.media.mediaText)
    303
    304
    305  if __name__ == '__main__':
    306      import unittest
    307      unittest.main()
    308