-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_lexr.py
88 lines (82 loc) · 2.59 KB
/
test_lexr.py
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
#
import lexr
import unittest
EOF = ('', '')
def W(s):
return ('W', s)
def T(s):
return ('T', s)
class baseTests(unittest.TestCase):
knownValues = (
("", [EOF]),
("a", [W('a'), EOF]),
("a b", [W('a'), W('b'), EOF]),
# Are each of the tokens recognized?
("AND && ( ) EXCEPT ! NOT",
[T('AND'), T('&&'), T('('), T(')'),
T("EXCEPT"), T("!"), T("NOT"), EOF]),
("a && b", [W("a"), T("&&"), W("b"), EOF]),
# Do we properly handle quoted strings, including embedded
# in full words (middle, start, or end)?
("'abc'", [W('abc'), EOF]),
("''", [W(''), EOF]),
("''''", [W("'"), EOF]),
("'abc''def'", [W("abc'def"), EOF]),
("abc'def'ghi", [W("abcdefghi"), EOF]),
("a'b''c'd", [W("ab'cd"), EOF]),
("'a'b", [W("ab"), EOF]),
("a'b'", [W("ab"), EOF]),
("'a b && d e'", [W("a b && d e"), EOF]),
# null-length quoted objects embedded in stuff.
("a''b", [W("ab"), EOF]),
("''ab", [W("ab"), EOF]),
("ab''", [W("ab"), EOF]),
# Are each of the tokens word-breakers?
# Conversely, are the word tokens not word-breakers?
("a&&b", [W("a"), T("&&"), W("b"), EOF]),
("aANDb", [W("aANDb"), EOF]),
("a(b", [W("a"), T("("), W("b"), EOF]),
("a)b", [W("a"), T(")"), W("b"), EOF]),
("a!b", [W("a"), T("!"), W("b"), EOF]),
("aEXCEPTb", [W("aEXCEPTb"), EOF]),
("aNOTb", [W("aNOTb"), EOF]),
# Should not match one &, or some letters of the lead.
("a&b", [W("a&b"), EOF]),
("aANb", [W("aANb"), EOF]),
# Look for quoting behavior.
("a'&&'b", [W("a&&b"), EOF]),
("a '&&' b", [W("a"), W("&&"), W("b"), EOF]),
("a'b'c&&d", [W("abc"), T("&&"), W("d"), EOF]),
("a b&&(c!d e 'f'g)",
[W("a"), W("b"), T('&&'), T('('), W("c"), T("!"), W("d"),
W("e"), W("fg"), T(")"), EOF]),
# Test that things break properly with things after them.
("ANDOVER", [W("ANDOVER"), EOF]),
("EXCEPTOVER", [W("EXCEPTOVER"), EOF]),
("NOTOVER", [W("NOTOVER"), EOF]),
("&&OVER", [T("&&"), W("OVER"), EOF]),
("(OVER", [T("("), W("OVER"), EOF]),
(")OVER", [T(")"), W("OVER"), EOF]),
("!OVER", [T("!"), W("OVER"), EOF]),
# Yes, this is a torture test. That's the fun!
("AND'OVER'", [W("ANDOVER"), EOF]),
("NOT", [T("NOT"), EOF]),
)
def testKnownValues(self):
"Test for known parses to behave consistently."
for p, res in self.knownValues:
self.assertEqual(lexr.tokenize(p), res)
class baseFailTests(unittest.TestCase):
knownFails = (
"'",
"'abc",
"'abc''",
"'abc''def",
"'''",
)
def testKnownFails(self):
"Test that certain known bad inputs raise exceptions."
for p in self.knownFails:
self.assertRaises(lexr.BadInput, lexr.tokenize, p)
if __name__ == "__main__":
unittest.main()