-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_basics.py
More file actions
96 lines (73 loc) · 2.47 KB
/
test_basics.py
File metadata and controls
96 lines (73 loc) · 2.47 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
96
import pytest
import llmscript
class Test:
def __init__(self, inside):
self.inside = inside
self.count = 0
def abc(self, val):
print(f"{self.inside} and {val}")
def increment(self):
self.count += 2
print(f"count is {self.count}")
return self.count
def add(self, a, b):
return a + b
def test_function_print(capfd):
a = Test("b")
instruction='x={_add(1,2)}; {abc({x})}'
llm = llmscript.LLMScript(a)
llm.run(instruction)
captured = capfd.readouterr()
assert captured.out == "b and 3\n"
def test_function_print_twice(capfd):
a = Test("b")
instruction='x={_add(1,2)}; {abc({x})}; {abc({x})}'
llm = llmscript.LLMScript(a)
llm.run(instruction)
captured = capfd.readouterr()
assert captured.out == "b and 3\nb and 3\n"
def test_simple_prime():
instruction='a prime number between 10 and 12'
llm = llmscript.LLMScript()
prime = llm.run(instruction, int)
assert prime == 11
def test_complex_prime():
a = Test(12)
instruction='a prime number between {_add(9,1)} and {inside}'
llm = llmscript.LLMScript(a)
prime = llm.run(instruction, int)
assert prime == 11
def test_change_variable():
a = Test("empty")
a.inside = ["bob", "tony", "phil"]
instruction='return a random name from {inside}'
llm = llmscript.LLMScript(a)
name = llm.run(instruction, str)
assert name in ["bob", "tony", "phil"]
def test_changing_variable():
a = Test("empty")
instruction='{_increment()} {_increment()} {_increment()} return {count}'
llm = llmscript.LLMScript(a)
count = llm.run(instruction, int, debug=True)
assert count == 6
def test_pass_local_variable():
instruction='return what genus of animal is {animal} in a single word'
llm = llmscript.LLMScript()
genus = llm.run(instruction, str, localVariables={"animal": "dog"})
assert genus == 'Canis'
def test_fake_db():
class FakeDB:
def getUser(self, name):
users = {
"bob": { "age": 65 },
"sue": { "age": 12 },
}
return users[name]
db = FakeDB()
llm = llmscript.LLMScript(db)
instruction='if {_getUser("bob")} is a senior citizen return "senior" else return "not senior"'
result = llm.run(instruction, str)
assert result == "senior"
instruction='if {_getUser("sue")} is a senior citizen return "senior" else return "not senior"'
result = llm.run(instruction, str)
assert result == "not senior"