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: 2008-02-14 22:12:02 +0100 (Do, 14 Feb 2008) $'
      6  __version__ = '$LastChangedRevision: 1052 $'
      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          # no init param
     26          self.assertEqual(None, self.r.href)
     27          self.assertEqual(None, self.r.hreftype)
     28          self.assertEqual(u'all', self.r.media.mediaText)
     29          self.assertEqual(
     30              cssutils.stylesheets.MediaList, type(self.r.media))
     31          self.assertEqual(None, self.r.name)
     32          self.assertEqual(None, self.r.styleSheet)
     33          self.assertEqual(u'', self.r.cssText)
     34
     35          # all
     36          r = cssutils.css.CSSImportRule(href='href', mediaText='tv', name='name')
     37          self.assertEqual(u'@import url(href) tv "name";', r.cssText)
     38          self.assertEqual("href", r.href)
     39          self.assertEqual(None, r.hreftype)
     40          self.assertEqual(u'tv', r.media.mediaText)
     41          self.assertEqual(
     42              cssutils.stylesheets.MediaList, type(r.media))
     43          self.assertEqual('name', r.name)
     44          self.assertEqual(None, r.parentRule) # see CSSRule
     45          self.assertEqual(None, r.parentStyleSheet) # see CSSRule
     46
     47          # href
     48          r = cssutils.css.CSSImportRule(u'x')
     49          self.assertEqual(u'@import url(x);', r.cssText)
     50          self.assertEqual('x', r.href)
     51          self.assertEqual(None, r.hreftype)
     52
     53          # href + mediaText
     54          r = cssutils.css.CSSImportRule(u'x', u'print')
     55          self.assertEqual(u'@import url(x) print;', r.cssText)
     56          self.assertEqual('x', r.href)
     57          self.assertEqual('print', r.media.mediaText)
     58
     59          # href + name
     60          r = cssutils.css.CSSImportRule(u'x', name=u'n')
     61          self.assertEqual(u'@import url(x) "n";', r.cssText)
     62          self.assertEqual('x', r.href)
     63          self.assertEqual('n', r.name)
     64
     65          # href + mediaText + name
     66          r = cssutils.css.CSSImportRule(u'x', u'print', 'n')
     67          self.assertEqual(u'@import url(x) print "n";', r.cssText)
     68          self.assertEqual('x', r.href)
     69          self.assertEqual('print', r.media.mediaText)
     70          self.assertEqual('n', r.name)
     71
     72          # media +name only
     73          self.r = cssutils.css.CSSImportRule(mediaText=u'print', name="n")
     74          self.assertEqual(cssutils.stylesheets.MediaList,
     75                           type(self.r.media))
     76          self.assertEqual(u'', self.r.cssText)
     77          self.assertEqual(u'print', self.r.media.mediaText)
     78          self.assertEqual(u'n', self.r.name)
     79
     80 -    def test_cssText(self):
     81          "CSSImportRule.cssText"
     82          tests = {
     83              # href string
     84              u'''@import "str";''': None,
     85              u'''@import"str";''': u'''@import "str";''',
     86              u'''@\\import "str";''': u'''@import "str";''',
     87              u'''@IMPORT "str";''': u'''@import "str";''',
     88              u'''@import 'str';''': u'''@import "str";''',
     89              u'''@import 'str' ;''': u'''@import "str";''',
     90              u'''@import "str";''': None,
     91              u'''@import "str"  ;''': u'''@import "str";''',
     92              ur'''@import "\""  ;''': ur'''@import "\"";''',
     93              u'''@import '\\'';''': ur'''@import "'";''',
     94              u'''@import '"';''': ur'''@import "\"";''',
     95              # href url
     96              u'''@import url(x.css);''': None,
     97              # nospace
     98              u'''@importurl(x.css);''': u'''@import url(x.css);''',
     99              u'''@import url(")");''': u'''@import url(")");''',
    100              u'''@import url("\\"");''': u'''@import url(");''',
    101              u'''@import url('\\'');''': u'''@import url(');''',
    102
    103              # href + media
    104              u'''@import "str" tv, print;''': None,
    105              u'''@import"str"tv,print;''': u'''@import "str" tv, print;''',
    106              u'''@import "str" tv, print, all;''': u'''@import "str";''',
    107              u'''@import "str" handheld, all;''': u'''@import "str" all, handheld;''',
    108              u'''@import "str" all, handheld;''': None,
    109              u'''@import "str" not tv;''': None,
    110              u'''@import "str" only tv;''': None,
    111              u'''@import "str" only tv and (color: 2);''': None,
    112
    113              # href + name
    114              u'''@import "str" "name";''': None,
    115              u'''@import "str" 'name';''': u'''@import "str" "name";''',
    116              u'''@import url(x) "name";''': None,
    117              u'''@import "str" "\\"";''': None,
    118              u'''@import "str" '\\'';''': u'''@import "str" "'";''',
    119
    120              # href + media + name
    121              u'''@import"str"tv"name";''': u'''@import "str" tv "name";''',
    122              u'''@import\t\r\f\n"str"\t\t\r\f\ntv\t\t\r\f\n"name"\t;''':
    123                  u'''@import "str" tv "name";''',
    124
    125              # comments
    126              u'''@import /*1*/ "str" /*2*/;''': None,
    127              u'''@import/*1*/"str"/*2*/;''':
    128                      u'''@import /*1*/ "str" /*2*/;''',
    129              u'''@import   \n  /*1*/ \n  "str" \n  /*2*/  \n   ;''':
    130                      u'''@import /*1*/ "str" /*2*/;''',
    131              }
    132          self.do_equal_r(tests) # set cssText
    133          tests.update({
    134              u'@import "x.css" tv': '@import "x.css" tv;',
    135              u'@import "x.css"': '@import "x.css";', # no ;
    136              u"@import 'x.css'": '@import "x.css";', # no ;
    137              u'@import url(x.css)': '@import url(x.css);', # no ;
    138              u'@import "x;': '@import "x;";', # no "!
    139              })
    140          self.do_equal_p(tests) # parse
    141
    142          tests = {
    143              u'''@import;''': xml.dom.SyntaxErr,
    144              u'''@import all;''': xml.dom.SyntaxErr,
    145              u'''@import all"name";''': xml.dom.SyntaxErr,
    146              u'''@import;''': xml.dom.SyntaxErr,
    147              u'''@import x";''': xml.dom.SyntaxErr,
    148              u'''@import "str" ,all;''': xml.dom.SyntaxErr,
    149              u'''@import "str" all,;''': xml.dom.SyntaxErr,
    150              u'''@import "str" all tv;''': xml.dom.SyntaxErr,
    151              u'''@import "str" "name" all;''': xml.dom.SyntaxErr,
    152              }
    153          self.do_raise_p(tests) # parse
    154          tests.update({
    155              u'@import "x.css"': xml.dom.SyntaxErr,
    156              u"@import 'x.css'": xml.dom.SyntaxErr,
    157              u'@import url(x.css)': xml.dom.SyntaxErr,
    158              u'@import "x.css" tv': xml.dom.SyntaxErr,
    159              u'@import "x;': xml.dom.SyntaxErr,
    160              u'''@import url("x);''': xml.dom.SyntaxErr,
    161              })
    162          self.do_raise_r(tests) # set cssText
    163
    164 -    def test_href(self):
    165          "CSSImportRule.href"
    166          # set
    167          self.r.href = 'x'
    168          self.assertEqual('x', self.r.href)
    169          self.assertEqual(u'@import url(x);', self.r.cssText)
    170
    171          # http
    172          self.r.href = 'http://www.example.com/x?css=z&v=1'
    173          self.assertEqual('http://www.example.com/x?css=z&v=1' , self.r.href)
    174          self.assertEqual(u'@import url(http://www.example.com/x?css=z&v=1);',
    175                           self.r.cssText)
    176
    177          # also if hreftype changed
    178          self.r.hreftype='string'
    179          self.assertEqual('http://www.example.com/x?css=z&v=1' , self.r.href)
    180          self.assertEqual(u'@import "http://www.example.com/x?css=z&v=1";',
    181                           self.r.cssText)
    182
    183          # string escaping?
    184          self.r.href = '"'
    185          self.assertEqual(u'@import "\\"";', self.r.cssText)
    186          self.r.hreftype='url'
    187          self.assertEqual(u'@import url(");', self.r.cssText)
    188
    189          # url escaping?
    190          self.r.href = ')'
    191          self.assertEqual(u'@import url(")");', self.r.cssText)
    192
    193          self.r.hreftype = 'NOT VALID' # using default
    194          self.assertEqual(u'@import url(")");', self.r.cssText)
    195
    196 -    def test_hreftype(self):
    197          "CSSImportRule.hreftype"
    198          self.r = cssutils.css.CSSImportRule()
    199
    200          self.r.cssText = '@import /*1*/url(org) /*2*/;'
    201          self.assertEqual('uri', self.r.hreftype)
    202          self.assertEqual(u'@import /*1*/ url(org) /*2*/;', self.r.cssText)
    203
    204          self.r.cssText = '@import /*1*/"org" /*2*/;'
    205          self.assertEqual('string', self.r.hreftype)
    206          self.assertEqual(u'@import /*1*/ "org" /*2*/;', self.r.cssText)
    207
    208          self.r.href = 'new'
    209          self.assertEqual(u'@import /*1*/ "new" /*2*/;', self.r.cssText)
    210
    211          self.r.hreftype='uri'
    212          self.assertEqual(u'@import /*1*/ url(new) /*2*/;', self.r.cssText)
    213
    214 -    def test_media(self):
    215          "CSSImportRule.media"
    216          self.r.href = 'x' # @import url(x)
    217
    218          # media is readonly
    219          self.assertRaises(AttributeError, self.r.__setattr__, 'media', None)
    220
    221          # but not static
    222          self.r.media.mediaText = 'print'
    223          self.assertEqual(u'@import url(x) print;', self.r.cssText)
    224
    225 -    def test_name(self):
    226          "CSSImportRule.name"
    227          r = cssutils.css.CSSImportRule('x')
    228
    229          r.name = "n"
    230          self.assertEqual('n', r.name)
    231          self.assertEqual(u'@import url(x) "n";', r.cssText)
    232          r.name = '"'
    233          self.assertEqual('"', r.name)
    234          self.assertEqual(u'@import url(x) "\\"";', r.cssText)
    235
    236 -    def test_incomplete(self):
    237          "CSSImportRule (incomplete)"
    238          tests = {
    239              u'@import "x.css': u'@import "x.css";',
    240              u"@import 'x": u'@import "x";',
    241              # TODO:
    242              u"@import url(x": u'@import url(x);',
    243              u"@import url('x": u'@import url(x);',
    244              u'@import url("x;': u'@import url("x;");',
    245              u'@import url( "x;': u'@import url("x;");',
    246              u'@import url("x ': u'@import url("x ");',
    247              u'@import url(x ': u'@import url(x);',
    248              u'''@import "a
    249                  @import "b";
    250                  @import "c";''': u'@import "c";'
    251          }
    252          self.do_equal_p(tests, raising=False) # parse
    253
    254 -    def test_InvalidModificationErr(self):
    255          "CSSImportRule.cssText InvalidModificationErr"
    256          self._test_InvalidModificationErr(u'@import')
    257
    258 -    def test_reprANDstr(self):
    259          "CSSImportRule.__repr__(), .__str__()"
    260          href = 'x.css'
    261          mediaText = 'tv, print'
    262          name = 'name'
    263          s = cssutils.css.CSSImportRule(href=href, mediaText=mediaText, name=name)
    264
    265          # str(): mediaText nor name are present here
    266          self.assert_(href in str(s))
    267
    268          # repr()
    269          s2 = eval(repr(s))
    270          self.assert_(isinstance(s2, s.__class__))
    271          self.assert_(href == s2.href)
    272          self.assert_(mediaText == s2.media.mediaText)
    273          self.assert_(name == s2.name)
    274
    275
    276  if __name__ == '__main__':
    277      import unittest
    278      unittest.main()
    279