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 1152 2008-03-18 22:36:39Z 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 -    def test_cssText(self):
     80          "CSSImportRule.cssText"
     81          tests = {
     82              # href string
     83              u'''@import "str";''': None,
     84              u'''@import"str";''': u'''@import "str";''',
     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";''': None,
     90              u'''@import "str"  ;''': u'''@import "str";''',
     91              ur'''@import "\""  ;''': ur'''@import "\"";''',
     92              u'''@import '\\'';''': ur'''@import "'";''',
     93              u'''@import '"';''': ur'''@import "\"";''',
     94              # href url
     95              u'''@import url(x.css);''': None,
     96              # nospace
     97              u'''@importurl(x.css);''': u'''@import url(x.css);''',
     98              u'''@import url(")");''': u'''@import url(")");''',
     99              u'''@import url("\\"");''': u'''@import url(");''',
    100              u'''@import url('\\'');''': u'''@import url(');''',
    101
    102              # href + media
    103              # all is removed
    104              u'''@import "str" all;''': u'''@import "str";''',
    105              u'''@import "str" tv, print;''': None,
    106              u'''@import"str"tv,print;''': u'''@import "str" tv, print;''',
    107              u'''@import "str" tv, print, all;''': u'''@import "str";''',
    108              u'''@import "str" handheld, all;''': u'''@import "str" all, handheld;''',
    109              u'''@import "str" all, handheld;''': None,
    110              u'''@import "str" not tv;''': None,
    111              u'''@import "str" only tv;''': None,
    112              u'''@import "str" only tv and (color: 2);''': None,
    113
    114              # href + name
    115              u'''@import "str" "name";''': None,
    116              u'''@import "str" 'name';''': u'''@import "str" "name";''',
    117              u'''@import url(x) "name";''': None,
    118              u'''@import "str" "\\"";''': None,
    119              u'''@import "str" '\\'';''': u'''@import "str" "'";''',
    120
    121              # href + media + name
    122              u'''@import"str"tv"name";''': u'''@import "str" tv "name";''',
    123              u'''@import\t\r\f\n"str"\t\t\r\f\ntv\t\t\r\f\n"name"\t;''':
    124                  u'''@import "str" tv "name";''',
    125
    126              # comments
    127              u'''@import /*1*/ "str" /*2*/;''': None,
    128              u'@import/*1*//*2*/"str"/*3*//*4*/all/*5*//*6*/"name"/*7*//*8*/ ;':
    129                  u'@import /*1*/ /*2*/ "str" /*3*/ /*4*/ all /*5*/ /*6*/ "name" /*7*/ /*8*/;',
    130              u'@import/*1*//*2*/url(u)/*3*//*4*/all/*5*//*6*/"name"/*7*//*8*/ ;':
    131                  u'@import /*1*/ /*2*/ url(u) /*3*/ /*4*/ all /*5*/ /*6*/ "name" /*7*/ /*8*/;',
    132              u'@import/*1*//*2*/url("u")/*3*//*4*/all/*5*//*6*/"name"/*7*//*8*/ ;':
    133                  u'@import /*1*/ /*2*/ url(u) /*3*/ /*4*/ all /*5*/ /*6*/ "name" /*7*/ /*8*/;',
    134              # WS
    135              u'@import\n\t\f "str"\n\t\f tv\n\t\f "name"\n\t\f ;':
    136                  u'@import "str" tv "name";',
    137              u'@import\n\t\f url(\n\t\f u\n\t\f )\n\t\f tv\n\t\f "name"\n\t\f ;':
    138                  u'@import url(u) tv "name";',
    139              u'@import\n\t\f url("u")\n\t\f tv\n\t\f "name"\n\t\f ;':
    140                  u'@import url(u) tv "name";',
    141              u'@import\n\t\f url(\n\t\f "u"\n\t\f )\n\t\f tv\n\t\f "name"\n\t\f ;':
    142                  u'@import url(u) tv "name";',
    143              }
    144          self.do_equal_r(tests) # set cssText
    145          tests.update({
    146              u'@import "x.css" tv': '@import "x.css" tv;',
    147              u'@import "x.css"': '@import "x.css";', # no ;
    148              u"@import 'x.css'": '@import "x.css";', # no ;
    149              u'@import url(x.css)': '@import url(x.css);', # no ;
    150              u'@import "x;': '@import "x;";', # no "!
    151              })
    152          self.do_equal_p(tests) # parse
    153
    154          tests = {
    155              u'''@import;''': xml.dom.SyntaxErr,
    156              u'''@import all;''': xml.dom.SyntaxErr,
    157              u'''@import all"name";''': xml.dom.SyntaxErr,
    158              u'''@import;''': xml.dom.SyntaxErr,
    159              u'''@import x";''': xml.dom.SyntaxErr,
    160              u'''@import "str" ,all;''': xml.dom.SyntaxErr,
    161              u'''@import "str" all,;''': xml.dom.SyntaxErr,
    162              u'''@import "str" all tv;''': xml.dom.SyntaxErr,
    163              u'''@import "str" "name" all;''': xml.dom.SyntaxErr,
    164              }
    165          self.do_raise_p(tests) # parse
    166          tests.update({
    167              u'@import "x.css"': xml.dom.SyntaxErr,
    168              u"@import 'x.css'": xml.dom.SyntaxErr,
    169              u'@import url(x.css)': xml.dom.SyntaxErr,
    170              u'@import "x.css" tv': xml.dom.SyntaxErr,
    171              u'@import "x;': xml.dom.SyntaxErr,
    172              u'''@import url("x);''': xml.dom.SyntaxErr,
    173              # trailing
    174              u'''@import "x";"a"''': xml.dom.SyntaxErr,
    175              # trailing S or COMMENT
    176              u'''@import "x";/**/''': xml.dom.SyntaxErr,
    177              u'''@import "x"; ''': xml.dom.SyntaxErr,
    178              })
    179          self.do_raise_r(tests) # set cssText
    180
    181 -    def test_href(self):
    182          "CSSImportRule.href"
    183          # set
    184          self.r.href = 'x'
    185          self.assertEqual('x', self.r.href)
    186          self.assertEqual(u'@import url(x);', self.r.cssText)
    187
    188          # http
    189          self.r.href = 'http://www.example.com/x?css=z&v=1'
    190          self.assertEqual('http://www.example.com/x?css=z&v=1' , self.r.href)
    191          self.assertEqual(u'@import url(http://www.example.com/x?css=z&v=1);',
    192                           self.r.cssText)
    193
    194          # also if hreftype changed
    195          self.r.hreftype='string'
    196          self.assertEqual('http://www.example.com/x?css=z&v=1' , self.r.href)
    197          self.assertEqual(u'@import "http://www.example.com/x?css=z&v=1";',
    198                           self.r.cssText)
    199
    200          # string escaping?
    201          self.r.href = '"'
    202          self.assertEqual(u'@import "\\"";', self.r.cssText)
    203          self.r.hreftype='url'
    204          self.assertEqual(u'@import url(");', self.r.cssText)
    205
    206          # url escaping?
    207          self.r.href = ')'
    208          self.assertEqual(u'@import url(")");', self.r.cssText)
    209
    210          self.r.hreftype = 'NOT VALID' # using default
    211          self.assertEqual(u'@import url(")");', self.r.cssText)
    212
    213 -    def test_hreftype(self):
    214          "CSSImportRule.hreftype"
    215          self.r = cssutils.css.CSSImportRule()
    216
    217          self.r.cssText = '@import /*1*/url(org) /*2*/;'
    218          self.assertEqual('uri', self.r.hreftype)
    219          self.assertEqual(u'@import /*1*/ url(org) /*2*/;', self.r.cssText)
    220
    221          self.r.cssText = '@import /*1*/"org" /*2*/;'
    222          self.assertEqual('string', self.r.hreftype)
    223          self.assertEqual(u'@import /*1*/ "org" /*2*/;', self.r.cssText)
    224
    225          self.r.href = 'new'
    226          self.assertEqual(u'@import /*1*/ "new" /*2*/;', self.r.cssText)
    227
    228          self.r.hreftype='uri'
    229          self.assertEqual(u'@import /*1*/ url(new) /*2*/;', self.r.cssText)
    230
    231 -    def test_media(self):
    232          "CSSImportRule.media"
    233          self.r.href = 'x' # @import url(x)
    234
    235          # media is readonly
    236          self.assertRaises(AttributeError, self.r.__setattr__, 'media', None)
    237
    238          # but not static
    239          self.r.media.mediaText = 'print'
    240          self.assertEqual(u'@import url(x) print;', self.r.cssText)
    241
    242 -    def test_name(self):
    243          "CSSImportRule.name"
    244          r = cssutils.css.CSSImportRule('x')
    245
    246          r.name = "n"
    247          self.assertEqual('n', r.name)
    248          self.assertEqual(u'@import url(x) "n";', r.cssText)
    249          r.name = '"'
    250          self.assertEqual('"', r.name)
    251          self.assertEqual(u'@import url(x) "\\"";', r.cssText)
    252
    253 -    def test_styleSheet(self):
    254          "CSSImportRule.styleSheet"
    255          i = [-1] #
    256          examples = [
    257              '@import "level2/css.css" "title2";',
    258              'a { color: red }'
    259          ]
    260          def m(*p, **kw):
    261              i[0] += 1
    262              return examples[i[0]]
    263          mock('cssutils.util._readURL', mock_obj=m)
    264          sheet = cssutils.parseString('@import "level1/anything.css" tv "title";',
    265                                       href='/root/')
    266          restore()
    267
    268          self.assertEqual(sheet.href, '/root/')
    269
    270          ir = sheet.cssRules[0]
    271          self.assertEqual(ir.href, 'level1/anything.css')
    272          self.assertEqual(ir.styleSheet.href, '/root/level1/anything.css')
    273          self.assertEqual(ir.styleSheet.ownerRule, ir)
    274          self.assertEqual(ir.styleSheet.media.mediaText, 'tv')
    275          self.assertEqual(ir.styleSheet.parentStyleSheet, sheet)
    276          self.assertEqual(ir.styleSheet.title, 'title')
    277          self.assertEqual(ir.styleSheet.cssText, '@import "level2/css.css" "title2";')
    278
    279          ir2 = ir.styleSheet.cssRules[0]
    280          self.assertEqual(ir2.href, 'level2/css.css')
    281          self.assertEqual(ir2.styleSheet.href, '/root/level1/level2/css.css')
    282          self.assertEqual(ir2.styleSheet.ownerRule, ir2)
    283          self.assertEqual(ir2.styleSheet.media.mediaText, 'all')
    284          self.assertEqual(ir2.styleSheet.parentStyleSheet, ir.styleSheet)
    285          self.assertEqual(ir2.styleSheet.title, 'title2')
    286          self.assertEqual(ir2.styleSheet.cssText, 'a {\n    color: red\n    }')
    287
    288          sheet = cssutils.parseString('@import "CANNOT-FIND.css";')
    289          ir = sheet.cssRules[0]
    290          self.assertEqual(ir.href, "CANNOT-FIND.css")
    291          self.assertEqual(ir.styleSheet, None)
    292
    293 -    def test_incomplete(self):
    294          "CSSImportRule (incomplete)"
    295          tests = {
    296              u'@import "x.css': u'@import "x.css";',
    297              u"@import 'x": u'@import "x";',
    298              # TODO:
    299              u"@import url(x": u'@import url(x);',
    300              u"@import url('x": u'@import url(x);',
    301              u'@import url("x;': u'@import url("x;");',
    302              u'@import url( "x;': u'@import url("x;");',
    303              u'@import url("x ': u'@import url("x ");',
    304              u'@import url(x ': u'@import url(x);',
    305              u'''@import "a
    306                  @import "b";
    307                  @import "c";''': u'@import "c";'
    308          }
    309          self.do_equal_p(tests, raising=False) # parse
    310
    311 -    def test_InvalidModificationErr(self):
    312          "CSSImportRule.cssText InvalidModificationErr"
    313          self._test_InvalidModificationErr(u'@import')
    314
    315 -    def test_reprANDstr(self):
    316          "CSSImportRule.__repr__(), .__str__()"
    317          href = 'x.css'
    318          mediaText = 'tv, print'
    319          name = 'name'
    320          s = cssutils.css.CSSImportRule(href=href, mediaText=mediaText, name=name)
    321
    322          # str(): mediaText nor name are present here
    323          self.assert_(href in str(s))
    324
    325          # repr()
    326          s2 = eval(repr(s))
    327          self.assert_(isinstance(s2, s.__class__))
    328          self.assert_(href == s2.href)
    329          self.assert_(mediaText == s2.media.mediaText)
    330          self.assert_(name == s2.name)
    331
    332
    333  if __name__ == '__main__':
    334      import unittest
    335      unittest.main()
    336