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: 2008-02-12 16:44:24 +0100 (Di, 12 Feb 2008) $'
      4  __version__ = '$LastChangedRevision: 1026 $'
      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"', "'string'", '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                  if value == "'string'":
    149                      # will be changed to " always
    150                      value = '"string"'
    151                  self.assertEqual(value, v.cssText)
    152                  self.assertEqual(name, v.cssValueTypeString)
    153                  self.assertEqual(getattr(v, name), v.cssValueType)
    154
    155 -    def test_readonly(self):
    156          "(CSSValue._readonly)"
    157          v = cssutils.css.CSSValue(cssText='inherit')
    158          self.assert_(False is v._readonly)
    159
    160          v = cssutils.css.CSSValue(cssText='inherit', readonly=True)
    161          self.assert_(True is v._readonly)
    162          self.assert_(u'inherit', v.cssText)
    163          self.assertRaises(xml.dom.NoModificationAllowedErr, v._setCssText, u'x')
    164          self.assert_(u'inherit', v.cssText)
    165
    166 -    def test_reprANDstr(self):
    167          "CSSValue.__repr__(), .__str__()"
    168          cssText='inherit'
    169
    170          s = cssutils.css.CSSValue(cssText=cssText)
    171
    172          self.assert_(cssText in str(s))
    173
    174          s2 = eval(repr(s))
    175          self.assert_(isinstance(s2, s.__class__))
    176          self.assert_(cssText == s2.cssText)
    177
    178
    179 -class CSSPrimitiveValueTestCase(basetest.BaseTestCase):
    180
    181 -    def test_init(self):
    182          "CSSPrimitiveValue.__init__()"
    183          v = cssutils.css.CSSPrimitiveValue(u'1')
    184          self.assert_(u'1' == v.cssText)
    185          self.assert_(v.valid == False)
    186
    187          self.assert_(v.CSS_PRIMITIVE_VALUE == v.cssValueType)
    188          self.assert_("CSS_PRIMITIVE_VALUE" == v.cssValueTypeString)
    189
    190          self.assert_(v.CSS_NUMBER == v.primitiveType)
    191          self.assert_("CSS_NUMBER" == v.primitiveTypeString)
    192
    193          # DUMMY to be able to test empty constructor call
    194          #self.assertRaises(xml.dom.SyntaxErr, v.__init__, None)
    195
    196          #self.assertRaises(xml.dom.InvalidAccessErr, v.getCounterValue)
    197          #self.assertRaises(xml.dom.InvalidAccessErr, v.getRGBColorValue)
    198          #self.assertRaises(xml.dom.InvalidAccessErr, v.getRectValue)
    199          #self.assertRaises(xml.dom.InvalidAccessErr, v.getStringValue)
    200
    201 -    def test_CSS_UNKNOWN(self):
    202          "CSSPrimitiveValue.CSS_UNKNOWN"
    203          v = cssutils.css.CSSPrimitiveValue(u'expression(false)')
    204          self.assert_(v.valid == False)
    205          self.assert_(v.CSS_UNKNOWN == v.primitiveType)
    206          self.assert_('CSS_UNKNOWN' == v.primitiveTypeString)
    207
    208 -    def test_CSS_NUMBER_AND_OTHER_DIMENSIONS(self):
    209          "CSSPrimitiveValue.CSS_NUMBER .. CSS_DIMENSION"
    210          defs = [
    211              ('', 'CSS_NUMBER'),
    212              ('%', 'CSS_PERCENTAGE'),
    213              ('em', 'CSS_EMS'),
    214              ('ex', 'CSS_EXS'),
    215              ('px', 'CSS_PX'),
    216              ('cm', 'CSS_CM'),
    217              ('mm', 'CSS_MM'),
    218              ('in', 'CSS_IN'),
    219              ('pt', 'CSS_PT'),
    220              ('pc', 'CSS_PC'),
    221              ('deg', 'CSS_DEG'),
    222              ('rad', 'CSS_RAD'),
    223              ('grad', 'CSS_GRAD'),
    224              ('ms', 'CSS_MS'),
    225              ('s', 'CSS_S'),
    226              ('hz', 'CSS_HZ'),
    227              ('khz', 'CSS_KHZ'),
    228              ('other_dimension', 'CSS_DIMENSION')]
    229          for dim, name in defs:
    230              for n in (0, 1, 1.1, -1, -1.1, -0):
    231                  v = cssutils.css.CSSPrimitiveValue('%i%s' % (n, dim))
    232                  self.assert_(getattr(v, name) == v.primitiveType)
    233                  self.assert_(name == v.primitiveTypeString)
    234
    235 -    def test_CSS_STRING_AND_OTHER(self):
    236          "CSSPrimitiveValue.CSS_STRING .. CSS_RGBCOLOR"
    237          defs = [
    238              (('""', "''", '"some thing"', "' A\\ND '"), 'CSS_STRING'),
    239              (('url(a)', 'url("a b")', "url(' ')"), 'CSS_URI'),
    240              (('some', 'or_anth-er'), 'CSS_IDENT'),
    241              (('attr(a)', 'attr(b)'), 'CSS_ATTR'),
    242              (('counter(1)', 'counter(2)'), 'CSS_COUNTER'),
    243              (('rect(1,2,3,4)',), 'CSS_RECT'),
    244              (('rgb(1,2,3)', 'rgb(10%, 20%, 30%)', '#123', '#123456'),
    245                   'CSS_RGBCOLOR'),
    246              (('rgba(1,2,3,4)','rgba(10%, 20%, 30%, 40%)', ),
    247                   'CSS_RGBACOLOR')
    248              ]
    249          for examples, name in defs:
    250              for x in examples:
    251                  v = cssutils.css.CSSPrimitiveValue(x)
    252                  #self.assertEqual(getattr(v, name), v.primitiveType)
    253                  self.assertEqual(name, v.primitiveTypeString)
    254
    255 -    def test_getFloat(self):
    256          "CSSPrimitiveValue.getFloatValue()"
    257          # NOT TESTED are float values as it seems difficult to
    258          # compare these. Maybe use decimal.Decimal?
    259
    260          v = cssutils.css.CSSPrimitiveValue(u'1px')
    261          tests = {
    262              '0': (v.CSS_NUMBER, 0),
    263              '-1.1': (v.CSS_NUMBER, -1.1),
    264              '1%': (v.CSS_PERCENTAGE, 1),
    265              '-1%': (v.CSS_PERCENTAGE, -1),
    266              '1em': (v.CSS_EMS, 1),
    267              '-1.1em': (v.CSS_EMS, -1.1),
    268              '1ex': (v.CSS_EXS, 1),
    269              '1px': (v.CSS_PX, 1),
    270
    271              '1cm': (v.CSS_CM, 1),
    272              '1cm': (v.CSS_MM, 10),
    273              '254cm': (v.CSS_IN, 100),
    274              '1mm': (v.CSS_MM, 1),
    275              '10mm': (v.CSS_CM, 1),
    276              '254mm': (v.CSS_IN, 10),
    277              '1in': (v.CSS_IN, 1),
    278              '100in': (v.CSS_CM, 254), # ROUNDED!!!
    279              '10in': (v.CSS_MM, 254), # ROUNDED!!!
    280
    281              '1pt': (v.CSS_PT, 1),
    282              '1pc': (v.CSS_PC, 1),
    283
    284              '1deg': (v.CSS_DEG, 1),
    285              '1rad': (v.CSS_RAD, 1),
    286              '1grad': (v.CSS_GRAD, 1),
    287
    288              '1ms': (v.CSS_MS, 1),
    289              '1000ms': (v.CSS_S, 1),
    290              '1s': (v.CSS_S, 1),
    291              '1s': (v.CSS_MS, 1000),
    292
    293              '1hz': (v.CSS_HZ, 1),
    294              '1000hz': (v.CSS_KHZ, 1),
    295              '1khz': (v.CSS_KHZ, 1),
    296              '1khz': (v.CSS_HZ, 1000),
    297
    298              '1DIMENSION': (v.CSS_DIMENSION, 1)
    299              }
    300          for cssText in tests:
    301              v.cssText = cssText
    302              unitType, exp = tests[cssText]
    303              val = v.getFloatValue(unitType)
    304              if unitType in (v.CSS_IN, v.CSS_CM):
    305                  val = round(val)
    306              self.assertEqual(val , exp)
    307
    308 -    def test_setFloat(self):
    309          "CSSPrimitiveValue.setFloatValue()"
    310          V = cssutils.css.CSSPrimitiveValue
    311
    312          tests = {
    313              # unitType, value
    314              (V.CSS_NUMBER, 1): [
    315                  # unitType, setvalue,
    316                  #    getvalue or expected exception, msg or cssText
    317                  (V.CSS_NUMBER, 0, 0, '0'),
    318                  (V.CSS_NUMBER, 0.1, 0.1, '0.1'),
    319                  (V.CSS_NUMBER, -0, 0, '0'),
    320                  (V.CSS_NUMBER, 2, 2, '2'),
    321                  (V.CSS_NUMBER, 2.0, 2, '2'),
    322                  (V.CSS_NUMBER, 2.1, 2.1, '2.1'),
    323                  (V.CSS_NUMBER, -2.1, -2.1, '-2.1'),
    324                  # setting with string does work
    325                  (V.CSS_NUMBER, '1', 1, '1'),
    326                  (V.CSS_NUMBER, '1.1', 1.1, '1.1'),
    327                  (V.CSS_PX, 1, xml.dom.InvalidAccessErr, None),
    328                  (V.CSS_DEG, 1, xml.dom.InvalidAccessErr, None),
    329                  (V.CSS_RAD, 1, xml.dom.InvalidAccessErr, None),
    330                  (V.CSS_GRAD, 1, xml.dom.InvalidAccessErr, None),
    331                  (V.CSS_S, 1, xml.dom.InvalidAccessErr, None),
    332                  (V.CSS_MS, 1, xml.dom.InvalidAccessErr, None),
    333                  (V.CSS_KHZ, 1, xml.dom.InvalidAccessErr, None),
    334                  (V.CSS_HZ, 1, xml.dom.InvalidAccessErr, None),
    335                  (V.CSS_DIMENSION, 1, xml.dom.InvalidAccessErr, None),
    336                  (V.CSS_MM, 2, xml.dom.InvalidAccessErr, None),
    337
    338                  (V.CSS_NUMBER, 'x', xml.dom.InvalidAccessErr,
    339                   "CSSPrimitiveValue: floatValue 'x' is not a float"),
    340                  (V.CSS_NUMBER, '1x', xml.dom.InvalidAccessErr,
    341                   "CSSPrimitiveValue: floatValue '1x' is not a float"),
    342
    343                  (V.CSS_STRING, 'x', xml.dom.InvalidAccessErr,
    344                   "CSSPrimitiveValue: unitType 'CSS_STRING' is not a float type"),
    345                  (V.CSS_URI, 'x', xml.dom.InvalidAccessErr,
    346                   "CSSPrimitiveValue: unitType 'CSS_URI' is not a float type"),
    347                  (V.CSS_ATTR, 'x', xml.dom.InvalidAccessErr,
    348                   "CSSPrimitiveValue: unitType 'CSS_ATTR' is not a float type"),
    349                  (V.CSS_IDENT, 'x', xml.dom.InvalidAccessErr,
    350                   "CSSPrimitiveValue: unitType 'CSS_IDENT' is not a float type"),
    351                  (V.CSS_RGBCOLOR, 'x', xml.dom.InvalidAccessErr,
    352                   "CSSPrimitiveValue: unitType 'CSS_RGBCOLOR' is not a float type"),
    353                  (V.CSS_RGBACOLOR, 'x', xml.dom.InvalidAccessErr,
    354                   "CSSPrimitiveValue: unitType 'CSS_RGBACOLOR' is not a float type"),
    355                  (V.CSS_RECT, 'x', xml.dom.InvalidAccessErr,
    356                   "CSSPrimitiveValue: unitType 'CSS_RECT' is not a float type"),
    357                  (V.CSS_COUNTER, 'x', xml.dom.InvalidAccessErr,
    358                   "CSSPrimitiveValue: unitType 'CSS_COUNTER' is not a float type"),
    359                  (V.CSS_EMS, 1, xml.dom.InvalidAccessErr,
    360                   "CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_NUMBER' to 'CSS_EMS'"),
    361                  (V.CSS_EXS, 1, xml.dom.InvalidAccessErr,
    362                   "CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_NUMBER' to 'CSS_EXS'")
    363              ],
    364              (V.CSS_MM, '1mm'): [
    365                  (V.CSS_MM, 2, 2, '2mm'),
    366                  (V.CSS_MM, 0, 0, '0mm'),
    367                  (V.CSS_MM, 0.1, 0.1, '0.1mm'),
    368                  (V.CSS_MM, -0, -0, '0mm'),
    369                  (V.CSS_MM, 3.0, 3, '3mm'),
    370                  (V.CSS_MM, 3.1, 3.1, '3.1mm'),
    371                  (V.CSS_MM, -3.1, -3.1, '-3.1mm'),
    372                  (V.CSS_CM, 1, 10, '10mm'),
    373                  (V.CSS_IN, 10, 254, '254mm'),
    374                  (V.CSS_PT, 1, 1828.8, '1828.8mm'),
    375                  (V.CSS_PX, 1, xml.dom.InvalidAccessErr, None),
    376                  (V.CSS_NUMBER, 2, xml.dom.InvalidAccessErr, None)
    377              ],
    378              (V.CSS_PT, '1pt'): [
    379                  (V.CSS_PT, 2, 2, '2pt'),
    380                  (V.CSS_PC, 12, 1, '1pt'),
    381                  (V.CSS_NUMBER, 1, xml.dom.InvalidAccessErr, None),
    382                  (V.CSS_DEG, 1, xml.dom.InvalidAccessErr, None),
    383                  (V.CSS_PX, 1, xml.dom.InvalidAccessErr, None)
    384              ],
    385              (V.CSS_KHZ, '1khz'): [
    386                  (V.CSS_HZ, 2000, 2, '2khz'),
    387                  (V.CSS_NUMBER, 1, xml.dom.InvalidAccessErr, None),
    388                  (V.CSS_DEG, 1, xml.dom.InvalidAccessErr, None),
    389                  (V.CSS_PX, 1, xml.dom.InvalidAccessErr, None)
    390              ]
    391          }
    392          for test in tests:
    393              initialType, initialValue = test
    394              pv = cssutils.css.CSSPrimitiveValue(initialValue)
    395              for setType, setValue, exp, msg in tests[test]:
    396                  if type(exp) == types.TypeType or\
    397                     type(exp) == types.ClassType: # 2.4 compactibility
    398                      if msg:
    399                          self.assertRaisesMsg(
    400                              exp, msg, pv.setFloatValue, setType, setValue)
    401                      else:
    402                          self.assertRaises(
    403                              exp, pv.setFloatValue, setType, setValue)
    404                  else:
    405                      pv.setFloatValue(setType, setValue)
    406                      self.assertEqual(pv.cssText, msg)
    407                      self.assertEqual(pv.getFloatValue(initialType), exp)
    408                      self.assertEqual(pv._value, msg)
    409
    410 -    def test_getString(self):
    411          "CSSPrimitiveValue.getStringValue()"
    412          v = cssutils.css.CSSPrimitiveValue(u'1px')
    413          self.assert_(v.primitiveType == v.CSS_PX)
    414          self.assertRaises(xml.dom.InvalidAccessErr,
    415                            v.getStringValue)
    416
    417          pv = cssutils.css.CSSPrimitiveValue
    418          tests = {
    419              pv.CSS_STRING: ("'red'", 'red'),
    420              pv.CSS_STRING: ('"red"', 'red'),
    421              pv.CSS_URI: ('url(http://example.com)', None),
    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_URI: ('url("http://example.com?)")',
    427                           u'http://example.com?)'),
    428              pv.CSS_IDENT: ('red', None),
    429              pv.CSS_ATTR: ('attr(att-name)',
    430                           u'att-name'), # the name of the attrr
    431              }
    432          for t in tests:
    433              val, exp = tests[t]
    434              if not exp:
    435                  exp = val
    436
    437              v = cssutils.css.CSSPrimitiveValue(val)
    438              self.assertEqual(v.primitiveType, t)
    439              self.assertEqual(v.getStringValue(), exp)
    440
    441 -    def test_setString(self):
    442          "CSSPrimitiveValue.setStringValue()"
    443          # CSS_STRING
    444          v = cssutils.css.CSSPrimitiveValue(u'"a"')
    445          self.assert_(v.CSS_STRING == v.primitiveType)
    446          v.setStringValue(v.CSS_STRING, 'b')
    447          self.assert_('"b"' == v._value)
    448          self.assert_('b' == v.getStringValue())
    449          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    450              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_STRING' to 'CSS_URI'",
    451              v.setStringValue, *(v.CSS_URI, 'x'))
    452          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    453              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_STRING' to 'CSS_IDENT'",
    454              v.setStringValue, *(v.CSS_IDENT, 'x'))
    455          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    456              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_STRING' to 'CSS_ATTR'",
    457              v.setStringValue, *(v.CSS_ATTR, 'x'))
    458
    459          # CSS_IDENT
    460          v = cssutils.css.CSSPrimitiveValue('new')
    461          v.setStringValue(v.CSS_IDENT, 'ident')
    462          self.assert_(v.CSS_IDENT == v.primitiveType)
    463          self.assert_('ident' == v._value)
    464          self.assert_('ident' == v.getStringValue())
    465          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    466              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_IDENT' to 'CSS_URI'",
    467              v.setStringValue, *(v.CSS_URI, 'x'))
    468          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    469              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_IDENT' to 'CSS_STRING'",
    470              v.setStringValue, *(v.CSS_STRING, '"x"'))
    471          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    472              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_IDENT' to 'CSS_ATTR'",
    473              v.setStringValue, *(v.CSS_ATTR, 'x'))
    474
    475          # CSS_URI
    476          v = cssutils.css.CSSPrimitiveValue('url(old)')
    477          v.setStringValue(v.CSS_URI, '(')
    478          self.assertEqual(u'url("(")', v._value)
    479          self.assertEqual(u'(', v.getStringValue())
    480
    481          v.setStringValue(v.CSS_URI, ')')
    482          self.assertEqual(u'url(")")', v._value)
    483          self.assertEqual(u')', v.getStringValue())
    484          self.assertEqual(u'url(")")', v._value)
    485
    486          v.setStringValue(v.CSS_URI, '""')
    487          self.assertEqual(ur'""', v.getStringValue())
    488          self.assertEqual(ur'url("\"\"")', v._value)
    489
    490          v.setStringValue(v.CSS_URI, ',')
    491          self.assertEqual(u'url(",")', v._value)
    492          self.assertEqual(u',', v.getStringValue())
    493
    494          v.setStringValue(v.CSS_URI, ' ')
    495          self.assertEqual(u'url(" ")', v._value)
    496          self.assertEqual(u' ', v.getStringValue())
    497
    498          v.setStringValue(v.CSS_URI, 'a)')
    499          self.assertEqual(u'url("a)")', v._value)
    500          self.assertEqual(u'a)', v.getStringValue())
    501
    502          v.setStringValue(v.CSS_URI, 'a')
    503          self.assert_(v.CSS_URI == v.primitiveType)
    504          self.assert_('url(a)' == v._value)
    505          self.assert_('a' == v.getStringValue())
    506          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    507              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_URI' to 'CSS_IDENT'",
    508              v.setStringValue, *(v.CSS_IDENT, 'x'))
    509          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    510              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_URI' to 'CSS_STRING'",
    511              v.setStringValue, *(v.CSS_STRING, '"x"'))
    512          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    513              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_URI' to 'CSS_ATTR'",
    514              v.setStringValue, *(v.CSS_ATTR, 'x'))
    515
    516          # CSS_ATTR
    517          v = cssutils.css.CSSPrimitiveValue('attr(old)')
    518          v.setStringValue(v.CSS_ATTR, 'a')
    519          self.assert_(v.CSS_ATTR == v.primitiveType)
    520          self.assert_('attr(a)' == v._value)
    521          self.assert_('a' == v.getStringValue())
    522          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    523              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_ATTR' to 'CSS_IDENT'",
    524              v.setStringValue, *(v.CSS_IDENT, 'x'))
    525          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    526              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_ATTR' to 'CSS_STRING'",
    527              v.setStringValue, *(v.CSS_STRING, '"x"'))
    528          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    529              u"CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_ATTR' to 'CSS_URI'",
    530              v.setStringValue, *(v.CSS_URI, 'x'))
    531
    532          # TypeError as 'x' is no valid type
    533          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    534              u"CSSPrimitiveValue: stringType 'x' (UNKNOWN TYPE) is not a string type",
    535              v.setStringValue, *('x', 'brown'))
    536          # IndexError as 111 is no valid type
    537          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    538              u"CSSPrimitiveValue: stringType 111 (UNKNOWN TYPE) is not a string type",
    539              v.setStringValue, *(111, 'brown'))
    540          # CSS_PX is no string type
    541          self.assertRaisesMsg(xml.dom.InvalidAccessErr,
    542              u"CSSPrimitiveValue: stringType CSS_PX is not a string type",
    543              v.setStringValue, *(v.CSS_PX, 'brown'))
    544
    545  #    def test_typeRGBColor(self):
    546  #        v = cssutils.cssprimitivevalue.PrimitiveValue('RGB(1, 5, 10)')
    547  #        self.assertEqual(v.CSS_RGBCOLOR, v.primitiveType)
    548  #        self.assertEqual(u'rgb(1, 5, 10)', v.cssText)
    549  #
    550  #        v = cssutils.cssprimitivevalue.PrimitiveValue('rgb(1, 5, 10)')
    551  #        self.assertEqual(v.CSS_RGBCOLOR, v.primitiveType)
    552  #        self.assertEqual(u'rgb(1, 5, 10)', v.cssText)
    553  #
    554  #        v = cssutils.cssprimitivevalue.PrimitiveValue('rgb(1%, 5%, 10%)')
    555  #        self.assertEqual(v.CSS_RGBCOLOR, v.primitiveType)
    556  #        self.assertEqual(u'rgb(1.0%, 5.0%, 10.0%)', v.cssText)
    557  #
    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)')
    561  #        self.assertEqual(v.CSS_RGBCOLOR, v.primitiveType)
    562  #        v = cssutils.cssprimitivevalue.PrimitiveValue('rgb(1%, .5%, 10.1%)')
    563  #        self.assertEqual(v.CSS_RGBCOLOR, v.primitiveType)
    564
    565 -    def test_reprANDstr(self):
    566          "CSSPrimitiveValue.__repr__(), .__str__()"
    567          v='111'
    568
    569          s = cssutils.css.CSSPrimitiveValue(v)
    570
    571          self.assert_(v in str(s))
    572          self.assert_('CSS_NUMBER' in str(s))
    573
    574          s2 = eval(repr(s))
    575          self.assert_(isinstance(s2, s.__class__))
    576          self.assert_(v == s2.cssText)
    577
    578
    579 -class CSSValueListTestCase(basetest.BaseTestCase):
    580
    581 -    def test_init(self):
    582          "CSSValueList.__init__()"
    583          v = cssutils.css.CSSValue(cssText=u'red blue', _propertyName='border-color')
    584          self.assert_(v.CSS_VALUE_LIST == v.cssValueType)
    585          self.assertEqual('red blue', v._value)
    586          self.assertEqual('red blue', v.cssText)
    587          self.assert_(True is v.valid)
    588
    589          self.assert_(2 == v.length)
    590
    591          item = v.item(0)
    592          item.setStringValue(item.CSS_IDENT, 'green')
    593          self.assertEqual('green blue', v._value)
    594          self.assertEqual('green blue', v.cssText)
    595
    596 -    def test_numbers(self):
    597          "CSSValueList.cssText"
    598          tests = {
    599              u'1 2 3 4': (None, 4),
    600              u'-1 -2 -3 -4': (None, 4),
    601              u'-1 2': (None, 2),
    602              u'-1px red "x"': (None, 3),
    603              u'-1cm, 2 3': (None, 2)
    604              }
    605          for test in tests:
    606              exp, num = tests[test]
    607              if not exp:
    608                  exp = test
    609              v = cssutils.css.CSSValue(cssText=test)
    610              self.assert_(v.CSS_VALUE_LIST == v.cssValueType)
    611              self.assertEqual(exp, v._value)
    612              self.assertEqual(num, v.length)
    613          self.assertEqual(exp, v.cssText)
    614
    615 -    def test_reprANDstr(self):
    616          "CSSValueList.__repr__(), .__str__()"
    617          v='1px 2px'
    618
    619          s = cssutils.css.CSSValue(v)
    620          self.assert_(isinstance(s, cssutils.css.CSSValueList))
    621
    622          self.assert_(v in str(s))
    623
    624          # not "eval()"able!
    625          #s2 = eval(repr(s))
    626
    627
    628  if __name__ == '__main__':
    629      import unittest
    630      unittest.main()
    631