-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cast_test.go
106 lines (98 loc) · 2.18 KB
/
cast_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
// Copyright 2022 Ainsley Clark. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package errors
import (
"fmt"
"reflect"
"testing"
)
func TestError_Code(t *testing.T) {
tt := map[string]struct {
input error
want string
}{
"Normal": {
&Error{Code: INTERNAL, Message: "test", Operation: "op", Err: fmt.Errorf("err")},
"internal",
},
"Nil Input": {
nil,
"",
},
"Nil Code": {
&Error{Code: "", Message: "test", Operation: "op", Err: fmt.Errorf("err")},
"internal",
},
}
for name, test := range tt {
t.Run(name, func(t *testing.T) {
got := Code(test.input)
if !reflect.DeepEqual(test.want, got) {
t.Fatalf("expecting %s, got %s", test.want, got)
}
})
}
}
func Test_Message(t *testing.T) {
tt := map[string]struct {
input error
want string
}{
"Normal": {
&Error{Code: INTERNAL, Message: "test", Operation: "op", Err: fmt.Errorf("err")},
"test",
},
"Nil Input": {
nil,
"",
},
"Nil Message": {
&Error{Code: "", Message: "", Operation: "op", Err: fmt.Errorf("err")},
GlobalError,
},
}
for name, test := range tt {
t.Run(name, func(t *testing.T) {
got := Message(test.input)
if !reflect.DeepEqual(test.want, got) {
t.Fatalf("expecting %s, got %s", test.want, got)
}
})
}
}
func TestError_ToError(t *testing.T) {
tt := map[string]struct {
input any
want *Error
}{
"Pointer": {
&Error{Code: INTERNAL, Message: "test", Operation: "op", Err: fmt.Errorf("err")},
&Error{Code: INTERNAL, Message: "test", Operation: "op", Err: fmt.Errorf("err")},
},
"Non Pointer": {
Error{Code: INTERNAL, Message: "test", Operation: "op", Err: fmt.Errorf("err")},
&Error{Code: INTERNAL, Message: "test", Operation: "op", Err: fmt.Errorf("err")},
},
"Error": {
fmt.Errorf("err"),
&Error{Err: fmt.Errorf("err")},
},
"String": {
"err",
&Error{Err: fmt.Errorf("err")},
},
"Default": {
nil,
nil,
},
}
for name, test := range tt {
t.Run(name, func(t *testing.T) {
got := ToError(test.input)
if !reflect.DeepEqual(test.want, got) {
t.Fatalf("expecting %s, got %s", test.want, got)
}
})
}
}