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