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