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