File: test_identifier.py

package info (click to toggle)
python-enable 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,280 kB
  • ctags: 13,899
  • sloc: cpp: 48,447; python: 28,502; ansic: 9,004; makefile: 315; sh: 44
file content (95 lines) | stat: -rw-r--r-- 2,934 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import unittest
import string
import sys
from pyparsing import ParseException, Regex, StringEnd
import enable.savage.svg.css.identifier as identifier

class TestEscaped(unittest.TestCase):
    def testEscapedSpecialChar(self):
        for char in string.punctuation:
            self.assertEqual(
                identifier.escaped.parseString("\\"+char)[0],
                char
            )

    def testEscapeDoesntMatchHexChars(self):
        return
        for char in string.hexdigits:
            self.assertRaises(
                ParseException,
                identifier.escaped.parseString,
                "\\"+char
            )


class TestHexUnicode(unittest.TestCase):
    parser = (identifier.hex_unicode.copy() + Regex(".+") + StringEnd()).leaveWhitespace()
    def testUnicodeConversion(self):
        self.assertEqual(
            u"&",
            identifier.hex_unicode.parseString(r"\000026")[0]
        )
        self.assertEqual(
            u"&",
            identifier.hex_unicode.parseString(r"\26")[0]
        )
    def testDoesntEatMoreThan6Chars(self):
        self.assertEqual(
            [u"&", "B"],
            list(self.parser.parseString(r"\000026B"))
        )
    def testConsumesFinalSpaceWith6Chars(self):
        self.assertEqual(
            [u"&", "B"],
            list(self.parser.parseString(r"\000026 B"))
        )
    def testConsumesFinalSpaceWithShortChars(self):
        self.assertEqual(
            [u"&", "B"],
            list(self.parser.parseString(r"\26 B"))
        )
    def testDoesntConsumeMoreThanOneSpace(self):
        self.assertEqual(
            [u"&", "  B"],
            list(self.parser.parseString(r"\26   B"))
        )


class TestEscape(unittest.TestCase):
    def testEscapeValues(self):
        self.assertEqual(u"&", identifier.escape.parseString(r"\26")[0])
        self.assertEqual(u'\x81', identifier.escape.parseString("\\" + unichr(129))[0])
        self.assertEqual(u"~", identifier.escape.parseString(r'\~')[0])


class TestNonAscii(unittest.TestCase):
    def testNoMatchInAsciiRange(self):
        for c in map(unichr, range(128)):
            self.assertRaises(
                ParseException,
                identifier.nonascii.parseString, c
            )

    def testMatchesOutsideAsciiRange(self):
        for c in map(unichr, xrange(128, sys.maxunicode+1)):
            self.assertEqual(
                c,
                identifier.nonascii.parseString(c)[0]
            )

class TestNmstart(unittest.TestCase):
    def testNmstartValues(self):
        self.assertRaises(
            ParseException,
            identifier.nmstart.parseString,
            "0"
        )


class TestIdentifier(unittest.TestCase):
    def testValidIdentifiers(self):
        for ident in ["import","color", "border-left"]:
            self.assertEqual(
                ident,
                identifier.identifier.parseString(ident)[0]
            )