-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
executable file
·174 lines (130 loc) · 3.02 KB
/
errors_test.go
File metadata and controls
executable file
·174 lines (130 loc) · 3.02 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package goerrors
import (
"errors"
"fmt"
"math/rand"
"reflect"
"testing"
"time"
"github.com/google/uuid"
)
type MyError struct {
GoError
}
var (
_errTest MyError
_errType = reflect.TypeOf(_errTest)
)
func samePtr(v1, v2 interface{}) bool {
return reflect.ValueOf(v1).Pointer() == reflect.ValueOf(v2).Pointer()
}
func TestSetType(t *testing.T) {
gerr := &MyError{}
gerr.setType(gerr)
if gerr.errType != _errType {
t.Fail()
}
}
func TestInitError(t *testing.T) {
src := fmt.Errorf("")
data := new(int)
gerr := MyError{}
_ = gerr.Init(gerr, "--message--", data, src, 0)
if gerr.errType != _errType {
t.Error("Bad type")
}
if gerr.message != "--message--" {
t.Error("Bad message")
}
if !samePtr(gerr.data, data) {
t.Error("Bad data")
}
if !samePtr(gerr.source, src) {
t.Error("Bad data")
}
}
func TestShowErrorNoDebug(t *testing.T) {
SetDebug(false)
moment := time.Now()
rand.Seed(moment.UnixNano())
r := rand.Int63()
id := uuid.New()
err := MakeError("%s:%d", id, r).(*tStandardError)
if err.message != fmt.Sprintf("%s:%d", id, r) {
t.Error("Failed on MakeError:", err.message, "!=", fmt.Sprintf("%s:%d", id, r))
}
if len(err.trace) != 0 {
t.Error("Failed on empty trace: ", err.trace)
}
if err.infos.Len() != 0 {
t.Error("Failed on no-infos")
}
}
func TestShowErrorDebug(t *testing.T) {
SetDebug(true)
err := MakeError("").(*tStandardError)
if len(err.trace) == 0 {
t.Error("Failed on stack trace")
}
}
func TestErrorMethods(t *testing.T) {
SetDebug(true)
err := MakeError("")
if len(err.Error()) == 0 {
t.Error("Failed on infos")
}
}
func TestErrorString(t *testing.T) {
gerr := &MyError{}
_ = gerr.Init(gerr, "--message--", 1234, errors.New("error"), 0)
_ = gerr.Error()
gerr.message = ""
_ = gerr.Error()
}
func TestGetSource(t *testing.T) {
_ = GetSource(nil)
_ = GetSource(DecorateError(fmt.Errorf("error")))
}
func TestErrorTry(t *testing.T) {
gerr := &MyError{}
_ = gerr.Init(gerr, "--message--", nil, nil, 0)
_ = gerr.Try(func(err IError) error {
return err
}, nil, func(err IError) error { return nil })
_ = gerr.Try(func(err IError) error {
err.Raise()
return nil
}, func(err IError) error {
return err
}, nil)
}
func TestErrorCatchWithPrimitiveError(t *testing.T) {
defer DiscardPanic()
gerr := &MyError{}
_ = gerr.Init(gerr, "--message--", nil, nil, 0)
_ = gerr.Try(func(err IError) error {
panic("error")
}, nil, nil)
}
func TestErrorCatchWithBasicError(t *testing.T) {
defer DiscardPanic()
gerr := &MyError{}
_ = gerr.Init(gerr, "--message--", nil, nil, 0)
_ = gerr.Try(func(err IError) error {
panic(errors.New("error"))
}, nil, nil)
}
func TestErrorCatchWithOtherError(t *testing.T) {
defer DiscardPanic()
gerr := &MyError{}
_ = gerr.Init(gerr, "--message--", nil, nil, 0)
_ = gerr.Try(func(err IError) error {
otherError := &struct{ GoError }{}
otherError.Init(otherError, "error", nil, nil, 0).Raise()
return nil
}, nil, nil)
}
func TestErrNakedRaise(t *testing.T) {
defer DiscardPanic()
(&MyError{}).raise(0)
}