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

#86 Updated tests and test file contents

parent 8af937f4
No related branches found
No related tags found
No related merge requests found
...@@ -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