-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors_test.go
More file actions
156 lines (144 loc) · 3.66 KB
/
errors_test.go
File metadata and controls
156 lines (144 loc) · 3.66 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package errors
import (
"encoding/json"
"errors"
"fmt"
"testing"
)
func TestNew(t *testing.T) {
code := "new test"
// way 1 to make error
// errors.New()
e := New(code)
if e.Code() != code {
t.Fatalf("want:%s,but:%s", code, e.Code())
return
}
if len(e.Error()) == 0 {
t.Fatal(e)
}
// way 2 to make error
// errors.As()
eAs := As(e, "this is value copy for As, not same as before")
if eAs.Code() != code {
t.Fatalf("want:%s,but:%s", code, eAs.Code())
return
}
if eAs == e {
t.Fatalf("%s, not same instance:%s", eAs, e)
return
}
// way 3 to make error
// errors.ParseError(error)
eParseErr := ParseError(e)
if eParseErr.Code() != code {
t.Fatalf("want:%s,but:%s", code, eParseErr.Code())
return
}
if eParseErr != e {
t.Fatalf("%s, not same instance:%s", eParseErr, e)
return
}
// way 4 to make error
// errors.Parse(string)
eParse := Parse(e.Error())
if eParse.Code() != code {
t.Fatalf("want:%s,but:%s", code, eParse.Code())
return
}
}
var equalTests = []struct {
err1 Error
err2 error
out bool
}{
{New("New"), New("New"), true},
{New("New"), ParseError(New("New")), true},
{New("New"), ParseError(errors.New("New")), true},
{New("New"), As(New("New")), true},
{New("New"), As(errors.New("New")), true},
{New("New"), As(New("New"), "reason"), true},
{New("New"), As(errors.New("New"), "reason"), true},
{ParseError(New("ParseErr")), ParseError(New("ParseErr")), true},
{ParseError(New("ParseErr")), ParseError(errors.New("ParseErr")), true},
{ParseError(New("ParseErr")), As(New("ParseErr")), true},
{ParseError(New("ParseErr")), As(errors.New("ParseErr")), true},
{ParseError(New("ParseErr")), As(New("ParseErr"), "reason"), true},
{ParseError(New("ParseErr")), As(errors.New("ParseErr"), "reason"), true},
}
func TestEqual(t *testing.T) {
for index, test := range equalTests {
if test.err1.Equal(test.err2) != test.out {
t.Fatalf("usercase %d,want:%t,but:%t", index, !test.out, test.out)
return
}
if Equal(test.err1, test.err2) != test.out {
t.Fatalf("usercase %d,want:%t,but:%t", index, !test.out, test.out)
return
}
}
}
func TestAs(t *testing.T) {
err1 := New("test")
err2 := New("test")
outErr1 := As(err1, "test", "test")
outErr1Wrap := Wrap(err1, "wrap called")
outErr2 := err2.As("test", "test")
outErr3 := As(err1, 123, 456)
outErr4 := err2.As(123, 456)
if len(outErr1.Error()) == 0 {
t.Fatal(outErr1)
}
if len(outErr2.Error()) == 0 {
t.Fatal(outErr2)
}
if len(outErr3.Error()) == 0 {
t.Fatal(outErr3)
}
if len(outErr4.Error()) == 0 {
t.Fatal(outErr4)
}
fmt.Println("As1:", outErr1)
fmt.Println("Wrap1:", outErr1Wrap)
fmt.Println("As2:", outErr2)
fmt.Println("As3:", outErr3)
fmt.Println("As4:", outErr4.As("call two as"))
fmt.Println("As5:", outErr4)
}
func TestError(t *testing.T) {
err1 := New("test")
err2 := New("test")
outErr1 := As(err1, "test", "test")
outErr2 := err2.As("test", "test")
outErr3 := As(err1, 123, 456)
outErr4 := err2.As(123, 456)
if len(outErr1.Error()) == 0 {
t.Fatal(outErr1)
}
if len(outErr2.Error()) == 0 {
t.Fatal(outErr2)
}
if len(outErr3.Error()) == 0 {
t.Fatal(outErr2)
}
if len(outErr4.Error()) == 0 {
t.Fatal(outErr4)
}
// TODO: concurrency testing
if err1 == outErr1 {
t.Fatal("need return another error")
}
if err2 == outErr2 {
t.Fatal("need return another error")
}
jsonIndent, err := json.MarshalIndent(err1.(*errImpl), "", " ")
if err != nil {
t.Fatal(err)
}
fmt.Println("Err:", outErr4.Error())
fmt.Println("Err:", err1.(*errImpl))
fmt.Println("Err:", err1.As(err2))
fmt.Println("Err:", As(New("two caller without args")))
fmt.Println("Json Indent:", string(jsonIndent))
fmt.Printf("Stack output:%+v\n", err1.Stack())
}