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

Source Code for Module cssutils.tests.test_parse

  1  # -*- coding: utf-8 -*- 
  2  """tests for parsing which does not raise Exceptions normally 
  3  """ 
  4  __author__ = '$LastChangedBy: cthedot $' 
  5  __date__ = '$LastChangedDate: 2008-01-12 22:36:38 +0100 (Sa, 12 Jan 2008) $' 
  6  __version__ = '$LastChangedRevision: 833 $' 
  7   
  8  import xml.dom 
  9  import basetest 
 10  import cssutils 
 11   
12 -class CSSStyleSheetTestCase(basetest.BaseTestCase):
13
14 - def test_roundtrip(self):
15 "cssutils encodings" 16 css1 = ur'''@charset "utf-8"; 17 /* ä */''' 18 s = cssutils.parseString(css1) 19 css2 = unicode(s.cssText, 'utf-8') 20 self.assertEqual(css1, css2) 21 22 s = cssutils.parseString(css2) 23 s.cssRules[0].encoding='ascii' 24 css3 = ur'''@charset "ascii"; 25 /* \E4 */''' 26 self.assertEqual(css3, unicode(s.cssText, 'utf-8'))
27
28 - def test_escapes(self):
29 "cssutils escapes" 30 css = ur'\43\x { \43\x: \43\x !import\41nt }' 31 sheet = cssutils.parseString(css) 32 self.assertEqual(sheet.cssText, ur'''C\x { 33 c\x: C\x !important 34 }''') 35 36 css = ur'\ x{\ x :\ x ;y:1} ' 37 sheet = cssutils.parseString(css) 38 self.assertEqual(sheet.cssText, ur'''\ x { 39 \ x: \ x; 40 y: 1 41 }''')
42
43 - def test_invalidstring(self):
44 "cssutils.parseString(INVALID_STRING)" 45 validfromhere = '@namespace "x";' 46 csss = ( 47 u'''@charset "ascii 48 ;''' + validfromhere, 49 u'''@charset 'ascii 50 ;''' + validfromhere, 51 u'''@namespace "y 52 ;''' + validfromhere, 53 u'''@import "y 54 ;''' + validfromhere, 55 u'''@import url('a 56 );''' + validfromhere, 57 u'''@unknown "y 58 ;''' + validfromhere) 59 for css in csss: 60 s = cssutils.parseString(css) 61 self.assertEqual(validfromhere, s.cssText) 62 63 css = u'''a { font-family: "Courier 64 ; }''' 65 s = cssutils.parseString(css) 66 self.assertEqual(u'', s.cssText)
67
68 - def test_invalid(self):
69 "cssutils.parseString(INVALID_CSS)" 70 tests = { 71 u'a {color: blue}} a{color: red} a{color: green}': 72 u'''a { 73 color: blue 74 } 75 a { 76 color: green 77 }''' 78 } 79 80 for css in tests: 81 exp = tests[css] 82 if exp == None: 83 exp = css 84 s = cssutils.parseString(css) 85 self.assertEqual(exp, s.cssText)
86
87 - def test_specialcases(self):
88 "cssutils.parseString(special_case)" 89 tests = { 90 u''' 91 a[title="a not s\ 92 o very long title"] {/*...*/}''': u'''a[title="a not so very long title"] { 93 /*...*/ 94 }''' 95 } 96 for css in tests: 97 exp = tests[css] 98 if exp == None: 99 exp = css 100 s = cssutils.parseString(css) 101 self.assertEqual(exp, s.cssText)
102
103 - def test_iehack(self):
104 "IEhack: $property" 105 # $color is not color! 106 css = 'a { color: green; $color: red; }' 107 s = cssutils.parseString(css) 108 109 p1 = s.cssRules[0].style.getProperty('color') 110 self.assertEqual('color', p1.name) 111 self.assertEqual('color', p1.literalname) 112 self.assertEqual('color', p1.normalname) # DEPRECATED 113 self.assertEqual('red', s.cssRules[0].style.getPropertyValue('$color')) 114 115 p2 = s.cssRules[0].style.getProperty('$color') 116 self.assertEqual('$color', p2.name) 117 self.assertEqual('$color', p2.literalname) 118 self.assertEqual('$color', p2.normalname) # DEPRECATED 119 self.assertEqual('green', s.cssRules[0].style.getPropertyValue('color')) 120 self.assertEqual('green', s.cssRules[0].style.color)
121 122
123 - def test_attributes(self):
124 "cssutils.parseString(href, media)" 125 s = cssutils.parseString("a{}", href="file:foo.css", media="screen, projection, tv") 126 self.assertEqual(s.href, "file:foo.css") 127 self.assertEqual(s.media.mediaText, "screen, projection, tv") 128 129 s = cssutils.parseString("a{}", href="file:foo.css", media=["screen", "projection", "tv"]) 130 self.assertEqual(s.media.mediaText, "screen, projection, tv")
131
132 - def tearDown(self):
133 # needs to be reenabled here for other tests 134 cssutils.log.raiseExceptions = True
135 136 137 if __name__ == '__main__': 138 import unittest 139 unittest.main() 140