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) {
filename: "should_compile_and_run_tests",
shouldCompile: true,
},
{
filename: "should_compile_with_faulty_test",
shouldCompile: true,
{ // TODO might change name from should compile to should succeed
filename: "should_compile_with_faulty_test", // Code is syntactically correct, but the test is faulty
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) {
// 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)
tests := []struct {
name string
a float64
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
_, err = Divide(10, 0)
if err == nil {
t.Error("Expected error for division by zero, got nil")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := Divide(tt.a, tt.b)
if (err != nil) != tt.wantErr {
t.Errorf("Expected error: %v, got: %v", tt.wantErr, err)
}
// 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)
if !tt.wantErr && result != tt.expected {
t.Errorf("Expected result: %v, got: %v", tt.expected, result)
}
})
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment