Debian Codegraph @

internal/ctxerr/context_test.go

raw
1 // Copyright (c) 2018, 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 "testing"
11
12 "github.com/maxatome/go-testdeep/internal/ctxerr"
13 "github.com/maxatome/go-testdeep/internal/location"
14 "github.com/maxatome/go-testdeep/internal/test"
15 )
16
17 func TestContext(t *testing.T) {
18 for _, maxErrors := range []int{0, 1} {
19 ctx := ctxerr.Context{
20 MaxErrors: maxErrors,
21 }
22
23 ctx.InitErrors()
24 if ctx.Errors != nil {
25 t.Errorf("Errors is non-nil for MaxErrors %d", maxErrors)
26 }
27 }
28
29 for _, maxErrors := range []int{-1, 2} {
30 ctx := ctxerr.Context{
31 MaxErrors: maxErrors,
32 }
33
34 ctx.InitErrors()
35 if ctx.Errors == nil {
36 t.Errorf("Errors is nil for MaxErrors %d", maxErrors)
37 continue
38 }
39
40 *ctx.Errors = append(*ctx.Errors, &ctxerr.Error{})
41
42 newc := ctx.ResetErrors()
43 if newc.Errors == nil {
44 t.Errorf("after ResetErrors, new Errors is nil for MaxErrors %d",
45 maxErrors)
46 continue
47 }
48 if len(*newc.Errors) > 0 {
49 t.Errorf("after ResetErrors, new Errors is not empty for MaxErrors %d",
50 maxErrors)
51 }
52 if ctx.Errors == nil {
53 t.Errorf("after ResetErrors, old Errors is nil for MaxErrors %d",
54 maxErrors)
55 continue
56 }
57 }
58 }
59
60 type MyGetLocationer struct{}
61
62 func (g MyGetLocationer) GetLocation() location.Location {
63 return location.Location{
64 File: "context_test.go",
65 Func: "MyFunc",
66 Line: 42,
67 }
68 }
69
70 func TestContextMergeErrors(t *testing.T) {
71 // No errors to merge
72 ctx := ctxerr.Context{}
73 if ctx.MergeErrors() != nil {
74 t.Error("ctx.MergeErrors() returned a *Error")
75 }
76
77 errors := []*ctxerr.Error{}
78 ctx = ctxerr.Context{
79 Errors: &errors,
80 }
81 if ctx.MergeErrors() != nil {
82 t.Error("ctx.MergeErrors() returned a *Error")
83 }
84
85 // Only 1 error to merge => itself
86 firstErr := &ctxerr.Error{}
87 errors = []*ctxerr.Error{firstErr}
88 ctx = ctxerr.Context{
89 Errors: &errors,
90 }
91 if ctx.MergeErrors() != firstErr {
92 t.Error("ctx.MergeErrors() did not return the only one error")
93 }
94
95 // Several errors to merge
96 secondErr, thirdErr := &ctxerr.Error{}, &ctxerr.Error{}
97 errors = []*ctxerr.Error{firstErr, secondErr, thirdErr}
98 ctx = ctxerr.Context{
99 Errors: &errors,
100 }
101 if ctx.MergeErrors() != firstErr {
102 t.Error("ctx.MergeErrors() did not return the first error")
103 return
104 }
105 if firstErr.Next != secondErr {
106 t.Error("ctx.MergeErrors() second error is not linked to first one")
107 return
108 }
109 if secondErr.Next != thirdErr {
110 t.Error("ctx.MergeErrors() third error is not linked to second one")
111 return
112 }
113 if thirdErr.Next != nil {
114 t.Error("ctx.MergeErrors() third error has a non-nil Next!")
115 }
116 }
117
118 func TestContextCollectError(t *testing.T) {
119 //
120 // Only one error kept
121 ctx := ctxerr.Context{}
122
123 if ctx.CollectError(nil) != nil {
124 t.Error("ctx.CollectError(nil) returned non-nil *Error")
125 }
126
127 err := ctxerr.Context{BooleanError: true}.CollectError(&ctxerr.Error{})
128 if err != ctxerr.BooleanError {
129 t.Error("boolean-ctx.CollectError(X) did not return BooleanError")
130 }
131
132 // !err.Location.IsInitialized() + ctx.CurOperator == nil
133 origErr := &ctxerr.Error{}
134 err = ctx.CollectError(origErr)
135 if err != origErr {
136 t.Error("ctx.CollectError(err) != err")
137 }
138
139 // !err.Location.IsInitialized() + ctx.CurOperator != nil
140 ctx.CurOperator = MyGetLocationer{}
141 origErr = &ctxerr.Error{}
142 err = ctx.CollectError(origErr)
143 if err != origErr {
144 t.Error("ctx.CollectError(err) != err")
145 }
146 test.EqualInt(t, err.Location.Line, 42, // see MyGetLocationer.GetLocation()
147 "ctx.CollectError(err) initialized err.Location")
148
149 // err.Location.IsInitialized()
150 origErr = &ctxerr.Error{
151 Location: location.Location{
152 File: "zz.go",
153 Func: "ErrFunc",
154 Line: 24,
155 },
156 }
157 err = ctx.CollectError(origErr)
158 if err != origErr {
159 t.Error("ctx.CollectError(err) != err")
160 }
161 test.EqualInt(t, err.Location.Line, 24,
162 "ctx.CollectError(err) did not touch err.Location")
163
164 //
165 // 2 errors kept max
166 errors := []*ctxerr.Error{}
167 ctx = ctxerr.Context{
168 Errors: &errors,
169 MaxErrors: 2,
170 }
171 origErr = &ctxerr.Error{}
172 if ctx.CollectError(origErr) != nil { // 1st error is accumulated
173 t.Error("ctx.CollectError(err) != nil")
174 return
175 }
176
177 secondErr := &ctxerr.Error{}
178 if ctx.CollectError(secondErr) != origErr {
179 t.Error("ctx.CollectError(err) != origErr")
180 return
181 }
182
183 if origErr.Next != secondErr {
184 t.Error("origErr.Next != secondErr")
185 return
186 }
187
188 if secondErr.Next != ctxerr.ErrTooManyErrors {
189 t.Error("secondErr.Next != ErrTooManyErrors")
190 return
191 }
192
193 //
194 // All errors kept
195 errors = nil
196 ctx = ctxerr.Context{
197 Errors: &errors,
198 MaxErrors: -1,
199 }
200 for i := 0; i < 100; i++ {
201 if ctx.CollectError(&ctxerr.Error{}) != nil { // 1st error is accumulated
202 t.Errorf("#%d: ctx.CollectError(err) != nil", i)
203 return
204 }
205 }
206 if len(errors) != 100 {
207 t.Errorf("Only %d errors accumulated instead of 100", len(errors))
208 }
209
210 //
211 // Do not collect 2 times the same error
212 errors = nil
213 ctx = ctxerr.Context{
214 Errors: &errors,
215 MaxErrors: -1,
216 }
217 ctx.CollectError(&ctxerr.Error{}) //nolint: errcheck
218 x := &ctxerr.Error{}
219 ctx.CollectError(x) //nolint: errcheck
220 ctx.CollectError(&ctxerr.Error{}) //nolint: errcheck
221 ctx.CollectError(x) //nolint: errcheck
222 ctx.CollectError(x) //nolint: errcheck
223 ctx.CollectError(&ctxerr.Error{}) //nolint: errcheck
224 if len(errors) != 4 {
225 t.Errorf("%d errors accumulated instead of 4", len(errors))
226 }
227 }
228
229 func TestCannotCompareError(t *testing.T) {
230 ctx := ctxerr.Context{BooleanError: true}
231
232 err := ctx.CannotCompareError()
233 if err != ctxerr.BooleanError {
234 t.Error("CannotCompareError does not return ctxerr.BooleanError")
235 }
236
237 ctx = ctxerr.Context{}
238 err = ctx.CannotCompareError()
239 test.EqualStr(t, err.Message, "cannot compare")
240 }
241
242 func TestContextPath(t *testing.T) {
243 ctx := ctxerr.Context{Path: ctxerr.NewPath("DATA")}
244 ctx = ctx.AddField("field")
245 test.EqualStr(t, ctx.Path.String(), "DATA.field")
246 test.EqualInt(t, ctx.Depth, 1)
247
248 ctx = ctx.AddPtr(2)
249 test.EqualStr(t, ctx.Path.String(), "**DATA.field")
250 test.EqualInt(t, ctx.Depth, 2)
251
252 ctx = ctx.AddField("another")
253 test.EqualStr(t, ctx.Path.String(), "(*DATA.field).another")
254 test.EqualInt(t, ctx.Depth, 3)
255
256 ctx = ctx.AddCustomLevel("→cust")
257 test.EqualStr(t, ctx.Path.String(), "(*DATA.field).another→cust")
258 test.EqualInt(t, ctx.Depth, 4)
259
260 ctx = ctxerr.Context{Path: ctxerr.NewPath("DATA")}
261 ctx = ctx.AddArrayIndex(18)
262 test.EqualStr(t, ctx.Path.String(), "DATA[18]")
263 test.EqualInt(t, ctx.Depth, 1)
264
265 ctx = ctxerr.Context{Path: ctxerr.NewPath("DATA")}
266 ctx = ctx.AddMapKey("foo")
267 test.EqualStr(t, ctx.Path.String(), `DATA["foo"]`) // special case of util.ToString()
268 test.EqualInt(t, ctx.Depth, 1)
269
270 ctx = ctxerr.Context{Path: ctxerr.NewPath("DATA")}
271 ctx = ctx.AddMapKey(12)
272 test.EqualStr(t, ctx.Path.String(), `DATA[12]`)
273 test.EqualInt(t, ctx.Depth, 1)
274
275 ctx = ctxerr.Context{Path: ctxerr.NewPath("DATA")}
276 ctx = ctx.AddFunctionCall("foobar")
277 test.EqualStr(t, ctx.Path.String(), "foobar(DATA)")
278 test.EqualInt(t, ctx.Depth, 1)
279
280 ctx = ctx.ResetPath("NEW")
281 test.EqualStr(t, ctx.Path.String(), "NEW")
282 test.EqualInt(t, ctx.Depth, 2)
283 }