-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_test.go
More file actions
166 lines (147 loc) · 3.75 KB
/
examples_test.go
File metadata and controls
166 lines (147 loc) · 3.75 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"strings"
"testing"
)
func TestParseExampleTests(t *testing.T) {
tests := []struct {
name string
prompt string
wantLen int
wantFunc string
}{
{
name: "arrow syntax",
prompt: `Write a function to check palindromes
Tests:
isPalindrome("") -> true
isPalindrome("aba") -> true
isPalindrome("abc") -> false`,
wantLen: 3,
wantFunc: "isPalindrome",
},
{
name: "double arrow syntax",
prompt: `isPalindrome("racecar") => true
isPalindrome("hello") => false`,
wantLen: 2,
wantFunc: "isPalindrome",
},
{
name: "should return syntax",
prompt: `factorial(5) should return 120
factorial(0) should return 1
factorial(1) returns 1`,
wantLen: 3,
wantFunc: "factorial",
},
{
name: "no tests",
prompt: "Write me a hello world program",
wantLen: 0,
},
{
name: "mixed formats",
prompt: `sum(1, 2) -> 3
sum(0, 0) => 0
sum(-1, 1) should return 0`,
wantLen: 3,
wantFunc: "sum",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ParseExampleTests(tt.prompt)
if tt.wantLen == 0 {
if result != nil && len(result.Tests) > 0 {
t.Errorf("ParseExampleTests() expected no tests, got %d", len(result.Tests))
}
return
}
if result == nil {
t.Fatal("ParseExampleTests() returned nil, expected tests")
}
if len(result.Tests) != tt.wantLen {
t.Errorf("ParseExampleTests() got %d tests, want %d", len(result.Tests), tt.wantLen)
}
if tt.wantFunc != "" && result.FunctionName != tt.wantFunc {
t.Errorf("ParseExampleTests() function name = %q, want %q", result.FunctionName, tt.wantFunc)
}
})
}
}
func TestStripMainFunction(t *testing.T) {
code := `#include <iostream>
bool isPalindrome(const std::string& s) {
int left = 0, right = s.length() - 1;
while (left < right) {
if (s[left++] != s[right--]) return false;
}
return true;
}
int main() {
std::cout << isPalindrome("aba") << std::endl;
return 0;
}`
result := stripMainFunction(code)
if strings.Contains(result, "int main") {
t.Error("stripMainFunction() did not remove main()")
}
if !strings.Contains(result, "isPalindrome") {
t.Error("stripMainFunction() removed too much code")
}
}
func TestGenerateTestHarness(t *testing.T) {
code := `#include <string>
bool isPalindrome(const std::string& s) {
int left = 0, right = s.length() - 1;
while (left < right) {
if (s[left++] != s[right--]) return false;
}
return true;
}
int main() {
return 0;
}`
examples := &ExampleTests{
Tests: []TestCase{
{FunctionCall: `isPalindrome("")`, Expected: "true", Line: 1},
{FunctionCall: `isPalindrome("aba")`, Expected: "true", Line: 2},
{FunctionCall: `isPalindrome("abc")`, Expected: "false", Line: 3},
},
FunctionName: "isPalindrome",
}
harness := GenerateTestHarness(code, examples)
// Check harness has test framework
if !strings.Contains(harness, "EXPECT_EQ") {
t.Error("Harness missing EXPECT_EQ macro")
}
// Check harness has test calls
if !strings.Contains(harness, `isPalindrome("")`) {
t.Error("Harness missing test case 1")
}
if !strings.Contains(harness, `isPalindrome("aba")`) {
t.Error("Harness missing test case 2")
}
// Check original main is stripped
if strings.Contains(harness, "return 0;\n}") && strings.Count(harness, "int main") > 1 {
t.Error("Harness has multiple main functions")
}
}
func TestHasExampleTests(t *testing.T) {
tests := []struct {
prompt string
want bool
}{
{"Write hello world", false},
{"factorial(5) -> 120", true},
{"sum(1,2) should return 3", true},
{"", false},
}
for _, tt := range tests {
got := HasExampleTests(tt.prompt)
if got != tt.want {
t.Errorf("HasExampleTests(%q) = %v, want %v", tt.prompt, got, tt.want)
}
}
}