-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_test.go
352 lines (311 loc) · 8.41 KB
/
main_test.go
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package main
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"fmt"
openai "github.com/sashabaranov/go-openai"
"github.com/stretchr/testify/assert"
)
// Test case for function createTestFile
func TestCreateTestFile(t *testing.T) {
tempDir := t.TempDir()
testCases := []testCase{
{
Name: "TestAdd",
Code: "func TestAdd(t *testing.T) { ... }",
},
{
Name: "TestMultiply",
Code: "func TestMultiply(t *testing.T) { ... }",
},
}
err := createTestFile(tempDir+"/sample.go", "main", testCases)
if err != nil {
t.Fatalf("createTestFile() error: %v", err)
}
generatedFile := tempDir + "/sample_test.go"
if _, err := os.Stat(generatedFile); os.IsNotExist(err) {
t.Fatalf("Test file not created: %v", err)
}
content, err := ioutil.ReadFile(generatedFile)
if err != nil {
t.Fatalf("Reading generated file error: %v", err)
}
expectedContent := `package main
import (
"testing"
)
// Test case for function TestAdd
func TestAdd(t *testing.T) { ... }
// Test case for function TestMultiply
func TestMultiply(t *testing.T) { ... }
`
if string(content) != expectedContent {
t.Fatalf("Generated file content doesn't match expected content")
}
}
// Test case for function generateTestCases
func TestGenerateTestCases(t *testing.T) {
ctx := context.Background()
// Initialize the OpenAI client with the API key.
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
t.Skip("Skipping test due to missing API key.")
}
client := openai.NewClient(apiKey)
// Create the testdata directory if it doesn't exist
testdataDir := filepath.Join(".", "__testdata")
if _, err := os.Stat(testdataDir); os.IsNotExist(err) {
err = os.MkdirAll(testdataDir, os.ModePerm)
if err != nil {
t.Fatalf("Error creating testdata directory: %v\n", err)
}
}
path := filepath.Join(testdataDir, "sample.go")
// Setup and cleanup
err := ioutil.WriteFile(path, []byte(`
package testdata
func sum(a, b int) int {
return a + b
}
func main() {
a := 1
b := 2
c := sum(a, b)
}
`),
os.ModePerm)
if err != nil {
t.Fatalf("Error writing testdata file: %v\n", err)
}
defer os.RemoveAll(testdataDir)
// Run the function and capture the output
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
generateTestCases(ctx, client, path, openai.GPT3Dot5Turbo)
w.Close()
out, _ := ioutil.ReadAll(r)
os.Stdout = old
// Check if the test case is generated for 'sum' function
if !strings.Contains(string(out), "Generated test case for function sum") {
t.Errorf("Expected to find test case for 'sum' function, but it was not generated")
}
}
// Test case for function chatGPTTestCases
func TestChatGPTTestCases(t *testing.T) {
packageName := "main"
functionName := "exampleFunction"
functionCode := "func exampleFunction() {\n\t// function code\n}\n"
ctx := context.Background()
// Initialize the OpenAI client with the API key.
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
t.Skip("Skipping test due to missing API key.")
}
client := openai.NewClient(apiKey)
_, _, err := chatGPTTestCases(ctx, client, packageName, functionName, functionCode, openai.GPT3Dot5Turbo)
assert.NoError(t, err, "There should be no error")
}
// Test case for function sanitizeCode
func TestSanitizeCode(t *testing.T) {
tests := []struct {
name string
rawCode string
expectedCode string
expectedImportContent []string
expectedError error
}{
{
name: "No code block",
rawCode: "This is a sample text without a code block",
expectedCode: "This is a sample text without a code block",
expectedImportContent: nil,
expectedError: nil,
},
{
name: "Code block without go",
rawCode: "```\nfunc hello() {\n\tfmt.Println(\"Hello, test!\")\n}\n```",
expectedCode: "func hello() {\n\tfmt.Println(\"Hello, test!\")\n}",
expectedImportContent: nil,
expectedError: nil,
},
{
name: "Code block with go",
rawCode: "```go\nfunc hello() {\n\tfmt.Println(\"Hello, test!\")\n}\n```",
expectedCode: "func hello() {\n\tfmt.Println(\"Hello, test!\")\n}",
expectedImportContent: nil,
expectedError: nil,
},
{
name: "Code block with package and import",
rawCode: "```go\npackage main\n\nimport \"fmt\"\n\nfunc hello() {\n\tfmt.Println(\"Hello, test!\")\n}\n```",
expectedCode: "func hello() {\n\tfmt.Println(\"Hello, test!\")\n}",
expectedImportContent: []string{"\"fmt\""},
expectedError: nil,
},
{
name: "Multiple code blocks",
rawCode: "```go\nfunc hello() {\n\tfmt.Println(\"Hello, test!\")\n}\n```\n\nSome text\n\n```go\nfunc world() {\n\tfmt.Println(\"World, test!\")\n}\n```",
expectedCode: "func hello() {\n\tfmt.Println(\"Hello, test!\")\n}",
expectedImportContent: nil,
expectedError: nil,
},
{
name: "Multiple imports",
rawCode: "package main\n\nimport (\n\t\"fmt\"\n\t\"os\"\n)\n\nfunc main() {\n\tfmt.Println(\"Hello, world!\")\n}",
expectedCode: "func main() {\n\tfmt.Println(\"Hello, world!\")\n}",
expectedImportContent: []string{`"fmt"`, `"os"`},
expectedError: nil,
},
{
name: "Single-line import with alias",
rawCode: "package main\n\nimport f \"fmt\"\n\nfunc main() {\n\tf.Println(\"Hello, world!\")\n}",
expectedCode: "func main() {\n\tf.Println(\"Hello, world!\")\n}",
expectedImportContent: []string{`f "fmt"`},
expectedError: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
code, importContent, err := sanitizeCode(test.rawCode)
if code != test.expectedCode {
t.Errorf("Expected code: %s, got: %s", test.expectedCode, code)
}
equal := true
for i, v := range importContent {
if v != test.expectedImportContent[i] {
equal = false
break
}
}
if equal != true {
t.Errorf("Expected importContent: %s, got: %s", test.expectedImportContent, importContent)
}
if err != test.expectedError {
t.Errorf("Expected error: %v, got: %v", test.expectedError, err)
}
})
}
}
// Test case for function extractImports
func TestExtractImports(t *testing.T) {
tests := []struct {
name string
importBlock string
wantImports []string
}{
{
name: "Single line import",
importBlock: `import "fmt"`,
wantImports: []string{`"fmt"`},
},
{
name: "Single line named import",
importBlock: `import openai "github.com/sashabaranov/go-openai"`,
wantImports: []string{`openai "github.com/sashabaranov/go-openai"`},
},
{
name: "Multi-line import",
importBlock: `
import (
"fmt"
"errors"
)
`,
wantImports: []string{`"fmt"`, `"errors"`},
},
{
name: "Multi-line import with extra spaces",
importBlock: `
import (
"fmt"
"errors"
)
`,
wantImports: []string{`"fmt"`, `"errors"`},
},
{
name: "Multi-line import with empty lines",
importBlock: `
import (
"fmt"
"errors"
)
`,
wantImports: []string{`"fmt"`, `"errors"`},
},
{
name: "Empty import block",
importBlock: "",
wantImports: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotImports := extractImports(tt.importBlock); !equalSlices(gotImports, tt.wantImports) {
t.Errorf("extractImports() = %v, want %v", gotImports, tt.wantImports)
}
})
}
}
func equalSlices(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
for i, val := range s1 {
if val != s2[i] {
return false
}
}
return true
}
// Test case for function findImportBlock
func TestFindImportBlock(t *testing.T) {
testCases := []struct {
code string
expected string
}{
{
code: `package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}`,
expected: `import "fmt"`,
},
{
code: `package main
import (
"fmt"
"os"
)
func main() {
fmt.Println("Hello, world!")
}`,
expected: `import (
"fmt"
"os"
)`,
},
{
code: `package main
func main() {
fmt.Println("Hello, world!")
}`,
expected: "",
},
}
for i, testCase := range testCases {
t.Run(fmt.Sprintf("Test Case %d", i+1), func(t *testing.T) {
result := findImportBlock(testCase.code)
if result != testCase.expected {
t.Errorf("Expected: %s, got: %s", testCase.expected, result)
}
})
}
}