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

# Source Code for Module cssutils.tests.test_cssmediarule

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