diff --git a/modules/compiler_v2/go_compiler_v2/go_compiler.go b/modules/compiler_v2/go_compiler_v2/go_compiler.go index 5d4f3028e43ef36f8bbc6960542f0f8e062058ee..800797b92fd3d5cd83723cfe5db05a120d91d590 100644 --- a/modules/compiler_v2/go_compiler_v2/go_compiler.go +++ b/modules/compiler_v2/go_compiler_v2/go_compiler.go @@ -38,7 +38,9 @@ func (gb *GoCompiler) CheckCompileErrors(srcCode []byte) ([]byte, error) { // Run go build cmdString += " && go build -o main " + fileName - //cmdSlice := strings.Fields(cmdString) + // Run tests + cmdString += " && go test " + fileName + cmd := utils.MakeCommand(cmdString) cmd.Dir = consts.TempOutputDir return cmd.CombinedOutput() diff --git a/modules/compiler_v2/go_compiler_v2/go_compiler_test.go b/modules/compiler_v2/go_compiler_v2/go_compiler_test.go index 3a7809b0902512c056aa9a77b3f362bad9a5854f..02ea84c6c7360a29aa4bb14687849b29060767df 100644 --- a/modules/compiler_v2/go_compiler_v2/go_compiler_test.go +++ b/modules/compiler_v2/go_compiler_v2/go_compiler_test.go @@ -27,6 +27,14 @@ func TestCompileStringToGo(t *testing.T) { filename: "should_compile_with_external_dependencies", shouldCompile: true, }, + { + filename: "should_compile_and_run_tests", + shouldCompile: true, + }, + { + filename: "should_compile_with_faulty_test", + shouldCompile: true, + }, } for _, test := range tests { diff --git a/modules/compiler_v2/go_compiler_v2/should_compile_and_run_tests b/modules/compiler_v2/go_compiler_v2/should_compile_and_run_tests new file mode 100644 index 0000000000000000000000000000000000000000..32c15eef9526acc3ee3129281f7650ba6732b35d --- /dev/null +++ b/modules/compiler_v2/go_compiler_v2/should_compile_and_run_tests @@ -0,0 +1,48 @@ +package main + +import ( + "errors" + "fmt" + "testing" +) + +// Divide divides two numbers and returns the result. +// Returns an error if division by zero is attempted. +func Divide(a, b float64) (float64, error) { + if b == 0 { + return 0, errors.New("cannot divide by zero") + } + return a / b, nil +} + +// 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) + } + + // Test case 2: Division by zero + _, err = Divide(10, 0) + if err == nil { + t.Error("Expected error for division by zero, got nil") + } + + // 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) + } +} + +// main function for demonstration purposes +func main() { + a, b := 10.0, 2.0 + result, err := Divide(a, b) + if err != nil { + fmt.Println("Error:", err) + } else { + fmt.Printf("Result of %.2f / %.2f = %.2f\n", a, b, result) + } +} diff --git a/modules/compiler_v2/go_compiler_v2/should_compile_with_faulty_test b/modules/compiler_v2/go_compiler_v2/should_compile_with_faulty_test new file mode 100644 index 0000000000000000000000000000000000000000..a98891139495585829592e5b2cf2645f43b97dd9 --- /dev/null +++ b/modules/compiler_v2/go_compiler_v2/should_compile_with_faulty_test @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "testing" +) + +// Add adds two integers and returns the result. +func Add(a, b int) int { + return a + b +} + +// Test cases for Add function +func TestAdd(t *testing.T) { + // Test case 1: Normal addition + result := Add(2, 3) + if result != 5 { + t.Errorf("Expected 5, got %v", result) + } + + // Faulty Test case 2: Incorrect expected result + result = Add(2, 2) + if result != 5 { // This is faulty, it should expect 4, not 5 + t.Errorf("Expected 5, got %v", result) + } + + // Test case 3: Adding negative numbers + result = Add(-2, -3) + if result != -5 { + t.Errorf("Expected -5, got %v", result) + } +} + +// main function for demonstration purposes +func main() { + a, b := 2, 3 + result := Add(a, b) + fmt.Printf("Result of %d + %d = %d\n", a, b, result) +}