-
Notifications
You must be signed in to change notification settings - Fork 2
/
error_handled_test.go
110 lines (94 loc) · 2.7 KB
/
error_handled_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
98
99
100
101
102
103
104
105
106
107
108
109
110
package webfmwk
import (
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
var (
_testOP = 200
_testContent = "ok"
_testingErrorHandled = handledError{op: _testOP, content: _testContent} //nolint:errname
)
func TestGetOPCode(t *testing.T) {
assert.Equal(t, _testOP, _testingErrorHandled.GetOPCode())
}
func TestGetContent(t *testing.T) {
assert.Equal(t, _testContent, _testingErrorHandled.GetContent())
}
func TestFactory(t *testing.T) {
test := factory(_testOP, _testContent)
assert.Equal(t, _testOP, test.GetOPCode())
assert.Equal(t, _testContent, test.GetContent())
}
func TestNewErrorHandled(t *testing.T) {
e := NewErrorHandled(_testOP, _testContent)
assert.Equal(t, _testOP, e.GetOPCode())
assert.Equal(t, _testContent, e.GetContent())
assert.Equal(t, `[200]: "ok"`, e.Error())
}
func TestResponse(t *testing.T) {
assert.Equal(t, "test", NewResponse("test").Message)
}
var errTest = errors.New("test")
func TestError(t *testing.T) {
asserter := assert.New(t)
e := NewError("testing")
asserter.True(e.Message == "testing")
e = NewCustomWrappedError(errTest, "testing")
asserter.True(errors.Is(e.e, errTest))
asserter.Equal("testing", e.Message)
e = NewErrorFromError(errTest)
asserter.Equal("test", e.Message)
asserter.True(errors.Is(e.e, errTest))
asserter.Equal("test", e.Message)
asserter.Equal("test", e.Error())
}
func TestMethod(t *testing.T) {
tests := map[string]struct {
actual, expected int
}{
"processing": {
NewProcessing(_testContent).GetOPCode(), http.StatusProcessing,
},
"no contet": {
NewNoContent().GetOPCode(), http.StatusNoContent,
},
"bad request": {
NewBadRequest(_testContent).GetOPCode(), http.StatusBadRequest,
},
"unauthorized": {
NewUnauthorized(_testContent).GetOPCode(), http.StatusUnauthorized,
},
"forbidden": {
NewForbidden(_testContent).GetOPCode(), http.StatusForbidden,
},
"not found": {
NewNotFound(_testContent).GetOPCode(), http.StatusNotFound,
},
"not acceptable": {
NewNotAcceptable(_testContent).GetOPCode(), http.StatusNotAcceptable,
},
"conflict": {
NewConflict(_testContent).GetOPCode(), http.StatusConflict,
},
"unprocessable": {
NewUnprocessable(_testContent).GetOPCode(), http.StatusUnprocessableEntity,
},
"internal": {
NewInternal(_testContent).GetOPCode(), http.StatusInternalServerError,
},
"not implemented": {
NewNotImplemented(_testContent).GetOPCode(), http.StatusNotImplemented,
},
"service unavailable": {
NewServiceUnavailable(_testContent).GetOPCode(), http.StatusServiceUnavailable,
},
}
for name, te := range tests {
test := te
t.Run(name, func(t *testing.T) {
assert.Equal(t, test.expected, test.actual)
})
}
}