-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bnf.py
161 lines (104 loc) · 3.07 KB
/
test_bnf.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
Test cases for BNF grammar
Run tests with
pytest test_bnf.py
"""
import lib
import math
import pytest
def test_char():
g = lib.Bnf('"c"')
assert g.expr == 'c'
def test_string():
g = lib.Bnf('"abc"')
assert g.expr == 'abc'
def test_singlequote():
g = lib.Bnf("'$'")
assert g.expr == '$'
def test_singlequote_backslash():
g = lib.Bnf("'\\'")
assert g.expr == '\\'
def test_str():
g = lib.Bnf('"y" "a" "m" "l"')
assert g.expr == ('concat', 'y', 'a', 'm', 'l')
def test_unicode():
g = lib.Bnf('x9')
assert g.expr == '\x09'
g = lib.Bnf('x10FFFF')
assert g.expr == '\U0010ffff'
def test_range():
g = lib.Bnf('[x30-x39]')
assert g.expr == range(0x30, 0x3A)
g = lib.Bnf('[xA0-xD7FF]')
assert g.expr == range(0xA0, 0xD800)
def test_rules():
g = lib.Bnf('s-indent(<n)')
assert g.expr == ("rule", "s-indent", "<n")
g = lib.Bnf('nb-json')
assert g.expr == ("rule", "nb-json")
g = lib.Bnf('s-separate(n,c)')
assert g.expr == ("rule", "s-separate", "n", "c")
def test_lookarounds():
g = lib.Bnf('[ lookahead = ns-plain-safe(c) ]')
assert g.expr == ("?=", ("rule", "ns-plain-safe", "c"))
g = lib.Bnf('[ lookahead ≠ ns-char ]')
assert g.expr == ("?!", ("rule", "ns-char"))
g = lib.Bnf('[ lookbehind = ns-char ]')
assert g.expr == ("?<=", ("rule", "ns-char"))
def test_special():
g = lib.Bnf('<start-of-line>')
assert g.expr == ("^",)
g = lib.Bnf('<end-of-input>')
assert g.expr == ("$",)
g = lib.Bnf('<empty>')
assert g.expr == ("concat",)
def test_or():
g = lib.Bnf('"0" | "9"')
assert g.expr == {'0', '9'}
def test_opt():
g = lib.Bnf('"a"?')
assert g.expr == ("repeat", 0, 1, "a")
def test_star():
g = lib.Bnf('"a"*')
assert g.expr == ("repeat", 0, math.inf, "a")
def test_plus():
g = lib.Bnf('"a"+')
assert g.expr == ("repeat", 1, math.inf, "a")
def test_curlyrepeat():
g = lib.Bnf('"a"{4}')
assert g.expr == ("repeat", 4, 4, "a")
def test_diff():
g = lib.Bnf('dig - x30')
assert g.expr == ("diff", ("rule", "dig"), "0")
def test_2diff():
g = lib.Bnf('dig - x30 - x31')
assert g.expr == ("diff", ("rule", "dig"), "0", "1")
def test_parens():
g = lib.Bnf('"x" (hex{2} ) "-"')
assert g.expr == ("concat", "x", ("repeat", 2, 2, ("rule", "hex")), "-")
def test_empty():
g = lib.Bnf(' ')
assert g.expr == ('concat',)
def test_comment():
g = lib.Bnf(' dig /* Empty */ ')
assert g.expr == ("rule", "dig")
def test_commenthash():
g = lib.Bnf(' # Empty ')
assert g.expr == ('concat',)
def test_comments():
g = lib.Bnf('[x41-x46] # A-F \n| [x61-x66] # a-f ')
assert g.expr == {range(0x41, 0x47), range(0x61, 0x67)}
def test_remaining():
with pytest.raises(ValueError) as e_info:
lib.Bnf('"1" ^^garbage')
assert 'garbage' in str(e_info.value)
assert 'remaining' in str(e_info.value)
def test_bad_string():
with pytest.raises(ValueError) as e_info:
lib.Bnf("'1\\'")
assert "'" in str(e_info.value)
assert 'expected' in str(e_info.value)
def test_load():
l = lib.Lib()
assert len(l.bnf) == 211
assert sum(len(defs) for defs in l.bnf.values()) == 244