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
}, },
} }
......
...@@ -17,22 +17,46 @@ func Divide(a, b float64) (float64, error) { ...@@ -17,22 +17,46 @@ func Divide(a, b float64) (float64, error) {
// 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)
} }
if !tt.wantErr && result != tt.expected {
// Test case 3: Division with negative numbers t.Errorf("Expected result: %v, got: %v", tt.expected, result)
result, err = Divide(-10, 2) }
if err != nil || result != -5 { })
t.Errorf("Expected -5, got %v, error: %v", result, err)
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment