-
Notifications
You must be signed in to change notification settings - Fork 1
/
codes_test.go
107 lines (98 loc) · 2.26 KB
/
codes_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
package errs
import (
"fmt"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"testing"
)
func TestCode_HTTP(t *testing.T) {
for i := 0; i < CodeSize; i++ {
testName := fmt.Sprintf("%s should not be zero", Code(i).String())
t.Run(testName, func(t *testing.T) {
if Code(i).HTTP() == 0 {
t.Errorf("Code(%d).HTTP() == 0", i)
}
})
}
}
func TestCode_GRPC(t *testing.T) {
for i := 0; i < CodeSize; i++ {
testName := fmt.Sprintf("%s should not be zero", Code(i).String())
t.Run(testName, func(t *testing.T) {
if Code(i).GRPC() == codes.OK {
t.Errorf("Code(%d).GRPC() == 0", i)
}
})
}
}
func TestCode_String(t *testing.T) {
for i := 0; i < CodeSize; i++ {
testName := fmt.Sprintf("%s should not be empty", Code(i).String())
t.Run(testName, func(t *testing.T) {
if Code(i).String() == "" {
t.Errorf("Code(%d).String() == \"\"", i)
}
})
}
}
func TestRegisterCode(t *testing.T) {
type args struct {
c Code
http int
grpc codes.Code
desc string
}
testCases := []struct {
name string
args args
shouldErr bool
}{
{
name: "registering entirely new codes",
args: args{
c: Code(100),
http: 100,
grpc: codes.Code(100),
desc: "test",
},
},
{
name: "overriding existing codes",
args: args{
c: NotFound,
http: 401,
grpc: codes.Unknown,
desc: "this is not found",
},
},
}
for _, tt := range testCases {
t.Run("Register "+tt.name, func(t *testing.T) {
// test register
RegisterCode(tt.args.c, tt.args.http, tt.args.grpc, tt.args.desc)
assert.Equal(t, tt.args.http, tt.args.c.HTTP())
assert.Equal(t, tt.args.grpc, tt.args.c.GRPC())
assert.Equal(t, tt.args.desc, tt.args.c.String())
// test register check
assert.True(t, IsRegistered(tt.args.c))
})
t.Run("Unregister "+tt.name, func(t *testing.T) {
// unregister and check
UnregisterCode(tt.args.c)
assert.False(t, IsRegistered(tt.args.c))
})
}
// test ClearCodeRegister
t.Run("ClearCodeRegister", func(t *testing.T) {
// given
for _, tt := range testCases {
RegisterCode(tt.args.c, tt.args.http, tt.args.grpc, tt.args.desc)
}
// when
ClearCodeRegister()
// then
for _, tt := range testCases {
assert.False(t, IsRegistered(tt.args.c))
}
})
}