Skip to content
Snippets Groups Projects
Commit 9cbf49ab authored by Aleksander Einarsen's avatar Aleksander Einarsen
Browse files

Merge branch 'feature/compiler-module' into 'dev'

Feature/compiler module

See merge request ammar68/self-healing-llm!10
parents 7659fe57 9def2284
Branches
No related tags found
No related merge requests found
...@@ -4,9 +4,11 @@ import ( ...@@ -4,9 +4,11 @@ import (
"compiler_V2/consts" "compiler_V2/consts"
"compiler_V2/utils" "compiler_V2/utils"
"os" "os"
"regexp"
) )
const fileName = "main.go" const fileName = "main.go"
const testFileName = "main_test.go"
type GoCompiler struct{} type GoCompiler struct{}
...@@ -27,19 +29,41 @@ func (gb *GoCompiler) CheckCompileErrors(srcCode []byte) ([]byte, error) { ...@@ -27,19 +29,41 @@ func (gb *GoCompiler) CheckCompileErrors(srcCode []byte) ([]byte, error) {
utils.SetupTempFolders(consts.TempOutputDir) utils.SetupTempFolders(consts.TempOutputDir)
defer utils.RemoveTempFolders(consts.TempOutputDir) defer utils.RemoveTempFolders(consts.TempOutputDir)
// Write code to file // Create regex to extract test functions from srcCode
err := os.WriteFile(consts.TempOutputDir+fileName, srcCode, 0644) re := regexp.MustCompile(`(?m)^func\s+(Test\w+)\s*\(t\s+\*testing\.T\)\s*{[\s\S]*?^}`)
// Get all test functions from srcCode
testFunctions := re.FindAllString(string(srcCode), -1)
// Remove the test code from the main code
nonTestContent := re.ReplaceAllString(string(srcCode), "")
// Write code to main file
err := os.WriteFile(consts.TempOutputDir+fileName, []byte(nonTestContent), 0644)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Init go mod and tidy
cmdString := "go mod init tempOutput && go mod tidy " // Construct the content for the _test.go file.
testFileContent := "package main\n\n"
for _, match := range testFunctions {
testFileContent += match + "\n\n"
}
// Write code to test file, we need this since the tests are in the same file as the code
err2 := os.WriteFile(consts.TempOutputDir+testFileName, []byte(testFileContent), 0644)
if err2 != nil {
return nil, err2
}
// Init go mod, tidy (for dependencies) and goimports (for imports)
cmdString := "go mod init tempOutput && go mod tidy && goimports -w ."
// Run go build // Run go build
cmdString += " && go build -o main " + fileName cmdString += " && go build -o main " + fileName
// Run tests // Run tests
cmdString += " && go test " + fileName cmdString += " && go test -v "
cmd := utils.MakeCommand(cmdString) cmd := utils.MakeCommand(cmdString)
cmd.Dir = consts.TempOutputDir cmd.Dir = consts.TempOutputDir
......
...@@ -31,9 +31,9 @@ func TestCompileStringToGo(t *testing.T) { ...@@ -31,9 +31,9 @@ func TestCompileStringToGo(t *testing.T) {
filename: "should_compile_and_run_tests", filename: "should_compile_and_run_tests",
shouldCompile: true, shouldCompile: true,
}, },
{ { // TODO might change name from should compile to should succeed
filename: "should_compile_with_faulty_test", filename: "should_compile_with_faulty_test", // Code is syntactically correct, but the test is faulty
shouldCompile: true, shouldCompile: false, // Here the test is faulty, so it will get a compiler error
}, },
} }
......
package main package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"testing" "testing"
) )
// Divide divides two numbers and returns the result. // Divide divides two numbers and returns the result.
// Returns an error if division by zero is attempted. // Returns an error if division by zero is attempted.
func Divide(a, b float64) (float64, error) { func Divide(a, b float64) (float64, error) {
if b == 0 { if b == 0 {
return 0, errors.New("cannot divide by zero") return 0, errors.New("cannot divide by zero")
} }
return a / b, nil return a / b, nil
} }
// Test cases for Divide function // Test cases for Divide function
func TestDivide(t *testing.T) { func TestDivide(t *testing.T) {
// Test case 1: Normal division tests := []struct {
result, err := Divide(10, 2) name string
if err != nil || result != 5 { a float64
t.Errorf("Expected 5, got %v, error: %v", result, err) b float64
} expected float64
wantErr bool
}{
{
name: "Normal division",
a: 10,
b: 2,
expected: 5,
wantErr: false,
},
{
name: "Division by zero",
a: 10,
b: 0,
expected: 0, // expected is not used when wantErr is true
wantErr: true,
},
{
name: "Division with negative numbers",
a: -10,
b: 2,
expected: -5,
wantErr: false,
},
}
// Test case 2: Division by zero for _, tt := range tests {
_, err = Divide(10, 0) t.Run(tt.name, func(t *testing.T) {
if err == nil { result, err := Divide(tt.a, tt.b)
t.Error("Expected error for division by zero, got nil") if (err != nil) != tt.wantErr {
} t.Errorf("Expected error: %v, got: %v", tt.wantErr, err)
}
// Test case 3: Division with negative numbers if !tt.wantErr && result != tt.expected {
result, err = Divide(-10, 2) t.Errorf("Expected result: %v, got: %v", tt.expected, result)
if err != nil || result != -5 { }
t.Errorf("Expected -5, got %v, error: %v", result, err) })
} }
} }
// main function for demonstration purposes // main function for demonstration purposes
func main() { func main() {
a, b := 10.0, 2.0 a, b := 10.0, 2.0
result, err := Divide(a, b) result, err := Divide(a, b)
if err != nil { if err != nil {
fmt.Println("Error:", err) fmt.Println("Error:", err)
} else { } else {
fmt.Printf("Result of %.2f / %.2f = %.2f\n", a, b, result) fmt.Printf("Result of %.2f / %.2f = %.2f\n", a, b, result)
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment