Debian Codegraph @

internal/ctxerr/error_test.go

raw
1 // Copyright (c) 2018-2021, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package ctxerr_test
8
9 import (
10 "reflect"
11 "testing"
12
13 "github.com/maxatome/go-testdeep/internal/color"
14 "github.com/maxatome/go-testdeep/internal/ctxerr"
15 "github.com/maxatome/go-testdeep/internal/location"
16 "github.com/maxatome/go-testdeep/internal/test"
17 "github.com/maxatome/go-testdeep/internal/types"
18 )
19
20 func TestError(t *testing.T) {
21 defer color.SaveState()()
22
23 checkWithoutColors := func(err *ctxerr.Error) {
24 t.Helper()
25 test.EqualStr(t, err.ErrorWithoutColors(), err.Error())
26 defer color.SaveState(true)()
27 test.IsTrue(t, err.ErrorWithoutColors() != err.Error())
28 }
29
30 err := ctxerr.Error{
31 Context: ctxerr.Context{
32 Path: ctxerr.NewPath("DATA").AddArrayIndex(12).AddField("Field"),
33 },
34 Message: "error message",
35 Got: 1,
36 Expected: 2,
37 }
38 test.EqualStr(t, err.Error(),
39 `DATA[12].Field: error message
40 got: 1
41 expected: 2`)
42 checkWithoutColors(&err)
43 test.EqualStr(t, err.GotString(), "1")
44 test.EqualStr(t, err.ExpectedString(), "2")
45 test.EqualStr(t, err.SummaryString(), "")
46
47 err.Message = "Value of %% differ"
48 test.EqualStr(t, err.Error(),
49 `Value of DATA[12].Field differ
50 got: 1
51 expected: 2`)
52 checkWithoutColors(&err)
53
54 err.Message = "Path at end: %%"
55 test.EqualStr(t, err.Error(),
56 `Path at end: DATA[12].Field
57 got: 1
58 expected: 2`)
59 checkWithoutColors(&err)
60
61 err.Message = "%% <- the path!"
62 test.EqualStr(t, err.Error(),
63 `DATA[12].Field <- the path!
64 got: 1
65 expected: 2`)
66 checkWithoutColors(&err)
67
68 err = ctxerr.Error{
69 Context: ctxerr.Context{
70 Path: ctxerr.NewPath("DATA").AddArrayIndex(12).AddField("Field"),
71 },
72 Message: "error message",
73 Got: 1,
74 Expected: 2,
75 Location: location.Location{
76 File: "file.go",
77 Func: "Operator",
78 Line: 23,
79 },
80 }
81 test.EqualStr(t, err.Error(),
82 `DATA[12].Field: error message
83 got: 1
84 expected: 2
85 [under operator Operator at file.go:23]`)
86 checkWithoutColors(&err)
87
88 err = ctxerr.Error{
89 Context: ctxerr.Context{
90 Path: ctxerr.NewPath("DATA").AddArrayIndex(12).AddField("Field"),
91 },
92 Message: "error message",
93 Summary: ctxerr.NewSummary("666"),
94 Location: location.Location{
95 File: "file.go",
96 Func: "Operator",
97 Line: 23,
98 },
99 Origin: &ctxerr.Error{
100 Context: ctxerr.Context{
101 Path: ctxerr.NewPath("DATA").AddArrayIndex(12).AddField("Field").AddCustomLevel("<All#1/2>"),
102 },
103 Message: "origin error message",
104 Summary: ctxerr.NewSummary("42"),
105 Location: location.Location{
106 File: "file2.go",
107 Func: "SubOperator",
108 Line: 236,
109 },
110 },
111 }
112 test.EqualStr(t, err.Error(),
113 `DATA[12].Field: error message
114 666
115 Originates from following error:
116 DATA[12].Field<All#1/2>: origin error message
117 42
118 [under operator SubOperator at file2.go:236]
119 [under operator Operator at file.go:23]`)
120 checkWithoutColors(&err)
121 test.EqualStr(t, err.GotString(), "")
122 test.EqualStr(t, err.ExpectedString(), "")
123 test.EqualStr(t, err.SummaryString(), "666")
124
125 err = ctxerr.Error{
126 Context: ctxerr.Context{
127 Path: ctxerr.NewPath("DATA").AddArrayIndex(12).AddField("Field"),
128 },
129 Message: "error message",
130 Summary: ctxerr.NewSummary("666"),
131 Location: location.Location{
132 File: "file.go",
133 Func: "Operator",
134 Line: 23,
135 },
136 Origin: &ctxerr.Error{
137 Context: ctxerr.Context{
138 Path: ctxerr.NewPath("DATA").AddArrayIndex(12).AddField("Field").AddCustomLevel("<All#1/2>"),
139 },
140 Message: "origin error message",
141 Summary: ctxerr.NewSummary("42"),
142 Location: location.Location{
143 File: "file2.go",
144 Func: "SubOperator",
145 Line: 236,
146 },
147 },
148 // Next error at same location
149 Next: &ctxerr.Error{
150 Context: ctxerr.Context{
151 Path: ctxerr.NewPath("DATA").AddArrayIndex(13).AddField("Field"),
152 },
153 Message: "error message",
154 Summary: ctxerr.NewSummary("888"),
155 Location: location.Location{
156 File: "file.go",
157 Func: "Operator",
158 Line: 23,
159 },
160 },
161 }
162 test.EqualStr(t, err.Error(),
163 `DATA[12].Field: error message
164 666
165 Originates from following error:
166 DATA[12].Field<All#1/2>: origin error message
167 42
168 [under operator SubOperator at file2.go:236]
169 DATA[13].Field: error message
170 888
171 [under operator Operator at file.go:23]`)
172 checkWithoutColors(&err)
173
174 err = ctxerr.Error{
175 Context: ctxerr.Context{Path: ctxerr.NewPath("DATA").AddArrayIndex(12).AddField("Field")},
176 Message: "error message",
177 Summary: ctxerr.NewSummary("666"),
178 Location: location.Location{
179 File: "file.go",
180 Func: "Operator",
181 Line: 23,
182 },
183 Origin: &ctxerr.Error{
184 Context: ctxerr.Context{
185 Path: ctxerr.NewPath("DATA").AddArrayIndex(12).AddField("Field").AddCustomLevel("<All#1/2>"),
186 },
187 Message: "origin error message",
188 Summary: ctxerr.NewSummary("42"),
189 Location: location.Location{
190 File: "file2.go",
191 Func: "SubOperator",
192 Line: 236,
193 },
194 },
195 // Next error at different location
196 Next: &ctxerr.Error{
197 Context: ctxerr.Context{
198 Path: ctxerr.NewPath("DATA").AddArrayIndex(13).AddField("Field"),
199 },
200 Message: "error message",
201 Summary: ctxerr.NewSummary("888"),
202 Location: location.Location{
203 File: "file.go",
204 Func: "Operator",
205 Line: 24,
206 },
207 },
208 }
209 test.EqualStr(t, err.Error(),
210 `DATA[12].Field: error message
211 666
212 Originates from following error:
213 DATA[12].Field<All#1/2>: origin error message
214 42
215 [under operator SubOperator at file2.go:236]
216 [under operator Operator at file.go:23]
217 DATA[13].Field: error message
218 888
219 [under operator Operator at file.go:24]`)
220 checkWithoutColors(&err)
221
222 //
223 // ErrTooManyErrors
224 test.EqualStr(t, ctxerr.ErrTooManyErrors.Error(),
225 `Too many errors (use TESTDEEP_MAX_ERRORS=-1 to see all)`)
226 }
227
228 func TestTypeMismatch(t *testing.T) {
229 rErr := ctxerr.TypeMismatch(reflect.TypeOf(0), reflect.TypeOf(""))
230 test.EqualStr(t, rErr.Message, "type mismatch")
231 test.EqualStr(t, string(rErr.Got.(types.RawString)), `int`)
232 test.EqualStr(t, string(rErr.Expected.(types.RawString)), `string`)
233
234 // It is the caller responsibility to check that both types
235 // differ. To ease testing we can pass twice the same type, it is
236 // the same as passing 2 different types but with the same short
237 // name (a/too.Type vs b/foo.Type), util.TypeFullName() is called
238 // for both types.
239 rErr = ctxerr.TypeMismatch(reflect.TypeOf(0), reflect.TypeOf(0))
240 test.EqualStr(t, rErr.Message, "type mismatch")
241 test.EqualStr(t, string(rErr.Got.(types.RawString)), `int`)
242 test.EqualStr(t, string(rErr.Expected.(types.RawString)), `int`)
243 }
244
245 func TestBooleanError(t *testing.T) {
246 if ctxerr.BooleanError.Error() != "" {
247 t.Errorf("BooleanError should stringify to empty string, not `%s'",
248 ctxerr.BooleanError.Error())
249 }
250 }