-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
112 lines (101 loc) · 2.93 KB
/
Copy patherrors_test.go
File metadata and controls
112 lines (101 loc) · 2.93 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package browserpm
import (
"errors"
"strings"
"testing"
)
func TestBpmError_Error(t *testing.T) {
e := NewError(ErrSessionNotFound, "session x not found")
got := e.Error()
if got == "" {
t.Fatal("Error() should not return empty string")
}
if !strings.Contains(got, "browserpm") {
t.Errorf("Error() should contain 'browserpm', got %q", got)
}
}
func TestBpmError_ErrorWithCause(t *testing.T) {
cause := errors.New("underlying")
e := WrapError(cause, ErrSessionNotFound, "wrapped")
got := e.Error()
if got == "" {
t.Fatal("Error() should not return empty string")
}
if !errors.Is(e, cause) {
t.Error("WrapError should be unwrappable to cause")
}
}
func TestBpmError_Unwrap(t *testing.T) {
cause := errors.New("cause")
e := WrapError(cause, ErrInternal, "msg")
if e.Unwrap() != cause {
t.Errorf("Unwrap() = %v, want %v", e.Unwrap(), cause)
}
e2 := NewError(ErrSessionNotFound, "msg")
if e2.Unwrap() != nil {
t.Errorf("NewError Unwrap() should be nil, got %v", e2.Unwrap())
}
}
func TestBpmError_Is(t *testing.T) {
e := NewError(ErrSessionNotFound, "msg")
if !errors.Is(e, ErrSessionNotFoundErr) {
t.Error("errors.Is(e, ErrSessionNotFoundErr) should be true")
}
if errors.Is(e, ErrSessionExistsErr) {
t.Error("errors.Is(e, ErrSessionExistsErr) should be false")
}
wrapped := WrapError(e, ErrInternal, "wrapped")
if !errors.Is(wrapped, ErrInternalErr) {
t.Error("errors.Is(wrapped, ErrInternalErr) should be true")
}
// wrapped wraps e (ErrSessionNotFound), so chain contains both
if !errors.Is(wrapped, ErrSessionNotFoundErr) {
t.Error("errors.Is(wrapped, ErrSessionNotFoundErr) should be true (cause in chain)")
}
}
func TestBpmError_As(t *testing.T) {
e := NewError(ErrPoolExhausted, "pool full")
var target *BpmError
if !errors.As(e, &target) {
t.Fatal("errors.As should succeed")
}
if target.Code != ErrPoolExhausted {
t.Errorf("target.Code = %v, want ErrPoolExhausted", target.Code)
}
if target.Message != "pool full" {
t.Errorf("target.Message = %q, want %q", target.Message, "pool full")
}
}
func TestErrorCode_String(t *testing.T) {
if ErrSessionNotFound.String() != "SessionNotFound" {
t.Errorf("ErrSessionNotFound.String() = %q", ErrSessionNotFound.String())
}
if ErrInternal.String() != "Internal" {
t.Errorf("ErrInternal.String() = %q", ErrInternal.String())
}
if ErrorCode(999).String() == "" {
t.Error("unknown ErrorCode should have non-empty String()")
}
}
func TestNewError(t *testing.T) {
e := NewError(ErrClosed, "manager closed")
if e.Code != ErrClosed {
t.Errorf("Code = %v", e.Code)
}
if e.Message != "manager closed" {
t.Errorf("Message = %q", e.Message)
}
if e.Cause != nil {
t.Error("Cause should be nil")
}
}
func TestWrapError(t *testing.T) {
cause := errors.New("io error")
e := WrapError(cause, ErrPageUnavailable, "page init failed")
if e.Code != ErrPageUnavailable {
t.Errorf("Code = %v", e.Code)
}
if e.Cause != cause {
t.Errorf("Cause = %v", e.Cause)
}
}