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