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: 2007-11-24 23:35:51 +0100 (Sa, 24 Nov 2007) $'
      4  __version__ = '$LastChangedRevision: 687 $'
      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')
     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.name)
     20          self.assertEqual('1px', p.value)
     21          self.assertEqual('1px', p.cssValue.cssText)
     22          self.assertEqual(u'', p.priority)
     23          self.assertEqual(True, p.valid)
     24          self.assertEqual(True, p.wellformed)
     25
     26          self.assertEqual([u'top'], p.seqs[0])
     27          self.assertEqual(type(cssutils.css.CSSPrimitiveValue(cssText="2px")), type(p.seqs[1]))
     28          self.assertEqual([], p.seqs[2])
     29
     30          self.assertEqual(True, p.valid)
     31
     32          # Prop of MediaQuery
     33          p = cssutils.css.property.Property('top', _mediaQuery=True)
     34          self.assertEqual('top', p.cssText)
     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': None,
     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              u'a': (xml.dom.SyntaxErr,
     67                     u'''Property: No ":" after name found: u'a' [1:1: a]'''),
     68              u'a !': (xml.dom.SyntaxErr,
     69                     u'''Property: No ":" after name found: u'a !' [1:3: !]'''),
     70              u'a:': (xml.dom.SyntaxErr,
     71                     u'''Property: No property value found: u'a:'. [1:2: :]'''),
     72              u'a: ': (xml.dom.SyntaxErr,
     73                     u'''CSSValue: Unknown syntax or no value: u' '.'''),
     74              u'a: 1!': (xml.dom.SyntaxErr,
     75                     u'''Property: Invalid priority: u'!'.'''),
     76              u'a: 1!importantX': (xml.dom.SyntaxErr,
     77                     u'''Property: Unexpected ident. [1:6: importantX]'''),
     78              u'a:!important': (xml.dom.SyntaxErr,
     79                     u'''CSSValue: Unknown syntax or no value: u''.'''),
     80              }
     81          for test in tests:
     82              ecp, msg = tests[test]
     83              self.assertRaisesMsg(ecp, msg, p._setCssText, test)
     84
     85
     86 -    def test_name(self):
     87          "Property.name"
     88          p = cssutils.css.property.Property('top', '1px')
     89          p.name = 'left'
     90          self.assertEqual('left', p.name)
     91
     92          tests = {
     93              u'top': None,
     94              u' top': u'top',
     95              u'top ': u'top',
     96              u' top ': u'top',
     97              u'/*x*/ top ': u'top',
     98              u' top /*x*/': u'top',
     99              u'/*x*/top/*x*/': u'top',
    100              u'\\x': None,
    101              u'a\\010': u'a\x10',
    102              u'a\\01': u'a\x01'
    103              }
    104          self.do_equal_r(tests, att='name')
    105
    106          tests = {
    107              u'': xml.dom.SyntaxErr,
    108              u' ': xml.dom.SyntaxErr,
    109              u'"\n': xml.dom.SyntaxErr,
    110              u'/*x*/': xml.dom.SyntaxErr,
    111              u':': xml.dom.SyntaxErr,
    112              u';': xml.dom.SyntaxErr,
    113              u'top:': xml.dom.SyntaxErr,
    114              u'top;': xml.dom.SyntaxErr,
    115              }
    116          self.do_raise_r(tests, att='_setName')
    117
    118 -    def test_validate(self):
    119          "Property.valid"
    120          p = cssutils.css.property.Property('left', '1px', '')
    121
    122          self.assertEqual(p.valid, True)
    123          self.assertEqual(p.cssValue.valid, True)
    124
    125          p.name = 'color'
    126          self.assertEqual(p.valid, False)
    127          self.assertEqual(p.cssValue.valid, False)
    128
    129          p.name = 'top'
    130          self.assertEqual(p.valid, True)
    131          self.assertEqual(p.cssValue.valid, True)
    132
    133          p.value = 'red'
    134          self.assertEqual(p.valid, False)
    135          self.assertEqual(p.cssValue.valid,False)
    136
    137 -    def test_cssValue(self):
    138          "Property.cssValue"
    139          pass
    140          #TODO
    141
    142 -    def test_priority(self):
    143          "Property.priority"
    144          p = cssutils.css.property.Property('top', '1px', '!important')
    145
    146          p.priority = ''
    147          self.assertEqual('', p.priority)
    148
    149          p.priority = '!important'
    150          self.assertEqual('!important', p.priority)
    151
    152          p.priority = None
    153          self.assertEqual('', p.priority)
    154
    155          p.priority = '!   important'
    156          self.assertEqual('!important', p.priority)
    157
    158          p.priority = '!/*1*/important'
    159          self.assertEqual('! /*1*/ important', p.priority)
    160
    161          tests = {
    162              u' ': xml.dom.SyntaxErr,
    163              u'"\n': xml.dom.SyntaxErr,
    164              u'important': xml.dom.SyntaxErr,
    165              u';': xml.dom.SyntaxErr,
    166              u'!important !important': xml.dom.SyntaxErr
    167              }
    168          self.do_raise_r(tests, att='_setPriority')
    169
    170 -    def test_value(self):
    171          "Property.value"
    172          p = cssutils.css.property.Property('top', u'1px')
    173          self.assertEqual('1px', p.value)
    174          p.value = '2px'
    175          self.assertEqual('2px', p.value)
    176
    177          tests = {
    178              u'1px': None,
    179              u' 2px': u'2px',
    180              u'3px ': u'3px',
    181              u' 4px ': u'4px',
    182              u'5px 1px': u'5px 1px',
    183              }
    184          self.do_equal_r(tests, att='value')
    185
    186          tests = {
    187              # no value
    188              None: xml.dom.SyntaxErr,
    189              u'': xml.dom.SyntaxErr,
    190              u' ': xml.dom.SyntaxErr,
    191              u'"\n': xml.dom.SyntaxErr,
    192              u'/*x*/': xml.dom.SyntaxErr,
    193              # not allowed:
    194              u':': xml.dom.SyntaxErr,
    195              u';': xml.dom.SyntaxErr,
    196              u'!important': xml.dom.SyntaxErr,
    197              }
    198          self.do_raise_r(tests, att='_setValue')
    199
    200 -    def test_reprANDstr(self):
    201          "Property.__repr__(), .__str__()"
    202          name="color"
    203          value="red"
    204          priority="!important"
    205
    206          s = cssutils.css.property.Property(name=name, value=value, priority=priority)
    207
    208          self.assert_(name in str(s))
    209          self.assert_(value in str(s))
    210          self.assert_(priority in str(s))
    211
    212          s2 = eval(repr(s))
    213          self.assert_(isinstance(s2, s.__class__))
    214          self.assert_(name == s2.name)
    215          self.assert_(value == s2.value)
    216          self.assert_(priority == s2.priority)
    217
    218
    219  if __name__ == '__main__':
    220      import unittest
    221      unittest.main()
    222