-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
53 lines (40 loc) · 1.3 KB
/
errors_test.go
File metadata and controls
53 lines (40 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package needle
import (
"errors"
"fmt"
"testing"
)
func TestError_Is_SameCode(t *testing.T) {
t.Parallel()
err1 := newError(ErrCodeServiceNotFound, "service A not found", nil)
err2 := newError(ErrCodeServiceNotFound, "service B not found", nil)
if !errors.Is(err1, err2) {
t.Error("errors with same code should match via Is")
}
}
func TestError_Is_DifferentCode(t *testing.T) {
t.Parallel()
err1 := newError(ErrCodeServiceNotFound, "not found", nil)
err2 := newError(ErrCodeCircularDependency, "cycle", nil)
if errors.Is(err1, err2) {
t.Error("errors with different codes should not match via Is")
}
}
func TestError_Is_DoesNotTraverseTargetChain(t *testing.T) {
t.Parallel()
inner := newError(ErrCodeServiceNotFound, "inner", nil)
wrapper := fmt.Errorf("wrapped: %w", inner)
check := newError(ErrCodeServiceNotFound, "check", nil)
if errors.Is(check, wrapper) {
t.Error("Is should not traverse target's chain, only direct type assertion on target")
}
}
func TestError_Is_WrappedSource(t *testing.T) {
t.Parallel()
inner := newError(ErrCodeServiceNotFound, "inner", nil)
wrapper := fmt.Errorf("wrapped: %w", inner)
target := newError(ErrCodeServiceNotFound, "target", nil)
if !errors.Is(wrapper, target) {
t.Error("errors.Is should find inner *Error via Unwrap chain of source")
}
}