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