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