-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_new_test.go
97 lines (71 loc) · 1.85 KB
/
error_new_test.go
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
package httperror
import (
"context"
"errors"
"testing"
)
func TestNewError(t *testing.T) {
err := NewError("message", 404)
var goodType Error
if !errors.As(err, &goodType) {
t.Error("NewError() should return an httperror.Error")
}
if goodType.Status() != 404 {
t.Error("Status() should return the status")
}
}
func TestNewErrorWithString(t *testing.T) {
err := NewError("message", 404)
var goodType Error
if !errors.As(err, &goodType) {
t.Error("NewError() should return an httperror.Error")
}
if goodType.Error() != "message" {
t.Error("Error() should return \"message\"")
}
}
func TestNewErrorWithError(t *testing.T) {
err := NewError(errors.New("message"), 404)
var goodType Error
if !errors.As(err, &goodType) {
t.Error("NewError() should return an httperror.Error")
}
if goodType.Error() != "message" {
t.Error("Error() should return \"message\"")
}
}
func TestNewErrorWithCustomType(t *testing.T) {
err := NewError(struct {
message string
}{
message: "message",
}, 404)
var goodType Error
if !errors.As(err, &goodType) {
t.Error("NewError() should return an httperror.Error")
}
if goodType.Error() != "{message}" {
t.Errorf("Error() should return \"{message}\", %v given", goodType.Error())
}
}
func TestNewErrorWithCodeOption(t *testing.T) {
err := NewError("message", 404, WithCode("code"))
var goodType Error
if !errors.As(err, &goodType) {
t.Error("NewError() should return an httperror.Error")
}
if goodType.Code() != "code" {
t.Error("Code() should return \"code\"")
}
}
func TestNewErrorWithContextOption(t *testing.T) {
ctx := context.Background()
err := NewError("message", 404, WithContext(ctx))
var goodType Error
if !errors.As(err, &goodType) {
t.Error("NewError() should return an httperror.Error")
}
if goodType.Context() != ctx {
t.Error("Context() should return the context")
}
}