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: 2007-10-27 21:33:00 +0200 (Sa, 27 Okt 2007) $'
      6  __version__ = '$LastChangedRevision: 579 $'
      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
     32          self.assertEqual(cssutils.stylesheets.MediaList, type(r.media))
     33          self.assertEqual('all', r.media.mediaText)
     34
     35          # until any rules
     36          self.assertEqual(u'', r.cssText)
     37
     38 -    def test_InvalidModificationErr(self):
     39          "CSSMediaRule.cssText InvalidModificationErr"
     40          self._test_InvalidModificationErr(u'@media')
     41
     42 -    def test_incomplete(self):
     43          "CSSMediaRule (incomplete)"
     44          tests = {
     45              u'@media all { @unknown;': # no }
     46                  u'@media all {\n    @unknown;\n    }',
     47              u'@media all { a {x:1}': # no }
     48                  u'@media all {\n    a {\n        x: 1\n        }\n    }',
     49          }
     50          self.do_equal_p(tests) # parse
     51
     52 -    def test_cssRules(self):
     53          "CSSMediaRule.cssRules"
     54          r = cssutils.css.CSSMediaRule()
     55          self.assertEqual([], r.cssRules)
     56          ir = cssutils.css.CSSImportRule()
     57          r.cssRules.append(ir)
     58          self.assertEqual([ir], r.cssRules)
     59
     60 -    def test_cssText(self):
     61          "CSSMediaRule.cssText"
     62          tests = {
     63              u'''@media all {}''': u'',
     64              u'''@media all{}''': u'',
     65              u'''@media/*x*/all{}''': u'',
     66              # TODO:
     67              #u'@media all { @x{': u'@media all {\n    @x{}\n    }',
     68              u'''@media all { a{ x: 1} }''': u'''@media all {\n    a {\n        x: 1\n        }\n    }''',
     69              u'''@MEDIA all { a{x:1} }''': u'''@media all {\n    a {\n        x: 1\n        }\n    }''',
     70              u'''@\\media all { a{x:1} }''': u'''@media all {\n    a {\n        x: 1\n        }\n    }''',
     71              u'''@media all {@x some;a{color: red;}b{color: green;}}''':
     72                  u'''@media all {
     73      @x some;
     74      a {
     75          color: red
     76          }
     77      b {
     78          color: green
     79          }
     80      }''',
     81              }
     82          self.do_equal_p(tests)
     83          self.do_equal_r(tests)
     84
     85          tests = {
     86              u'@media;': xml.dom.SyntaxErr,
     87              u'@media all;': xml.dom.SyntaxErr,
     88              u'@media all; @x{}': xml.dom.SyntaxErr,
     89              # no medialist
     90              u'@media {}': xml.dom.SyntaxErr,
     91              u'@media/*only comment*/{}': xml.dom.SyntaxErr,
     92
     93              u'@media all { @charset "x"; a{}}': xml.dom.HierarchyRequestErr,
     94              u'@media all { @import "x"; a{}}': xml.dom.HierarchyRequestErr,
     95              u'@media all { @media all {} }': xml.dom.HierarchyRequestErr,
     96              }
     97          self.do_raise_p(tests)
     98          self.do_raise_r(tests)
     99
    100          tests = {
    101              # extra stuff
    102              u'@media all { , }': xml.dom.SyntaxErr,
    103              u'@media all { x{} } a{}': xml.dom.SyntaxErr,
    104              }
    105          self.do_raise_r(tests)
    106
    107          m = cssutils.css.CSSMediaRule()
    108          m.cssText = u'''@media all {@x; /*1*/a{color: red;}}'''
    109          for r in m.cssRules:
    110              self.assertEqual(m, r.parentRule)
    111
    112          cssutils.ser.prefs.useDefaults()
    113
    114 -    def test_deleteRule(self):
    115          "CSSMediaRule.deleteRule"
    116          # see CSSStyleSheet.deleteRule
    117          m = cssutils.css.CSSMediaRule()
    118          m.cssText = u'''@media all {
    119              @a;
    120              /* x */
    121              @b;
    122              @c;
    123              @d;
    124          }'''
    125          self.assertEqual(5, m.cssRules.length)
    126          self.assertRaises(xml.dom.IndexSizeErr, m.deleteRule, 5)
    127
    128          # end -1
    129          # check parentRule
    130          r = m.cssRules[-1]
    131          self.assertEqual(m, r.parentRule)
    132          m.deleteRule(-1)
    133          self.assertEqual(None, r.parentRule)
    134
    135          self.assertEqual(4, m.cssRules.length)
    136          self.assertEqual(
    137              u'@media all {\n    @a;\n    /* x */\n    @b;\n    @c;\n    }', m.cssText)
    138          # beginning
    139          m.deleteRule(0)
    140          self.assertEqual(3, m.cssRules.length)
    141          self.assertEqual(u'@media all {\n    /* x */\n    @b;\n    @c;\n    }', m.cssText)
    142          # middle
    143          m.deleteRule(1)
    144          self.assertEqual(2, m.cssRules.length)
    145          self.assertEqual(u'@media all {\n    /* x */\n    @c;\n    }', m.cssText)
    146          # end
    147          m.deleteRule(1)
    148          self.assertEqual(1, m.cssRules.length)
    149          self.assertEqual(u'@media all {\n    /* x */\n    }', m.cssText)
    150
    151 -    def test_insertRule(self):
    152          "CSSMediaRule.insertRule"
    153          # see CSSStyleSheet.insertRule
    154          r = cssutils.css.CSSMediaRule()
    155          charsetrule = cssutils.css.CSSCharsetRule('ascii')
    156          importrule = cssutils.css.CSSImportRule('x')
    157          namespacerule = cssutils.css.CSSNamespaceRule()
    158          pagerule = cssutils.css.CSSPageRule()
    159          mediarule = cssutils.css.CSSMediaRule()
    160          unknownrule = cssutils.css.CSSUnknownRule('@x;')
    161          stylerule = cssutils.css.CSSStyleRule('a')
    162          stylerule.cssText = u'a { x: 1}'
    163          comment1 = cssutils.css.CSSComment(u'/*1*/')
    164          comment2 = cssutils.css.CSSComment(u'/*2*/')
    165
    166          # hierarchy
    167          self.assertRaises(xml.dom.HierarchyRequestErr,
    168                            r.insertRule, charsetrule, 0)
    169          self.assertRaises(xml.dom.HierarchyRequestErr,
    170                            r.insertRule, importrule, 0)
    171          self.assertRaises(xml.dom.HierarchyRequestErr,
    172                            r.insertRule, namespacerule, 0)
    173          self.assertRaises(xml.dom.HierarchyRequestErr,
    174                            r.insertRule, pagerule, 0)
    175          self.assertRaises(xml.dom.HierarchyRequestErr,
    176                            r.insertRule, mediarule, 0)
    177
    178          # start insert
    179          r.insertRule(stylerule, 0)
    180          self.assertEqual(r, stylerule.parentRule)
    181          # before
    182          r.insertRule(comment1, 0)
    183          self.assertEqual(r, comment1.parentRule)
    184          # end explicit
    185          r.insertRule(unknownrule, 2)
    186          self.assertEqual(r, unknownrule.parentRule)
    187          # end implicit
    188          r.insertRule(comment2)
    189          self.assertEqual(r, comment2.parentRule)
    190          self.assertEqual(
    191              '@media all {\n    /*1*/\n    a {\n        x: 1\n        }\n    @x;\n    /*2*/\n    }',
    192              r.cssText)
    193
    194          # index
    195          self.assertRaises(xml.dom.IndexSizeErr,
    196                    r.insertRule, stylerule, -1)
    197          self.assertRaises(xml.dom.IndexSizeErr,
    198                    r.insertRule, stylerule, r.cssRules.length + 1)
    199
    200 -    def test_media(self):
    201          "CSSMediaRule.media"
    202          # see CSSImportRule.media
    203
    204          # setting not allowed
    205          self.assertRaises(AttributeError,
    206                            self.r.__setattr__, 'media', None)
    207          self.assertRaises(AttributeError,
    208                            self.r.__setattr__, 'media', 0)
    209
    210          # set mediaText instead
    211          self.r.media.mediaText = 'print'
    212          self.r.insertRule(self.stylerule)
    213          self.assertEqual(u'', self.r.cssText)
    214          cssutils.ser.prefs.keepEmptyRules = True
    215          self.assertEqual(u'@media print {\n    a {}\n    }', self.r.cssText)
    216          cssutils.ser.prefs.useDefaults()
    217
    218 -    def test_reprANDstr(self):
    219          "CSSMediaRule.__repr__(), .__str__()"
    220          mediaText='tv, print'
    221
    222          s = cssutils.css.CSSMediaRule(mediaText=mediaText)
    223
    224          self.assert_(mediaText in str(s))
    225
    226          s2 = eval(repr(s))
    227          self.assert_(isinstance(s2, s.__class__))
    228          self.assert_(mediaText == s2.media.mediaText)
    229
    230
    231  if __name__ == '__main__':
    232      import unittest
    233      unittest.main()
    234