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

History | View | Annotate | Download (23.7 KB)

1
"""Testcases for cssutils.css.CSSValue and CSSPrimitiveValue."""
2
__version__ = '$Id: test_cssvalue.py 1443 2008-08-31 13:54:39Z cthedot $'
3

    
4
import sys
5
import basetest
6
import cssutils
7

    
8
CSS2 = (cssutils.profile.CSS_LEVEL_2,)
9
C3BUI = (cssutils.profile.CSS3_BASIC_USER_INTERFACE,)
10
C3BB = (cssutils.profile.CSS3_BACKGROUNDS_AND_BORDERS,)
11
CM3 = (cssutils.profile.CSS3_COLOR,)
12
FM3 = (cssutils.profile.CSS3_FONTS,)
13
C3PM = (cssutils.profile.CSS3_PAGED_MEDIA,)
14
C3T = (cssutils.profile.CSS3_TEXT,)
15
FM3FF = (cssutils.profile.CSS3_FONT_FACE,)
16
CSS2_CM3 = (CM3[0], CSS2[0])
17
CSS2_FM3 = (FM3[0], CSS2[0])
18

    
19

    
20
class ProfilesTestCase(basetest.BaseTestCase):
21
    M1 = {
22
          'testvalue': 'x'
23
          }
24
    P1 = {
25
        '-test-tokenmacro': '({num}{w}){1,2}', 
26
        '-test-macro': '{ident}|{percentage}',
27
        '-test-custommacro': '{testvalue}',
28
        # custom validation function 
29
        '-test-funcval': lambda(v): int(v) > 0 
30
        }  
31

    
32
    def test_knownNames(self):
33
        "Profiles.knownNames"
34
        p = cssutils.profiles.Profiles()
35
        p.removeProfile(all=True)
36
        p.addProfile('test', self.P1, self.M1)
37
        self.assertEqual(p.knownNames, self.P1.keys())
38
        p.removeProfile(all=True)
39
        self.assertEqual(p.knownNames, [])
40
        
41
    def test_profiles(self):
42
        "Profiles.profiles"
43
        p = cssutils.profiles.Profiles()
44
        p.removeProfile(all=True)
45
        p.addProfile('test', self.P1, self.M1)
46
        self.assertEqual(p.profiles, ['test'])
47
        p.removeProfile(all=True)
48
        self.assertEqual(p.profiles, [])
49

    
50
    def test_validate2(self):
51
        "Profiles.validate()"
52
        #save
53
        saved = cssutils.profile
54
        
55
        # test        
56
        p = cssutils.profiles.Profiles()
57
        cssutils.profile = p
58
        
59
        pvs = [('color', 'red'),
60
               ('color', 'rgba(0,0,0,0)'),
61
               ('color', 'XXX')
62
               ]
63
        
64
        def check(*results):
65
            for i, pv in enumerate(pvs):
66
                self.assertEqual(p.validate(*pv), results[i])
67
                        
68
        check(True, True, False)
69

    
70
        p.removeProfile(p.CSS3_COLOR)
71
        check(True, False, False)
72

    
73
        cssutils.profile.addProfile('test', {}, {'color': 'XXX'})
74
        check(False, False, True)
75

    
76
        p.removeProfile(all=True)
77
        check(False, False, False)
78

    
79
        # TODO: validateWithProfile
80
        
81
        # restore
82
        cssutils.profile = saved
83

    
84
    def test_addProfile(self):
85
        "Profiles.addProfile with custom validation function"
86
        # unknown profile
87
        self.assertRaises(cssutils.profiles.NoSuchProfileException, 
88
                          lambda: list(cssutils.profile.propertiesByProfile('NOTSET')) )
89

    
90
        # new profile
91
        cssutils.profile.addProfile('test', self.P1, self.M1)
92
        
93
        props = self.P1.keys()
94
        props.sort()
95
        self.assertEqual(props, list(cssutils.profile.propertiesByProfile('test')))
96
        
97
        cssutils.log.raiseExceptions = False
98
        tests = {
99
            ('-test-tokenmacro', '1'): True,     
100
            ('-test-tokenmacro', '1 -2'): True,     
101
            ('-test-tokenmacro', '1 2 3'): False,     
102
            ('-test-tokenmacro', 'a'): False,     
103
            ('-test-macro', 'a'): True,     
104
            ('-test-macro', '0.1%'): True,
105
            ('-test-custommacro', 'x'): True,     
106
            ('-test-custommacro', '1'): False,     
107
            ('-test-custommacro', 'y'): False,     
108
            ('-test-funcval', '1'): True,     
109
            ('-test-funcval', '-1'): False,     
110
            ('-test-funcval', 'x'): False     
111
            }
112
        for test, v in tests.items():
113
            self.assertEqual(v, cssutils.profile.validate(*test))
114
            
115
            self.assertEqual((v, v, ['test']), 
116
                             cssutils.profile.validateWithProfile(*test))
117
            
118
        cssutils.log.raiseExceptions = True
119
        
120
        # raises:
121
        expmsg = u"invalid literal for int() with base 10: 'x'"        
122
        # Python upto 2.4 and Jython have different msg format...
123
        if sys.version_info[0:2] == (2,4):
124
            expmsg = u"invalid literal for int(): x" 
125
        elif sys.platform.startswith('java'):
126
            expmsg = u"invalid literal for int() with base 10: x"
127
            
128
        self.assertRaisesMsg(Exception, expmsg, 
129
                             cssutils.profile.validate, u'-test-funcval', u'x')
130

    
131
    def test_removeProfile(self):
132
        "Profiles.removeProfile()"
133
        p = cssutils.profiles.Profiles()
134
        self.assertEqual(9, len(p.profiles))
135
        p.removeProfile(p.CSS_LEVEL_2)
136
        self.assertEqual(8, len(p.profiles))
137
        p.removeProfile(all=True)
138
        self.assertEqual(0, len(p.profiles))
139

    
140
    # TODO: FIX
141
#    def test_validateWithProfile(self):
142
#        "Profiles.validate(), Profiles.validateWithProfile()"
143
#        p = cssutils.profiles.Profiles()
144
#        tests = {
145
#            ('color', 'red', None): (True, True, [p.CSS_LEVEL_2]),
146
#            ('color', 'red', p.CSS_LEVEL_2): (True, True,[p.CSS_LEVEL_2]),
147
#            ('color', 'red', p.CSS3_COLOR): (True, False, [p.CSS_LEVEL_2]),
148
#            ('color', 'rgba(0,0,0,0)', None): (True, True, [p.CSS3_COLOR]),
149
#            ('color', 'rgba(0,0,0,0)', p.CSS_LEVEL_2): (True, False, [p.CSS3_COLOR]),
150
#            ('color', 'rgba(0,0,0,0)', p.CSS3_COLOR): (True, True, [p.CSS3_COLOR]),
151
#            ('color', '1px', None): (False, False, [p.CSS3_COLOR, p.CSS_LEVEL_2]),
152
#            ('color', '1px', p.CSS_LEVEL_2): (False, False, [p.CSS3_COLOR, p.CSS_LEVEL_2]),
153
#            ('color', '1px', p.CSS3_COLOR): (False, False, [p.CSS3_COLOR, p.CSS_LEVEL_2]),
154
#            ('color', 'aliceblue', None): (True, True, [p.CSS_LEVEL_2]),
155
#
156
#            ('opacity', '1', None): (True, True, [p.CSS3_COLOR]),
157
#            ('opacity', '1', p.CSS_LEVEL_2): (True, False, [p.CSS3_COLOR]),
158
#            ('opacity', '1', p.CSS3_COLOR): (True, True, [p.CSS3_COLOR]),
159
#            ('opacity', '1px', None): (False, False, [p.CSS3_COLOR]),
160
#            ('opacity', '1px', p.CSS_LEVEL_2): (False, False, [p.CSS3_COLOR]),
161
#            ('opacity', '1px', p.CSS3_COLOR): (False, False, [p.CSS3_COLOR]),
162
#
163
#            ('-x', '1', None): (False, False, []),
164
#            ('-x', '1', p.CSS_LEVEL_2): (False, False, []),
165
#            ('-x', '1', p.CSS3_COLOR): (False, False, []),
166
#        }
167
#        for test, r in tests.items():
168
#            self.assertEqual(p.validate(test[0], test[1]), r[0])
169
#            self.assertEqual(p.validateWithProfile(*test), r)
170
           
171
    def test_propertiesByProfile(self):
172
        "Profiles.propertiesByProfile"
173
        self.assertEqual(['opacity'], #'color',  
174
                         list(cssutils.profile.propertiesByProfile(
175
                                            cssutils.profile.CSS3_COLOR)))
176
        
177
    def test_csscolorlevel3(self):
178
        "CSS Color Module Level 3"
179
        # (propname, propvalue): (valid, validprofile)
180
        namedcolors = '''transparent, orange,
181
                         aqua, black, blue, fuchsia, gray, green, lime, maroon,
182
                         navy, olive, purple, red, silver, teal, white, yellow'''
183
        for color in namedcolors.split(','):
184
            color = color.strip()            
185
            self.assertEqual(True, cssutils.profile.validate('color', color))
186
            
187
            self.assertEqual((True, True, list(CSS2)), 
188
                             cssutils.profile.validateWithProfile('color', color))
189

    
190
            # CSS2 only:
191
        uicolor = 'ActiveBorder|ActiveCaption|AppWorkspace|Background|ButtonFace|ButtonHighlight|ButtonShadow|ButtonText|CaptionText|GrayText|Highlight|HighlightText|InactiveBorder|InactiveCaption|InactiveCaptionText|InfoBackground|InfoText|Menu|MenuText|Scrollbar|ThreeDDarkShadow|ThreeDFace|ThreeDHighlight|ThreeDLightShadow|ThreeDShadow|Window|WindowFrame|WindowText'
192
        for color in uicolor.split('|'):
193
            self.assertEqual(False, cssutils.profile.validate('color', color))
194
            
195
            # TODO: Fix
196
            #self.assertEqual((True, True, list(CSS2)), 
197
            #                 cssutils.profile.validateWithProfile('color', color))
198
        
199
    def test_validate(self):
200
        "Profiles.validate()"
201
        tests = {
202
            # name, values: valid, matching, profile
203
            
204
            # background-position
205
            ('background-position', ('inherit',
206
                                     '0',
207
                                     '1%',
208
                                     '1px',
209
                                     '0 0',
210
                                     '1% 1%',
211
                                     '1px 1px',
212
                                     '1px 1%',
213
                                     'top', 'bottom',
214
                                     'left', 'right',
215
                                     'center center',
216
                                     'center',
217
                                     'top left', 'top center', 'top right',
218
                                     'bottom left', 'bottom center', 'bottom right',
219
                                     'center left', 'center center', 'center right',
220
                                     '0 center', 'center 0',
221
                                     '0 top', '10% bottom',
222
                                     'left 0', 'right 10%',
223
                                     '1% center', 'center 1%'
224
                                     )): (True, True, CSS2),
225
            ('background-position', ('0 left',
226
                                     'top 0'
227
                                     )): (False, False, CSS2),
228

    
229

    
230
            ('border-top-right-radius', ('1px',
231
                                         '1%',
232
                                         '1% -1px',
233
                                         '1% 0',
234
                                         )): (True, True, C3BB),
235
            ('border-top-right-radius', ('1px 2px 2px', 
236
                                         '/ 1px',
237
                                         'black')): (False, False, C3BB),
238

    
239
            ('border-radius', ('1px',
240
                               '1%',
241
                               '0',
242
                               '1px 1px', 
243
                               '1px/ 1px', 
244
                               '1px /1px', 
245
                               '1px  /  1px', 
246
                               '1px 1px 1px 1px',
247
                               '1px 1px 1px 1px / 1px 1px 1px 1px',
248
                               )): (True, True, C3BB),
249
            ('border-radius', ('1px /', 
250
                               '/ 1px',
251
                               '1px / 1px / 1px',
252
                               '1px 1px 1px 1px 1px',
253
                               '1px / 1px 1px 1px 1px 1px',
254
                               'black')): (False, False, C3BB),
255

    
256
            ('border', ('1px',
257
                        'solid',
258
                        'red',
259
                        '1px solid red',
260
                        '1px red solid',
261
                        'red 1px solid',
262
                        'red solid 1px',
263
                        'solid 1px red',
264
                        'solid red 1px',
265
                               )): (True, True, C3BB),
266
            ('border', ('1px 1px', 
267
                        'red red 1px',
268
                        )): (False, False, C3BB),
269
            
270
            ('box-shadow', ('none',
271
                            '1px 1px', 
272
                            '1px 1px 1px',
273
                            '1px 1px 1px 1px',
274
                            '1px 1px 1px 1px red',
275
                            'inset 1px 1px',
276
                            'inset 1px 1px 1px 1px black')): (True, True, C3BB),
277
            ('box-shadow', ('1px', 
278
                            '1px 1px 1px 1px 1px',
279
                            'x 1px 1px',
280
                            'inset',
281
                            '1px black',
282
                            'black')): (False, False, C3BB),
283
            
284
            # color
285
            ('color', ('x',
286
                       '#',
287
                       '#0',
288
                       '#00',
289
                       '#0000',
290
                       '#00000',
291
                       '#0000000',
292
                       '#00j',
293
                       '#j00000',
294
                       'rgb(0.0,1,1)',
295
                       'rgb(0)',
296
                       'rgb(0, 1)',
297
                       'rgb(0, 1, 1, 1)',
298
                       'rgb(0, 1, 0%)',
299
                       'rgba(0)',
300
                       'rgba(0, 1, 1.0, 1)',
301
                       'rgba(0, 1)',
302
                       'rgba(0, 1, 1, 1, 1)',
303
                       'rgba(100%, 0%, 0%, 1%)',
304
                       'rgba(100%, 0%, 0, 1)',
305
                       'hsl(1.5,1%,1%)',
306
                       'hsl(1,1,1%)',
307
                       'hsl(1,1%,1)',
308
                       'hsla(1.5,1%,1%, 1)',
309
                       'hsla(1,1,1%, 1)',
310
                       'hsla(1,1%,1, 1)',
311
                       'hsla(1,1%,1%, 1%)'
312
                       )): (False,False, CSS2_CM3),
313
            ('color', ('inherit',
314
                       'black',
315
                       '#000',
316
                       '#000000',
317
                       'rgb(0,1,1)',
318
                       'rgb( 0 , 1 , 1 )',
319
                       'rgb(-10,555,1)',
320
                       'rgb(100%, 1.5%, 0%)',
321
                       'rgb(150%, -20%, 0%)',
322
                       )): (True, True, CSS2),
323
            ('color', ('currentcolor',
324
                       'aliceblue',
325
                       'rgba(1,1,1,1)',
326
                       'rgba( 1 , 1 , 1 , 1 )',
327
                       'rgba(100%, 0%, 0%, 1)',
328
                       'hsl(1,1%,1%)',
329
                       'hsl( 1 , 1% , 1% )',
330
                       'hsl(-1000,555.5%,-61.5%)',
331
                       'hsla(1,1%,1%,1)',
332
                       'hsla( 1, 1% , 1% , 1 )',
333
                       'hsla(-1000,555.5%,-61.5%, 0.5)'                      
334
                       )): (True, True, CM3),
335
            # TODO?:
336
            #('color', 'rgb(/**/ 0 /**/ , /**/ 1 /**/ , /**/ 1 /**/ )'): (True, True, CSS2),
337

    
338
            # content
339
            ('content', ('none', 
340
                         'normal',
341
                         '""',
342
                         "'x'",
343
                         )): (True, True, CSS2),
344

    
345
            ('cursor', ('url(1), auto', 
346
                        'url(1) 2 3, help',
347
                        'wait',
348
                        'inherit',
349
                        'none')): (True, True, C3BUI),
350
            ('cursor', ('url(1), auto, wait', 
351
                        'url(1) 2, help',
352
                        '1')): (False, False, C3BUI),
353
            
354
            # FONTS
355
            ('font-family', ('serif, x',
356
                             )): (True, True, CSS2), #CSS2_FM3),
357
                             
358
            ('font-family', ('inherit',
359
                             'a, b',
360
                             'a,b,c',
361
                             'a, "b", c',
362
                             '"a", b, "c"',
363
                             '"a", "b", "c"',
364
                             '"x y"',
365
                             'serif',
366
                             '"serif"', # valid but CSS2: font with name serif, CSS3: same as `serif`
367
                             'a  b', # should use quotes but valid
368
                             'a, b   b, d',
369
                             )): (True, True, CSS2),
370

    
371
            ('font-weight', ('normal', 'bold', 'bolder', 'lighter', 'inherit', 
372
                             '100', '200', '300', '400', '500', '600', '700', '800', '900'
373
                             )): (True, True, CSS2),
374

    
375
            ('font-stretch', ('normal', 'wider', 'narrower', 'ultra-condensed',
376
                              'extra-condensed', 'condensed', 'semi-condensed',
377
                              'semi-expanded', 'expanded', 'extra-expanded',
378
                              'ultra-expanded', 'inherit'                          
379
                             )): (True, True, FM3),
380

    
381
            ('font-style', ('normal', 'italic', 'oblique', 'inherit'
382
                             )): (True, True, CSS2),
383

    
384
            ('font-variant', ('normal', 'small-caps', 'inherit'
385
                             )): (True, True, CSS2),
386
            ('font-size', ('-1em',
387
                           )): (False, False, CSS2),
388
            ('font-size', ('xx-small', 'x-small', 'small', 'medium', 'large', 
389
                           'x-large', 'xx-large', 'larger', 'smaller', 
390
                           '1em', '1%', 'inherit'
391
                           )): (True, True, CSS2),
392

    
393
            ('font-size-adjust', ('1.0', 
394
                                  'none', 'inherit'
395
                                  )): (True, True, FM3),
396

    
397
            ('font', ('italic small-caps bold 1px/3 a, "b", serif',
398
                      '12pt/14pt sans-serif',
399
                      '80% sans-serif',
400
                      'x-large/110% "new century schoolbook", serif',
401
                      'bold italic large Palatino, serif',
402
                      'normal small-caps 120%/120% fantasy',                     
403
                      'oblique 12pt "Helvetica Nue", serif', 
404
                      'caption', 'icon', 'menu', 'message-box', 'small-caption',
405
                      'status-bar', 'inherit'
406
                      )): (True, True, CSS2),
407
            
408
            ('nav-index', ('1', 'auto', 'inherit')): (True, True, C3BUI),
409
            ('nav-index', ('x', '1 2', '1px')): (False, False, C3BUI),
410
            
411
            ('opacity', ('inherit',
412
                         '0', '0.0', '0.42342', '1', '1.0',
413
                         # should be clipped but valid
414
                         '-0', '-0.1', '-10', '2'
415
                         )): (True, True, CM3),
416
            ('opacity', ('a', '#000', '+1')): (False, False, CM3),
417

    
418
            ('outline', ('red dotted 1px', 
419
                         'dotted 1px red',
420
                         '1px red dotted',
421
                         'red', '1px', 'dotted',
422
                         'red 1px', '1px dotted', 'red dotted',
423
                         'inherit'
424
                               )): (True, True, C3BUI),
425
            ('outline', ('red #fff', 'solid dotted', 'Url(x)', '1px 1px'
426
                               )): (False, False, C3BUI),
427
            ('outline-color', ('red', '#fff', 'inherit'
428
                               )): (True, True, C3BUI),
429
            ('outline-color', ('0', '1em'
430
                               )): (False, False, C3BUI),
431
            ('outline-offset', ('0', '1em', 'inherit'
432
                               )): (True, True, C3BUI),
433
            ('outline-offset', ('1%', 'red'
434
                               )): (False, False, C3BUI),
435
            ('outline-style', ('auto', 'dotted', 'inherit'
436
                               )): (True, True, C3BUI),
437
            ('outline-style', ('0', '1em', 'red'
438
                               )): (False, False, C3BUI),
439
            ('outline-width', ('0', '1em', 'inherit'
440
                               )): (True, True, C3BUI),
441
            ('outline-width', ('auto', 'red', 'dotted'
442
                               )): (False, False, C3BUI),
443
            
444
            ('resize', ('none', 
445
                        'both', 
446
                        'horizontal', 
447
                        'vertical',
448
                        'inherit')): (True, True, C3BUI),
449
            ('resize', ('1', 
450
                        'auto', 
451
                        '1px', 
452
                        '2%')): (False, False, C3BUI),
453

    
454
            ('size', ('1cm',
455
                      '1mm 20cm',
456
                      'auto',
457
                      'landscape letter', 
458
                      'a4 portrait', 
459
                      'landscape', 
460
                      'a5', 
461
                      #'inherit'
462
                      )): (True, True, C3PM),
463
            ('size', ( 
464
                      'portrait landscape', 
465
                      'a5 letter', 
466
                      '2%')): (False, False, C3PM),
467

    
468
            ('src', ('url(  a  )',
469
                     'local(  x  )',
470
                     'local("x")',
471
                     'local(  "x"  )',
472
                     'url(../fonts/LateefRegAAT.ttf) format(  "truetype-aat"  )',
473
                     'url(a) format(  "123x"  , "a"   )',
474
                     'url(a) format( "123x"  , "a"   ), url(a) format( "123x"  , "a"   )',
475
                     'local(HiraKakuPro-W3), local(Meiryo), local(IPAPGothic)',
476
                     'local(Gentium), url(/fonts/Gentium.ttf)',
477
                     'local("Gentium"), url("/fonts/Gentium.ttf")',
478
                     'local(Futura-Medium), url(fonts.svg#MyGeometricModern) format("svg")',
479
                    )): (True, True, FM3FF),
480

    
481

    
482
            ('text-shadow', ('none',
483
                            '1px 1px', 
484
                            '1px 1px 1px',
485
                            '1px 1px 1px 1px',
486
                            '1px 1px 1px 1px red',
487
                            'inset 1px 1px',
488
                            'inset 1px 1px 1px 1px black')): (True, True, C3T),
489
            ('text-shadow', ('1px', 
490
                            '1px 1px 1px 1px 1px',
491
                            'x 1px 1px',
492
                            'inset',
493
                            '1px black',
494
                            'black')): (False, False, C3T),
495

    
496
            ('unicode-range', ('u+1', 'U+111111-ffffff',
497
                               'u+123456  ,  U+1-f'
498
                               )): (True, True, FM3FF),
499
        
500
        }
501
        # TODO!!!
502
        for (name, values), (valid, matching, profile) in tests.items(): 
503
            for value in values:
504
                self.assertEqual(valid, cssutils.profile.validate(name, value))
505
                
506
                
507
#                if (valid, matching, list(profile)) != cssutils.profile.validateWithProfile(name, value):
508
#                    print
509
#                    print '###############', name, value
510
#                    print (valid, matching, list(profile)), cssutils.profile.validateWithProfile(name, value)
511
             
512
             # TODO: fix                       
513
#                self.assertEqual((valid, matching, list(profile)), 
514
#                                 cssutils.profile.validateWithProfile(name, value))
515

    
516
     # TODO: fix                       
517
#    def test_validateByProfile(self):
518
#        "Profiles.validateByProfile()"
519
#        # testing for valid values overwritten in a profile
520
#        tests = {
521
#            (FM3FF, 'font-family', ('y', '"y"' # => name should be "y"!!!
522
#                                     )): (True, True, FM3FF),    
523
#            (FM3FF, 'font-family', ('"y", "a"', 'a, b', 'a a'
524
#                                     )): (True, False, CSS2),    
525
#            (FM3FF, 'font-stretch', ('normal', 'wider', 'narrower', 'inherit'
526
#                                     )): (True, False, FM3),    
527
#            (FM3FF, 'font-style', ('inherit',
528
#                                     )): (True, False, CSS2),    
529
#            (FM3FF, 'font-weight', ('bolder', 'lighter', 'inherit',
530
#                                     )): (True, False, CSS2),    
531
#            }
532
#        for (profiles, name, values), (v, m, p) in tests.items():            
533
#            for value in values:
534
#                self.assertEqual((v, m, list(p)), 
535
#                                 cssutils.profile.validateWithProfile(name, 
536
#                                                                      value, 
537
#                                                                      profiles))
538

    
539

    
540
if __name__ == '__main__':
541
    import unittest
542
    unittest.main()