Statistics
| Revision:

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_cssstyledeclaration.py @ 475

History | View | Annotate | Download (22.9 KB)

1
"""Testcases for cssutils.css.cssstyledelaration.CSSStyleDeclaration."""
2

    
3
import xml.dom
4
import basetest
5
import cssutils
6

    
7
class CSSStyleDeclarationTestCase(basetest.BaseTestCase):
8

    
9
    def setUp(self):
10
        self.r = cssutils.css.CSSStyleDeclaration()
11

    
12
    def test_init(self):
13
        "CSSStyleDeclaration.__init__()"
14
        s = cssutils.css.CSSStyleDeclaration()
15
        self.assertEqual(u'', s.cssText)
16
        self.assertEqual(0, s.length)
17
        self.assertEqual(None, s.parentRule)
18

    
19
        s = cssutils.css.CSSStyleDeclaration(cssText='left: 0')
20
        self.assertEqual(u'left: 0', s.cssText)
21
        self.assertEqual('0', s.getPropertyValue('left'))
22

    
23
        sheet = cssutils.css.CSSStyleRule()
24
        s = cssutils.css.CSSStyleDeclaration(parentRule=sheet)
25
        self.assertEqual(sheet, s.parentRule)
26

    
27
        # should not be used but ordered parameter test
28
        s = cssutils.css.CSSStyleDeclaration('top: 0', sheet)
29
        self.assertEqual(u'top: 0', s.cssText)
30
        self.assertEqual(sheet, s.parentRule)
31

    
32
    def test_items(self):
33
        "CSSStyleDeclaration[CSSName]"
34
        s = cssutils.css.CSSStyleDeclaration()
35
        name, value, priority = 'color', 'name', ''
36
        s[name] = value
37
        self.assertEqual(value, s[name])
38
        self.assertEqual(value, s.__getattribute__(name))
39
        self.assertEqual(value, s.getProperty(name).value)
40
        self.assertEqual(priority, s.getProperty(name).priority)
41

    
42
        name, value, priority = 'UnKnown-ProPERTY', 'unknown value', 'important'
43
        s[name] = (value, priority)
44
        self.assertEqual(value, s[name])
45
        self.assertEqual(value, s[name.lower()]) # will be normalized
46
        self.assertRaises(AttributeError, s.__getattribute__, name)
47
        self.assertEqual(value, s.getProperty(name).value)
48
        self.assertEqual(priority, s.getProperty(name).priority)
49

    
50
        name, value, priority = 'item', '1', ''
51
        s[name] = value
52
        self.assertEqual(value, s[name])
53
        self.assertEqual(value, s.getProperty(name).value)
54
        self.assertEqual(priority, s.getProperty(name).priority)
55

    
56
        del s[name]
57
        self.assertEqual(u'', s[name])
58
        self.assertEqual(u'', s['never set'])
59

    
60
    def test__contains__(self):
61
        "CSSStyleDeclaration.__contains__(nameOrProperty)"
62
        s = cssutils.css.CSSStyleDeclaration(cssText=r'x: 1;\y: 2')
63
        for test in ('x', r'x', 'y', r'y'):
64
            self.assertTrue(test in s)
65
            self.assertTrue(test.upper() in s)
66
            self.assertTrue(cssutils.css.Property(test, '1') in s)
67
        self.assertTrue('z' not in s)
68
        self.assertTrue(cssutils.css.Property('z', '1') not in s)
69

    
70
    def test__iter__item(self):
71
        "CSSStyleDeclaration.__iter__ and .item"
72
        s = cssutils.css.CSSStyleDeclaration()
73
        s.cssText = ur'''
74
            color: red; c\olor: blue; CO\lor: green;
75
            left: 1px !important; left: 0;
76
            border: 0;
77
        '''
78
        # __iter__
79
        ps = []
80
        for p in s:
81
            ps.append((p.literalname, p.value, p.priority))
82
        self.assertEqual(len(ps), 3)
83
        self.assertEqual(ps[0], (ur'co\lor', 'green', ''))
84
        self.assertEqual(ps[1], (ur'left', '1px', 'important'))
85
        self.assertEqual(ps[2], (ur'border', '0', ''))
86

    
87
        # item
88
        self.assertEqual(s.length, 3)
89
        self.assertEqual(s.item(0), u'color')
90
        self.assertEqual(s.item(1), u'left')
91
        self.assertEqual(s.item(2), u'border')
92
        self.assertEqual(s.item(10), u'')
93

    
94
    def test_keys(self):
95
        "CSSStyleDeclaration.keys()"
96
        s = cssutils.parseStyle('x:1; x:2; y:1')
97
        self.assertEqual(['x', 'y'], s.keys())
98
        self.assertEqual(s['x'], '2')
99
        self.assertEqual(s['y'], '1')
100

    
101
    def test_parse(self):
102
        "CSSStyleDeclaration parse"
103
        # error but parse
104
        tests = {
105
            # property names are caseinsensitive
106
            u'TOP:0': u'top: 0',
107
            u'top:0': u'top: 0',
108
            # simple escape
109
            u'c\\olor: red; color:green': u'color: green',
110
            u'color:g\\reen': u'color: g\\reen',
111
            # http://www.w3.org/TR/2009/CR-CSS2-20090423/syndata.html#illegalvalues
112
            u'color:green': u'color: green',
113
            u'color:green; color': u'color: green',
114
            u'color:red;   color; color:green': u'color: green',
115
            u'color:green; color:': u'color: green',
116
            u'color:red;   color:; color:green': u'color: green',
117
            u'color:green; color{;color:maroon}': u'color: green',
118
            u'color:red; color{;color:maroon}; color:green': u'color: green',
119
            # tantek hack
120
            ur'''color: red;
121
voice-family: "\"}\"";
122
voice-family:inherit;
123
color: green;''': 'voice-family: inherit;\ncolor: green',
124
            ur'''col\or: blue;
125
                font-family: 'Courier New Times
126
                color: red;
127
                color: green;''': u'color: green',
128

    
129
            # special IE hacks are not preserved anymore (>=0.9.5b3)
130
            u'/color: red; color: green': u'color: green',
131
            u'/ color: red; color: green': u'color: green',
132
            u'1px: red; color: green': u'color: green',
133
            u'0: red; color: green': u'color: green',
134
            u'1px:: red; color: green': u'color: green',
135
            ur'$top: 0': u'',
136
            ur'$: 0': u'', # really invalid!
137
            # unknown rule but valid
138
            u'@x;\ncolor: red': None,
139
            u'@x {\n    }\ncolor: red': None,
140
            u'/**/\ncolor: red': None,
141
            u'/**/\ncolor: red;\n/**/': None,
142
            #issue #28
143
            u';color: red': u'color: red',
144
            u';;color: red;;': u'color: red'
145
            }
146
        cssutils.ser.prefs.keepAllProperties = False
147
        for test, exp in tests.items():
148
            sh = cssutils.parseString('a { %s }' % test)
149
            if exp is None:
150
                exp = u'%s' % test
151
            elif exp != u'':
152
                exp = u'%s' % exp
153
            self.assertEqual(exp, sh.cssRules[0].style.cssText)
154

    
155
        cssutils.ser.prefs.useDefaults()
156

    
157
    def test_serialize(self):
158
        "CSSStyleDeclaration serialize"
159
        s = cssutils.css.CSSStyleDeclaration()
160
        tests = {
161
            u'a:1 !important; a:2;b:1': (u'a: 1 !important;\nb: 1',
162
                                         u'a: 1 !important;\na: 2;\nb: 1')
163
        }
164
        for test, exp in tests.items():
165
            s.cssText = test
166
            cssutils.ser.prefs.keepAllProperties = False
167
            self.assertEqual(exp[0], s.cssText)
168
            cssutils.ser.prefs.keepAllProperties = True
169
            self.assertEqual(exp[1], s.cssText)
170

    
171
        cssutils.ser.prefs.useDefaults()
172

    
173
    def test_children(self):
174
        "CSSStyleDeclaration.children()"
175
        style = u'/*1*/color: red; color: green; @x;'
176
        types = [
177
            cssutils.css.CSSComment,
178
            cssutils.css.Property,
179
            cssutils.css.Property,
180
            cssutils.css.CSSUnknownRule
181
        ]
182
        def t(s):
183
            for i, x in enumerate(s.children()):
184
                self.assertEqual(types[i], type(x))
185
                self.assertEqual(x.parent, s)
186

    
187
        t(cssutils.parseStyle(style))
188
        t(cssutils.parseString(u'a {'+style+'}').cssRules[0].style)
189
        t(cssutils.parseString(u'@media all {a {'+style+'}}').cssRules[0].cssRules[0].style)
190

    
191
        s = cssutils.parseStyle(style)
192
        s['x'] = '0'
193
        self.assertEqual(s, s.getProperty('x').parent)
194
        s.setProperty('y', '1')
195
        self.assertEqual(s, s.getProperty('y').parent)
196

    
197
    def test_cssText(self):
198
        "CSSStyleDeclaration.cssText"
199
        # empty
200
        s = cssutils.css.CSSStyleDeclaration()
201
        tests = {
202
            u'': u'',
203
            u' ': u'',
204
            u' \t \n  ': u'',
205
            u'/*x*/': u'/*x*/'
206
            }
207
        for test, exp in tests.items():
208
            s.cssText = 'left: 0;' # dummy to reset s
209
            s.cssText = test
210
            self.assertEqual(exp, s.cssText)
211

    
212
        # normal
213
        s = cssutils.css.CSSStyleDeclaration()
214
        tests = {
215
            u';': u'',
216
            u'left: 0': u'left: 0',
217
            u'left:0': u'left: 0',
218
            u' left : 0 ': u'left: 0',
219
            u'left: 0;': u'left: 0',
220
            u'left: 0 !important ': u'left: 0 !important',
221
            u'left:0!important': u'left: 0 !important',
222
            u'left: 0; top: 1': u'left: 0;\ntop: 1',
223
            # comments
224
            # TODO: spaces?
225
            u'/*1*//*2*/left/*3*//*4*/:/*5*//*6*/0/*7*//*8*/!/*9*//*a*/important/*b*//*c*/;':
226
                u'/*1*/\n/*2*/\nleft/*3*//*4*/: /*5*/ /*6*/ 0 /*7*/ /*8*/ !/*9*//*a*/important/*b*//*c*/',
227
            u'/*1*/left: 0;/*2*/ top: 1/*3*/':
228
                u'/*1*/\nleft: 0;\n/*2*/\ntop: 1 /*3*/',
229
            u'left:0; top:1;': u'left: 0;\ntop: 1',
230
            u'/*1*/left: 0;/*2*/ top: 1;/*3*/':
231
                u'/*1*/\nleft: 0;\n/*2*/\ntop: 1;\n/*3*/',
232
            # WS
233
            u'left:0!important;margin:1px 2px 3px 4px!important;': u'left: 0 !important;\nmargin: 1px 2px 3px 4px !important',
234
            u'\n\r\f\t left\n\r\f\t :\n\r\f\t 0\n\r\f\t !\n\r\f\t important\n\r\f\t ;\n\r\f\t margin\n\r\f\t :\n\r\f\t 1px\n\r\f\t 2px\n\r\f\t 3px\n\r\f\t 4px;':
235
            u'left: 0 !important;\nmargin: 1px 2px 3px 4px',
236
            }
237
        for test, exp in tests.items():
238
            s.cssText = test
239
            self.assertEqual(exp, s.cssText)
240

    
241
        # exception
242
        tests = {
243
            u'color: #xyz': xml.dom.SyntaxErr,
244
            u'top': xml.dom.SyntaxErr,
245
            u'top:': xml.dom.SyntaxErr,
246
            u'top : ': xml.dom.SyntaxErr,
247
            u'top:!important': xml.dom.SyntaxErr,
248
            u'top:!important;': xml.dom.SyntaxErr,
249
            u'top:;': xml.dom.SyntaxErr,
250
            u'top 0': xml.dom.SyntaxErr,
251
            u'top 0;': xml.dom.SyntaxErr,
252

    
253
            u':': xml.dom.SyntaxErr,
254
            u':0': xml.dom.SyntaxErr,
255
            u':0;': xml.dom.SyntaxErr,
256
            u':0!important': xml.dom.SyntaxErr,
257
            u':;': xml.dom.SyntaxErr,
258
            u': ;': xml.dom.SyntaxErr,
259
            u':!important;': xml.dom.SyntaxErr,
260
            u': !important;': xml.dom.SyntaxErr,
261

    
262
            u'0': xml.dom.SyntaxErr,
263
            u'0!important': xml.dom.SyntaxErr,
264
            u'0!important;': xml.dom.SyntaxErr,
265
            u'0;': xml.dom.SyntaxErr,
266

    
267
            u'!important': xml.dom.SyntaxErr,
268
            u'!important;': xml.dom.SyntaxErr,
269
            }
270
        self.do_raise_r(tests)
271

    
272
    def test_getCssText(self):
273
        "CSSStyleDeclaration.getCssText(separator)"
274
        s = cssutils.css.CSSStyleDeclaration(cssText=u'a:1;b:2')
275
        self.assertEqual(u'a: 1;\nb: 2', s.getCssText())
276
        self.assertEqual(u'a: 1;b: 2', s.getCssText(separator=u''))
277
        self.assertEqual(u'a: 1;/*x*/b: 2', s.getCssText(separator=u'/*x*/'))
278

    
279
    def test_parentRule(self):
280
        "CSSStyleDeclaration.parentRule"
281
        s = cssutils.css.CSSStyleDeclaration()
282
        sheet = cssutils.css.CSSStyleRule()
283
        s.parentRule = sheet
284
        self.assertEqual(sheet, s.parentRule)
285

    
286
        sheet = cssutils.parseString(u'a{x:1}')
287
        s = sheet.cssRules[0]
288
        d = s.style
289
        self.assertEqual(s, d.parentRule)
290

    
291
        s = cssutils.parseString('''
292
        @font-face {
293
            font-weight: bold;
294
            }
295
        a {
296
            font-weight: bolder;
297
            }
298
        @page {
299
            font-weight: bolder;
300
            }
301
        ''')
302
        for r in s:
303
            self.assertEqual(r.style.parentRule, r)
304

    
305
    def test_getProperty(self):
306
        "CSSStyleDeclaration.getProperty"
307
        s = cssutils.css.CSSStyleDeclaration()
308
        P = cssutils.css.Property
309
        s.cssText = ur'''
310
            color: red; c\olor: blue; CO\lor: green;
311
            left: 1px !important; left: 0;
312
            border: 0;
313
        '''
314
        self.assertEqual(s.getProperty('color').cssText, ur'co\lor: green')
315
        self.assertEqual(s.getProperty(r'COLO\r').cssText, ur'co\lor: green')
316
        self.assertEqual(s.getProperty('left').cssText, ur'left: 1px !important')
317
        self.assertEqual(s.getProperty('border').cssText, ur'border: 0')
318

    
319
    def test_getProperties(self):
320
        "CSSStyleDeclaration.getProperties()"
321
        s = cssutils.css.CSSStyleDeclaration(cssText=
322
                                             u'/*1*/y:0;x:a !important;y:1; \\x:b;')
323
        tests = {
324
            # name, all
325
            (None, False): [(u'y', u'1', u''),
326
                            (u'x', u'a', u'important')],
327
            (None, True): [(u'y', u'0', u''),
328
                           (u'x', u'a', u'important'),
329
                           (u'y', u'1', u''),
330
                           (u'\\x', u'b', u'')
331
                           ],
332
            ('x', False): [(u'x', u'a', u'important')],
333
            ('\\x', False): [(u'x', u'a', u'important')],
334
            ('x', True): [(u'x', u'a', u'important'),
335
                           (u'\\x', u'b', u'')],
336
            ('\\x', True): [(u'x', u'a', u'important'),
337
                           (u'\\x', u'b', u'')],
338
            }
339
        for test in tests:
340
            name, all = test
341
            expected = tests[test]
342
            actual = s.getProperties(name, all)
343
            self.assertEqual(len(expected), len(actual))
344
            for i, ex in enumerate(expected):
345
                a = actual[i]
346
                self.assertEqual(ex, (a.literalname, a.value, a.priority))
347

    
348
        # order is be effective properties set
349
        s = cssutils.css.CSSStyleDeclaration(cssText=
350
                                             u'a:0;b:1;a:1')
351
        self.assertEqual(u'ba', u''.join([p.name for p in s]))
352

    
353
    def test_getPropertyCSSValue(self):
354
        "CSSStyleDeclaration.getPropertyCSSValue()"
355
        s = cssutils.css.CSSStyleDeclaration(cssText='color: red;c\\olor: green')
356
        self.assertEqual(u'green', s.getPropertyCSSValue('color').cssText)
357
        self.assertEqual(u'green', s.getPropertyCSSValue('c\\olor').cssText)
358
        self.assertEqual(u'red', s.getPropertyCSSValue('color', False).cssText)
359
        self.assertEqual(u'green', s.getPropertyCSSValue('c\\olor', False).cssText)
360
#        # shorthand CSSValue should be None
361
#        SHORTHAND = [
362
#            u'background',
363
#            u'border',
364
#            u'border-left', u'border-right',
365
#            u'border-top', u'border-bottom',
366
#            u'border-color', u'border-style', u'border-width',
367
#            u'cue',
368
#            u'font',
369
#            u'list-style',
370
#            u'margin',
371
#            u'outline',
372
#            u'padding',
373
#            u'pause']
374
#        for short in SHORTHAND:
375
#            s.setProperty(short, u'inherit')
376
#            self.assertEqual(None, s.getPropertyCSSValue(short))
377

    
378
    def test_getPropertyValue(self):
379
        "CSSStyleDeclaration.getPropertyValue()"
380
        s = cssutils.css.CSSStyleDeclaration()
381
        self.assertEqual(u'', s.getPropertyValue('unset'))
382

    
383
        s.setProperty(u'left', '0')
384
        self.assertEqual(u'0', s.getPropertyValue('left'))
385

    
386
        s.setProperty(u'border', '1px  solid  green')
387
        self.assertEqual(u'1px solid green', s.getPropertyValue('border'))
388

    
389
        s = cssutils.css.CSSStyleDeclaration(cssText='color: red;c\\olor: green')
390
        self.assertEqual(u'green', s.getPropertyValue('color'))
391
        self.assertEqual(u'green', s.getPropertyValue('c\\olor'))
392
        self.assertEqual(u'red', s.getPropertyValue('color', False))
393
        self.assertEqual(u'green', s.getPropertyValue('c\\olor', False))
394

    
395
        tests = {
396
            ur'color: red; color: green': 'green',
397
            ur'c\olor: red; c\olor: green': 'green',
398
            ur'color: red; c\olor: green': 'green',
399
            ur'color: red !important; color: green !important': 'green',
400
            ur'color: green !important; color: red': 'green',
401
            }
402
        for test in tests:
403
            s = cssutils.css.CSSStyleDeclaration(cssText=test)
404
            self.assertEqual(tests[test], s.getPropertyValue('color'))
405

    
406
    def test_getPropertyPriority(self):
407
        "CSSStyleDeclaration.getPropertyPriority()"
408
        s = cssutils.css.CSSStyleDeclaration()
409
        self.assertEqual(u'', s.getPropertyPriority('unset'))
410

    
411
        s.setProperty(u'left', u'0', u'!important')
412
        self.assertEqual(u'important', s.getPropertyPriority('left'))
413

    
414
        s = cssutils.css.CSSStyleDeclaration(cssText=
415
            'x: 1 !important;\\x: 2;x: 3 !important;\\x: 4')
416
        self.assertEqual(u'important', s.getPropertyPriority('x'))
417
        self.assertEqual(u'important', s.getPropertyPriority('\\x'))
418
        self.assertEqual(u'important', s.getPropertyPriority('x', True))
419
        self.assertEqual(u'', s.getPropertyPriority('\\x', False))
420

    
421
    def test_removeProperty(self):
422
        "CSSStyleDeclaration.removeProperty()"
423
        cssutils.ser.prefs.useDefaults()
424
        s = cssutils.css.CSSStyleDeclaration()
425
        css = ur'\x:0 !important; x:1; \x:2; x:3'
426

    
427
        # normalize=True DEFAULT
428
        s.cssText = css
429
        self.assertEqual(u'0', s.removeProperty('x'))
430
        self.assertEqual(u'', s.cssText)
431

    
432
        # normalize=False
433
        s.cssText = css
434
        self.assertEqual(u'3', s.removeProperty('x', normalize=False))
435
        self.assertEqual(ur'\x: 0 !important;\x: 2', s.getCssText(separator=u''))
436
        self.assertEqual(u'0', s.removeProperty(r'\x', normalize=False))
437
        self.assertEqual(u'', s.cssText)
438

    
439
        s.cssText = css
440
        self.assertEqual(u'0', s.removeProperty(r'\x', normalize=False))
441
        self.assertEqual(ur'x: 1;x: 3', s.getCssText(separator=u''))
442
        self.assertEqual(u'3', s.removeProperty('x', normalize=False))
443
        self.assertEqual(u'', s.cssText)
444

    
445
    def test_setProperty(self):
446
        "CSSStyleDeclaration.setProperty()"
447
        s = cssutils.css.CSSStyleDeclaration()
448
        s.setProperty('top', '0', '!important')
449
        self.assertEqual('0', s.getPropertyValue('top'))
450
        self.assertEqual('important', s.getPropertyPriority('top'))
451
        s.setProperty('top', '1px')
452
        self.assertEqual('1px', s.getPropertyValue('top'))
453
        self.assertEqual('', s.getPropertyPriority('top'))
454

    
455
        s.setProperty('top', '2px')
456
        self.assertEqual('2px', s.getPropertyValue('top'))
457

    
458
        s.setProperty('\\top', '3px')
459
        self.assertEqual('3px', s.getPropertyValue('top'))
460

    
461
        s.setProperty('\\top', '4px', normalize=False)
462
        self.assertEqual('4px', s.getPropertyValue('top'))
463
        self.assertEqual('4px', s.getPropertyValue('\\top', False))
464
        self.assertEqual('3px', s.getPropertyValue('top', False))
465

    
466
        # case insensitive
467
        s.setProperty('TOP', '0', '!IMPORTANT')
468
        self.assertEqual('0', s.getPropertyValue('top'))
469
        self.assertEqual('important', s.getPropertyPriority('top'))
470

    
471
        tests = {
472
            (u'left', u'0', u''): u'left: 0',
473
            (u'left', u'0', u'important'): u'left: 0 !important',
474
            (u'LEFT', u'0', u'important'): u'left: 0 !important',
475
            (u'left', u'0', u'important'): u'left: 0 !important',
476
            }
477
        for test, exp in tests.items():
478
            s = cssutils.css.CSSStyleDeclaration()
479
            n, v, p = test
480
            s.setProperty(n, v, p)
481
            self.assertEqual(exp, s.cssText)
482
            self.assertEqual(v, s.getPropertyValue(n))
483
            self.assertEqual(p, s.getPropertyPriority(n))
484

    
485
        # empty
486
        s = cssutils.css.CSSStyleDeclaration()
487
        self.assertEqual('', s.top)
488
        s.top = '0'
489
        self.assertEqual('0', s.top)
490
        s.top = ''
491
        self.assertEqual('', s.top)
492
        s.top = None
493
        self.assertEqual('', s.top)
494

    
495
    def test_setProperty(self):
496
        "CSSStyleDeclaration.setProperty(replace=)"
497
        s = cssutils.css.CSSStyleDeclaration()
498
        s.setProperty('top', '1px')
499
        self.assertEqual(len(s.getProperties('top', all=True)), 1)
500

    
501
        s.setProperty('top', '2px')
502
        self.assertEqual(len(s.getProperties('top', all=True)), 1)
503
        self.assertEqual(s.getPropertyValue('top'), '2px')
504

    
505
        s.setProperty('top', '3px', replace=False)
506
        self.assertEqual(len(s.getProperties('top', all=True)), 2)
507
        self.assertEqual(s.getPropertyValue('top'), '3px')
508

    
509
    def test_length(self):
510
        "CSSStyleDeclaration.length"
511
        s = cssutils.css.CSSStyleDeclaration()
512

    
513
        # cssText
514
        s.cssText = u'left: 0'
515
        self.assertEqual(1, s.length)
516
        self.assertEqual(1, len(s.seq))
517
        s.cssText = u'/*1*/left/*x*/:/*x*/0/*x*/;/*2*/ top: 1;/*3*/'
518
        self.assertEqual(2, s.length)
519
        self.assertEqual(5, len(s.seq))
520

    
521
        # set
522
        s = cssutils.css.CSSStyleDeclaration()
523
        s.setProperty('top', '0', '!important')
524
        self.assertEqual(1, s.length)
525
        s.setProperty('top', '1px')
526
        self.assertEqual(1, s.length)
527
        s.setProperty('left', '1px')
528

    
529
    def test_nameParameter(self):
530
        "CSSStyleDeclaration.XXX(name)"
531
        s = cssutils.css.CSSStyleDeclaration()
532
        s.setProperty('top', '1px', '!important')
533

    
534
        self.assertEqual('1px', s.getPropertyValue('top'))
535
        self.assertEqual('1px', s.getPropertyValue('TOP'))
536
        self.assertEqual('1px', s.getPropertyValue('T\op'))
537

    
538
        self.assertEqual('important', s.getPropertyPriority('top'))
539
        self.assertEqual('important', s.getPropertyPriority('TOP'))
540
        self.assertEqual('important', s.getPropertyPriority('T\op'))
541

    
542
        s.setProperty('top', '2px', '!important')
543
        self.assertEqual('2px', s.removeProperty('top'))
544
        s.setProperty('top', '2px', '!important')
545
        self.assertEqual('2px', s.removeProperty('TOP'))
546
        s.setProperty('top', '2px', '!important')
547
        self.assertEqual('2px', s.removeProperty('T\op'))
548

    
549
    def test_css2properties(self):
550
        "CSSStyleDeclaration.$css2property get set del"
551
        s = cssutils.css.CSSStyleDeclaration(
552
            cssText='left: 1px;color: red; font-style: italic')
553

    
554
        s.color = 'green'
555
        s.fontStyle = 'normal'
556
        self.assertEqual('green', s.color)
557
        self.assertEqual('normal', s.fontStyle)
558
        self.assertEqual('green', s.getPropertyValue('color'))
559
        self.assertEqual('normal', s.getPropertyValue('font-style'))
560
        self.assertEqual(
561
            u'''left: 1px;\ncolor: green;\nfont-style: normal''',
562
            s.cssText)
563

    
564
        del s.color
565
        self.assertEqual(
566
            u'''left: 1px;\nfont-style: normal''',
567
            s.cssText)
568
        del s.fontStyle
569
        self.assertEqual(u'left: 1px', s.cssText)
570

    
571
        self.assertRaises(AttributeError, s.__setattr__, 'UNKNOWN', 'red')
572
        # unknown properties must be set with setProperty!
573
        s.setProperty('UNKNOWN', 'red')
574
        # but are still not usable as property!
575
        self.assertRaises(AttributeError, s.__getattribute__, 'UNKNOWN')
576
        self.assertRaises(AttributeError, s.__delattr__, 'UNKNOWN')
577
        # but are kept
578
        self.assertEqual('red', s.getPropertyValue('UNKNOWN'))
579
        self.assertEqual(
580
            '''left: 1px;\nunknown: red''', s.cssText)
581

    
582
    def test_reprANDstr(self):
583
        "CSSStyleDeclaration.__repr__(), .__str__()"
584
        s = cssutils.css.CSSStyleDeclaration(cssText='a:1;b:2')
585

    
586
        self.assertTrue("2" in str(s)) # length
587

    
588
        s2 = eval(repr(s))
589
        self.assertTrue(isinstance(s2, s.__class__))
590

    
591

    
592
if __name__ == '__main__':
593
    import unittest
594
    unittest.main()