-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathtest_interpreter_python.py
89 lines (74 loc) · 2.4 KB
/
test_interpreter_python.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
import os
from GeneralAgent.interpreter import PythonInterpreter
def test_python_interpreter():
# test run
serialize_path = "test/data/test_interpreter.bin"
if os.path.exists(serialize_path):
os.remove(serialize_path)
interpreter = PythonInterpreter(serialize_path=serialize_path)
result, is_stop = interpreter.output_parse(
'```python\n#run code\n"hello world"\n```'
)
print(result)
assert "hello world" in result.strip()
# assert is_stop is False
# test aug assignment
interpreter.set_variable("a", 10)
result, is_stop = interpreter.output_parse("```python\n#run code\na += 1\n```")
a = interpreter.get_variable("a")
assert a == 11
result, is_stop = interpreter.output_parse("```python\n#run code\na += 1\n```")
a = interpreter.get_variable("a")
assert a == 12
# test ann assignment
result, is_stop = interpreter.output_parse("```python\n#run code\na: int = 1\n```")
a = interpreter.get_variable("a")
assert a == 1
# test normal assignment
result, is_stop = interpreter.output_parse("```python\n#run code\nb = 1\n```")
b = interpreter.get_variable("b")
assert b == 1
# test multiline code
result, is_stop = interpreter.output_parse(
"```python\n#run code\n[\n 1,\n 2,\n 3\n]\n```"
)
assert "[1, 2, 3]" == result.split("\n")[-2]
# test multiple assignment
result, is_stop = interpreter.output_parse("```python\n#run code\na, b = 1, 2\n```")
a = interpreter.get_variable("a")
b = interpreter.get_variable("b")
assert a == 1
assert b == 2
assert "(1, 2)" == result.split("\n")[-2]
def test_stack_code():
serialize_path = "test/data/test_interpreter.bin"
if os.path.exists(serialize_path):
os.remove(serialize_path)
interpreter = PythonInterpreter(serialize_path=serialize_path)
code = """
```python
#run code
a = 10
code = "```python\\na += 1\\n```"
interpreter.output_parse(code)
a
```
"""
interpreter.set_variable("interpreter", interpreter)
result, is_stop = interpreter.output_parse(code)
# print(result)
assert "11" in result.strip()
# output:
# 11
# python runs result:
# run successfully
def test_run_code():
code = """
def test():
return "hello world"
test()
"""
interpreter = PythonInterpreter()
result, is_stop = interpreter.run_code(code)
# print(result)
assert "hello world" in result.strip()