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-01-12 22:36:38 +0100 (Sa, 12 Jan 2008) $'
      4  __version__ = '$LastChangedRevision: 833 $'
      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.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': None,
     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          p.priority = ''
    160          self.assertEqual('', p.priority)
    161
    162          p.priority = '!important'
    163          self.assertEqual('!important', p.priority)
    164
    165          p.priority = None
    166          self.assertEqual('', p.priority)
    167
    168          p.priority = '!   important'
    169          self.assertEqual('!important', p.priority)
    170
    171          p.priority = '!/*1*/important'
    172          self.assertEqual('! /*1*/ important', p.priority)
    173
    174          tests = {
    175              u' ': xml.dom.SyntaxErr,
    176              u'"\n': xml.dom.SyntaxErr,
    177              u'important': xml.dom.SyntaxErr,
    178              u';': xml.dom.SyntaxErr,
    179              u'!important !important': xml.dom.SyntaxErr
    180              }
    181          self.do_raise_r(tests, att='_setPriority')
    182
    183 -    def test_value(self):
    184          "Property.value"
    185          p = cssutils.css.property.Property('top', u'1px')
    186          self.assertEqual('1px', p.value)
    187          p.value = '2px'
    188          self.assertEqual('2px', p.value)
    189
    190          tests = {
    191              u'1px': None,
    192              u' 2px': u'2px',
    193              u'3px ': u'3px',
    194              u' 4px ': u'4px',
    195              u'5px 1px': u'5px 1px',
    196              }
    197          self.do_equal_r(tests, att='value')
    198
    199          tests = {
    200              # no value
    201              None: xml.dom.SyntaxErr,
    202              u'': xml.dom.SyntaxErr,
    203              u' ': xml.dom.SyntaxErr,
    204              u'"\n': xml.dom.SyntaxErr,
    205              u'/*x*/': xml.dom.SyntaxErr,
    206              # not allowed:
    207              u':': xml.dom.SyntaxErr,
    208              u';': xml.dom.SyntaxErr,
    209              u'!important': xml.dom.SyntaxErr,
    210              }
    211          self.do_raise_r(tests, att='_setValue')
    212
    213 -    def test_reprANDstr(self):
    214          "Property.__repr__(), .__str__()"
    215          name="color"
    216          value="red"
    217          priority="!important"
    218
    219          s = cssutils.css.property.Property(name=name, value=value, priority=priority)
    220
    221          self.assert_(name in str(s))
    222          self.assert_(value in str(s))
    223          self.assert_(priority in str(s))
    224
    225          s2 = eval(repr(s))
    226          self.assert_(isinstance(s2, s.__class__))
    227          self.assert_(name == s2.name)
    228          self.assert_(value == s2.value)
    229          self.assert_(priority == s2.priority)
    230
    231
    232  if __name__ == '__main__':
    233      import unittest
    234      unittest.main()
    235