Skip to content

Commit dc4ddaa

Browse files
committed
Make grammar tests
1 parent 12ea25a commit dc4ddaa

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

parser/grammar_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ func TestGrammar(t *testing.T) {
1414
mode string
1515
out string
1616
}{
17-
{"", "exec", "Module(Body=[])"},
18-
{"pass", "exec", "Module(Body=[Pass()])"},
19-
{"()", "eval", "Expression(Body=Tuple(Elts=[],Ctx=UnknownExprContext(0)))"},
20-
{"()", "exec", "Module(Body=[ExprStmt(Value=Tuple(Elts=[],Ctx=UnknownExprContext(0)))])"},
21-
{"[ ]", "exec", "Module(Body=[ExprStmt(Value=List(Elts=[],Ctx=UnknownExprContext(0)))])"},
17+
// START TESTS
18+
{"", "exec", "Module(body=[])"},
19+
{"pass", "exec", "Module(body=[Pass()])"},
20+
{"()", "eval", "Expression(body=Tuple(elts=[], ctx=Load()))"},
21+
{"()", "exec", "Module(body=[Expr(value=Tuple(elts=[], ctx=Load()))])"},
22+
{"[ ]", "exec", "Module(body=[Expr(value=List(elts=[], ctx=Load()))])"},
23+
// END TESTS
2224
} {
2325
Ast, err := ParseString(test.in, test.mode)
2426
if err != nil {

parser/make_grammar_test.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Read in grammar_test.go, and re-write the tests section
4+
"""
5+
6+
import sys
7+
import ast
8+
9+
inp = [
10+
("", "exec"),
11+
("pass", "exec"),
12+
("()", "eval"),
13+
("()", "exec"),
14+
("[ ]", "exec"),
15+
]
16+
17+
def dump(source, mode):
18+
"""Dump source after parsing with mode"""
19+
a = ast.parse(source, mode=mode)
20+
return ast.dump(a, annotate_fields=True, include_attributes=False)
21+
22+
def escape(x):
23+
"""Encode strings with backslashes for python/go"""
24+
return x.encode("unicode_escape").decode("utf-8")
25+
26+
def main():
27+
"""Read in grammar_test.go, and re-write the tests section"""
28+
path = "grammar_test.go"
29+
with open(path) as f:
30+
grammar_test = f.read()
31+
lines = grammar_test.split("\n")
32+
while lines[-1] == "":
33+
lines = lines[:-1]
34+
out = []
35+
in_tests = False
36+
for line in lines:
37+
if "START TESTS" in line:
38+
out.append(line)
39+
for source, mode in inp:
40+
out.append('\t\t{"%s", "%s", "%s"},' % (escape(source), mode, escape(dump(source, mode))))
41+
in_tests = True
42+
elif "END TESTS" in line:
43+
in_tests = False
44+
if not in_tests:
45+
out.append(line)
46+
print("Rewriting %s" % path)
47+
with open(path, "w") as f:
48+
f.write("\n".join(out))
49+
f.write("\n")
50+
51+
if __name__ == "__main__":
52+
main()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy