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

# Source Code for Module cssutils.tests.test_cssimportrule

      1  """Testcases for cssutils.css.CSSImportRule"""
      2  __version__ = '$Id: test_cssimportrule.py 1175 2008-03-20 17:43:26Z cthedot $'
      3
      4  import xml.dom
      5  from minimock import mock, restore
      6  import test_cssrule
      7  import cssutils
      8
      9 -class CSSImportRuleTestCase(test_cssrule.CSSRuleTestCase):
     10
     11 -    def setUp(self):
     12          super(CSSImportRuleTestCase, self).setUp()
     13          self.r = cssutils.css.CSSImportRule()
     14          self.rRO = cssutils.css.CSSImportRule(readonly=True)
     15          self.r_type = cssutils.css.CSSImportRule.IMPORT_RULE
     16          self.r_typeString = 'IMPORT_RULE'
     17
     18 -    def test_init(self):
     19          "CSSImportRule.__init__()"
     20          super(CSSImportRuleTestCase, self).test_init()
     21
     22          # no init param
     23          self.assertEqual(None, self.r.href)
     24          self.assertEqual(None, self.r.hreftype)
     25          self.assertEqual(u'all', self.r.media.mediaText)
     26          self.assertEqual(
     27              cssutils.stylesheets.MediaList, type(self.r.media))
     28          self.assertEqual(None, self.r.name)
     29          self.assertEqual(None, self.r.styleSheet)
     30          self.assertEqual(u'', self.r.cssText)
     31          self.assertEqual(None, self.r.styleSheet)
     32
     33          # all
     34          r = cssutils.css.CSSImportRule(href='href', mediaText='tv', name='name')
     35          self.assertEqual(u'@import url(href) tv "name";', r.cssText)
     36          self.assertEqual("href", r.href)
     37          self.assertEqual(None, r.hreftype)
     38          self.assertEqual(u'tv', r.media.mediaText)
     39          self.assertEqual(
     40              cssutils.stylesheets.MediaList, type(r.media))
     41          self.assertEqual('name', r.name)
     42          self.assertEqual(None, r.parentRule) # see CSSRule
     43          self.assertEqual(None, r.parentStyleSheet) # see CSSRule
     44          self.assertEqual(None, r.styleSheet)
     45
     46          # href
     47          r = cssutils.css.CSSImportRule(u'x')
     48          self.assertEqual(u'@import url(x);', r.cssText)
     49          self.assertEqual('x', r.href)
     50          self.assertEqual(None, r.hreftype)
     51
     52          # href + mediaText
     53          r = cssutils.css.CSSImportRule(u'x', u'print')
     54          self.assertEqual(u'@import url(x) print;', r.cssText)
     55          self.assertEqual('x', r.href)
     56          self.assertEqual('print', r.media.mediaText)
     57
     58          # href + name
     59          r = cssutils.css.CSSImportRule(u'x', name=u'n')
     60          self.assertEqual(u'@import url(x) "n";', r.cssText)
     61          self.assertEqual('x', r.href)
     62          self.assertEqual('n', r.name)
     63
     64          # href + mediaText + name
     65          r = cssutils.css.CSSImportRule(u'x', u'print', 'n')
     66          self.assertEqual(u'@import url(x) print "n";', r.cssText)
     67          self.assertEqual('x', r.href)
     68          self.assertEqual('print', r.media.mediaText)
     69          self.assertEqual('n', r.name)
     70
     71          # media +name only
     72          self.r = cssutils.css.CSSImportRule(mediaText=u'print', name="n")
     73          self.assertEqual(cssutils.stylesheets.MediaList,
     74                           type(self.r.media))
     75          self.assertEqual(u'', self.r.cssText)
     76          self.assertEqual(u'print', self.r.media.mediaText)
     77          self.assertEqual(u'n', self.r.name)
     78
     79          # only possible to set @... similar name
     80          self.assertRaises(xml.dom.InvalidModificationErr, self.r._setAtkeyword, 'x')
     81
     82 -    def test_cssText(self):
     83          "CSSImportRule.cssText"
     84          tests = {
     85              # href string
     86              u'''@import "str";''': None,
     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';''': u'''@import "str";''',
     91              u'''@import 'str' ;''': u'''@import "str";''',
     92              u'''@import "str";''': None,
     93              u'''@import "str"  ;''': u'''@import "str";''',
     94              ur'''@import "\""  ;''': ur'''@import "\"";''',
     95              u'''@import '\\'';''': ur'''@import "'";''',
     96              u'''@import '"';''': ur'''@import "\"";''',
     97              # href url
     98              u'''@import url(x.css);''': None,
     99              # nospace
    100              u'''@importurl(x.css);''': u'''@import url(x.css);''',
    101              u'''@import url(")");''': u'''@import url(")");''',
    102              u'''@import url("\\"");''': u'''@import url(");''',
    103              u'''@import url('\\'');''': u'''@import url(');''',
    104
    105              # href + media
    106              # all is removed
    107              u'''@import "str" all;''': u'''@import "str";''',
    108              u'''@import "str" tv, print;''': None,
    109              u'''@import"str"tv,print;''': u'''@import "str" tv, print;''',
    110              u'''@import "str" tv, print, all;''': u'''@import "str";''',
    111              u'''@import "str" handheld, all;''': u'''@import "str" all, handheld;''',
    112              u'''@import "str" all, handheld;''': None,
    113              u'''@import "str" not tv;''': None,
    114              u'''@import "str" only tv;''': None,
    115              u'''@import "str" only tv and (color: 2);''': None,
    116
    117              # href + name
    118              u'''@import "str" "name";''': None,
    119              u'''@import "str" 'name';''': u'''@import "str" "name";''',
    120              u'''@import url(x) "name";''': None,
    121              u'''@import "str" "\\"";''': None,
    122              u'''@import "str" '\\'';''': u'''@import "str" "'";''',
    123
    124              # href + media + name
    125              u'''@import"str"tv"name";''': u'''@import "str" tv "name";''',
    126              u'''@import\t\r\f\n"str"\t\t\r\f\ntv\t\t\r\f\n"name"\t;''':
    127                  u'''@import "str" tv "name";''',
    128
    129              # comments
    130              u'''@import /*1*/ "str" /*2*/;''': None,
    131              u'@import/*1*//*2*/"str"/*3*//*4*/all/*5*//*6*/"name"/*7*//*8*/ ;':
    132                  u'@import /*1*/ /*2*/ "str" /*3*/ /*4*/ all /*5*/ /*6*/ "name" /*7*/ /*8*/;',
    133              u'@import/*1*//*2*/url(u)/*3*//*4*/all/*5*//*6*/"name"/*7*//*8*/ ;':
    134                  u'@import /*1*/ /*2*/ url(u) /*3*/ /*4*/ all /*5*/ /*6*/ "name" /*7*/ /*8*/;',
    135              u'@import/*1*//*2*/url("u")/*3*//*4*/all/*5*//*6*/"name"/*7*//*8*/ ;':
    136                  u'@import /*1*/ /*2*/ url(u) /*3*/ /*4*/ all /*5*/ /*6*/ "name" /*7*/ /*8*/;',
    137              # WS
    138              u'@import\n\t\f "str"\n\t\f tv\n\t\f "name"\n\t\f ;':
    139                  u'@import "str" tv "name";',
    140              u'@import\n\t\f url(\n\t\f u\n\t\f )\n\t\f tv\n\t\f "name"\n\t\f ;':
    141                  u'@import url(u) tv "name";',
    142              u'@import\n\t\f url("u")\n\t\f tv\n\t\f "name"\n\t\f ;':
    143                  u'@import url(u) tv "name";',
    144              u'@import\n\t\f url(\n\t\f "u"\n\t\f )\n\t\f tv\n\t\f "name"\n\t\f ;':
    145                  u'@import url(u) tv "name";',
    146              }
    147          self.do_equal_r(tests) # set cssText
    148          tests.update({
    149              u'@import "x.css" tv': '@import "x.css" tv;',
    150              u'@import "x.css"': '@import "x.css";', # no ;
    151              u"@import 'x.css'": '@import "x.css";', # no ;
    152              u'@import url(x.css)': '@import url(x.css);', # no ;
    153              u'@import "x;': '@import "x;";', # no "!
    154              })
    155          self.do_equal_p(tests) # parse
    156
    157          tests = {
    158              u'''@import;''': xml.dom.SyntaxErr,
    159              u'''@import all;''': xml.dom.SyntaxErr,
    160              u'''@import all"name";''': xml.dom.SyntaxErr,
    161              u'''@import;''': xml.dom.SyntaxErr,
    162              u'''@import x";''': xml.dom.SyntaxErr,
    163              u'''@import "str" ,all;''': xml.dom.SyntaxErr,
    164              u'''@import "str" all,;''': xml.dom.SyntaxErr,
    165              u'''@import "str" all tv;''': xml.dom.SyntaxErr,
    166              u'''@import "str" "name" all;''': xml.dom.SyntaxErr,
    167              }
    168          self.do_raise_p(tests) # parse
    169          tests.update({
    170              u'@import "x.css"': xml.dom.SyntaxErr,
    171              u"@import 'x.css'": xml.dom.SyntaxErr,
    172              u'@import url(x.css)': xml.dom.SyntaxErr,
    173              u'@import "x.css" tv': xml.dom.SyntaxErr,
    174              u'@import "x;': xml.dom.SyntaxErr,
    175              u'''@import url("x);''': xml.dom.SyntaxErr,
    176              # trailing
    177              u'''@import "x";"a"''': xml.dom.SyntaxErr,
    178              # trailing S or COMMENT
    179              u'''@import "x";/**/''': xml.dom.SyntaxErr,
    180              u'''@import "x"; ''': xml.dom.SyntaxErr,
    181              })
    182          self.do_raise_r(tests) # set cssText
    183
    184 -    def test_href(self):
    185          "CSSImportRule.href"
    186          # set
    187          self.r.href = 'x'
    188          self.assertEqual('x', self.r.href)
    189          self.assertEqual(u'@import url(x);', self.r.cssText)
    190
    191          # http
    192          self.r.href = 'http://www.example.com/x?css=z&v=1'
    193          self.assertEqual('http://www.example.com/x?css=z&v=1' , self.r.href)
    194          self.assertEqual(u'@import url(http://www.example.com/x?css=z&v=1);',
    195                           self.r.cssText)
    196
    197          # also if hreftype changed
    198          self.r.hreftype='string'
    199          self.assertEqual('http://www.example.com/x?css=z&v=1' , self.r.href)
    200          self.assertEqual(u'@import "http://www.example.com/x?css=z&v=1";',
    201                           self.r.cssText)
    202
    203          # string escaping?
    204          self.r.href = '"'
    205          self.assertEqual(u'@import "\\"";', self.r.cssText)
    206          self.r.hreftype='url'
    207          self.assertEqual(u'@import url(");', self.r.cssText)
    208
    209          # url escaping?
    210          self.r.href = ')'
    211          self.assertEqual(u'@import url(")");', self.r.cssText)
    212
    213          self.r.hreftype = 'NOT VALID' # using default
    214          self.assertEqual(u'@import url(")");', self.r.cssText)
    215
    216 -    def test_hreftype(self):
    217          "CSSImportRule.hreftype"
    218          self.r = cssutils.css.CSSImportRule()
    219
    220          self.r.cssText = '@import /*1*/url(org) /*2*/;'
    221          self.assertEqual('uri', self.r.hreftype)
    222          self.assertEqual(u'@import /*1*/ url(org) /*2*/;', self.r.cssText)
    223
    224          self.r.cssText = '@import /*1*/"org" /*2*/;'
    225          self.assertEqual('string', self.r.hreftype)
    226          self.assertEqual(u'@import /*1*/ "org" /*2*/;', self.r.cssText)
    227
    228          self.r.href = 'new'
    229          self.assertEqual(u'@import /*1*/ "new" /*2*/;', self.r.cssText)
    230
    231          self.r.hreftype='uri'
    232          self.assertEqual(u'@import /*1*/ url(new) /*2*/;', self.r.cssText)
    233
    234 -    def test_media(self):
    235          "CSSImportRule.media"
    236          self.r.href = 'x' # @import url(x)
    237
    238          # media is readonly
    239          self.assertRaises(AttributeError, self.r.__setattr__, 'media', None)
    240
    241          # but not static
    242          self.r.media.mediaText = 'print'
    243          self.assertEqual(u'@import url(x) print;', self.r.cssText)
    244
    245 -    def test_name(self):
    246          "CSSImportRule.name"
    247          r = cssutils.css.CSSImportRule('x', name='0')
    248          self.assertEqual('0', r.name)
    249          self.assertEqual(u'@import url(x) "0";', r.cssText)
    250
    251          r.name = "n"
    252          self.assertEqual('n', r.name)
    253          self.assertEqual(u'@import url(x) "n";', r.cssText)
    254          r.name = '"'
    255          self.assertEqual('"', r.name)
    256          self.assertEqual(u'@import url(x) "\\"";', r.cssText)
    257
    258          r.hreftype = 'string'
    259          self.assertEqual(u'@import "x" "\\"";', r.cssText)
    260          r.name = "123"
    261          self.assertEqual(u'@import "x" "123";', r.cssText)
    262
    263          r.name = None
    264          self.assertEqual(None, r.name)
    265          self.assertEqual(u'@import "x";', r.cssText)
    266
    267          r.name = ""
    268          self.assertEqual(None, r.name)
    269          self.assertEqual(u'@import "x";', r.cssText)
    270
    271          self.assertRaises(xml.dom.SyntaxErr, r._setName, 0)
    272          self.assertRaises(xml.dom.SyntaxErr, r._setName, 123)
    273
    274 -    def test_styleSheet(self):
    275          "CSSImportRule.styleSheet"
    276          i = [-1] #
    277          examples = [
    278              '@import "level2/css.css" "title2";',
    279              'a { color: red }'
    280          ]
    281          def m(*p, **kw):
    282              i[0] += 1
    283              return examples[i[0]]
    284          mock('cssutils.util._readURL', mock_obj=m)
    285          sheet = cssutils.parseString('@import "level1/anything.css" tv "title";',
    286                                       href='/root/')
    287          restore()
    288
    289          self.assertEqual(sheet.href, '/root/')
    290
    291          ir = sheet.cssRules[0]
    292          self.assertEqual(ir.href, 'level1/anything.css')
    293          self.assertEqual(ir.styleSheet.href, '/root/level1/anything.css')
    294          self.assertEqual(ir.styleSheet.ownerRule, ir)
    295          self.assertEqual(ir.styleSheet.media.mediaText, 'tv')
    296          self.assertEqual(ir.styleSheet.parentStyleSheet, sheet)
    297          self.assertEqual(ir.styleSheet.title, 'title')
    298          self.assertEqual(ir.styleSheet.cssText, '@import "level2/css.css" "title2";')
    299
    300          ir2 = ir.styleSheet.cssRules[0]
    301          self.assertEqual(ir2.href, 'level2/css.css')
    302          self.assertEqual(ir2.styleSheet.href, '/root/level1/level2/css.css')
    303          self.assertEqual(ir2.styleSheet.ownerRule, ir2)
    304          self.assertEqual(ir2.styleSheet.media.mediaText, 'all')
    305          self.assertEqual(ir2.styleSheet.parentStyleSheet, ir.styleSheet)
    306          self.assertEqual(ir2.styleSheet.title, 'title2')
    307          self.assertEqual(ir2.styleSheet.cssText, 'a {\n    color: red\n    }')
    308
    309          sheet = cssutils.parseString('@import "CANNOT-FIND.css";')
    310          ir = sheet.cssRules[0]
    311          self.assertEqual(ir.href, "CANNOT-FIND.css")
    312          self.assertEqual(ir.styleSheet, None)
    313
    314 -    def test_incomplete(self):
    315          "CSSImportRule (incomplete)"
    316          tests = {
    317              u'@import "x.css': u'@import "x.css";',
    318              u"@import 'x": u'@import "x";',
    319              # TODO:
    320              u"@import url(x": u'@import url(x);',
    321              u"@import url('x": u'@import url(x);',
    322              u'@import url("x;': u'@import url("x;");',
    323              u'@import url( "x;': u'@import url("x;");',
    324              u'@import url("x ': u'@import url("x ");',
    325              u'@import url(x ': u'@import url(x);',
    326              u'''@import "a
    327                  @import "b";
    328                  @import "c";''': u'@import "c";'
    329          }
    330          self.do_equal_p(tests, raising=False) # parse
    331
    332 -    def test_InvalidModificationErr(self):
    333          "CSSImportRule.cssText InvalidModificationErr"
    334          self._test_InvalidModificationErr(u'@import')
    335
    336 -    def test_reprANDstr(self):
    337          "CSSImportRule.__repr__(), .__str__()"
    338          href = 'x.css'
    339          mediaText = 'tv, print'
    340          name = 'name'
    341          s = cssutils.css.CSSImportRule(href=href, mediaText=mediaText, name=name)
    342
    343          # str(): mediaText nor name are present here
    344          self.assert_(href in str(s))
    345
    346          # repr()
    347          s2 = eval(repr(s))
    348          self.assert_(isinstance(s2, s.__class__))
    349          self.assert_(href == s2.href)
    350          self.assert_(mediaText == s2.media.mediaText)
    351          self.assert_(name == s2.name)
    352
    353
    354  if __name__ == '__main__':
    355      import unittest
    356      unittest.main()
    357