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

# Source Code for Module cssutils.tests.test_property

      1  """Testcases for cssutils.css.property._Property."""
      2  __author__ = '$LastChangedBy: cthedot $'
      3  __date__ = '$LastChangedDate: 2008-02-12 23:43:05 +0100 (Di, 12 Feb 2008) $'
      4  __version__ = '$LastChangedRevision: 1049 $'
      5
      6  import xml.dom
      7  import basetest
      8  import cssutils
      9
     10 -class PropertyTestCase(basetest.BaseTestCase):
     11
     12 -    def setUp(self):
     13          self.r = cssutils.css.property.Property('top', '1px')#, 'important')
     14
     15 -    def test_init(self):
     16          "Property.__init__()"
     17          p = cssutils.css.property.Property('top', '1px')
     18          self.assertEqual('top: 1px', p.cssText)
     19          self.assertEqual('top', p.literalname)
     20          self.assertEqual('top', p.name)
     21          self.assertEqual('1px', p.value)
     22          self.assertEqual('1px', p.cssValue.cssText)
     23          self.assertEqual(u'', p.priority)
     24          self.assertEqual(True, p.valid)
     25          self.assertEqual(True, p.wellformed)
     26
     27          self.assertEqual([u'top'], p.seqs[0])
     28          self.assertEqual(type(cssutils.css.CSSPrimitiveValue(cssText="2px")), type(p.seqs[1]))
     29          self.assertEqual([], p.seqs[2])
     30
     31          self.assertEqual(True, p.valid)
     32
     33          # Prop of MediaQuery
     34          p = cssutils.css.property.Property('top', _mediaQuery=True)
     35          self.assertEqual('top', p.cssText)
     36          self.assertEqual('top', p.literalname)
     37          self.assertEqual('top', p.name)
     38          self.assertEqual('', p.value)
     39          self.assertEqual('', p.cssValue.cssText)
     40          self.assertEqual(u'', p.priority)
     41          self.assertEqual(True, p.valid)
     42          p.cssValue.cssText = '1px'
     43          self.assertEqual('top: 1px', p.cssText)
     44          p.cssValue = ''
     45          self.assertEqual('top', p.cssText)
     46
     47 -    def test_cssText(self):
     48          "Property.cssText"
     49          p = cssutils.css.property.Property()
     50
     51          tests = {
     52              u'a: 1': None,
     53              u'a: 1px 2px': None,
     54              u'a: 1 !important': None,
     55              u'a: 1 !IMPORTANT': u'a: 1 !important',
     56              u'a: 1 !impor\\tant': u'a: 1 !important',
     57              # TODO: important with unicode escapes!
     58              u'font: normal 1em/1.5 serif': None,
     59              u'font: normal 1em/serif': None
     60              }
     61          self.do_equal_r(tests)
     62
     63          tests = {
     64              u'': (xml.dom.SyntaxErr,
     65                     u'''Property: No property name found: u''.'''),
     66              u':': (xml.dom.SyntaxErr,
     67                     u'''Property: No property name found: u':'. [1:1: :]'''),
     68              u'a': (xml.dom.SyntaxErr,
     69                     u'''Property: No ":" after name found: u'a' [1:1: a]'''),
     70              u'a !': (xml.dom.SyntaxErr,
     71                     u'''Property: No ":" after name found: u'a !' [1:3: !]'''),
     72              u'a:': (xml.dom.SyntaxErr,
     73                     u'''Property: No property value found: u'a:'. [1:2: :]'''),
     74              u'a: ': (xml.dom.SyntaxErr,
     75                     u'''CSSValue: Unknown syntax or no value: u' '.'''),
     76              u'a: 1!': (xml.dom.SyntaxErr,
     77                     u'''Property: Invalid priority: u'!'.'''),
     78              u'a: 1!importantX': (xml.dom.SyntaxErr,
     79                     u'''Property: Unexpected ident. [1:6: importantX]'''),
     80              u'a:!important': (xml.dom.SyntaxErr,
     81                     u'''CSSValue: Unknown syntax or no value: u''.'''),
     82              }
     83          for test in tests:
     84              ecp, msg = tests[test]
     85              self.assertRaisesMsg(ecp, msg, p._setCssText, test)
     86
     87 -    def test_name(self):
     88          "Property.name"
     89          p = cssutils.css.property.Property('top', '1px')
     90          p.name = 'left'
     91          self.assertEqual('left', p.name)
     92
     93          tests = {
     94              u'top': None,
     95              u' top': u'top',
     96              u'top ': u'top',
     97              u' top ': u'top',
     98              u'/*x*/ top ': u'top',
     99              u' top /*x*/': u'top',
    100              u'/*x*/top/*x*/': u'top',
    101              u'\\x': u'x',
    102              u'a\\010': u'a\x10',
    103              u'a\\01': u'a\x01'
    104              }
    105          self.do_equal_r(tests, att='name')
    106
    107          tests = {
    108              u'': xml.dom.SyntaxErr,
    109              u' ': xml.dom.SyntaxErr,
    110              u'"\n': xml.dom.SyntaxErr,
    111              u'/*x*/': xml.dom.SyntaxErr,
    112              u':': xml.dom.SyntaxErr,
    113              u';': xml.dom.SyntaxErr,
    114              u'top:': xml.dom.SyntaxErr,
    115              u'top;': xml.dom.SyntaxErr,
    116              }
    117          self.do_raise_r(tests, att='_setName')
    118
    119          p = cssutils.css.property.Property(r'c\olor', 'red')
    120          self.assertEqual(r'c\olor', p.literalname)
    121          self.assertEqual('color', p.name)
    122          self.assertEqual('color', p.normalname) # DEPRECATED
    123
    124 -    def test_literalname(self):
    125          "Property.literalname"
    126          p = cssutils.css.property.Property(r'c\olor', 'red')
    127          self.assertEqual(r'c\olor', p.literalname)
    128          self.assertRaisesMsg(AttributeError, "can't set attribute", p.__setattr__,
    129                               'literalname', 'color')
    130
    131 -    def test_validate(self):
    132          "Property.valid"
    133          p = cssutils.css.property.Property('left', '1px', '')
    134
    135          self.assertEqual(p.valid, True)
    136          self.assertEqual(p.cssValue.valid, True)
    137
    138          p.name = 'color'
    139          self.assertEqual(p.valid, False)
    140          self.assertEqual(p.cssValue.valid, False)
    141
    142          p.name = 'top'
    143          self.assertEqual(p.valid, True)
    144          self.assertEqual(p.cssValue.valid, True)
    145
    146          p.value = 'red'
    147          self.assertEqual(p.valid, False)
    148          self.assertEqual(p.cssValue.valid,False)
    149
    150 -    def test_cssValue(self):
    151          "Property.cssValue"
    152          pass
    153          #TODO
    154
    155 -    def test_priority(self):
    156          "Property.priority"
    157          p = cssutils.css.property.Property('top', '1px', 'important')
    158
    159          for prio in (None, u''):
    160              p.priority = prio
    161              self.assertEqual(u'', p.priority)
    162              self.assertEqual(u'', p.literalpriority)
    163
    164          for prio in (u'!important',
    165                       u'! important',
    166                       u'!/* x */ important',
    167                       u'!/* x */ important /**/',
    168                       u'important',
    169                       u'IMPORTANT',
    170                       ur'im\portant'
    171                       ):
    172              p.priority = prio
    173              self.assertEqual(u'important', p.priority)
    174              if prio.startswith('!'):
    175                  prio = prio[1:].strip()
    176              if u'/*' in prio:
    177                  check = 'important'
    178              else:
    179                  check = prio
    180              self.assertEqual(check, p.literalpriority)
    181
    182          tests = {
    183              u' ': xml.dom.SyntaxErr,
    184              u'"\n': xml.dom.SyntaxErr,
    185              #u'important': xml.dom.SyntaxErr,
    186              u';': xml.dom.SyntaxErr,
    187              u'!important !important': xml.dom.SyntaxErr
    188              }
    189          self.do_raise_r(tests, att='_setPriority')
    190
    191 -    def test_value(self):
    192          "Property.value"
    193          p = cssutils.css.property.Property('top', u'1px')
    194          self.assertEqual('1px', p.value)
    195          p.value = '2px'
    196          self.assertEqual('2px', p.value)
    197
    198          tests = {
    199              u'1px': None,
    200              u' 2px': u'2px',
    201              u'3px ': u'3px',
    202              u' 4px ': u'4px',
    203              u'5px 1px': u'5px 1px',
    204              }
    205          self.do_equal_r(tests, att='value')
    206
    207          tests = {
    208              # no value
    209              None: xml.dom.SyntaxErr,
    210              u'': xml.dom.SyntaxErr,
    211              u' ': xml.dom.SyntaxErr,
    212              u'"\n': xml.dom.SyntaxErr,
    213              u'/*x*/': xml.dom.SyntaxErr,
    214              # not allowed:
    215              u':': xml.dom.SyntaxErr,
    216              u';': xml.dom.SyntaxErr,
    217              u'!important': xml.dom.SyntaxErr,
    218              }
    219          self.do_raise_r(tests, att='_setValue')
    220
    221 -    def test_reprANDstr(self):
    222          "Property.__repr__(), .__str__()"
    223          name="color"
    224          value="red"
    225          priority="important"
    226
    227          s = cssutils.css.property.Property(name=name, value=value, priority=priority)
    228
    229          self.assert_(name in str(s))
    230          self.assert_(value in str(s))
    231          self.assert_(priority in str(s))
    232
    233          s2 = eval(repr(s))
    234          self.assert_(isinstance(s2, s.__class__))
    235          self.assert_(name == s2.name)
    236          self.assert_(value == s2.value)
    237          self.assert_(priority == s2.priority)
    238
    239
    240  if __name__ == '__main__':
    241      import unittest
    242      unittest.main()
    243