-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors_test.go
97 lines (83 loc) · 2.23 KB
/
errors_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
package errors
import (
stderrors "errors"
"testing"
)
func TestNew(t *testing.T) {
testText := "test text"
err := New(testText)
if err == nil {
t.Error("Error shouldn't be nil")
}
if err != nil && err.Error() != "test text" {
t.Errorf("Error should be '%s', instead of %s", testText, err.Error())
}
}
func TestUnwrap(t *testing.T) {
originalErr := New("test")
wrappedErr := stderrors.New("text")
err := Wrap(originalErr, wrappedErr)
unwrappedErr := stderrors.Unwrap(err)
if unwrappedErr == nil || unwrappedErr.Error() != "test" {
t.Error("Unwrapped error should be 'test'")
}
}
func TestIs(t *testing.T) {
testErr1 := New("test err 1")
testErr2 := New("test err 2")
testErr3 := New("test err 3")
testErr4 := New("test err 4")
testFail := New("test fail")
err := Wrap(New("origin"), testErr1)
err = Wrap(err, testErr2)
err = Wrap(err, testErr3)
err = Wrap(err, testErr4)
testtrue(t, stderrors.Is(err, testErr1), "testErr1")
testtrue(t, stderrors.Is(err, testErr2), "testErr2")
testtrue(t, stderrors.Is(err, testErr3), "testErr3")
testtrue(t, stderrors.Is(err, testErr4), "testErr4")
testtrue(t, stderrors.Is(err, err), "own")
testfalse(t, stderrors.Is(err, testFail), "testFail")
}
func TestWrappedError(t *testing.T) {
testErr1 := New("test err 1")
testErr2 := New("test err 2")
testErr3 := New("test err 3")
testErr4 := New("test err 4")
err := Wrap(New("origin"), testErr1)
err = Wrap(err, testErr2)
err = Wrap(err, testErr3)
err = Wrap(err, testErr4)
expected := "test err 4: test err 3: test err 2: test err 1: origin"
if err == nil || err.Error() != expected {
t.Errorf("Error should be %s, instead of %s", expected, err.Error())
}
}
//func BenchmarkNew(b *testing.B) {
// b.ReportAllocs()
// for i := 0; i < b.N; i++ {
// err := New("test err 1")
// keep(err)
// }
//}
func BenchmarkWrap(b *testing.B) {
testErr1 := New("test err 1")
err := New("origin")
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
ne := Wrap(err, testErr1)
keep(ne)
}
}
func keep(err error) {}
func testtrue(t *testing.T, value bool, msg ...string) {
if !value {
t.Errorf("Should be true, %v", msg)
}
}
func testfalse(t *testing.T, value bool, msg ...string) {
if value {
t.Errorf("Should be false, %v", msg)
}
}