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  __version__ = '$Id: test_parse.py 1116 2008-03-05 13:52:23Z cthedot $' 
  4   
  5  import xml.dom 
  6  import basetest 
  7  import cssutils 
  8   
9 -class CSSStyleSheetTestCase(basetest.BaseTestCase):
10
11 - def test_parseString(self):
12 tests = { 13 # for unicode encoding is ignored 14 (u'@namespace "a";', 'BOGUS'): u'@namespace "a";', 15 ('@namespace "a";', None): u'@namespace "a";', 16 ('@namespace "a";', 'ascii'): u'@namespace "a";', 17 # automatic convertion 18 ('@namespace "b";', 'ascii'): '@namespace "b";', 19 # result is str not unicode 20 ('@namespace "\xc3\xa4";', None): '@namespace "\xc3\xa4";', 21 ('@namespace "\xc3\xa4";', 'utf-8'): '@namespace "\xc3\xa4";' 22 } 23 for test in tests: 24 css, encoding = test 25 sheet = cssutils.parseString(css, encoding=encoding) 26 self.assertEqual(tests[test], sheet.cssText)
27
28 - def test_roundtrip(self):
29 "cssutils encodings" 30 css1 = ur'''@charset "utf-8"; 31 /* ä */''' 32 s = cssutils.parseString(css1) 33 css2 = unicode(s.cssText, 'utf-8') 34 self.assertEqual(css1, css2) 35 36 s = cssutils.parseString(css2) 37 s.cssRules[0].encoding='ascii' 38 css3 = ur'''@charset "ascii"; 39 /* \E4 */''' 40 self.assertEqual(css3, unicode(s.cssText, 'utf-8'))
41
42 - def test_escapes(self):
43 "cssutils escapes" 44 css = ur'\43\x { \43\x: \43\x !import\41nt }' 45 sheet = cssutils.parseString(css) 46 self.assertEqual(sheet.cssText, ur'''C\x { 47 c\x: C\x !important 48 }''') 49 50 css = ur'\ x{\ x :\ x ;y:1} ' 51 sheet = cssutils.parseString(css) 52 self.assertEqual(sheet.cssText, ur'''\ x { 53 \ x: \ x; 54 y: 1 55 }''')
56
57 - def test_invalidstring(self):
58 "cssutils.parseString(INVALID_STRING)" 59 validfromhere = '@namespace "x";' 60 csss = ( 61 u'''@charset "ascii 62 ;''' + validfromhere, 63 u'''@charset 'ascii 64 ;''' + validfromhere, 65 u'''@namespace "y 66 ;''' + validfromhere, 67 u'''@import "y 68 ;''' + validfromhere, 69 u'''@import url('a 70 );''' + validfromhere, 71 u'''@unknown "y 72 ;''' + validfromhere) 73 for css in csss: 74 s = cssutils.parseString(css) 75 self.assertEqual(validfromhere, s.cssText) 76 77 css = u'''a { font-family: "Courier 78 ; }''' 79 s = cssutils.parseString(css) 80 self.assertEqual(u'', s.cssText)
81
82 - def test_invalid(self):
83 "cssutils.parseString(INVALID_CSS)" 84 tests = { 85 u'a {color: blue}} a{color: red} a{color: green}': 86 u'''a { 87 color: blue 88 } 89 a { 90 color: green 91 }''' 92 } 93 94 for css in tests: 95 exp = tests[css] 96 if exp == None: 97 exp = css 98 s = cssutils.parseString(css) 99 self.assertEqual(exp, s.cssText)
100
101 - def test_nesting(self):
102 "cssutils.parseString nesting" 103 # examples from csslist 27.11.2007 104 tests = { 105 '@1; div{color:green}': u'div {\n color: green\n }', 106 '@1 []; div{color:green}': u'div {\n color: green\n }', 107 '@1 [{}]; div { color:green; }': u'div {\n color: green\n }', 108 '@media all { @ } div{color:green}': 109 u'div {\n color: green\n }', 110 # should this be u''? 111 '@1 { [ } div{color:green}': u'', 112 # red was eaten: 113 '@1 { [ } ] div{color:red}div{color:green}': u'div {\n color: green\n }', 114 } 115 for css, exp in tests.items(): 116 self.assertEqual(exp, cssutils.parseString(css).cssText)
117
118 - def test_specialcases(self):
119 "cssutils.parseString(special_case)" 120 tests = { 121 u''' 122 a[title="a not s\ 123 o very long title"] {/*...*/}''': u'''a[title="a not so very long title"] { 124 /*...*/ 125 }''' 126 } 127 for css in tests: 128 exp = tests[css] 129 if exp == None: 130 exp = css 131 s = cssutils.parseString(css) 132 self.assertEqual(exp, s.cssText)
133
134 - def test_iehack(self):
135 "IEhack: $property" 136 # $color is not color! 137 css = 'a { color: green; $color: red; }' 138 s = cssutils.parseString(css) 139 140 p1 = s.cssRules[0].style.getProperty('color') 141 self.assertEqual('color', p1.name) 142 self.assertEqual('color', p1.literalname) 143 self.assertEqual('color', p1.normalname) # DEPRECATED 144 self.assertEqual('red', s.cssRules[0].style.getPropertyValue('$color')) 145 146 p2 = s.cssRules[0].style.getProperty('$color') 147 self.assertEqual('$color', p2.name) 148 self.assertEqual('$color', p2.literalname) 149 self.assertEqual('$color', p2.normalname) # DEPRECATED 150 self.assertEqual('green', s.cssRules[0].style.getPropertyValue('color')) 151 self.assertEqual('green', s.cssRules[0].style.color)
152
153 - def test_attributes(self):
154 "cssutils.parseString(href, media)" 155 s = cssutils.parseString("a{}", href="file:foo.css", media="screen, projection, tv") 156 self.assertEqual(s.href, "file:foo.css") 157 self.assertEqual(s.media.mediaText, "screen, projection, tv") 158 159 s = cssutils.parseString("a{}", href="file:foo.css", media=["screen", "projection", "tv"]) 160 self.assertEqual(s.media.mediaText, "screen, projection, tv")
161
162 - def tearDown(self):
163 # needs to be reenabled here for other tests 164 cssutils.log.raiseExceptions = True
165 166 167 if __name__ == '__main__': 168 import unittest 169 unittest.main() 170