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