gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / cssutils / tests / test_cssrule.py @ 475
History | View | Annotate | Download (8.29 KB)
1 |
"""Testcases for cssutils.css.CSSRule"""
|
---|---|
2 |
|
3 |
import xml.dom |
4 |
import basetest |
5 |
import cssutils.css |
6 |
|
7 |
class CSSRuleTestCase(basetest.BaseTestCase): |
8 |
"""
|
9 |
base class for all CSSRule subclass tests
|
10 |
|
11 |
overwrite setUp with the appriopriate values, will be used in
|
12 |
test_init and test_readonly
|
13 |
overwrite all tests as you please, use::
|
14 |
|
15 |
super(CLASSNAME, self).test_TESTNAME(params)
|
16 |
|
17 |
to use the base class tests too
|
18 |
"""
|
19 |
def setUp(self): |
20 |
"""
|
21 |
OVERWRITE!
|
22 |
self.r is the rule
|
23 |
self.rRO the readonly rule
|
24 |
relf.r_type the type as defined in CSSRule
|
25 |
"""
|
26 |
super(CSSRuleTestCase, self).setUp() |
27 |
|
28 |
self.sheet = cssutils.css.CSSStyleSheet()
|
29 |
self.r = cssutils.css.CSSRule()
|
30 |
self.rRO = cssutils.css.CSSRule()
|
31 |
self.rRO._readonly = True # must be set here! |
32 |
self.r_type = cssutils.css.CSSRule.UNKNOWN_RULE
|
33 |
self.r_typeString = 'UNKNOWN_RULE' |
34 |
|
35 |
def tearDown(self): |
36 |
cssutils.ser.prefs.useDefaults() |
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 |
|
46 |
def test_parentRule_parentStyleSheet_type(self): |
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.rRO = cssutils.css.CSSRule()
|
185 |
self.rRO._readonly = True |
186 |
self.assertEqual(True, self.rRO._readonly) |
187 |
self.assertEqual(u'', self.rRO.cssText) |
188 |
self.assertRaises(xml.dom.NoModificationAllowedErr,
|
189 |
self.rRO._setCssText, u'x') |
190 |
self.assertEqual(u'', self.rRO.cssText) |
191 |
|
192 |
def _test_InvalidModificationErr(self, startwithspace): |
193 |
"""
|
194 |
CSSRule.cssText InvalidModificationErr
|
195 |
|
196 |
called by subclasses
|
197 |
|
198 |
startwithspace
|
199 |
|
200 |
for test starting with this not the test but " test" is tested
|
201 |
e.g. " @page {}"
|
202 |
exception is the style rule test
|
203 |
"""
|
204 |
tests = (u'',
|
205 |
u'/* comment */',
|
206 |
u'@charset "utf-8";',
|
207 |
u'@font-face {}',
|
208 |
u'@import url(x);',
|
209 |
u'@media all {}',
|
210 |
u'@namespace "x";'
|
211 |
u'@page {}',
|
212 |
u'@unknown;',
|
213 |
u'@variables;',
|
214 |
# TODO:
|
215 |
#u'@top-left {}'
|
216 |
u'a style rule {}'
|
217 |
) |
218 |
for test in tests: |
219 |
if startwithspace in (u'a style rule', ) and test in ( |
220 |
u'/* comment */', u'a style rule {}'): |
221 |
continue
|
222 |
|
223 |
if test.startswith(startwithspace):
|
224 |
test = u' %s' % test
|
225 |
|
226 |
self.assertRaises(xml.dom.InvalidModificationErr,
|
227 |
self.r._setCssText, test)
|
228 |
|
229 |
# check that type is readonly
|
230 |
self.assertRaises(AttributeError, self.r.__setattr__, 'parentRule', None) |
231 |
self.assertRaises(AttributeError, self.r.__setattr__, 'parentStyleSheet', None) |
232 |
self.assertRaises(AttributeError, self.r.__setattr__, 'type', 1) |
233 |
self.assertRaises(AttributeError, self.r.__setattr__, 'typeString', "") |
234 |
|
235 |
|
236 |
if __name__ == '__main__': |
237 |
import unittest |
238 |
unittest.main() |