-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_syntax_augmentation.py
148 lines (130 loc) · 4.44 KB
/
test_syntax_augmentation.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
# -*- coding: utf-8 -*-
import ast
import sys
import pyccolo as pyc
if sys.version_info >= (3, 8): # noqa
add_42_spec = pyc.AugmentationSpec(
aug_type=pyc.AugmentationType.binop, token="++", replacement="+"
)
def test_augmented_plus():
class Add42(pyc.BaseTracer):
@property
def syntax_augmentation_specs(self):
return [add_42_spec]
@pyc.after_binop(when=lambda node: isinstance(node.op, ast.Add))
def handle_add(self, ret, node, *_, **__):
if add_42_spec in self.get_augmentations(id(node)):
return ret + 42
else:
return ret
tracer = Add42.instance()
env = tracer.exec("x = 21 ++ 21")
assert env["x"] == 84, "got %s" % env["x"]
env = tracer.exec("x = x + 21", local_env=env)
assert env["x"] == 105
coalesce_dot_spec = pyc.AugmentationSpec(
aug_type=pyc.AugmentationType.dot, token="?.", replacement="."
)
def test_optional_chaining():
from pyccolo.examples import OptionalChainer
OptionalChainer.instance().exec(
"""
class Foo:
def __init__(self, x):
self.x = x
foo = Foo(Foo(Foo(None)))
try:
bar = foo.x.x.x.x
except:
pass
else:
assert False
assert foo.x.x.x?.x is None
assert foo.x.x.x?.x() is None
assert foo.x.x.x?.x?.whatever is None
assert isinstance(foo?.x?.x, Foo)
assert isinstance(foo.x?.x, Foo)
assert isinstance(foo?.x.x, Foo)
"""
)
prefix_spec = pyc.AugmentationSpec(
aug_type=pyc.AugmentationType.prefix, token="$", replacement=""
)
suffix_spec = pyc.AugmentationSpec(
aug_type=pyc.AugmentationType.suffix, token="$$", replacement=""
)
def test_prefix_suffix():
class IncrementAugmentedTracer(pyc.BaseTracer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._delayed_increment = 0
@property
def syntax_augmentation_specs(self):
return [suffix_spec, prefix_spec]
@pyc.load_name
def handle_name(self, ret, node, *_, **__):
self._delayed_increment = 0
node_id = id(node)
augs = self.get_augmentations(node_id)
assert not (prefix_spec in augs and suffix_spec in augs)
if prefix_spec in augs:
offset = 1
elif suffix_spec in augs:
offset = 2
else:
offset = 0
if isinstance(ret, int):
return ret + offset
else:
self._delayed_increment = offset
return ret
@pyc.after_attribute_load
def handle_attr(self, ret, node, *_, **__):
if isinstance(node.value, ast.Name):
ret += self._delayed_increment
self._delayed_increment = 0
augs = self.get_augmentations(id(node))
assert not (prefix_spec in augs and suffix_spec in augs)
if prefix_spec in augs:
return ret + 1
elif suffix_spec in augs:
return ret + 2
else:
return ret
with IncrementAugmentedTracer.instance():
assert (
pyc.exec(
"""
class Foo:
y = 4
x = 3
foo = Foo()
z = $x + foo.y$$
"""
)["z"]
== 10
)
assert (
pyc.exec(
"""
class Foo:
y = 4
x = 3
foo = Foo()
z = $x + $foo.y
"""
)["z"]
== 9
)
assert (
pyc.exec(
"""
class Foo:
y = 4
x = 3
foo = Foo()
z = $x + $foo.y$$
"""
)["z"]
== 11
)