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

# Source Code for Module cssutils.tests.test_cssimportrule

      1  """
      2  testcases for cssutils.css.CSSImportRule
      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 CSSImportRuleTestCase(test_cssrule.CSSRuleTestCase):
     13
     14 -    def setUp(self):
     15          super(CSSImportRuleTestCase, self).setUp()
     16          self.r = cssutils.css.CSSImportRule()
     17          self.rRO = cssutils.css.CSSImportRule(readonly=True)
     18          self.r_type = cssutils.css.CSSImportRule.IMPORT_RULE
     19          self.r_typeString = 'IMPORT_RULE'
     20
     21 -    def test_init(self):
     22          "CSSImportRule.__init__()"
     23          super(CSSImportRuleTestCase, self).test_init()
     24
     25          self.assertEqual(None, self.r.href)
     26          self.assertEqual(None, self.r.hreftype)
     27          self.assertEqual(
     28              cssutils.stylesheets.MediaList, type(self.r.media))
     29          self.assertEqual(u'all', self.r.media.mediaText)
     30          self.assertEqual(None, self.r.styleSheet)
     31          self.assertEqual(u'', self.r.cssText)
     32
     33 -    def test_InvalidModificationErr(self):
     34          "CSSImportRule.cssText InvalidModificationErr"
     35          self._test_InvalidModificationErr(u'@import')
     36
     37 -    def test_incomplete(self):
     38          "CSSImportRule (incomplete)"
     39          tests = {
     40              u'@import "x.css': u'@import "x.css";',
     41              u"@import 'x": u'@import "x";',
     42              u"@import url(x": u'@import url(x);',
     43              u"@import url('x": u'@import url(\'x\');',
     44              u'@import url("x;': u'@import url("x;");',
     45              u'@import url("x ': u'@import url("x ");',
     46              u'@import url(x ': u'@import url(x);',
     47          }
     48          self.do_equal_p(tests) # parse
     49
     50 -    def test_initparameter(self):
     51          "CSSImportRule.__init__(href, media, hreftype)"
     52          r = cssutils.css.CSSImportRule(u'x', u'print')
     53          self.assertEqual(None, r.hreftype)
     54          self.assertEqual(u'@import url(x) print;', r.cssText)
     55
     56          r = cssutils.css.CSSImportRule(
     57              u'x', u'print, tv', hreftype='string')
     58          self.assertEqual('string', r.hreftype)
     59          self.assertEqual(u'@import "x" print, tv;', r.cssText)
     60
     61          r = cssutils.css.CSSImportRule(u'x', u'print, tv', hreftype='uri')
     62          self.assertEqual('uri', r.hreftype)
     63          self.assertEqual(u'@import url(x) print, tv;', r.cssText)
     64
     65          # media but no href
     66          self.r = cssutils.css.CSSImportRule(mediaText=u'tv, screen')
     67          self.assertEqual(cssutils.stylesheets.MediaList,
     68                           type(self.r.media))
     69          self.assertEqual(u'tv, screen', self.r.media.mediaText)
     70          self.assertEqual(None, self.r.href)
     71          self.assertEqual(u'', self.r.cssText)
     72
     73 -    def test_href(self):
     74          "CSSImportRule.href"
     75          # set
     76          self.r.href = 'x'
     77          self.assertEqual('x' , self.r.href)
     78          self.assertEqual(
     79              u'@import url(x);', self.r.cssText)
     80
     81          # http
     82          self.r.href = 'http://www.example.com/x?css=z&v=1'
     83          self.assertEqual(
     84              'http://www.example.com/x?css=z&v=1' , self.r.href)
     85          self.assertEqual(
     86              u'@import url(http://www.example.com/x?css=z&v=1);',
     87              self.r.cssText)
     88
     89          # also if hreftype changed
     90          self.r.hreftype='string'
     91          self.assertEqual(
     92              'http://www.example.com/x?css=z&v=1' , self.r.href)
     93          self.assertEqual(
     94              u'@import "http://www.example.com/x?css=z&v=1";',
     95              self.r.cssText)
     96          # strange and probably invalid uri with "
     97          self.r.href = 'XXX"123'
     98          self.assertEqual(
     99              u'@import "XXX\\"123";',
    100              self.r.cssText)
    101
    102 -    def test_hreftype(self):
    103          "CSSImportRule.hreftype"
    104          self.r = cssutils.css.CSSImportRule()
    105
    106          self.r.cssText = '@import /*1*/url(org) /*2*/;'
    107          self.assertEqual('uri', self.r.hreftype)
    108          self.assertEqual(u'@import /*1*/url(org) /*2*/;', self.r.cssText)
    109
    110          self.r.cssText = '@import /*1*/"org" /*2*/;'
    111          self.assertEqual('string', self.r.hreftype)
    112          self.assertEqual(u'@import /*1*/"org" /*2*/;', self.r.cssText)
    113
    114          self.r.href = 'new'
    115          self.assertEqual(u'@import /*1*/"new" /*2*/;', self.r.cssText)
    116
    117          self.r.hreftype='uri'
    118          self.assertEqual(u'@import /*1*/url(new) /*2*/;', self.r.cssText)
    119
    120 -    def test_media(self):
    121          "CSSImportRule.media"
    122          self.r.href = 'x' # @import url(x)
    123
    124          # only medialist allowed
    125          self.assertRaises(AttributeError,
    126                            self.r.__setattr__, 'media', None)
    127
    128          # set mediaText instead
    129          self.r.media.mediaText = 'print'
    130          self.assertEqual(u'@import url(x) print;', self.r.cssText)
    131
    132 -    def test_cssText1(self):
    133          "CSSImportRule.cssText 1"
    134          tests = {
    135              u'''@import "str";''': None,
    136              u'''@\\import "str";''': u'''@import "str";''',
    137              u'''@IMPORT "str";''': u'''@import "str";''',
    138
    139              u'''@import 'str';''': u'''@import "str";''',
    140              u'''@import 'str' ;''': u'''@import "str";''',
    141              u'''@import "str";''': None,
    142              u'''@import "str"  ;''': u'''@import "str";''',
    143
    144              u'''@import /*1*/"str" /*2*/;''': None,
    145              u'''@import/*1*/ "str"/*2*/ ;''':
    146                  u'''@import /*1*/"str" /*2*/;''',
    147
    148              u'''@import "str";''': None,
    149              u'''@import "str" tv, print;''': None,
    150              u'''@import "str" tv, print, all;''': u'''@import "str";''',
    151              u'''@import "str" handheld, all;''': None,
    152              u'''@import "str" all, handheld;''': None,
    153
    154              u'''@import url(x.css);''': None,
    155              }
    156          self.do_equal_r(tests) # set cssText
    157          tests.update({
    158              u'@import "x.css"': '@import "x.css";', # no ;
    159              u"@import 'x.css'": '@import "x.css";', # no ;
    160              u'@import url(x.css)': '@import url(x.css);', # no ;
    161              u'@import "x.css" tv': '@import "x.css" tv;',
    162              u'@import "x;': '@import "x;";', # no "!
    163              })
    164          self.do_equal_p(tests) # parse
    165
    166          tests = {
    167              u'''@import;''': xml.dom.SyntaxErr,
    168              u'''@import all;''': xml.dom.SyntaxErr,
    169              u'''@import;''': xml.dom.SyntaxErr,
    170
    171              u'''@import x";''': xml.dom.SyntaxErr,
    172
    173              u'''@import "str" ,all;''': xml.dom.SyntaxErr,
    174              u'''@import "str" all,;''': xml.dom.SyntaxErr,
    175              u'''@import "str" all tv;''': xml.dom.SyntaxErr,
    176              }
    177          self.do_raise_p(tests) # parse
    178          tests.update({
    179              u'@import "x.css"': xml.dom.SyntaxErr,
    180              u"@import 'x.css'": xml.dom.SyntaxErr,
    181              u'@import url(x.css)': xml.dom.SyntaxErr,
    182              u'@import "x.css" tv': xml.dom.SyntaxErr,
    183              u'@import "x;': xml.dom.SyntaxErr,
    184              u'''@import url("x);''': xml.dom.SyntaxErr,
    185              })
    186          self.do_raise_r(tests) # set cssText
    187
    188 -    def test_reprANDstr(self):
    189          "CSSImportRule.__repr__(), .__str__()"
    190          href='x.css'
    191          mediaText='tv, print'
    192
    193          s = cssutils.css.CSSImportRule(href=href, mediaText=mediaText)
    194
    195          self.assert_(href in str(s))
    196          # mediaText is not present in str(s)
    197
    198          s2 = eval(repr(s))
    199          self.assert_(isinstance(s2, s.__class__))
    200          self.assert_(href == s2.href)
    201          self.assert_(mediaText == s2.media.mediaText)
    202
    203
    204  if __name__ == '__main__':
    205      import unittest
    206      unittest.main()
    207