| 1 |
#!/usr/bin/env python |
|---|
| 2 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 3 |
##~ Copyright (C) 2002-2004 TechGame Networks, LLC. |
|---|
| 4 |
##~ |
|---|
| 5 |
##~ This library is free software; you can redistribute it and/or |
|---|
| 6 |
##~ modify it under the terms of the BSD style License as found in the |
|---|
| 7 |
##~ LICENSE file included with this distribution. |
|---|
| 8 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 9 |
|
|---|
| 10 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 11 |
#~ Imports |
|---|
| 12 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 13 |
|
|---|
| 14 |
import unittest |
|---|
| 15 |
import sys |
|---|
| 16 |
|
|---|
| 17 |
from TG.w3c import css |
|---|
| 18 |
|
|---|
| 19 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 20 |
#~ Definitions |
|---|
| 21 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 22 |
|
|---|
| 23 |
class TestCSSErrors(unittest.TestCase): |
|---|
| 24 |
def setUp(self): |
|---|
| 25 |
self.parserStrict = css.CSSParser() |
|---|
| 26 |
self.parserStrict.bParseStrict = True |
|---|
| 27 |
self.parserTolerant = css.CSSParser() |
|---|
| 28 |
def tearDown(self): |
|---|
| 29 |
del self.parserStrict |
|---|
| 30 |
del self.parserTolerant |
|---|
| 31 |
|
|---|
| 32 |
def parseCSS(self, testcss, errortext): |
|---|
| 33 |
result = self.parseStrictCSS(testcss, errortext) |
|---|
| 34 |
result = self.parseTolerantCSS(testcss, errortext) |
|---|
| 35 |
return result |
|---|
| 36 |
|
|---|
| 37 |
def parseStrictCSS(self, testcss, errortext): |
|---|
| 38 |
try: |
|---|
| 39 |
result = self.parserStrict.parse(testcss) |
|---|
| 40 |
except css.CSSParseError: |
|---|
| 41 |
result = None |
|---|
| 42 |
else: |
|---|
| 43 |
self.fail('Should fail strict test: ' + errortext) |
|---|
| 44 |
return result |
|---|
| 45 |
|
|---|
| 46 |
def parseTolerantCSS(self, testcss, errortext): |
|---|
| 47 |
try: |
|---|
| 48 |
result = self.parserTolerant.parse(testcss) |
|---|
| 49 |
except css.CSSParseError: |
|---|
| 50 |
raise |
|---|
| 51 |
self.fail('Should NOT fail tolerant test: ' + errortext) |
|---|
| 52 |
return result |
|---|
| 53 |
|
|---|
| 54 |
def testMissingColon(self): |
|---|
| 55 |
self.parseCSS('p { color:green; color } ', 'Malformed declaration missing \':\' and value') |
|---|
| 56 |
self.parseCSS('p { color:green; color orange} ', 'Malformed declaration missing \':\' and value') |
|---|
| 57 |
self.parseCSS('p { color:red; color; color:green }', 'Malformed declaration missing \':\' with expected recovery') |
|---|
| 58 |
self.parseCSS('p { color:red; color orange; color:green }', 'Malformed declaration missing \':\' with expected recovery') |
|---|
| 59 |
|
|---|
| 60 |
def testMissingValue(self): |
|---|
| 61 |
self.parseCSS('p { color:green; color: }', 'Malformed declaration missing value at end of construct') |
|---|
| 62 |
self.parseCSS('p { color:red; color:; color:green }', 'Malformed declaration missing value in the middle of construct') |
|---|
| 63 |
|
|---|
| 64 |
def testUnexpectedBlock(self): |
|---|
| 65 |
self.parseCSS('p { color:green; color{;color:maroon} }', 'Unexpected tokens { }') |
|---|
| 66 |
self.parseCSS('p { color:red; color{;color:maroon}; color:green }', 'Unexpected tokens {} with recovery') |
|---|
| 67 |
|
|---|
| 68 |
def testInvalidAtKeywords(self): |
|---|
| 69 |
self.parseCSS('''@two-dee blah blah blah;''', '''@two-dee should be ignored''') |
|---|
| 70 |
self.parseCSS(''' |
|---|
| 71 |
@three-dee { |
|---|
| 72 |
@background-lighting { |
|---|
| 73 |
azimuth: 30deg; |
|---|
| 74 |
elevation: 190deg; |
|---|
| 75 |
} |
|---|
| 76 |
h1 { color: red } |
|---|
| 77 |
} |
|---|
| 78 |
h1 { color: blue } |
|---|
| 79 |
''', '@three-dee should be ignored') |
|---|
| 80 |
self.parseCSS(''' |
|---|
| 81 |
@three-dee |
|---|
| 82 |
@background-lighting { |
|---|
| 83 |
azimuth: 30deg; |
|---|
| 84 |
elevation: 190deg; |
|---|
| 85 |
} |
|---|
| 86 |
h1 { color: red } |
|---|
| 87 |
} |
|---|
| 88 |
h1 { color: blue } |
|---|
| 89 |
''', '@three-dee should be ignored even though it is missing a start bracket') |
|---|
| 90 |
|
|---|
| 91 |
def testUnexpectedEndOfStylesheet(self): |
|---|
| 92 |
self.parseCSS(''' |
|---|
| 93 |
p { content: 'Hello' |
|---|
| 94 |
''', 'Should parse as if all open constructs were closed') |
|---|
| 95 |
self.parseCSS(''' |
|---|
| 96 |
h1 { content: 'Hello |
|---|
| 97 |
''', 'Should parse as if all open constructs were closed') |
|---|
| 98 |
self.parseCSS(''' |
|---|
| 99 |
@media screen { |
|---|
| 100 |
p:before { content: 'Hello |
|---|
| 101 |
''', 'Should parse as if all open constructs were closed') |
|---|
| 102 |
self.parseCSS(''' |
|---|
| 103 |
@media screen { |
|---|
| 104 |
p:before { content: 'Hello' |
|---|
| 105 |
''', 'Should parse as if all open constructs were closed') |
|---|
| 106 |
|
|---|
| 107 |
def testUnexpectedEndOfString(self): |
|---|
| 108 |
self.parseCSS(''' |
|---|
| 109 |
p { |
|---|
| 110 |
color: green; |
|---|
| 111 |
font-family: 'Courier New Times |
|---|
| 112 |
color: red; |
|---|
| 113 |
color: green; |
|---|
| 114 |
} |
|---|
| 115 |
''', 'Should skip open string value') |
|---|
| 116 |
self.parseCSS(''' |
|---|
| 117 |
p { |
|---|
| 118 |
color: green; |
|---|
| 119 |
font-family: 'Courier New Times |
|---|
| 120 |
color: red; |
|---|
| 121 |
color: green; |
|---|
| 122 |
} |
|---|
| 123 |
''', 'Should skip open string value') |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 127 |
#~ Unittest Main |
|---|
| 128 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 129 |
|
|---|
| 130 |
if __name__=='__main__': |
|---|
| 131 |
unittest.main() |
|---|
| 132 |
|
|---|