Skip to content

Commit

Permalink
implement Is method to errorsx
Browse files Browse the repository at this point in the history
Refactor views test using errors.Is

introduce xerrors.Is instead of errors.Is

Revert "Refactor views test using errors.Is"

This reverts commit f09d336.
  • Loading branch information
sryoya committed Dec 9, 2020
1 parent 686c209 commit c78729e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/errorsx/errorsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ type String string
func (t String) Error() string {
return string(t)
}

// Is reports whether String matches with the target error
func (t String) Is(target error) bool {
if target == nil {
return false
}

return t.Error() == target.Error()
}
18 changes: 18 additions & 0 deletions internal/errorsx/errrorsx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package errorsx

import (
"errors"
"testing"
)

func TestIs(t *testing.T) {
test := func(receiver String, target error, expected bool) {
if result := receiver.Is(target); result != expected {
t.Errorf(`expected "%s Is %s" should be %t , got: %t`, receiver, target, expected, result)
}
}

test(String("test error"), errors.New("test error"), true)
test(String("test error"), errors.New("different error"), false)
test(String("test error"), nil, false)
}

0 comments on commit c78729e

Please sign in to comment.