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

Source Code for Module cssutils.tests.test_cssrule

  1  """testcases for cssutils.css.CSSRule 
  2  """ 
  3  __author__ = '$LastChangedBy: cthedot $' 
  4  __date__ = '$LastChangedDate: 2008-02-03 15:10:34 +0100 (So, 03 Feb 2008) $' 
  5  __version__ = '$LastChangedRevision: 980 $' 
  6   
  7  import xml.dom 
  8  import basetest 
  9  import cssutils.css 
 10   
11 -class CSSRuleTestCase(basetest.BaseTestCase):
12 """ 13 base class for all CSSRule subclass tests 14 15 overwrite setUp with the appriopriate values, will be used in 16 test_init and test_readonly 17 overwrite all tests as you please, use:: 18 19 super(CLASSNAME, self).test_TESTNAME(params) 20 21 to use the base class tests too 22 """
23 - def setUp(self):
24 """ 25 OVERWRITE! 26 self.r is the rule 27 self.rRO the readonly rule 28 relf.r_type the type as defined in CSSRule 29 """ 30 super(CSSRuleTestCase, self).setUp() 31 self.sheet = cssutils.css.CSSStyleSheet() 32 self.r = cssutils.css.CSSRule() 33 self.rRO = cssutils.css.CSSRule() 34 self.rRO._readonly = True # must be set here! 35 self.r_type = cssutils.css.CSSRule.UNKNOWN_RULE 36 self.r_typeString = 'UNKNOWN_RULE'
37
38 - def test_init(self):
39 "CSSRule.type and init" 40 self.assertEqual(self.r_type, self.r.type) 41 self.assertEqual(self.r_typeString, self.r.typeString) 42 self.assertEqual(u'', self.r.cssText) 43 self.assertEqual(None, self.r.parentRule) 44 self.assertEqual(None, self.r.parentStyleSheet)
45
47 "CSSRule.parentRule .parentStyleSheet .type" 48 rules = [ 49 ('@charset "ascii";', cssutils.css.CSSRule.CHARSET_RULE), 50 ('@import "x";', cssutils.css.CSSRule.IMPORT_RULE), 51 ('@namespace "x";', cssutils.css.CSSRule.NAMESPACE_RULE), 52 ('@font-face { src: url(x) }', cssutils.css.CSSRule.FONT_FACE_RULE), 53 ('''@media all { 54 @x; 55 a { color: red } 56 /* c */ 57 }''', cssutils.css.CSSRule.MEDIA_RULE), 58 ('@page :left { color: red }', cssutils.css.CSSRule.PAGE_RULE), 59 ('@unknown;', cssutils.css.CSSRule.UNKNOWN_RULE), 60 ('b { left: 0 }', cssutils.css.CSSRule.STYLE_RULE), 61 ('/*1*/', cssutils.css.CSSRule.COMMENT) # must be last for add test 62 ] 63 mrt = [cssutils.css.CSSRule.UNKNOWN_RULE, 64 cssutils.css.CSSRule.STYLE_RULE, 65 cssutils.css.CSSRule.COMMENT] 66 def test(s): 67 for i, rule in enumerate(s): 68 self.assertEqual(rule.parentRule, None) 69 self.assertEqual(rule.parentStyleSheet, s) 70 self.assertEqual(rule.type, rules[i][1]) 71 if rule.MEDIA_RULE == rule.type: 72 for j, r in enumerate(rule): 73 self.assertEqual(r.parentRule, rule) 74 self.assertEqual(r.parentStyleSheet, s) 75 self.assertEqual(r.type, mrt[j]) 76 77 if i == 0: # check encoding 78 self.assertEqual('ascii', s.encoding) 79 elif i == 2: # check namespaces 80 self.assertEqual('x', s.namespaces[''])
81 82 cssText = u''.join(r[0] for r in rules) 83 # parsing 84 s = cssutils.parseString(cssText) 85 test(s) 86 # sheet.cssText 87 s = cssutils.css.CSSStyleSheet() 88 s.cssText = cssText 89 test(s) 90 # sheet.add CSS 91 s = cssutils.css.CSSStyleSheet() 92 for css, type_ in rules: 93 s.add(css) 94 test(s) 95 # sheet.insertRule CSS 96 s = cssutils.css.CSSStyleSheet() 97 for css, type_ in rules: 98 s.insertRule(css) 99 test(s) 100 101 types = [cssutils.css.CSSCharsetRule, 102 cssutils.css.CSSImportRule, 103 cssutils.css.CSSNamespaceRule, 104 cssutils.css.CSSFontFaceRule, 105 cssutils.css.CSSMediaRule, 106 cssutils.css.CSSPageRule, 107 cssutils.css.CSSUnknownRule, 108 cssutils.css.CSSStyleRule, 109 cssutils.css.CSSComment] 110 # sheet.add CSSRule 111 s = cssutils.css.CSSStyleSheet() 112 for i, (css, type_) in enumerate(rules): 113 rule = types[i]() 114 rule.cssText = css 115 s.add(rule) 116 test(s) 117 # sheet.insertRule CSSRule 118 s = cssutils.css.CSSStyleSheet() 119 for i, (css, type_) in enumerate(rules): 120 rule = types[i]() 121 rule.cssText = css 122 s.insertRule(rule) 123 test(s)
124
125 - def test_CSSMediaRule_cssRules_parentRule_parentStyleSheet_type(self):
126 "CSSMediaRule.cssRules.parentRule .parentStyleSheet .type" 127 rules = [ 128 ('b { left: 0 }', cssutils.css.CSSRule.STYLE_RULE), 129 ('/*1*/', cssutils.css.CSSRule.COMMENT), 130 ('@x', cssutils.css.CSSRule.UNKNOWN_RULE) 131 ] 132 def test(s): 133 mr = s.cssRules[0] 134 for i, rule in enumerate(mr): 135 self.assertEqual(rule.parentRule, mr) 136 self.assertEqual(rule.parentStyleSheet, s) 137 self.assertEqual(rule.parentStyleSheet, mr.parentStyleSheet) 138 self.assertEqual(rule.type, rules[i][1])
139 140 cssText = '@media all { %s }' % u''.join(r[0] for r in rules) 141 # parsing 142 s = cssutils.parseString(cssText) 143 test(s) 144 # sheet.cssText 145 s = cssutils.css.CSSStyleSheet() 146 s.cssText = cssText 147 test(s) 148 149 def getMediaSheet(): 150 s = cssutils.css.CSSStyleSheet() 151 s.cssText = '@media all {}' 152 return s, s.cssRules[0] 153 # sheet.add CSS 154 s, mr = getMediaSheet() 155 for css, type_ in rules: 156 mr.add(css) 157 test(s) 158 # sheet.insertRule CSS 159 s, mr = getMediaSheet() 160 for css, type_ in rules: 161 mr.insertRule(css) 162 test(s) 163 164 types = [cssutils.css.CSSStyleRule, 165 cssutils.css.CSSComment, 166 cssutils.css.CSSUnknownRule] 167 # sheet.add CSSRule 168 s, mr = getMediaSheet() 169 for i, (css, type_) in enumerate(rules): 170 rule = types[i]() 171 rule.cssText = css 172 mr.add(rule) 173 test(s) 174 # sheet.insertRule CSSRule 175 s, mr = getMediaSheet() 176 for i, (css, type_) in enumerate(rules): 177 rule = types[i]() 178 rule.cssText = css 179 mr.insertRule(rule) 180 test(s) 181
182 - def test_readonly(self):
183 "CSSRule readonly" 184 self.assertEqual(True, self.rRO._readonly) 185 self.assertEqual(u'', self.rRO.cssText) 186 self.assertRaises(xml.dom.NoModificationAllowedErr, 187 self.rRO._setCssText, u'x') 188 self.assertEqual(u'', self.rRO.cssText)
189
190 - def _test_InvalidModificationErr(self, startwithspace):
191 """ 192 CSSRule.cssText InvalidModificationErr 193 194 called by subclasses 195 196 startwithspace 197 198 for test starting with this not the test but " test" is tested 199 e.g. " @page {}" 200 exception is the style rule test 201 """ 202 tests = (u'', 203 u'/* comment */', 204 u'@charset "utf-8";', 205 u'@font-face {}', 206 u'@import url(x);', 207 u'@media all {}', 208 u'@namespace "x";' 209 u'@page {}', 210 u'@unknown;', 211 u'a style rule {}' 212 ) 213 for test in tests: 214 if startwithspace in (u'a style rule', ) and test in ( 215 u'/* comment */', u'a style rule {}'): 216 continue 217 218 if test.startswith(startwithspace): 219 test = u' %s' % test 220 221 self.assertRaises(xml.dom.InvalidModificationErr, 222 self.r._setCssText, test)
223 224 225 if __name__ == '__main__': 226 import unittest 227 unittest.main() 228