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

# Source Code for Module cssutils.tests.test_medialist

      1  # -*- coding: iso-8859-1 -*-
      2  """Testcases for cssutils.stylesheets.MediaList"""
      3  __version__ = '$Id: test_medialist.py 1116 2008-03-05 13:52:23Z cthedot $'
      4
      5  import xml.dom
      6  import basetest
      7  import cssutils.stylesheets
      8
      9 -class MediaListTestCase(basetest.BaseTestCase):
     10
     11 -    def setUp(self):
     12          super(MediaListTestCase, self).setUp()
     13          self.r = cssutils.stylesheets.MediaList()
     14
     15 -    def test_set(self):
     16          "MediaList.mediaText 1"
     17          ml = cssutils.stylesheets.MediaList()
     18
     19          self.assertEqual(0, ml.length)
     20          self.assertEqual(u'all', ml.mediaText)
     21
     22          ml.mediaText = u' print   , screen '
     23          self.assertEqual(2, ml.length)
     24          self.assertEqual(u'print, screen', ml.mediaText)
     25
     26          ml.mediaText = u' print , all  , screen '
     27          self.assertEqual(u'all', ml.mediaText)
     28          self.assertEqual(1, ml.length)
     29
     30          self.assertRaises(xml.dom.InvalidCharacterErr,
     31                            ml.appendMedium, u'test')
     32
     33 -    def test_appendMedium(self):
     34          "MediaList.appendMedium() 1"
     35          ml = cssutils.stylesheets.MediaList()
     36
     37          ml.appendMedium(u'print')
     38          self.assertEqual(1, ml.length)
     39          self.assertEqual(u'print', ml.mediaText)
     40
     41          ml.appendMedium(u'screen')
     42          self.assertEqual(2, ml.length)
     43          self.assertEqual(u'print, screen', ml.mediaText)
     44
     45          # automatic del and append!
     46          ml.appendMedium(u'print')
     47          self.assertEqual(2, ml.length)
     48          self.assertEqual(u'screen, print', ml.mediaText)
     49
     50          # automatic del and append!
     51          ml.appendMedium(u'SCREEN')
     52          self.assertEqual(2, ml.length)
     53          self.assertEqual(u'print, SCREEN', ml.mediaText)
     54
     55          # append invalid MediaQuery
     56          mq = cssutils.stylesheets.MediaQuery()
     57          ml.appendMedium(mq)
     58          self.assertEqual(2, ml.length)
     59          self.assertEqual(u'print, SCREEN', ml.mediaText)
     60
     61          # append()
     62          mq = cssutils.stylesheets.MediaQuery('tv')
     63          ml.append(mq)
     64          self.assertEqual(3, ml.length)
     65          self.assertEqual(u'print, SCREEN, tv', ml.mediaText)
     66
     67          # __setitem__
     68          self.assertRaises(IndexError, ml.__setitem__, 10, 'all')
     69          ml[0] = 'handheld'
     70          self.assertEqual(3, ml.length)
     71          self.assertEqual(u'handheld, SCREEN, tv', ml.mediaText)
     72
     73
     74 -    def test_appendAll(self):
     75          "MediaList.append() 2"
     76          ml = cssutils.stylesheets.MediaList()
     77          ml.appendMedium(u'print')
     78          ml.appendMedium(u'tv')
     79          self.assertEqual(2, ml.length)
     80          self.assertEqual(u'print, tv', ml.mediaText)
     81
     82          ml.appendMedium(u'all')
     83          self.assertEqual(1, ml.length)
     84          self.assertEqual(u'all', ml.mediaText)
     85
     86          ml.appendMedium(u'print')
     87          self.assertEqual(1, ml.length)
     88          self.assertEqual(u'all', ml.mediaText)
     89
     90          self.assertRaises(xml.dom.InvalidCharacterErr, ml.appendMedium, u'test')
     91
     92 -    def test_delete(self):
     93          "MediaList.deleteMedium()"
     94          ml = cssutils.stylesheets.MediaList()
     95
     96          self.assertRaises(xml.dom.NotFoundErr, ml.deleteMedium, u'all')
     97          self.assertRaises(xml.dom.NotFoundErr, ml.deleteMedium, u'test')
     98
     99          ml.appendMedium(u'print')
    100          ml.deleteMedium(u'print')
    101          ml.appendMedium(u'tV')
    102          ml.deleteMedium(u'Tv')
    103          self.assertEqual(0, ml.length)
    104          self.assertEqual(u'all', ml.mediaText)
    105
    106 -    def test_item(self):
    107          "MediaList.item()"
    108          ml = cssutils.stylesheets.MediaList()
    109          ml.appendMedium(u'print')
    110          ml.appendMedium(u'screen')
    111
    112          self.assertEqual(u'print', ml.item(0))
    113          self.assertEqual(u'screen', ml.item(1))
    114          self.assertEqual(None, ml.item(2))
    115
    116 -    def test_handheld(self):
    117          "MediaList handheld"
    118          ml = cssutils.stylesheets.MediaList()
    119
    120          ml.mediaText = u' handheld , all  , screen '
    121          self.assertEqual(2, ml.length)
    122          self.assertEqual(u'all, handheld', ml.mediaText)
    123
    124 -    def test_mediaText(self):
    125          "MediaList.mediaText 2"
    126          tests = {
    127              u'': u'all',
    128              u'ALL': u'ALL',
    129              u'Tv': u'Tv',
    130              u'all': None,
    131              u'all, handheld': None,
    132              u'tv': None,
    133              u'tv, handheld, print': None,
    134              u'tv and (color), handheld and (width: 1px) and (color)': None,
    135              }
    136          self.do_equal_r(tests, att='mediaText')
    137
    138          tests = {
    139              u'UNKNOWN': xml.dom.InvalidCharacterErr,
    140              u'a,b': xml.dom.InvalidCharacterErr,
    141              u'a and (color)': xml.dom.InvalidCharacterErr,
    142              u'not': xml.dom.SyntaxErr, # known but need media
    143              u'only': xml.dom.SyntaxErr, # known but need media
    144              u'not tv,': xml.dom.SyntaxErr, # known but need media
    145              u'all;': xml.dom.SyntaxErr,
    146              u'all, and(color)': xml.dom.SyntaxErr,
    147              u'all,': xml.dom.SyntaxErr,
    148              u'all, ': xml.dom.SyntaxErr,
    149              u'all ,': xml.dom.SyntaxErr,
    150              u'all, /*1*/': xml.dom.SyntaxErr,
    151              u'all and (color),': xml.dom.SyntaxErr,
    152              u'all tv, print': xml.dom.SyntaxErr,
    153              }
    154          self.do_raise_r(tests, att='_setMediaText')
    155
    156 -    def test_comments(self):
    157          "MediaList.mediaText comments"
    158          tests = {
    159              u'/*1*/ tv /*2*/, /*3*/ handheld /*4*/, print': None,
    160              }
    161          self.do_equal_r(tests, att='mediaText')
    162
    163 -    def test_reprANDstr(self):
    164          "MediaList.__repr__(), .__str__()"
    165          mediaText='tv, print'
    166
    167          s = cssutils.stylesheets.MediaList(mediaText=mediaText)
    168
    169          self.assert_(mediaText in str(s))
    170
    171          s2 = eval(repr(s))
    172          self.assert_(isinstance(s2, s.__class__))
    173          self.assert_(mediaText == s2.mediaText)
    174
    175
    176  if __name__ == '__main__':
    177      import unittest
    178      unittest.main()
    179