26
26
from ..fixtures import kitchen_sink_query # noqa: F401
27
27
28
28
29
- def get_node_by_path (ast , path ):
30
- result = ast
31
- for key in path :
32
- if isinstance (key , int ):
33
- assert isinstance (result , list )
34
- try :
35
- result = result [key ]
36
- except IndexError :
37
- fail (f"invalid index { key } in node list { result } " )
38
- elif isinstance (key , str ):
39
- assert isinstance (result , Node )
40
- try :
41
- result = getattr (result , key )
42
- except AttributeError :
43
- fail (f"invalid key { key } in node { result } " )
44
- else :
45
- fail (f"invalid key { key !r} in path { path } " )
46
- return result
47
-
48
-
49
29
def check_visitor_fn_args (ast , node , key , parent , path , ancestors , is_edited = False ):
50
30
assert isinstance (node , Node )
51
31
@@ -59,18 +39,42 @@ def check_visitor_fn_args(ast, node, key, parent, path, ancestors, is_edited=Fal
59
39
return
60
40
61
41
assert isinstance (key , (int , str ))
62
- assert get_node_by_path (parent , [key ]) is not None
42
+
43
+ if isinstance (key , int ):
44
+ assert isinstance (parent , list )
45
+ assert 0 <= key <= len (parent )
46
+ else :
47
+ assert isinstance (parent , Node )
48
+ assert hasattr (parent , key )
49
+
63
50
assert isinstance (path , list )
64
51
assert path [- 1 ] == key
52
+
65
53
assert isinstance (ancestors , list )
66
54
assert len (ancestors ) == len (path ) - 1
67
55
68
56
if not is_edited :
69
- assert get_node_by_path ( parent , [ key ]) is node
70
- assert get_node_by_path ( ast , path ) is node
57
+ current_node = ast
58
+
71
59
for i , ancestor in enumerate (ancestors ):
72
- ancestor_path = path [:i ]
73
- assert ancestor == get_node_by_path (ast , ancestor_path )
60
+ assert ancestor is current_node
61
+ k = path [i ]
62
+ assert isinstance (k , (int , str ))
63
+ if isinstance (k , int ):
64
+ assert isinstance (current_node , list )
65
+ assert 0 <= k <= len (current_node )
66
+ current_node = current_node [k ]
67
+ else :
68
+ assert isinstance (current_node , Node )
69
+ assert hasattr (current_node , k )
70
+ current_node = getattr (current_node , k )
71
+ assert current_node is not None
72
+
73
+ assert parent is current_node
74
+ if isinstance (key , int ):
75
+ assert parent [key ] is node
76
+ else :
77
+ assert getattr (parent , key ) is node
74
78
75
79
76
80
def describe_visitor ():
0 commit comments