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 1116 2008-03-05 13:52:23Z 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 -    def test_cssText(self):
     54          "CSSNamespaceRule.cssText"
     55          # cssText may only be set initalially
     56          r = cssutils.css.CSSNamespaceRule()
     57          css = u'@namespace p "u";'
     58          r.cssText = css
     59          self.assertEqual(r.cssText, css)
     60          self.assertRaises(xml.dom.NoModificationAllowedErr, r._setCssText,
     61                            u'@namespace p "OTHER";')
     62
     63          tests = {
     64              u'@namespace "";': None,
     65              u'@namespace "u";': None,
     66              u'@namespace p "u";': None,
     67              u'@namespace empty "";': None,
     68              u'@namespacep"u";': u'@namespace p "u";',
     69              u'@namespaceempty"";': u'@namespace empty "";',
     70              u'@namespacep"u"   ;': u'@namespace p "u";',
     71
     72              u'@namespace p "p";': None,
     73              u"@namespace p 'u';": u'@namespace p "u";',
     74
     75              u'@\\namespace p "u";': u'@namespace p "u";',
     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              u'@namespace p "u";': u'@namespace p "u";',
     81
     82              u'@namespace/*1*/"u"/*2*/;': u'@namespace /*1*/ "u" /*2*/;',
     83              u'@namespace/*1*/p/*2*/"u"/*3*/;': u'@namespace /*1*/ p /*2*/ "u" /*3*/;',
     84
     85              u'@namespace p url(u);': u'@namespace p "u";',
     86              u'@namespace p url(\'u\');': u'@namespace p "u";',
     87              u'@namespace p url(\"u\");': u'@namespace p "u";',
     88              u'@namespace p url( \"u\" );': u'@namespace p "u";',
     89
     90              # comments
     91              u'@namespace/*1*//*2*/p/*3*//*4*/url(u)/*5*//*6*/;':
     92                  u'@namespace /*1*/ /*2*/ p /*3*/ /*4*/ "u" /*5*/ /*6*/;',
     93              u'@namespace/*1*//*2*/p/*3*//*4*/"u"/*5*//*6*/;':
     94                  u'@namespace /*1*/ /*2*/ p /*3*/ /*4*/ "u" /*5*/ /*6*/;',
     95              u'@namespace/*1*//*2*/p/*3*//*4*/url("u")/*5*//*6*/;':
     96                  u'@namespace /*1*/ /*2*/ p /*3*/ /*4*/ "u" /*5*/ /*6*/;',
     97
     98              u'@namespace/*1*//*2*/url(u)/*5*//*6*/;':
     99                  u'@namespace /*1*/ /*2*/ "u" /*5*/ /*6*/;',
    100
    101              # WS
    102              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 ;':
    103                  u'@namespace p "u";',
    104              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 ;':
    105                  u'@namespace p "u";',
    106              u'@namespace\n\r\t\f p\n\r\t\f "str"\n\r\t\f ;':
    107                  u'@namespace p "str";',
    108              u'@namespace\n\r\t\f "str"\n\r\t\f ;':
    109                  u'@namespace "str";',
    110              }
    111          self.do_equal_p(tests)
    112          #self.do_equal_r(tests) # cannot use here as always new r is needed
    113          for test, expected in tests.items():
    114              r = cssutils.css.CSSNamespaceRule(cssText=test)
    115              if expected is None:
    116                  expected = test
    117              self.assertEqual(expected, r.cssText)
    118
    119          tests = {
    120              u'@namespace;': xml.dom.SyntaxErr, # nothing
    121              u'@namespace p;': xml.dom.SyntaxErr, # no namespaceURI
    122              u'@namespace "u" p;': xml.dom.SyntaxErr, # order
    123              u'@namespace "u";EXTRA': xml.dom.SyntaxErr,
    124              u'@namespace p "u";EXTRA': xml.dom.SyntaxErr,
    125              }
    126          self.do_raise_p(tests) # parse
    127          tests.update({
    128              u'@namespace p url(x)': xml.dom.SyntaxErr, # missing ;
    129              u'@namespace p "u"': xml.dom.SyntaxErr, # missing ;
    130              # trailing
    131              u'@namespace "u"; ': xml.dom.SyntaxErr,
    132              u'@namespace "u";/**/': xml.dom.SyntaxErr,
    133              u'@namespace p "u"; ': xml.dom.SyntaxErr,
    134              u'@namespace p "u";/**/': xml.dom.SyntaxErr,
    135              })
    136          def _do(test):
    137              r = cssutils.css.CSSNamespaceRule(cssText=test)
    138          for test, expected in tests.items():
    139              self.assertRaises(expected, _do, test)
    140
    141 -    def test_namespaceURI(self):
    142          "CSSNamespaceRule.namespaceURI"
    143          # set only initially
    144          r = cssutils.css.CSSNamespaceRule(namespaceURI='x')
    145          self.assertEqual('x' , r.namespaceURI)
    146          self.assertEqual(u'@namespace "x";', r.cssText)
    147
    148          r = cssutils.css.CSSNamespaceRule(namespaceURI='"')
    149          self.assertEqual(u'@namespace "\\"";', r.cssText)
    150
    151          self.assertRaises(xml.dom.NoModificationAllowedErr,
    152                            r._setNamespaceURI, 'x')
    153
    154          self.assertRaises(xml.dom.NoModificationAllowedErr,
    155                            r._setCssText, '@namespace "u";')
    156
    157 -    def test_prefix(self):
    158          "CSSNamespaceRule.prefix"
    159          r = cssutils.css.CSSNamespaceRule(namespaceURI='u')
    160          r.prefix = 'p'
    161          self.assertEqual('p' , r.prefix)
    162          self.assertEqual(u'@namespace p "u";', r.cssText)
    163
    164          r = cssutils.css.CSSNamespaceRule(cssText='@namespace x "u";')
    165          r.prefix = 'p'
    166          self.assertEqual('p' , r.prefix)
    167          self.assertEqual(u'@namespace p "u";', r.cssText)
    168
    169          valid = (None, u'')
    170          for prefix in valid:
    171              r.prefix = prefix
    172              self.assertEqual(r.prefix, u'')
    173              self.assertEqual(u'@namespace "u";', r.cssText)
    174
    175          valid = ('a', '_x', 'a1', 'a-1')
    176          for prefix in valid:
    177              r.prefix = prefix
    178              self.assertEqual(r.prefix, prefix)
    179              self.assertEqual(u'@namespace %s "u";' % prefix, r.cssText)
    180
    181          invalid = ('1', ' x', ' ', ',')
    182          for prefix in invalid:
    183              self.assertRaises(xml.dom.SyntaxErr, r._setPrefix, prefix)
    184
    185 -    def test_InvalidModificationErr(self):
    186          "CSSNamespaceRule.cssText InvalidModificationErr"
    187          self._test_InvalidModificationErr(u'@namespace')
    188
    189 -    def test_incomplete(self):
    190          "CSSNamespaceRule (incomplete)"
    191          tests = {
    192              u'@namespace "uri': u'@namespace "uri";',
    193              u"@namespace url(x": u'@namespace "x";',
    194              u"@namespace url('x": u'@namespace "x";',
    195              u'@namespace url("x;': u'@namespace "x;";',
    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          }
    200          self.do_equal_p(tests) # parse
    201          tests = {
    202              u'@namespace "uri': xml.dom.SyntaxErr,
    203              u"@namespace url(x": xml.dom.SyntaxErr,
    204              u"@namespace url('x": xml.dom.SyntaxErr,
    205              u'@namespace url("x;': 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              }
    210          self.do_raise_r(tests) # set cssText
    211
    212 -    def test_reprANDstr(self):
    213          "CSSNamespaceRule.__repr__(), .__str__()"
    214          namespaceURI=u'http://example.com'
    215          prefix=u'prefix'
    216
    217          s = cssutils.css.CSSNamespaceRule(namespaceURI=namespaceURI, prefix=prefix)
    218
    219          self.assert_(namespaceURI in str(s))
    220          self.assert_(prefix in str(s))
    221
    222          s2 = eval(repr(s))
    223          self.assert_(isinstance(s2, s.__class__))
    224          self.assert_(namespaceURI == s2.namespaceURI)
    225          self.assert_(prefix == s2.prefix)
    226
    227
    228  if __name__ == '__main__':
    229      import unittest
    230      unittest.main()
    231