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-02-11 20:28:23 +0100 (Mo, 11 Feb 2008) $' 
  6  __version__ = '$LastChangedRevision: 1004 $' 
  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_nesting(self):
88 "cssutils.parseString nesting" 89 # examples from csslist 27.11.2007 90 tests = { 91 '@1; div{color:green}': u'div {\n color: green\n }', 92 '@1 []; div{color:green}': u'div {\n color: green\n }', 93 '@1 [{}]; div { color:green; }': u'div {\n color: green\n }', 94 '@media all { @ } div{color:green}': 95 u'div {\n color: green\n }', 96 # should this be u''? 97 '@1 { [ } div{color:green}': u'', 98 # red was eaten: 99 '@1 { [ } ] div{color:red}div{color:green}': u'div {\n color: green\n }', 100 } 101 for css, exp in tests.items(): 102 self.assertEqual(exp, cssutils.parseString(css).cssText)
103
104 - def test_specialcases(self):
105 "cssutils.parseString(special_case)" 106 tests = { 107 u''' 108 a[title="a not s\ 109 o very long title"] {/*...*/}''': u'''a[title="a not so very long title"] { 110 /*...*/ 111 }''' 112 } 113 for css in tests: 114 exp = tests[css] 115 if exp == None: 116 exp = css 117 s = cssutils.parseString(css) 118 self.assertEqual(exp, s.cssText)
119
120 - def test_iehack(self):
121 "IEhack: $property" 122 # $color is not color! 123 css = 'a { color: green; $color: red; }' 124 s = cssutils.parseString(css) 125 126 p1 = s.cssRules[0].style.getProperty('color') 127 self.assertEqual('color', p1.name) 128 self.assertEqual('color', p1.literalname) 129 self.assertEqual('color', p1.normalname) # DEPRECATED 130 self.assertEqual('red', s.cssRules[0].style.getPropertyValue('$color')) 131 132 p2 = s.cssRules[0].style.getProperty('$color') 133 self.assertEqual('$color', p2.name) 134 self.assertEqual('$color', p2.literalname) 135 self.assertEqual('$color', p2.normalname) # DEPRECATED 136 self.assertEqual('green', s.cssRules[0].style.getPropertyValue('color')) 137 self.assertEqual('green', s.cssRules[0].style.color)
138
139 - def test_attributes(self):
140 "cssutils.parseString(href, media)" 141 s = cssutils.parseString("a{}", href="file:foo.css", media="screen, projection, tv") 142 self.assertEqual(s.href, "file:foo.css") 143 self.assertEqual(s.media.mediaText, "screen, projection, tv") 144 145 s = cssutils.parseString("a{}", href="file:foo.css", media=["screen", "projection", "tv"]) 146 self.assertEqual(s.media.mediaText, "screen, projection, tv")
147
148 - def tearDown(self):
149 # needs to be reenabled here for other tests 150 cssutils.log.raiseExceptions = True
151 152 153 if __name__ == '__main__': 154 import unittest 155 unittest.main() 156