-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgrace_test.go
137 lines (126 loc) · 2.58 KB
/
grace_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
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
package grace
import (
"strconv"
"strings"
"testing"
"time"
)
type test struct {
s string
i int
t time.Duration
}
func TestDef(t *testing.T) {
err := error(nil)
result := Def(err, "test")
if result.Error() != "test" {
t.Fatal("Unexpected error")
}
err = New("")
result = Def(err, "test")
if result.Error() != "test" {
t.Fatal("Unexpected error")
}
err = New("ok")
result = Def(err, "test")
if result.Error() == "test" {
t.Fatal("Unexpected error")
}
}
func TestSet(t *testing.T) {
err := error(nil)
result := Set(err, "test")
if result != nil {
t.Fatal("Unexpected error")
}
err = New("")
result = Set(err, "test")
if result.Error() == "test" {
t.Fatal("Unexpected error")
}
err = New("test1")
result = Set(err, "test")
if result.Error() != "test" {
t.Fatal("Unexpected error")
}
}
func TestNew(t *testing.T) {
// string
s := "test"
err := New(s)
if err.Error() != s {
t.Fatal("Unexpected error", err.Error(), "instead", s)
}
// int
i := 10
err = New(i)
if err.Error() != strconv.Itoa(i) {
t.Fatal("Unexpected error", err.Error(), "instead", strconv.Itoa(i))
}
// interface
str := test{
s: "test",
i: 10,
t: time.Duration(1),
}
int := 100
err = New(int, str)
if !strings.Contains(err.Error(), strconv.Itoa(i)) {
t.Fatal("Unexpected error", err.Error(), "should contains", strconv.Itoa(i))
}
if !strings.Contains(err.Error(), s) {
t.Fatal("Unexpected error", err.Error(), "should contains", strconv.Itoa(i))
}
}
func TestEmpty(t *testing.T) {
err := error(nil)
result := Empty(err)
if !result {
t.Fatal("Unexpected error")
}
err = New("")
if !result {
t.Fatal("Unexpected error")
}
}
func TestEqual(t *testing.T) {
err1 := New("test")
err2 := New("test1")
if Equal(err1, err2) {
t.Fatal("Unexpected error", err1, err2)
}
err2 = New("test")
if !Equal(err1, err2) {
t.Fatal("Unexpected error", err1, err2)
}
}
func TestPrefix(t *testing.T) {
prefix := "pre"
error := New("test")
result := Prefix(error, prefix)
if !strings.Contains(result.Error(), "pre") {
t.Fatal("Unexpected error")
}
error = New("test")
error = Multi(error).Add(New("test1"))
result = Prefix(error, prefix)
list := Multi(result).Get()
if len(list) <= 1 {
t.Fatal("Unexpected len erors list", list)
}
for _, e := range list {
if !strings.Contains(e.Error(), "pre") {
t.Fatal("Unexpected error", e.Error())
}
}
}
func TestGrace_Error(t *testing.T) {
err := New("test")
if err.Error() != "test" {
t.Fatal("Unexpected error", err)
}
err = Multi(err).Add(New("test1"))
if err.Error() != "test1" {
t.Fatal("Unexpected error", err)
}
}