Repository navigation
Expand file tree
/
Copy pathtest_nameutil.py
More file actions
67 lines (52 loc) · 2.49 KB
/
Copy pathtest_nameutil.py
File metadata and controls
67 lines (52 loc) · 2.49 KB
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
# -*- coding: utf-8 -*-
"""Utilities for working with identifiers in macros."""
from ...syntax import macros, test
from ...test.fixtures import session, testset
from mcpyrate.quotes import macros, q, h # noqa: F401, F811
from mcpyrate.expander import MacroExpander
from ...syntax.nameutil import (isx, getname,
is_unexpanded_expr_macro, is_unexpanded_block_macro)
from ast import Call
# test data
def capture_this(): # the function must be defined at top level so h[] can pickle the object
pass # pragma: no cover
def runtests():
with testset("isx"):
barename = q[ok] # noqa: F821
captured = q[h[capture_this]()]
attribute = q[someobj.ok] # noqa: F821
test[isx(barename, "ok")]
test[type(captured) is Call]
test[isx(captured.func, "capture_this")]
test[isx(attribute, "ok")]
test[not isx(attribute, "ok", accept_attr=False)]
with testset("getname"):
test[getname(barename) == "ok"]
test[getname(captured.func) == "capture_this"]
test[getname(attribute) == "ok"]
test[getname(attribute, accept_attr=False) is None]
with testset("is_unexpanded_expr_macro"):
def dummymacro(tree, **kw):
return tree
m = MacroExpander({"dummy": dummymacro}, filename="<fake filename for tests in test_nameutil.py>")
# The tree being tested needs to invoke a macro that is bound in the expander we pass to the analyzer.
# Note we detect whether the invocation is bound to the macro function we expect,
# and we don't care about the name (because in `mcpyrate`, macros can be as-imported).
test[is_unexpanded_expr_macro(dummymacro, m, q[dummy[...]])] # noqa: F821, only quoted
test[not is_unexpanded_expr_macro(dummymacro, m, q[notdummy[...]])] # noqa: F821, only quoted
test[not is_unexpanded_expr_macro(dummymacro, m, q[42])]
with testset("is_unexpanded_block_macro"):
with q as quoted:
with dummy: # noqa: F821, only quoted
...
test[is_unexpanded_block_macro(dummymacro, m, quoted[0])]
with q as quoted:
with notdummy: # noqa: F821, only quoted
...
test[not is_unexpanded_block_macro(dummymacro, m, quoted[0])]
with q as quoted:
a = 42 # noqa: F841
test[not is_unexpanded_block_macro(dummymacro, m, quoted[0])]
if __name__ == '__main__': # pragma: no cover
with session(__file__):
runtests()