Skip to content

Commit

Permalink
assert: add ErrorIs function
Browse files Browse the repository at this point in the history
  • Loading branch information
cornelk committed Nov 11, 2024
1 parent 931e147 commit 10e43f1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
18 changes: 18 additions & 0 deletions assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package assert

import (
"errors"
"fmt"
"reflect"
"testing"
Expand Down Expand Up @@ -47,6 +48,23 @@ func Error(t *testing.T, err error, expectedError string, errorMessage ...string
fail(t, msg, errorMessage...)
}

// ErrorIs asserts that a function returned an error that matches the specified error.
func ErrorIs(t *testing.T, err, expectedError error, errorMessage ...string) {
t.Helper()
if err == nil {
msg := fmt.Sprintf("Error not returned: \nexpected: %v\nactual : nil", expectedError)
fail(t, msg, errorMessage...)
return
}

if errors.Is(err, expectedError) {
return
}

msg := fmt.Sprintf("Error not equal: \nexpected: %v\nactual : %v", expectedError, err)
fail(t, msg, errorMessage...)
}

// True asserts that the specified value is true.
func True(t *testing.T, value bool, errorMessage ...string) {
t.Helper()
Expand Down
33 changes: 32 additions & 1 deletion assert/assert_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
package assert

import "testing"
import (
"errors"
"fmt"
"testing"
)

func TestEqual(t *testing.T) {
Equal(t, 1, 1)
}

func TestNoError(t *testing.T) {
NoError(t, nil)
}

func TestError(t *testing.T) {
err := errors.New("error text")
Error(t, err, err.Error())
}

func TestErrorIs(t *testing.T) {
errTest := errors.New("error")
err := fmt.Errorf("error: %w", errTest)
ErrorIs(t, err, errTest)
}

func TestTrue(t *testing.T) {
True(t, true)
}

func TestFalse(t *testing.T) {
False(t, false)
}

func TestInterfaceNilEqual(t *testing.T) {
var values []int
Expand Down

0 comments on commit 10e43f1

Please sign in to comment.