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
|
class ParserModuleTest:
spaceconfig = dict(usemodules=["parser"])
def setup_class(cls):
cls.w_m = cls.space.appexec([], """():
import parser
return parser""")
cls.w_symbol = cls.space.appexec([], """():
import symbol
return symbol""")
class AppTestParser(ParserModuleTest):
def test_suite(self):
s = self.m.suite("x = 4")
assert isinstance(s, self.m.STType)
assert self.m.issuite(s)
assert s.issuite()
assert not self.m.isexpr(s)
assert not s.isexpr()
def test_expr(self):
s = self.m.expr("x")
assert isinstance(s, self.m.STType)
assert self.m.isexpr(s)
assert s.isexpr()
assert not self.m.issuite(s)
assert not s.issuite()
def test_totuple_and_tolist(self):
for meth, tp in (("totuple", tuple), ("tolist", list)):
s = self.m.suite("x = 4")
seq = getattr(s, meth)()
assert isinstance(seq, tp)
assert len(seq) == 4
assert seq[0] == self.symbol.file_input
assert len(seq[2]) == 2
assert len(seq[3]) == 2
assert seq[2][0] == 4
assert seq[3][0] == 0
seq = getattr(s, meth)(True)
assert len(seq[2]) == 3
assert seq[2][2] == 1
seq = getattr(s, meth)(True, True)
assert len(seq[2]) == 4
assert seq[2][2] == 1
assert seq[2][3] == 0
def test_compile(self):
import types
for code in (self.m.suite("x = 4").compile(),
self.m.compilest(self.m.suite("x = 4"))):
assert isinstance(code, types.CodeType)
assert code.co_filename == "<syntax-tree>"
def test_error(self):
assert repr(self.m.ParserError) == "<class 'parser.ParserError'>"
def test_roundtrip(self):
def roundtrip(f, s):
st1 = f(s)
t = st1.totuple()
st2 = self.m.sequence2st(t)
assert t == st2.totuple()
def check_expr(s):
roundtrip(self.m.expr, s)
def check_suite(s):
roundtrip(self.m.suite, s)
check_expr("foo(1)")
check_suite("def f(): yield 1")
def test_bad_tree(self):
import parser
# from import a
tree = \
(257,
(267,
(268,
(269,
(281,
(283, (1, 'from'), (1, 'import'),
(286, (284, (1, 'fred')))))),
(4, ''))),
(4, ''), (0, ''))
raises(parser.ParserError,
parser.sequence2st, tree)
|