-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalternative_tests.py
More file actions
95 lines (71 loc) · 3.18 KB
/
alternative_tests.py
File metadata and controls
95 lines (71 loc) · 3.18 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
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
""" An attempt at making the tests faster by exploiting
the fact that most actions just fail and cause no other effect"""
import actions as act
from copy import copy, deepcopy
import queue
import core_logic
import ast
def test_action_sequences(default_ast, max_action_sequence_length):
actions = list(act.actions.keys())
to_test = queue.SimpleQueue()
get_vim_input = lambda x: 'USER_INPUT'
# Insert the initial set of actions
for action in actions:
to_test.put((deepcopy(default_ast), [], [], action))
while (case_to_test := to_test.get()) if not to_test.empty() else False:
tree, cursor_trail, actions_performed, action_name = case_to_test
action_function, is_local = act.actions[action_name]
try:
cursor_trail_after, tree_after = core_logic.core_act(
action_function,
is_local,
copy(cursor_trail),
deepcopy(tree),
get_vim_input)
except Exception as e:
print("Action sequence: ", str(actions_performed + [action_name]))
raise e
# The whole point of this alternative test:
# If the action kept the state intact, don't bother following it up with anything
if cursor_trail == cursor_trail_after and identical_nodes(tree, tree_after):
continue
# Assert the invariant (The ast should be valid at all times)
assert core_logic.is_valid_ast(tree
), "actions: " + str(actions_performed + [action_name])
# This print makes it satisfying to watch the number of remainig checks going down
print(to_test.qsize())
if len(actions_performed) < max_action_sequence_length:
for action in actions:
to_test.put((tree_after, cursor_trail_after,
actions_performed + [action_name], action))
def identical_nodes(tree1, tree2):
""" Tests if the two ast nodes are identical """
# TODO: Check more than the fields for the module node
if isinstance(tree1, list) and isinstance(tree2, list):
if len(tree1) != len(tree2):
#print("Different lens: ", len(tree1), len(tree2))
return False
for (v1, v2) in zip(tree1, tree2):
if not identical_nodes(v1, v2):
#print(v1, " different from ", v2)
return False
return True
# Do regular equality on regular objects
if not (isinstance(tree1, ast.AST) and isinstance(tree2, ast.AST)):
#print("regular equality: ", tree1, tree2)
return tree1 == tree2
if not tree1._fields == tree2._fields:
#print("Different field")
return False
# Check if each field has an identical node
for fieldname in tree1._fields:
if not identical_nodes(getattr(tree1, fieldname), getattr(tree2, fieldname)):
#print("Non identical ", fieldname)
return False
return True
if __name__ == '__main__':
with open('python_file_examples/sudoku.py', 'r') as f:
tree = ast.parse(f.read())
test_action_sequences(tree, 3)
#copy = deepcopy(tree)
#print(identical_nodes(tree, copy))