-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from Syuparn/support-errors-is-as
feat: support errors.Is(), errors.As() in RedisError
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package redsync | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestRedisErrorIs(t *testing.T) { | ||
cases := map[string]error{ | ||
"defined_error": ErrFailed, | ||
"other_error": errors.New("other error"), | ||
"wrapping_error": fmt.Errorf("wrapping: %w", ErrFailed), | ||
} | ||
|
||
for k, v := range cases { | ||
|
||
t.Run(k, func(t *testing.T) { | ||
err := RedisError{ | ||
Node: 0, | ||
Err: v, | ||
} | ||
|
||
if !errors.Is(err, v) { | ||
t.Errorf("errors.Is must be true") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRedisErrorAs(t *testing.T) { | ||
derr := dummyError{} | ||
|
||
cases := map[string]error{ | ||
"simple_error": derr, | ||
"wrapping_error": fmt.Errorf("wrapping: %w", derr), | ||
} | ||
|
||
for k, v := range cases { | ||
|
||
t.Run(k, func(t *testing.T) { | ||
err := RedisError{ | ||
Node: 0, | ||
Err: v, | ||
} | ||
|
||
var target dummyError | ||
if !errors.As(err, &target) { | ||
t.Errorf("errors.As must be true") | ||
} | ||
|
||
if target != derr { | ||
t.Errorf("Expected target == %q, got %q", derr, target) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
type dummyError struct{} | ||
|
||
func (err dummyError) Error() string { | ||
return "dummy" | ||
} |