From c41cfe670abd5c9302f91ac1a8108a4e6adafadd Mon Sep 17 00:00:00 2001 From: Aleksander Einarsen <aleksace@stud.ntnu.no> Date: Wed, 16 Oct 2024 18:29:20 +0200 Subject: [PATCH] #86 Added code for running tests, but "go test" cannot run tests that are not in a _test.go file... --- .../compiler_v2/go_compiler_v2/go_compiler.go | 4 +- .../go_compiler_v2/go_compiler_test.go | 8 ++++ .../should_compile_and_run_tests | 48 +++++++++++++++++++ .../should_compile_with_faulty_test | 39 +++++++++++++++ 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 modules/compiler_v2/go_compiler_v2/should_compile_and_run_tests create mode 100644 modules/compiler_v2/go_compiler_v2/should_compile_with_faulty_test diff --git a/modules/compiler_v2/go_compiler_v2/go_compiler.go b/modules/compiler_v2/go_compiler_v2/go_compiler.go index 5d4f302..800797b 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 3a7809b..02ea84c 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 0000000..32c15ee --- /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 0000000..a988911 --- /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) +} -- GitLab