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
No related branches found
No related tags found
No related merge requests found
......@@ -4,9 +4,11 @@ import (
"compiler_V2/consts"
"compiler_V2/utils"
"os"
"regexp"
)
const fileName = "main.go"
const testFileName = "main_test.go"
type GoCompiler struct{}
......@@ -27,19 +29,41 @@ func (gb *GoCompiler) CheckCompileErrors(srcCode []byte) ([]byte, error) {
utils.SetupTempFolders(consts.TempOutputDir)
defer utils.RemoveTempFolders(consts.TempOutputDir)
// Write code to file
err := os.WriteFile(consts.TempOutputDir+fileName, srcCode, 0644)
// Create regex to extract test functions from srcCode
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 {
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
cmdString += " && go build -o main " + fileName
// Run tests
cmdString += " && go test " + fileName
cmdString += " && go test -v "
cmd := utils.MakeCommand(cmdString)
cmd.Dir = consts.TempOutputDir
......
......@@ -31,9 +31,9 @@ func TestCompileStringToGo(t *testing.T) {
filename: "should_compile_and_run_tests",
shouldCompile: true,
},
{
filename: "should_compile_with_faulty_test",
shouldCompile: true,
{ // TODO might change name from should compile to should succeed
filename: "should_compile_with_faulty_test", // Code is syntactically correct, but the test is faulty
shouldCompile: false, // Here the test is faulty, so it will get a compiler error
},
}
......
......@@ -17,22 +17,46 @@ func Divide(a, b float64) (float64, error) {
// Test cases for Divide function
func TestDivide(t *testing.T) {
// Test case 1: Normal division
result, err := Divide(10, 2)
if err != nil || result != 5 {
t.Errorf("Expected 5, got %v, error: %v", result, err)
tests := []struct {
name string
a float64
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
_, err = Divide(10, 0)
if err == nil {
t.Error("Expected error for division by zero, got nil")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := Divide(tt.a, tt.b)
if (err != nil) != tt.wantErr {
t.Errorf("Expected error: %v, got: %v", tt.wantErr, err)
}
// Test case 3: Division with negative numbers
result, err = Divide(-10, 2)
if err != nil || result != -5 {
t.Errorf("Expected -5, got %v, error: %v", result, err)
if !tt.wantErr && result != tt.expected {
t.Errorf("Expected result: %v, got: %v", tt.expected, result)
}
})
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment