Debian Codegraph @

errors_test.go

raw symbols SCIP
1 package errors
2
3 import (
4 "encoding/json"
5 "fmt"
6 "strings"
7 "testing"
8
9 "github.com/google/go-cmp/cmp"
10 "github.com/google/go-cmp/cmp/cmpopts"
11 )
12
13 var err = fmt.Errorf("test error")
14
15 func TestFormat(t *testing.T) {
16 // %+s check for unwrapped error
17 e1 := Newf("test")
18 str := fmt.Sprintf("%+s", e1)
19 if str != e1.m {
20 t.Errorf("Error message invalid %s, expected %s", str, e1.m)
21 }
22
23 // %+s check for wrapped error
24 e2 := Annotatef(e1, "another")
25 str = fmt.Sprintf("%+s", e2)
26 val := e2.m + ": " + e2.c.Error()
27 if str != val {
28 t.Errorf("Error message invalid %s, expected %s", str, val)
29 }
30
31 // %v check for unwrapped error with trace
32 includeBacktrace.Store(true)
33 e3 := Newf("test1")
34 str = fmt.Sprintf("%+v", e3)
35 if !strings.Contains(str, e3.m) {
36 t.Errorf("Error message %s\n should contain %s", str, e3.m)
37 }
38 if !strings.Contains(str, fmt.Sprintf("%+v", e3.t.StackTrace())) {
39 t.Errorf("Error message %s\n should contain %+v", str, e3.t.StackTrace())
40 }
41 }
42
43 func TestMarshalJSON(t *testing.T) {
44 includeBacktrace.Store(true)
45 e := Newf("test")
46
47 b, err := e.t.StackTrace().MarshalJSON()
48 if err != nil {
49 t.Errorf("MarshalJSON failed with error: %+s", err)
50 }
51 stack := make([]json.RawMessage, 0)
52 err = json.Unmarshal(b, &stack)
53 if err != nil {
54 t.Errorf("JSON message could not be unmarshaled: %+s", err)
55 }
56 if len(stack) != len(e.t) {
57 t.Errorf("Count of stack frames different after marshaling, expected %d got %d", len(e.t), len(stack))
58 }
59 }
60
61 func TestAnnotatef(t *testing.T) {
62 const testStr = "Annotatef string"
63 te := Annotatef(err, testStr)
64 if !cmp.Equal(te.c, err, cmpopts.EquateErrors()) {
65 t.Errorf("Invalid parent error %s", cmp.Diff(te.c, err, cmpopts.EquateErrors()))
66 }
67 if te.m != testStr {
68 t.Errorf("Invalid error message %s, expected %s", te.m, testStr)
69 }
70 }
71
72 var homeVal = "$HOME"
73
74 func TestNewf(t *testing.T) {
75 const testStr = "Newf string"
76 te := Newf(testStr)
77 if te.c != nil {
78 t.Errorf("Invalid parent error %T:%s, expected nil", te.c, te.c)
79 }
80 if te.m != testStr {
81 t.Errorf("Invalid error message %s, expected %s", te.m, testStr)
82 }
83 }
84
85 func TestErrorf(t *testing.T) {
86 const testStr = "Errorf string"
87 err := Errorf(testStr)
88 if te, ok := err.(*Err); ok {
89 if te.c != nil {
90 t.Errorf("Invalid parent error %T:%s, expected nil", te.c, te.c)
91 }
92 if te.m != testStr {
93 t.Errorf("Invalid error message %s, expected %s", te.m, testStr)
94 }
95 } else {
96 t.Errorf("Invalid error type returned %T, expected type %T", err, &Err{})
97 }
98 }
99
100 /*
101 func TestErr_As(t *testing.T) {
102 e := Err{m: "test", l: 11, f: "random", t: []uintptr{0x6, 0x6, 0x6}, c: fmt.Errorf("ttt")}
103 if e.As(&err) {
104 t.Errorf("%T should not be assertable as %T", err, e)
105 }
106 type clone = Err
107 e1 := clone{}
108 if !e.As(&e1) {
109 t.Errorf("%T should be assertable as %T", e, e1)
110 }
111 if e1.m != e.m {
112 t.Errorf("%T message should equal %T's, received %s, expected %s", e1, e, e1.m, e.m)
113 }
114 if e1.l != e.l {
115 t.Errorf("%T line should equal %T's, received %d, expected %d", e1, e, e1.l, e.l)
116 }
117 if e1.f != e.f {
118 t.Errorf("%T file should equal %T's, received %s, expected %s", e1, e, e1.f, e.f)
119 }
120 if !bytes.Equal(e1.t, e.t) {
121 t.Errorf("%T trace should equal %T's, received %2x, expected %2x", e1, e, e1.t, e.t)
122 }
123 if e1.c != e.c {
124 t.Errorf("%T parent error should equal %T's, received %T[%s], expected %T[%s]", e1, e, e1.c, e1.c, e.c, e.c)
125 }
126 e2 := &e
127 if !e.As(&e2) {
128 t.Errorf("%T should be assertable as %T", e, e2)
129 }
130 if e2.m != e.m {
131 t.Errorf("%T message should equal %T's, received %s, expected %s", e2, e, e2.m, e.m)
132 }
133 if e2.l != e.l {
134 t.Errorf("%T line should equal %T's, received %d, expected %d", e2, e, e2.l, e.l)
135 }
136 if e2.f != e.f {
137 t.Errorf("%T file should equal %T's, received %s, expected %s", e2, e, e2.f, e.f)
138 }
139 if !bytes.Equal(e2.t, e.t) {
140 t.Errorf("%T trace should equal %T's, received %2x, expected %2x", e2, e, e2.t, e.t)
141 }
142 if e2.c != e.c {
143 t.Errorf("%T parent error should equal %T's, received %T[%s], expected %T[%s]", e2, e, e2.c, e2.c, e.c, e.c)
144 }
145 }
146
147 func TestErr_Error(t *testing.T) {
148 e := Err{m: "test"}
149 if e.Error() != e.m {
150 t.Errorf("Error() returned %s, expected %s", e.Error(), e.m)
151 }
152 }
153
154 func TestErr_Location(t *testing.T) {
155 e := Err{l: 11, f: "random"}
156 if f, l := e.Location(); l != e.l || f != e.f {
157 t.Errorf("Location() returned: %s:%d, expected: %s:%d", f, l, e.f, e.l)
158 }
159 }
160
161 func TestErr_StackTrace(t *testing.T) {
162 e := Err{t: []byte{0x6, 0x6, 0x6}}
163 if !bytes.Equal(e.StackTrace(), e.t) {
164 t.Errorf("StackTrace() returned: %2x, expected: %2x", e.StackTrace(), e.t)
165 }
166 }
167
168 func TestErr_Unwrap(t *testing.T) {
169 e := Err{c: fmt.Errorf("ttt")}
170 w := e.Unwrap()
171 if w != e.c {
172 t.Errorf("Unwrap() returned: %T[%s], expected: %T[%s]", w, w, e.c, e.c)
173 }
174 }
175 */
176
177 func TestErr_Error(t *testing.T) {
178 type fields struct {
179 m string
180 c error
181 t stack
182 }
183 tests := []struct {
184 quiet bool
185 name string
186 fields fields
187 want string
188 }{
189 {
190 name: "empty",
191 fields: fields{},
192 want: "",
193 },
194 {
195 name: "just text",
196 fields: fields{m: "test"},
197 want: "test",
198 },
199 {
200 name: "text with single wrapped error",
201 fields: fields{m: "test", c: fmt.Errorf("error")},
202 want: "test: error",
203 },
204 {
205 name: "text with two wrapped errors",
206 fields: fields{m: "test", c: fmt.Errorf("error: %w", Newf("some error"))},
207 want: "test: error: some error",
208 },
209 {
210 name: "text with two wrapped errors, but no unwrapping",
211 quiet: true,
212 fields: fields{m: "test", c: fmt.Errorf("error: %w", Newf("some error"))},
213 want: "test",
214 },
215 {
216 name: "text with two wrapped Err errors",
217 fields: fields{m: "test", c: &Err{m: "error", c: &Err{m: "another error"}}},
218 want: "test: error: another error",
219 },
220 {
221 name: "text with two wrapped Err errors, without unwrapping",
222 quiet: true,
223 fields: fields{m: "test", c: &Err{m: "error", c: &Err{m: "another error"}}},
224 want: "test",
225 },
226 }
227 for _, tt := range tests {
228 t.Run(tt.name, func(t *testing.T) {
229 includeBacktrace.Store(tt.quiet)
230 e := Err{
231 m: tt.fields.m,
232 c: tt.fields.c,
233 t: tt.fields.t,
234 }
235 if got := e.Error(); got != tt.want {
236 t.Errorf("Error() = %v, want %v", got, tt.want)
237 }
238 })
239 }
240 }
241
242 func areErrors(a, b any) bool {
243 _, ok1 := a.(error)
244 _, ok2 := b.(error)
245 return ok1 && ok2
246 }
247
248 func compareErrors(x, y interface{}) bool {
249 xe := x.(error)
250 ye := y.(error)
251 if Is(xe, ye) || Is(ye, xe) {
252 return true
253 }
254 return xe.Error() == ye.Error()
255 }
256
257 var EquateWeakErrors = cmp.FilterValues(areErrors, cmp.Comparer(compareErrors))