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