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

# Source Code for Module cssutils.tests.test_cssvalue

      1  """Testcases for cssutils.css.CSSValue and CSSPrimitiveValue."""
      2  __version__ = '$Id: test_cssvalue.py 1116 2008-03-05 13:52:23Z cthedot $'
      3
      4  # from decimal import Decimal # maybe for later tests?
      5  import xml.dom
      6  import basetest
      7  import cssutils
      8  import types
      9
     10 -class CSSValueTestCase(basetest.BaseTestCase):
     11
     12 -    def test_init(self):
     13          "CSSValue.__init__()"
     14          v = cssutils.css.CSSValue()
     15          self.assert_(u'' == v.cssText)
     16          self.assert_(None is  v.cssValueType)
     17          self.assert_(None == v.cssValueTypeString)
     18
     19 -    def test_escapes(self):
     20          "CSSValue Escapes"
     21          v = cssutils.css.CSSValue()
     22          v.cssText = u'1px'
     23          self.assert_(v.CSS_PRIMITIVE_VALUE == v.cssValueType)
     24          self.assert_(v.CSS_PX == v.primitiveType)
     25          self.assert_(u'1px' == v.cssText)
     26
     27          v.cssText = u'1PX'
     28          self.assert_(v.CSS_PRIMITIVE_VALUE == v.cssValueType)
     29          self.assert_(v.CSS_PX == v.primitiveType)
     30          # TODO: maybe normalize?
     31          self.assert_(u'1PX' == v.cssText)
     32
     33          v.cssText = u'1p\\x'
     34          self.assert_(v.CSS_PRIMITIVE_VALUE == v.cssValueType)
     35          self.assert_(v.CSS_PX == v.primitiveType)
     36          # TODO: maybe normalize?
     37          self.assert_(u'1p\\x' == v.cssText)
     38
     39 -    def test_cssText(self):
     40          "CSSValue.cssText"
     41          v = cssutils.css.CSSValue()
     42          v.cssText = u'1px'
     43          self.assert_(v.CSS_PRIMITIVE_VALUE == v.cssValueType)
     44          self.assert_(v.CSS_PX == v.primitiveType)
     45          self.assert_(u'1px' == v.cssText)
     46          self.assert_(False is v.valid)
     47
     48          v = cssutils.css.CSSValue(_propertyName="left")
     49          v.cssText = u'1px'
     50          self.assert_(v.CSS_PRIMITIVE_VALUE == v.cssValueType)
     51          self.assert_(v.CSS_PX == v.primitiveType)
     52          self.assert_(u'1px' == v.cssText)
     53          self.assert_(True is v.valid)
     54
     55          v = cssutils.css.CSSValue(_propertyName="font-family")
     56          v.cssText = u'a  ,b,  c  ,"d or d", "e, " '
     57          self.assertEqual(v.CSS_PRIMITIVE_VALUE, v.cssValueType)
     58          self.assertEqual(v.CSS_STRING, v.primitiveType)
     59          self.assertEqual(u'a, b, c, "d or d", "e, "', v.cssText)
     60          self.assert_(True is v.valid)
     61
     62          v.cssText = u'  1   px    '
     63          self.assert_(v.CSS_VALUE_LIST == v.cssValueType)
     64          self.assert_('1 px' == v._value)
     65          self.assert_('1 px' == v.cssText)
     66          self.assert_(False is v.valid)
     67
     68          v.cssText = u'  normal 1px a, b, "c" end   '
     69          self.assert_(v.CSS_VALUE_LIST == v.cssValueType)
     70          self.assert_('normal 1px a, b, "c" end' == v._value)
     71          self.assert_('normal 1px a, b, "c" end' == v.cssText)
     72          self.assert_(False is v.valid)
     73          for i, x in enumerate(v):
     74              self.assertEqual(x.CSS_PRIMITIVE_VALUE, x.cssValueType)
     75              if x == 0:
     76                  self.assertEqual(x.CSS_IDENT, x.primitiveType)
     77                  self.assertEqual(u'normal', x.cssText)
     78              elif x == 1:
     79                  self.assertEqual(x.CSS_PX, x.primitiveType)
     80                  self.assertEqual(u'1px', x.cssText)
     81              if x == 2:
     82                  self.assertEqual(x.CSS_STRING, x.primitiveType)
     83                  self.assertEqual(u'a, b, "c"', x.cssText)
     84              if x == 3:
     85                  self.assertEqual(x.CSS_IDENT, x.primitiveType)
     86                  self.assertEqual(u'end', x.cssText)
     87
     88
     89          v = cssutils.css.CSSValue(_propertyName="left")
     90          v.cssText = u'  1   px    '
     91          self.assert_(v.CSS_VALUE_LIST == v.cssValueType)
     92          self.assert_(u'1 px' == v.cssText)
     93          self.assert_(False is v.valid)
     94
     95          v.cssText = u'expression(document.body.clientWidth > 972 ? "1014px": "100%" )'
     96          self.assert_(v.CSS_PRIMITIVE_VALUE == v.cssValueType)
     97          self.assert_(v.CSS_UNKNOWN == v.primitiveType)
     98          self.assertEqual('expression(document.body.clientWidth>972?"1014px":"100%")',
     99                           v._value)
    100          self.assertEqual('expression(document.body.clientWidth>972?"1014px":"100%")',
    101                           v.cssText)
    102          self.assert_(False is v.valid)
    103
    104 -    def test_incomplete(self):
    105          "CSSValue (incomplete)"
    106          tests = {
    107              u'url("a': u'url("a")',
    108              u'url(a': u'url(a)'
    109          }
    110          for v, exp in tests.items():
    111              s = cssutils.parseString('a { background: %s' % v)
    112              v = s.cssRules[0].style.background
    113              self.assertEqual(v, exp)
    114
    115 -    def test_valid(self):
    116          "CSSValue.valid"
    117          # context property must be set
    118          tests = [
    119              ('color', '1', False),
    120              ('color', 'red', True),
    121              ('left', '1', False),
    122              ('left', '1px', True),
    123              ('font', 'normal 1em/1.5 serif', True)
    124              ]
    125          for n, v, exp in tests:
    126              v = cssutils.css.CSSValue(cssText=v, _propertyName=n)
    127              self.assertEqual(v.valid, exp)
    128              self.assert_(v.wellformed, True)
    129
    130 -    def test_cssValueType(self):
    131          "CSSValue.cssValueType .cssValueTypeString"
    132          tests = [
    133              ([u'inherit'], 'CSS_INHERIT'),
    134              (['1', '1%', '1em', '1ex', '1px', '1cm', '1mm', '1in', '1pt', '1pc',
    135                '1deg', '1rad', '1grad', '1ms', '1s', '1hz', '1khz', '1other',
    136                 '"string"', "'string'", 'url(x)', 'red',
    137                 'attr(a)', 'counter()', 'rect(1px,2px,3px,4px)',
    138                 'rgb(0,0,0)', '#000', '#000000', 'rgba(0,0,0,0)'],
    139               'CSS_PRIMITIVE_VALUE'),
    140              ([u'1px 1px', 'red blue green x'], 'CSS_VALUE_LIST'),
    141              ([], 'CSS_CUSTOM') # what is a custom value?
    142              ]
    143          for values, name in tests:
    144              for value in values:
    145                  v = cssutils.css.CSSValue(cssText=value)
    146                  if value == "'string'":
    147                      # will be changed to " always
    148                      value = '"string"'
    149                  self.assertEqual(value, v.cssText)
    150                  self.assertEqual(name, v.cssValueTypeString)
    151                  self.assertEqual(getattr(v, name), v.cssValueType)
    152
    153 -    def test_readonly(self):
    154          "(CSSValue._readonly)"
    155          v = cssutils.css.CSSValue(cssText='inherit')
    156          self.assert_(False is v._readonly)
    157
    158          v = cssutils.css.CSSValue(cssText='inherit', readonly=True)
    159          self.assert_(True is v._readonly)
    160          self.assert_(u'inherit', v.cssText)
    161          self.assertRaises(xml.dom.NoModificationAllowedErr, v._setCssText, u'x')
    162          self.assert_(u'inherit', v.cssText)
    163
    164 -    def test_reprANDstr(self):
    165          "CSSValue.__repr__(), .__str__()"
    166          cssText='inherit'
    167
    168          s = cssutils.css.CSSValue(cssText=cssText)
    169
    170          self.assert_(cssText in str(s))
    171
    172          s2 = eval(repr(s))
    173          self.assert_(isinstance(s2, s.__class__))
    174          self.assert_(cssText == s2.cssText)
    175
    176
    177 -class CSSPrimitiveValueTestCase(basetest.BaseTestCase):
    178
    179 -    def test_init(self):
    180          "CSSPrimitiveValue.__init__()"
    181          v = cssutils.css.CSSPrimitiveValue(u'1')
    182          self.assert_(u'1' == v.cssText)
    183          self.assert_(v.valid == False)
    184
    185          self.assert_(v.CSS_PRIMITIVE_VALUE == v.cssValueType)
    186          self.assert_("CSS_PRIMITIVE_VALUE" == v.cssValueTypeString)
    187
    188          self.assert_(v.CSS_NUMBER == v.primitiveType)
    189          self.assert_("CSS_NUMBER" == v.primitiveTypeString)
    190
    191          # DUMMY to be able to test empty constructor call
    192          #self.assertRaises(xml.dom.SyntaxErr, v.__init__, None)
    193
    194          #self.assertRaises(xml.dom.InvalidAccessErr, v.getCounterValue)
    195          #self.assertRaises(xml.dom.InvalidAccessErr, v.getRGBColorValue)
    196          #self.assertRaises(xml.dom.InvalidAccessErr, v.getRectValue)
    197          #self.assertRaises(xml.dom.InvalidAccessErr, v.getStringValue)
    198
    199 -    def test_CSS_UNKNOWN(self):
    200          "CSSPrimitiveValue.CSS_UNKNOWN"
    201          v = cssutils.css.CSSPrimitiveValue(u'expression(false)')
    202          self.assert_(v.valid == False)
    203          self.assert_(v.CSS_UNKNOWN == v.primitiveType)
    204          self.assert_('CSS_UNKNOWN' == v.primitiveTypeString)
    205
    206 -    def test_CSS_NUMBER_AND_OTHER_DIMENSIONS(self):
    207          "CSSPrimitiveValue.CSS_NUMBER .. CSS_DIMENSION"
    208          defs = [
    209              ('', 'CSS_NUMBER'),
    210              ('%', 'CSS_PERCENTAGE'),
    211              ('em', 'CSS_EMS'),
    212              ('ex', 'CSS_EXS'),
    213              ('px', 'CSS_PX'),
    214              ('cm', 'CSS_CM'),
    215              ('mm', 'CSS_MM'),
    216              ('in', 'CSS_IN'),
    217              ('pt', 'CSS_PT'),
    218              ('pc', 'CSS_PC'),
    219              ('deg', 'CSS_DEG'),
    220              ('rad', 'CSS_RAD'),
    221              ('grad', 'CSS_GRAD'),
    222              ('ms', 'CSS_MS'),
    223              ('s', 'CSS_S'),
    224              ('hz', 'CSS_HZ'),
    225              ('khz', 'CSS_KHZ'),
    226              ('other_dimension', 'CSS_DIMENSION')]
    227          for dim, name in defs:
    228              for n in (0, 1, 1.1, -1, -1.1, -0):
    229                  v = cssutils.css.CSSPrimitiveValue('%i%s' % (n, dim))
    230                  self.assert_(getattr(v, name) == v.primitiveType)
    231                  self.assert_(name == v.primitiveTypeString)
    232
    233 -    def test_CSS_STRING_AND_OTHER(self):
    234          "CSSPrimitiveValue.CSS_STRING .. CSS_RGBCOLOR"
    235          defs = [
    236              (('""', "''", '"some thing"', "' A\\ND '"), 'CSS_STRING'),
    237              (('url(a)', 'url("a b")', "url(' ')"), 'CSS_URI'),
    238              (('some', 'or_anth-er'), 'CSS_IDENT'),
    239              (('attr(a)', 'attr(b)'), 'CSS_ATTR'),
    240              (('counter(1)', 'counter(2)'), 'CSS_COUNTER'),
    241              (('rect(1,2,3,4)',), 'CSS_RECT'),
    242              (('rgb(1,2,3)', 'rgb(10%, 20%, 30%)', '#123', '#123456'),
    243                   'CSS_RGBCOLOR'),
    244              (('rgba(1,2,3,4)','rgba(10%, 20%, 30%, 40%)', ),
    245                   'CSS_RGBACOLOR')
    246              ]
    247          for examples, name in defs:
    248              for x in examples:
    249                  v = cssutils.css.CSSPrimitiveValue(x)
    250                  #self.assertEqual(getattr(v, name), v.primitiveType)
    251                  self.assertEqual(name, v.primitiveTypeString)
    252
    253 -    def test_getFloat(self):
    254          "CSSPrimitiveValue.getFloatValue()"
    255          # NOT TESTED are float values as it seems difficult to
    256          # compare these. Maybe use decimal.Decimal?
    257
    258          v = cssutils.css.CSSPrimitiveValue(u'1px')
    259          tests = {
    260              '0': (v.CSS_NUMBER, 0),
    261              '-1.1': (v.CSS_NUMBER, -1.1),
    262              '1%': (v.CSS_PERCENTAGE, 1),
    263              '-1%': (v.CSS_PERCENTAGE, -1),
    264              '1em': (v.CSS_EMS, 1),
    265              '-1.1em': (v.CSS_EMS, -1.1),
    266              '1ex': (v.CSS_EXS, 1),
    267              '1px': (v.CSS_PX, 1),
    268
    269              '1cm': (v.CSS_CM, 1),
    270              '1cm': (v.CSS_MM, 10),
    271              '254cm': (v.CSS_IN, 100),
    272              '1mm': (v.CSS_MM, 1),
    273              '10mm': (v.CSS_CM, 1),
    274              '254mm': (v.CSS_IN, 10),
    275              '1in': (v.CSS_IN, 1),
    276              '100in': (v.CSS_CM, 254), # ROUNDED!!!
    277              '10in': (v.CSS_MM, 254), # ROUNDED!!!
    278
    279              '1pt': (v.CSS_PT, 1),
    280              '1pc': (v.CSS_PC, 1),
    281
    282              '1deg': (v.CSS_DEG, 1),
    283              '1rad': (v.CSS_RAD, 1),
    284              '1grad': (v.CSS_GRAD, 1),
    285
    286              '1ms': (v.CSS_MS, 1),
    287              '1000ms': (v.CSS_S, 1),
    288              '1s': (v.CSS_S, 1),
    289              '1s': (v.CSS_MS, 1000),
    290
    291              '1hz': (v.CSS_HZ, 1),
    292              '1000hz': (v.CSS_KHZ, 1),
    293              '1khz': (v.CSS_KHZ, 1),
    294              '1khz': (v.CSS_HZ, 1000),
    295
    296              '1DIMENSION': (v.CSS_DIMENSION, 1)
    297              }
    298          for cssText in tests:
    299              v.cssText = cssText
    300              unitType, exp = tests[cssText]
    301              val = v.getFloatValue(unitType)
    302              if unitType in (v.CSS_IN, v.CSS_CM):
    303                  val = round(val)
    304              self.assertEqual(val , exp)
    305
    306 -    def test_setFloat(self):
    307          "CSSPrimitiveValue.setFloatValue()"
    308          V = cssutils.css.CSSPrimitiveValue
    309
    310          tests = {
    311              # unitType, value
    312              (V.CSS_NUMBER, 1): [
    313                  # unitType, setvalue,
    314                  #    getvalue or expected exception, msg or cssText
    315                  (V.CSS_NUMBER, 0, 0, '0'),
    316                  (V.CSS_NUMBER, 0.1, 0.1, '0.1'),
    317                  (V.CSS_NUMBER, -0, 0, '0'),
    318                  (V.CSS_NUMBER, 2, 2, '2'),
    319                  (V.CSS_NUMBER, 2.0, 2, '2'),
    320                  (V.CSS_NUMBER, 2.1, 2.1, '2.1'),
    321                  (V.CSS_NUMBER, -2.1, -2.1, '-2.1'),
    322                  # setting with string does work
    323                  (V.CSS_NUMBER, '1', 1, '1'),
    324                  (V.CSS_NUMBER, '1.1', 1.1, '1.1'),
    325                  (V.CSS_PX, 1, xml.dom.InvalidAccessErr, None),
    326                  (V.CSS_DEG, 1, xml.dom.InvalidAccessErr, None),
    327                  (V.CSS_RAD, 1, xml.dom.InvalidAccessErr, None),
    328                  (V.CSS_GRAD, 1, xml.dom.InvalidAccessErr, None),
    329                  (V.CSS_S, 1, xml.dom.InvalidAccessErr, None),
    330                  (V.CSS_MS, 1, xml.dom.InvalidAccessErr, None),
    331                  (V.CSS_KHZ, 1, xml.dom.InvalidAccessErr, None),
    332                  (V.CSS_HZ, 1, xml.dom.InvalidAccessErr, None),
    333                  (V.CSS_DIMENSION, 1, xml.dom.InvalidAccessErr, None),
    334                  (V.CSS_MM, 2, xml.dom.InvalidAccessErr, None),
    335
    336                  (V.CSS_NUMBER, 'x', xml.dom.InvalidAccessErr,
    337                   "CSSPrimitiveValue: floatValue 'x' is not a float"),
    338                  (V.CSS_NUMBER, '1x', xml.dom.InvalidAccessErr,
    339                   "CSSPrimitiveValue: floatValue '1x' is not a float"),
    340
    341                  (V.CSS_STRING, 'x', xml.dom.InvalidAccessErr,
    342                   "CSSPrimitiveValue: unitType 'CSS_STRING' is not a float type"),
    343                  (V.CSS_URI, 'x', xml.dom.InvalidAccessErr,
    344                   "CSSPrimitiveValue: unitType 'CSS_URI' is not a float type"),
    345                  (V.CSS_ATTR, 'x', xml.dom.InvalidAccessErr,
    346                   "CSSPrimitiveValue: unitType 'CSS_ATTR' is not a float type"),
    347                  (V.CSS_IDENT, 'x', xml.dom.InvalidAccessErr,
    348                   "CSSPrimitiveValue: unitType 'CSS_IDENT' is not a float type"),
    349                  (V.CSS_RGBCOLOR, 'x', xml.dom.InvalidAccessErr,
    350                   "CSSPrimitiveValue: unitType 'CSS_RGBCOLOR' is not a float type"),
    351                  (V.CSS_RGBACOLOR, 'x', xml.dom.InvalidAccessErr,
    352                   "CSSPrimitiveValue: unitType 'CSS_RGBACOLOR' is not a float type"),
    353                  (V.CSS_RECT, 'x', xml.dom.InvalidAccessErr,
    354                   "CSSPrimitiveValue: unitType 'CSS_RECT' is not a float type"),
    355                  (V.CSS_COUNTER, 'x', xml.dom.InvalidAccessErr,
    356                   "CSSPrimitiveValue: unitType 'CSS_COUNTER' is not a float type"),
    357                  (V.CSS_EMS, 1, xml.dom.InvalidAccessErr,
    358                   "CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_NUMBER' to 'CSS_EMS'"),
    359                  (V.CSS_EXS, 1, xml.dom.InvalidAccessErr,
    360                   "CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_NUMBER' to 'CSS_EXS'")
    361              ],
    362              (V.CSS_MM, '1mm'): [
    363                  (V.CSS_MM, 2, 2, '2mm'),
    364                  (V.CSS_MM, 0, 0, '0mm'),
    365                  (V.CSS_MM, 0.1, 0.1, '0.1mm'),
    366                  (V.CSS_MM, -0, -0, '0mm'),
    367                  (V.CSS_MM, 3.0, 3, '3mm'),
    368                  (V.CSS_MM, 3.1, 3.1, '3.1mm'),
    369                  (V.CSS_MM, -3.1, -3.1, '-3.1mm'),
    370                  (V.CSS_CM, 1, 10, '10mm'),
    371                  (V.CSS_IN, 10, 254, '254mm'),
    372                  (V.CSS_PT, 1, 1828.8, '1828.8mm'),
    373                  (V.CSS_PX, 1, xml.dom.InvalidAccessErr, None),
    374                  (V.CSS_NUMBER, 2, xml.dom.InvalidAccessErr, None)
    375              ],
    376              (V.CSS_PT, '1pt'): [
    377                  (V.CSS_PT, 2, 2, '2pt'),
    378                  (V.CSS_PC, 12, 1, '1pt'),
    379                  (V.CSS_NUMBER, 1, xml.dom.InvalidAccessErr, None),
    380                  (V.CSS_DEG, 1, xml.dom.InvalidAccessErr, None),
    381                  (V.CSS_PX, 1, xml.dom.InvalidAccessErr, None)
    382              ],
    383              (V.CSS_KHZ, '1khz'): [
    384                  (V.CSS_HZ, 2000, 2, '2khz'),
    385                  (V.CSS_NUMBER, 1, xml.dom.InvalidAccessErr, None),
    386                  (V.CSS_DEG, 1, xml.dom.InvalidAccessErr, None),
    387                  (V.CSS_PX, 1, xml.dom.InvalidAccessErr, None)
    388              ]
    389          }
    390          for test in tests:
    391              initialType, initialValue = test
    392              pv = cssutils.css.CSSPrimitiveValue(initialValue)
    393              for setType, setValue, exp, msg in tests[test]:
    394                  if type(exp) == types.TypeType or\
    395                     type(exp) == types.ClassType: # 2.4 compactibility
    396                      if msg:
    397                          self.assertRaisesMsg(
    398                              exp, msg, pv.setFloatValue, setType, setValue)
    399                      else:
    400                          self.assertRaises(
    401                              exp, pv.setFloatValue, setType, setValue)
    402                  else:
    403                      pv.setFloatValue(setType, setValue)
    404                      self.assertEqual(pv.cssText, msg)
    405                      self.assertEqual(pv.getFloatValue(initialType), exp)
    406                      self.assertEqual(pv._value, msg)
    407
    408 -    def test_getString(self):
    409          "CSSPrimitiveValue.getStringValue()"
    410          v = cssutils.css.CSSPrimitiveValue(u'1px')
    411          self.assert_(v.primitiveType == v.CSS_PX)
    412          self.assertRaises(xml.dom.InvalidAccessErr,
    413                            v.getStringValue)
    414
    415          pv = cssutils.css.CSSPrimitiveValue
    416          tests = {
    417              pv.CSS_STRING: ("'red'", 'red'),
    418              pv.CSS_STRING: ('"red"', 'red'),
    419              pv.CSS_URI: ('url(http://example.com)', None),
    420              pv.CSS_URI: ("url('http://example.com')",
    421                           u"http://example.com"),
    422              pv.CSS_URI: ('url("http://example.com")',
    423                           u'http://example.com'),
    424              pv.CSS_URI: ('url("http://example.com?)")',
    425                           u'http://example.com?)'),
    426              pv.CSS_IDENT: ('red', None),
    427              pv.CSS_ATTR: ('attr(att-name)',
    428                           u'att-name'), # the name of the attrr
    429              }
    430          for t in tests:
    431              val, exp = tests[t]
    432              if not exp:
    433                  exp = val
    434
    435              v = cssutils.css.CSSPrimitiveValue(val)
    436              self.assertEqual(v.primitiveType, t)
    437              self.assertEqual(v.getStringValue(), exp)
    438
    439 -    def test_setString(self):
    440          "CSSPrimitiveValue.setStringValue()"
    441          # CSS_STRING
    442          v = cssutils.css.CSSPrimitiveValue(u'"a"')
    443          self.assert_(v.CSS_STRING == v.primitiveType)
    444          v.setStringValue(v.CSS_STRING, 'b')
    445          self.assert_('"b"' == v._value)
    446          self.assert_('b' == v.getStringValue())
    447          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    448              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_STRING' to 'CSS_URI'",
    449              v.setStringValue, *(v.CSS_URI, 'x'))
    450          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    451              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_STRING' to 'CSS_IDENT'",
    452              v.setStringValue, *(v.CSS_IDENT, 'x'))
    453          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    454              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_STRING' to 'CSS_ATTR'",
    455              v.setStringValue, *(v.CSS_ATTR, 'x'))
    456
    457          # CSS_IDENT
    458          v = cssutils.css.CSSPrimitiveValue('new')
    459          v.setStringValue(v.CSS_IDENT, 'ident')
    460          self.assert_(v.CSS_IDENT == v.primitiveType)
    461          self.assert_('ident' == v._value)
    462          self.assert_('ident' == v.getStringValue())
    463          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    464              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_IDENT' to 'CSS_URI'",
    465              v.setStringValue, *(v.CSS_URI, 'x'))
    466          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    467              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_IDENT' to 'CSS_STRING'",
    468              v.setStringValue, *(v.CSS_STRING, '"x"'))
    469          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    470              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_IDENT' to 'CSS_ATTR'",
    471              v.setStringValue, *(v.CSS_ATTR, 'x'))
    472
    473          # CSS_URI
    474          v = cssutils.css.CSSPrimitiveValue('url(old)')
    475          v.setStringValue(v.CSS_URI, '(')
    476          self.assertEqual(u'url("(")', v._value)
    477          self.assertEqual(u'(', v.getStringValue())
    478
    479          v.setStringValue(v.CSS_URI, ')')
    480          self.assertEqual(u'url(")")', v._value)
    481          self.assertEqual(u')', v.getStringValue())
    482          self.assertEqual(u'url(")")', v._value)
    483
    484          v.setStringValue(v.CSS_URI, '""')
    485          self.assertEqual(ur'""', v.getStringValue())
    486          self.assertEqual(ur'url("\"\"")', v._value)
    487
    488          v.setStringValue(v.CSS_URI, ',')
    489          self.assertEqual(u'url(",")', v._value)
    490          self.assertEqual(u',', v.getStringValue())
    491
    492          v.setStringValue(v.CSS_URI, ' ')
    493          self.assertEqual(u'url(" ")', v._value)
    494          self.assertEqual(u' ', v.getStringValue())
    495
    496          v.setStringValue(v.CSS_URI, 'a)')
    497          self.assertEqual(u'url("a)")', v._value)
    498          self.assertEqual(u'a)', v.getStringValue())
    499
    500          v.setStringValue(v.CSS_URI, 'a')
    501          self.assert_(v.CSS_URI == v.primitiveType)
    502          self.assert_('url(a)' == v._value)
    503          self.assert_('a' == v.getStringValue())
    504          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    505              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_URI' to 'CSS_IDENT'",
    506              v.setStringValue, *(v.CSS_IDENT, 'x'))
    507          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    508              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_URI' to 'CSS_STRING'",
    509              v.setStringValue, *(v.CSS_STRING, '"x"'))
    510          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    511              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_URI' to 'CSS_ATTR'",
    512              v.setStringValue, *(v.CSS_ATTR, 'x'))
    513
    514          # CSS_ATTR
    515          v = cssutils.css.CSSPrimitiveValue('attr(old)')
    516          v.setStringValue(v.CSS_ATTR, 'a')
    517          self.assert_(v.CSS_ATTR == v.primitiveType)
    518          self.assert_('attr(a)' == v._value)
    519          self.assert_('a' == v.getStringValue())
    520          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    521              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_ATTR' to 'CSS_IDENT'",
    522              v.setStringValue, *(v.CSS_IDENT, 'x'))
    523          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    524              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_ATTR' to 'CSS_STRING'",
    525              v.setStringValue, *(v.CSS_STRING, '"x"'))
    526          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    527              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_ATTR' to 'CSS_URI'",
    528              v.setStringValue, *(v.CSS_URI, 'x'))
    529
    530          # TypeError as 'x' is no valid type
    531          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    532              u"CSSPrimitiveValue: stringType 'x' (UNKNOWN TYPE) is not a string type",
    533              v.setStringValue, *('x', 'brown'))
    534          # IndexError as 111 is no valid type
    535          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    536              u"CSSPrimitiveValue: stringType 111 (UNKNOWN TYPE) is not a string type",
    537              v.setStringValue, *(111, 'brown'))
    538          # CSS_PX is no string type
    539          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    540              u"CSSPrimitiveValue: stringType CSS_PX is not a string type",
    541              v.setStringValue, *(v.CSS_PX, 'brown'))
    542
    543  #    def test_typeRGBColor(self):
    544  #        v = cssutils.cssprimitivevalue.PrimitiveValue('RGB(1, 5, 10)')
    545  #        self.assertEqual(v.CSS_RGBCOLOR, v.primitiveType)
    546  #        self.assertEqual(u'rgb(1, 5, 10)', v.cssText)
    547  #
    548  #        v = cssutils.cssprimitivevalue.PrimitiveValue('rgb(1, 5, 10)')
    549  #        self.assertEqual(v.CSS_RGBCOLOR, v.primitiveType)
    550  #        self.assertEqual(u'rgb(1, 5, 10)', v.cssText)
    551  #
    552  #        v = cssutils.cssprimitivevalue.PrimitiveValue('rgb(1%, 5%, 10%)')
    553  #        self.assertEqual(v.CSS_RGBCOLOR, v.primitiveType)
    554  #        self.assertEqual(u'rgb(1.0%, 5.0%, 10.0%)', v.cssText)
    555  #
    556  #        v = cssutils.cssprimitivevalue.PrimitiveValue('  rgb(  1 ,5,  10  )')
    557  #        self.assertEqual(v.CSS_RGBCOLOR, v.primitiveType)
    558  #        v = cssutils.cssprimitivevalue.PrimitiveValue('rgb (1,5,10)')
    559  #        self.assertEqual(v.CSS_RGBCOLOR, v.primitiveType)
    560  #        v = cssutils.cssprimitivevalue.PrimitiveValue('rgb(1%, .5%, 10.1%)')
    561  #        self.assertEqual(v.CSS_RGBCOLOR, v.primitiveType)
    562
    563 -    def test_reprANDstr(self):
    564          "CSSPrimitiveValue.__repr__(), .__str__()"
    565          v='111'
    566
    567          s = cssutils.css.CSSPrimitiveValue(v)
    568
    569          self.assert_(v in str(s))
    570          self.assert_('CSS_NUMBER' in str(s))
    571
    572          s2 = eval(repr(s))
    573          self.assert_(isinstance(s2, s.__class__))
    574          self.assert_(v == s2.cssText)
    575
    576
    577 -class CSSValueListTestCase(basetest.BaseTestCase):
    578
    579 -    def test_init(self):
    580          "CSSValueList.__init__()"
    581          v = cssutils.css.CSSValue(cssText=u'red blue', _propertyName='border-color')
    582          self.assert_(v.CSS_VALUE_LIST == v.cssValueType)
    583          self.assertEqual('red blue', v._value)
    584          self.assertEqual('red blue', v.cssText)
    585          self.assert_(True is v.valid)
    586
    587          self.assert_(2 == v.length)
    588
    589          item = v.item(0)
    590          item.setStringValue(item.CSS_IDENT, 'green')
    591          self.assertEqual('green blue', v._value)
    592          self.assertEqual('green blue', v.cssText)
    593
    594 -    def test_numbers(self):
    595          "CSSValueList.cssText"
    596          tests = {
    597              u'1 2 3 4': (None, 4),
    598              u'-1 -2 -3 -4': (None, 4),
    599              u'-1 2': (None, 2),
    600              u'-1px red "x"': (None, 3),
    601              u'-1cm, 2 3': (None, 2)
    602              }
    603          for test in tests:
    604              exp, num = tests[test]
    605              if not exp:
    606                  exp = test
    607              v = cssutils.css.CSSValue(cssText=test)
    608              self.assert_(v.CSS_VALUE_LIST == v.cssValueType)
    609              self.assertEqual(exp, v._value)
    610              self.assertEqual(num, v.length)
    611          self.assertEqual(exp, v.cssText)
    612
    613 -    def test_reprANDstr(self):
    614          "CSSValueList.__repr__(), .__str__()"
    615          v='1px 2px'
    616
    617          s = cssutils.css.CSSValue(v)
    618          self.assert_(isinstance(s, cssutils.css.CSSValueList))
    619
    620          self.assert_(v in str(s))
    621
    622          # not "eval()"able!
    623          #s2 = eval(repr(s))
    624
    625
    626  if __name__ == '__main__':
    627      import unittest
    628      unittest.main()
    629