forked from javi11/nntppool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
154 lines (142 loc) · 3.85 KB
/
errors_test.go
File metadata and controls
154 lines (142 loc) · 3.85 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package nntppool
import (
"errors"
"fmt"
"testing"
)
func TestError_Error(t *testing.T) {
tests := []struct {
code int
msg string
want string
}{
{430, "no such article", "nntp: 430 no such article"},
{480, "authentication required", "nntp: 480 authentication required"},
{502, "", "nntp: 502 "},
{0, "empty", "nntp: 0 empty"},
}
for _, tt := range tests {
e := &Error{Code: tt.code, Message: tt.msg}
if got := e.Error(); got != tt.want {
t.Errorf("Error{%d, %q}.Error() = %q, want %q", tt.code, tt.msg, got, tt.want)
}
}
}
func TestError_Is(t *testing.T) {
// 423 and 430 are both "article not found" category
err423 := &Error{Code: 423, Message: "no article with that number"}
err430 := &Error{Code: 430, Message: "no such article"}
err480 := &Error{Code: 480, Message: "auth required"}
// Same category: 423 matches 430
if !errors.Is(err423, err430) {
t.Error("423 should match 430 (same category)")
}
if !errors.Is(err430, err423) {
t.Error("430 should match 423 (same category)")
}
if !errors.Is(err430, ErrArticleNotFound) {
t.Error("430 should match ErrArticleNotFound")
}
if !errors.Is(err423, ErrArticleNotFound) {
t.Error("423 should match ErrArticleNotFound")
}
// Cross-category rejection
if errors.Is(err423, err480) {
t.Error("423 should NOT match 480")
}
if errors.Is(err480, err430) {
t.Error("480 should NOT match 430")
}
// Non-*Error target
if errors.Is(err423, fmt.Errorf("random")) {
t.Error("*Error should not match non-*Error")
}
}
func TestErrorCategory(t *testing.T) {
tests := []struct {
code int
want int
}{
{423, 430},
{430, 430},
{480, 480},
{200, 200},
{0, 0},
}
for _, tt := range tests {
if got := errorCategory(tt.code); got != tt.want {
t.Errorf("errorCategory(%d) = %d, want %d", tt.code, got, tt.want)
}
}
}
func TestToError(t *testing.T) {
tests := []struct {
code int
status string
want error
isNil bool
}{
{200, "OK", nil, true},
{201, "posting ok", nil, true},
{399, "upper bound success", nil, true},
{423, "no article", ErrArticleNotFound, false},
{430, "no such article", ErrArticleNotFound, false},
{480, "auth required", ErrAuthRequired, false},
{481, "auth rejected", ErrAuthRejected, false},
{502, "service unavailable", ErrServiceUnavailable, false},
}
for _, tt := range tests {
got := toError(tt.code, tt.status)
if tt.isNil {
if got != nil {
t.Errorf("toError(%d, %q) = %v, want nil", tt.code, tt.status, got)
}
continue
}
if got == nil {
t.Errorf("toError(%d, %q) = nil, want %v", tt.code, tt.status, tt.want)
continue
}
if !errors.Is(got, tt.want) {
t.Errorf("toError(%d, %q) = %v, want errors.Is(%v)", tt.code, tt.status, got, tt.want)
}
}
// Unknown code → generic *Error
got := toError(599, "weird")
var e *Error
if !errors.As(got, &e) {
t.Fatal("toError(599) should return *Error")
}
if e.Code != 599 || e.Message != "weird" {
t.Errorf("toError(599, weird) = {%d, %q}, want {599, weird}", e.Code, e.Message)
}
}
func TestGreetingError_Error(t *testing.T) {
e := &greetingError{StatusCode: 502, Message: "service permanently unavailable"}
want := "nntp greeting: 502 service permanently unavailable"
if got := e.Error(); got != want {
t.Errorf("greetingError.Error() = %q, want %q", got, want)
}
}
func TestGreetingError_Is(t *testing.T) {
tests := []struct {
code int
want bool
}{
{502, true},
{400, true},
{200, false},
{481, false},
}
for _, tt := range tests {
e := &greetingError{StatusCode: tt.code, Message: "test"}
if got := errors.Is(e, ErrMaxConnections); got != tt.want {
t.Errorf("greetingError{%d}.Is(ErrMaxConnections) = %v, want %v", tt.code, got, tt.want)
}
}
// Non-ErrMaxConnections target
e := &greetingError{StatusCode: 502, Message: "test"}
if errors.Is(e, ErrArticleNotFound) {
t.Error("greetingError should not match ErrArticleNotFound")
}
}