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

#86 Added code for running tests, but "go test" cannot run tests that are not in a _test.go file...

parent 11ccb98a
No related branches found
No related tags found
No related merge requests found
......@@ -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()
......
......@@ -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 {
......
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)
}
}
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)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment