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