| 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 |
| 8 |
|
| 9 |
import ( |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/maxatome/go-testdeep/internal/anchors" |
| 13 |
"github.com/maxatome/go-testdeep/internal/hooks" |
| 14 |
"github.com/maxatome/go-testdeep/internal/location" |
| 15 |
"github.com/maxatome/go-testdeep/internal/visited" |
| 16 |
) |
| 17 |
|
| 18 |
// Context is used internally to keep track of the Cmp in-depth |
| 19 |
// traversal. |
| 20 |
type Context struct { |
| 21 |
Path Path |
| 22 |
Visited visited.Visited |
| 23 |
CurOperator location.GetLocationer |
| 24 |
Depth int |
| 25 |
// 0 ≤ MaxErrors ≤ 1 stops when first error encoutered (without the |
| 26 |
// "Too many errors" error); |
| 27 |
// MaxErrors > 1 stops when MaxErrors'th error encoutered (with a |
| 28 |
// last "Too many errors" error); |
| 29 |
// < 0 do not stop until comparison ends. |
| 30 |
MaxErrors int |
| 31 |
Errors *[]*Error |
| 32 |
Anchors *anchors.Info |
| 33 |
Hooks *hooks.Info |
| 34 |
OriginalTB testing.TB // only used by Code operator |
| 35 |
// If true, the contents of the returned *Error will not be |
| 36 |
// checked. Can be used to avoid filling Error{} with expensive |
| 37 |
// computations. |
| 38 |
BooleanError bool |
| 39 |
// See ContextConfig.FailureIsFatal for details. |
| 40 |
FailureIsFatal bool |
| 41 |
// See ContextConfig.UseEqual for details. |
| 42 |
UseEqual bool |
| 43 |
// See ContextConfig.BeLax for details. |
| 44 |
BeLax bool |
| 45 |
// See ContextConfig.IgnoreUnexported for details. |
| 46 |
IgnoreUnexported bool |
| 47 |
// See ContextConfig.TestDeepInGotOK for details. |
| 48 |
TestDeepInGotOK bool |
| 49 |
} |
| 50 |
|
| 51 |
// InitErrors initializes [Context] *Errors slice, if MaxErrors < 0 or |
| 52 |
// MaxErrors > 1. |
| 53 |
func (c *Context) InitErrors() { |
| 54 |
if c.MaxErrors != 0 && c.MaxErrors != 1 { |
| 55 |
var errors []*Error |
| 56 |
c.Errors = &errors |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// ResetErrors returns a new [Context] without any Error set. |
| 61 |
func (c Context) ResetErrors() (newc Context) { |
| 62 |
newc = c |
| 63 |
newc.InitErrors() |
| 64 |
return |
| 65 |
} |
| 66 |
|
| 67 |
// CollectError collects an error in the context. It returns an error |
| 68 |
// if the collector is full, nil otherwise. |
| 69 |
// |
| 70 |
// In boolean context, it ignores the passed error and returns the |
| 71 |
// [BooleanError]. |
| 72 |
func (c Context) CollectError(err *Error) *Error { |
| 73 |
if err == nil { |
| 74 |
return nil |
| 75 |
} |
| 76 |
|
| 77 |
// The boolean error must not be altered! user errors are never replaced |
| 78 |
if c.BooleanError && !err.User { |
| 79 |
return BooleanError |
| 80 |
} |
| 81 |
|
| 82 |
// Error context not initialized yet |
| 83 |
if err.Context.Depth == 0 { |
| 84 |
err.Context = c |
| 85 |
} |
| 86 |
|
| 87 |
if !err.Location.IsInitialized() && c.CurOperator != nil { |
| 88 |
err.Location = c.CurOperator.GetLocation() |
| 89 |
} |
| 90 |
|
| 91 |
// Stop when first error encoutered |
| 92 |
if c.Errors == nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
|
| 96 |
// Skip it if already encountered as Re in JSON(`[$1,$1]`, Re(123)) |
| 97 |
for _, cur := range *c.Errors { |
| 98 |
if cur == err { |
| 99 |
return nil |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// Else, accumulate... |
| 104 |
*c.Errors = append(*c.Errors, err) |
| 105 |
if c.MaxErrors >= 0 && len(*c.Errors) >= c.MaxErrors { |
| 106 |
*c.Errors = append(*c.Errors, ErrTooManyErrors) |
| 107 |
return c.MergeErrors() |
| 108 |
} |
| 109 |
return nil |
| 110 |
} |
| 111 |
|
| 112 |
// MergeErrors merges all collected errors in the first one and |
| 113 |
// returns it. It returns nil if no errors have been collected. |
| 114 |
func (c Context) MergeErrors() *Error { |
| 115 |
if c.Errors == nil || len(*c.Errors) == 0 { |
| 116 |
return nil |
| 117 |
} |
| 118 |
|
| 119 |
if len(*c.Errors) > 1 { |
| 120 |
for idx, last := 0, len(*c.Errors)-2; idx <= last; idx++ { |
| 121 |
(*c.Errors)[idx].Next = (*c.Errors)[idx+1] |
| 122 |
} |
| 123 |
} |
| 124 |
return (*c.Errors)[0] |
| 125 |
} |
| 126 |
|
| 127 |
// CannotCompareError returns a generic error used when the access of |
| 128 |
// unexported fields cannot be overridden. |
| 129 |
func (c Context) CannotCompareError() *Error { |
| 130 |
if c.BooleanError { |
| 131 |
return BooleanError |
| 132 |
} |
| 133 |
return &Error{ |
| 134 |
Message: "cannot compare", |
| 135 |
Summary: NewSummary("unexported field that cannot be overridden"), |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
// AddCustomLevel creates a new [Context] from current one plus pathAdd. |
| 140 |
func (c Context) AddCustomLevel(pathAdd string) (newc Context) { |
| 141 |
newc = c |
| 142 |
newc.Path = newc.Path.AddCustomLevel(pathAdd) |
| 143 |
newc.Depth++ |
| 144 |
return |
| 145 |
} |
| 146 |
|
| 147 |
// AddField creates a new [Context] from current one plus "." + field. |
| 148 |
func (c Context) AddField(field string) (newc Context) { |
| 149 |
newc = c |
| 150 |
newc.Path = newc.Path.AddField(field) |
| 151 |
newc.Depth++ |
| 152 |
return |
| 153 |
} |
| 154 |
|
| 155 |
// AddArrayIndex creates a new [Context] from current one plus an array |
| 156 |
// dereference for index-th item. |
| 157 |
func (c Context) AddArrayIndex(index int) (newc Context) { |
| 158 |
newc = c |
| 159 |
newc.Path = newc.Path.AddArrayIndex(index) |
| 160 |
newc.Depth++ |
| 161 |
return |
| 162 |
} |
| 163 |
|
| 164 |
// AddMapKey creates a new [Context] from current one plus a map |
| 165 |
// dereference for key key. |
| 166 |
func (c Context) AddMapKey(key any) (newc Context) { |
| 167 |
newc = c |
| 168 |
newc.Path = newc.Path.AddMapKey(key) |
| 169 |
newc.Depth++ |
| 170 |
return |
| 171 |
} |
| 172 |
|
| 173 |
// AddPtr creates a new [Context] from current one plus a pointer dereference. |
| 174 |
func (c Context) AddPtr(num int) (newc Context) { |
| 175 |
newc = c |
| 176 |
newc.Path = newc.Path.AddPtr(num) |
| 177 |
newc.Depth++ |
| 178 |
return |
| 179 |
} |
| 180 |
|
| 181 |
// AddFunctionCall creates a new [Context] from current one inside a |
| 182 |
// function call. |
| 183 |
func (c Context) AddFunctionCall(fn string) (newc Context) { |
| 184 |
newc = c |
| 185 |
newc.Path = newc.Path.AddFunctionCall(fn) |
| 186 |
newc.Depth++ |
| 187 |
return |
| 188 |
} |
| 189 |
|
| 190 |
// ResetPath creates a new [Context] from current one but reinitializing Path. |
| 191 |
func (c Context) ResetPath(newRoot string) (newc Context) { |
| 192 |
newc = c |
| 193 |
newc.Path = NewPath(newRoot) |
| 194 |
newc.Depth++ |
| 195 |
return |
| 196 |
} |