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

# Source Code for Module cssutils.tests.test_cssnamespacerule

      1  """Testcases for cssutils.css.CSSImportRule"""
      2  __version__ = '$Id: test_cssnamespacerule.py 1175 2008-03-20 17:43:26Z cthedot $'
      3
      4  import xml.dom
      5  import test_cssrule
      6  import cssutils
      7
      8 -class CSSNamespaceRuleTestCase(test_cssrule.CSSRuleTestCase):
      9
     10 -    def setUp(self):
     11          super(CSSNamespaceRuleTestCase, self).setUp()
     12          self.r = cssutils.css.CSSNamespaceRule(namespaceURI='x')
     13          #self.rRO = cssutils.css.CSSNamespaceRule(namespaceURI='x',
     14          #                                         readonly=True)
     15          self.r_type = cssutils.css.CSSRule.NAMESPACE_RULE
     16          self.r_typeString = 'NAMESPACE_RULE'
     17
     18 -    def test_init(self):
     19          "CSSNamespaceRule.__init__()"
     20          # cannot use here as self.r and self rRO and not useful
     21          #super(CSSNamespaceRuleTestCase, self).test_init()
     22          tests = [
     23                   (None, None),
     24                   ('', ''),
     25                   (None, u''),
     26                   (u'', None),
     27                   (u'', u'no-uri'),
     28                   ]
     29          for uri, p in tests:
     30              r = cssutils.css.CSSNamespaceRule(namespaceURI=uri, prefix=p)
     31              self.assertEqual(None, r.namespaceURI)
     32              self.assertEqual(u'', r.prefix)
     33              self.assertEqual(u'', r.cssText)
     34              self.assertEqual(None, r.parentStyleSheet)
     35              self.assertEqual(None, r.parentRule)
     36
     37          r = cssutils.css.CSSNamespaceRule(namespaceURI='example')
     38          self.assertEqual('example', r.namespaceURI)
     39          self.assertEqual(u'', r.prefix)
     40          self.assertEqual(u'@namespace "example";', r.cssText)
     41          self.sheet.add(r)
     42          self.assertEqual(self.sheet, r.parentStyleSheet)
     43
     44          r = cssutils.css.CSSNamespaceRule(namespaceURI='example', prefix='p')
     45          self.assertEqual('example', r.namespaceURI)
     46          self.assertEqual(u'p', r.prefix)
     47          self.assertEqual(u'@namespace p "example";', r.cssText)
     48
     49          css = u'@namespace p "u";'
     50          r = cssutils.css.CSSNamespaceRule(cssText=css)
     51          self.assertEqual(r.cssText, css)
     52
     53          # only possible to set @... similar name
     54          self.assertRaises(xml.dom.InvalidModificationErr, self.r._setAtkeyword, 'x')
     55
     56 -    def test_cssText(self):
     57          "CSSNamespaceRule.cssText"
     58          # cssText may only be set initalially
     59          r = cssutils.css.CSSNamespaceRule()
     60          css = u'@namespace p "u";'
     61          r.cssText = css
     62          self.assertEqual(r.cssText, css)
     63          self.assertRaises(xml.dom.NoModificationAllowedErr, r._setCssText,
     64                            u'@namespace p "OTHER";')
     65
     66          tests = {
     67              u'@namespace "";': None,
     68              u'@namespace "u";': None,
     69              u'@namespace p "u";': None,
     70              u'@namespace empty "";': None,
     71              u'@namespacep"u";': u'@namespace p "u";',
     72              u'@namespaceempty"";': u'@namespace empty "";',
     73              u'@namespacep"u"   ;': u'@namespace p "u";',
     74
     75              u'@namespace p "p";': None,
     76              u"@namespace p 'u';": u'@namespace p "u";',
     77
     78              u'@\\namespace p "u";': u'@namespace p "u";',
     79              u'@NAMESPACE p "u";': u'@namespace p "u";',
     80
     81              u'@namespace  p  "u"  ;': u'@namespace p "u";',
     82              u'@namespace p"u";': u'@namespace p "u";',
     83              u'@namespace p "u";': u'@namespace p "u";',
     84
     85              u'@namespace/*1*/"u"/*2*/;': u'@namespace /*1*/ "u" /*2*/;',
     86              u'@namespace/*1*/p/*2*/"u"/*3*/;': u'@namespace /*1*/ p /*2*/ "u" /*3*/;',
     87
     88              u'@namespace p url(u);': u'@namespace p "u";',
     89              u'@namespace p url(\'u\');': u'@namespace p "u";',
     90              u'@namespace p url(\"u\");': u'@namespace p "u";',
     91              u'@namespace p url( \"u\" );': u'@namespace p "u";',
     92
     93              # comments
     94              u'@namespace/*1*//*2*/p/*3*//*4*/url(u)/*5*//*6*/;':
     95                  u'@namespace /*1*/ /*2*/ p /*3*/ /*4*/ "u" /*5*/ /*6*/;',
     96              u'@namespace/*1*//*2*/p/*3*//*4*/"u"/*5*//*6*/;':
     97                  u'@namespace /*1*/ /*2*/ p /*3*/ /*4*/ "u" /*5*/ /*6*/;',
     98              u'@namespace/*1*//*2*/p/*3*//*4*/url("u")/*5*//*6*/;':
     99                  u'@namespace /*1*/ /*2*/ p /*3*/ /*4*/ "u" /*5*/ /*6*/;',
    100
    101              u'@namespace/*1*//*2*/url(u)/*5*//*6*/;':
    102                  u'@namespace /*1*/ /*2*/ "u" /*5*/ /*6*/;',
    103
    104              # WS
    105              u'@namespace\n\r\t\f p\n\r\t\f url(\n\r\t\f u\n\r\t\f )\n\r\t\f ;':
    106                  u'@namespace p "u";',
    107              u'@namespace\n\r\t\f p\n\r\t\f url(\n\r\t\f "u"\n\r\t\f )\n\r\t\f ;':
    108                  u'@namespace p "u";',
    109              u'@namespace\n\r\t\f p\n\r\t\f "str"\n\r\t\f ;':
    110                  u'@namespace p "str";',
    111              u'@namespace\n\r\t\f "str"\n\r\t\f ;':
    112                  u'@namespace "str";',
    113              }
    114          self.do_equal_p(tests)
    115          #self.do_equal_r(tests) # cannot use here as always new r is needed
    116          for test, expected in tests.items():
    117              r = cssutils.css.CSSNamespaceRule(cssText=test)
    118              if expected is None:
    119                  expected = test
    120              self.assertEqual(expected, r.cssText)
    121
    122          tests = {
    123              u'@namespace;': xml.dom.SyntaxErr, # nothing
    124              u'@namespace p;': xml.dom.SyntaxErr, # no namespaceURI
    125              u'@namespace "u" p;': xml.dom.SyntaxErr, # order
    126              u'@namespace "u";EXTRA': xml.dom.SyntaxErr,
    127              u'@namespace p "u";EXTRA': xml.dom.SyntaxErr,
    128              }
    129          self.do_raise_p(tests) # parse
    130          tests.update({
    131              u'@namespace p url(x)': xml.dom.SyntaxErr, # missing ;
    132              u'@namespace p "u"': xml.dom.SyntaxErr, # missing ;
    133              # trailing
    134              u'@namespace "u"; ': xml.dom.SyntaxErr,
    135              u'@namespace "u";/**/': xml.dom.SyntaxErr,
    136              u'@namespace p "u"; ': xml.dom.SyntaxErr,
    137              u'@namespace p "u";/**/': xml.dom.SyntaxErr,
    138              })
    139          def _do(test):
    140              r = cssutils.css.CSSNamespaceRule(cssText=test)
    141          for test, expected in tests.items():
    142              self.assertRaises(expected, _do, test)
    143
    144 -    def test_namespaceURI(self):
    145          "CSSNamespaceRule.namespaceURI"
    146          # set only initially
    147          r = cssutils.css.CSSNamespaceRule(namespaceURI='x')
    148          self.assertEqual('x' , r.namespaceURI)
    149          self.assertEqual(u'@namespace "x";', r.cssText)
    150
    151          r = cssutils.css.CSSNamespaceRule(namespaceURI='"')
    152          self.assertEqual(u'@namespace "\\"";', r.cssText)
    153
    154          self.assertRaises(xml.dom.NoModificationAllowedErr,
    155                            r._setNamespaceURI, 'x')
    156
    157          self.assertRaises(xml.dom.NoModificationAllowedErr,
    158                            r._setCssText, '@namespace "u";')
    159
    160 -    def test_prefix(self):
    161          "CSSNamespaceRule.prefix"
    162          r = cssutils.css.CSSNamespaceRule(namespaceURI='u')
    163          r.prefix = 'p'
    164          self.assertEqual('p' , r.prefix)
    165          self.assertEqual(u'@namespace p "u";', r.cssText)
    166
    167          r = cssutils.css.CSSNamespaceRule(cssText='@namespace x "u";')
    168          r.prefix = 'p'
    169          self.assertEqual('p' , r.prefix)
    170          self.assertEqual(u'@namespace p "u";', r.cssText)
    171
    172          valid = (None, u'')
    173          for prefix in valid:
    174              r.prefix = prefix
    175              self.assertEqual(r.prefix, u'')
    176              self.assertEqual(u'@namespace "u";', r.cssText)
    177
    178          valid = ('a', '_x', 'a1', 'a-1')
    179          for prefix in valid:
    180              r.prefix = prefix
    181              self.assertEqual(r.prefix, prefix)
    182              self.assertEqual(u'@namespace %s "u";' % prefix, r.cssText)
    183
    184          invalid = ('1', ' x', ' ', ',')
    185          for prefix in invalid:
    186              self.assertRaises(xml.dom.SyntaxErr, r._setPrefix, prefix)
    187
    188 -    def test_InvalidModificationErr(self):
    189          "CSSNamespaceRule.cssText InvalidModificationErr"
    190          self._test_InvalidModificationErr(u'@namespace')
    191
    192 -    def test_incomplete(self):
    193          "CSSNamespaceRule (incomplete)"
    194          tests = {
    195              u'@namespace "uri': u'@namespace "uri";',
    196              u"@namespace url(x": u'@namespace "x";',
    197              u"@namespace url('x": u'@namespace "x";',
    198              u'@namespace url("x;': u'@namespace "x;";',
    199              u'@namespace url( "x;': u'@namespace "x;";',
    200              u'@namespace url("x ': u'@namespace "x ";',
    201              u'@namespace url(x ': u'@namespace "x";',
    202          }
    203          self.do_equal_p(tests) # parse
    204          tests = {
    205              u'@namespace "uri': xml.dom.SyntaxErr,
    206              u"@namespace url(x": xml.dom.SyntaxErr,
    207              u"@namespace url('x": xml.dom.SyntaxErr,
    208              u'@namespace url("x;': xml.dom.SyntaxErr,
    209              u'@namespace url( "x;': xml.dom.SyntaxErr,
    210              u'@namespace url("x ': xml.dom.SyntaxErr,
    211              u'@namespace url(x ': xml.dom.SyntaxErr
    212              }
    213          self.do_raise_r(tests) # set cssText
    214
    215 -    def test_reprANDstr(self):
    216          "CSSNamespaceRule.__repr__(), .__str__()"
    217          namespaceURI=u'http://example.com'
    218          prefix=u'prefix'
    219
    220          s = cssutils.css.CSSNamespaceRule(namespaceURI=namespaceURI, prefix=prefix)
    221
    222          self.assert_(namespaceURI in str(s))
    223          self.assert_(prefix in str(s))
    224
    225          s2 = eval(repr(s))
    226          self.assert_(isinstance(s2, s.__class__))
    227          self.assert_(namespaceURI == s2.namespaceURI)
    228          self.assert_(prefix == s2.prefix)
    229
    230
    231  if __name__ == '__main__':
    232      import unittest
    233      unittest.main()
    234