| 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 td |
| 8 |
|
| 9 |
import ( |
| 10 |
"os" |
| 11 |
"strconv" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/maxatome/go-testdeep/internal/anchors" |
| 15 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 16 |
"github.com/maxatome/go-testdeep/internal/hooks" |
| 17 |
"github.com/maxatome/go-testdeep/internal/visited" |
| 18 |
) |
| 19 |
|
| 20 |
// ContextConfig allows to configure finely how tests failures are rendered. |
| 21 |
// |
| 22 |
// See [NewT] function to use it. |
| 23 |
type ContextConfig struct { |
| 24 |
// RootName is the string used to represent the root of got data. It |
| 25 |
// defaults to "DATA". For an HTTP response body, it could be "BODY" |
| 26 |
// for example. |
| 27 |
RootName string |
| 28 |
forkedFromCtx *ctxerr.Context |
| 29 |
// MaxErrors is the maximal number of errors to dump in case of Cmp* |
| 30 |
// failure. |
| 31 |
// |
| 32 |
// It defaults to 10 except if the environment variable |
| 33 |
// TESTDEEP_MAX_ERRORS is set. In this latter case, the |
| 34 |
// TESTDEEP_MAX_ERRORS value is converted to an int and used as is. |
| 35 |
// |
| 36 |
// Setting it to 0 has the same effect as 1: only the first error |
| 37 |
// will be dumped without the "Too many errors" error. |
| 38 |
// |
| 39 |
// Setting it to a negative number means no limit: all errors |
| 40 |
// will be dumped. |
| 41 |
MaxErrors int |
| 42 |
anchors *anchors.Info |
| 43 |
hooks *hooks.Info |
| 44 |
// FailureIsFatal allows to Fatal() (instead of Error()) when a test |
| 45 |
// fails. Using *testing.T or *testing.B instance as t.TB value, FailNow() |
| 46 |
// is called behind the scenes when Fatal() is called. See testing |
| 47 |
// documentation for details. |
| 48 |
FailureIsFatal bool |
| 49 |
// UseEqual allows to use the Equal method on got (if it exists) or |
| 50 |
// on any of its component to compare got and expected values. |
| 51 |
// |
| 52 |
// The signature of the Equal method should be: |
| 53 |
// (A) Equal(B) bool |
| 54 |
// with B assignable to A. |
| 55 |
// |
| 56 |
// See time.Time as an example of accepted Equal() method. |
| 57 |
// |
| 58 |
// See (*T).UseEqual method to only apply this property to some |
| 59 |
// specific types. |
| 60 |
UseEqual bool |
| 61 |
// BeLax allows to compare different but convertible types. If set |
| 62 |
// to false (default), got and expected types must be the same. If |
| 63 |
// set to true and expected type is convertible to got one, expected |
| 64 |
// is first converted to go type before its comparison. See CmpLax |
| 65 |
// function/method and Lax operator to set this flag without |
| 66 |
// providing a specific configuration. |
| 67 |
BeLax bool |
| 68 |
// IgnoreUnexported allows to ignore unexported struct fields. Be |
| 69 |
// careful about structs entirely composed of unexported fields |
| 70 |
// (like time.Time for example). With this flag set to true, they |
| 71 |
// are all equal. In such case it is advised to set UseEqual flag, |
| 72 |
// to use (*T).UseEqual method or to add a Cmp hook using |
| 73 |
// (*T).WithCmpHooks method. |
| 74 |
// |
| 75 |
// See (*T).IgnoreUnexported method to only apply this property to some |
| 76 |
// specific types. |
| 77 |
IgnoreUnexported bool |
| 78 |
// TestDeepInGotOK allows to accept TestDeep operator in got Cmp* |
| 79 |
// parameter. By default it is forbidden and a panic occurs, because |
| 80 |
// most of the time it is a mistake to compare (expected, got) |
| 81 |
// instead of official (got, expected). |
| 82 |
TestDeepInGotOK bool |
| 83 |
} |
| 84 |
|
| 85 |
// Equal returns true if both c and o are equal. Only public fields |
| 86 |
// are taken into account to check equality. |
| 87 |
func (c ContextConfig) Equal(o ContextConfig) bool { |
| 88 |
return c.RootName == o.RootName && |
| 89 |
c.MaxErrors == o.MaxErrors && |
| 90 |
c.FailureIsFatal == o.FailureIsFatal && |
| 91 |
c.UseEqual == o.UseEqual && |
| 92 |
c.BeLax == o.BeLax && |
| 93 |
c.IgnoreUnexported == o.IgnoreUnexported && |
| 94 |
c.TestDeepInGotOK == o.TestDeepInGotOK |
| 95 |
} |
| 96 |
|
| 97 |
// OriginalPath returns the current path when the [ContextConfig] has |
| 98 |
// been built. It always returns ContextConfig.RootName except if c |
| 99 |
// has been built by [Code] operator. See [Code] documentation for an |
| 100 |
// example of use. |
| 101 |
func (c ContextConfig) OriginalPath() string { |
| 102 |
if c.forkedFromCtx == nil { |
| 103 |
return c.RootName |
| 104 |
} |
| 105 |
return c.forkedFromCtx.Path.String() |
| 106 |
} |
| 107 |
|
| 108 |
const ( |
| 109 |
contextDefaultRootName = "DATA" |
| 110 |
contextPanicRootName = "FUNCTION" |
| 111 |
envMaxErrors = "TESTDEEP_MAX_ERRORS" |
| 112 |
) |
| 113 |
|
| 114 |
func getMaxErrorsFromEnv() int { |
| 115 |
env := os.Getenv(envMaxErrors) |
| 116 |
if env != "" { |
| 117 |
n, err := strconv.Atoi(env) |
| 118 |
if err == nil { |
| 119 |
return n |
| 120 |
} |
| 121 |
} |
| 122 |
return 10 |
| 123 |
} |
| 124 |
|
| 125 |
// DefaultContextConfig is the default configuration used to render |
| 126 |
// tests failures. If overridden, new settings will impact all Cmp* |
| 127 |
// functions and [*T] methods (if not specifically configured.) |
| 128 |
var DefaultContextConfig = ContextConfig{ |
| 129 |
RootName: contextDefaultRootName, |
| 130 |
MaxErrors: getMaxErrorsFromEnv(), |
| 131 |
FailureIsFatal: false, |
| 132 |
UseEqual: false, |
| 133 |
BeLax: false, |
| 134 |
IgnoreUnexported: false, |
| 135 |
TestDeepInGotOK: false, |
| 136 |
} |
| 137 |
|
| 138 |
func (c *ContextConfig) sanitize() { |
| 139 |
if c.RootName == "" { |
| 140 |
c.RootName = DefaultContextConfig.RootName |
| 141 |
} |
| 142 |
if c.MaxErrors == 0 { |
| 143 |
c.MaxErrors = DefaultContextConfig.MaxErrors |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
// newContext creates a new ctxerr.Context using DefaultContextConfig |
| 148 |
// configuration. |
| 149 |
func newContext(t TestingT) ctxerr.Context { |
| 150 |
if tt, ok := t.(*T); ok { |
| 151 |
return newContextWithConfig(tt, tt.Config) |
| 152 |
} |
| 153 |
tb, _ := t.(testing.TB) |
| 154 |
return newContextWithConfig(tb, DefaultContextConfig) |
| 155 |
} |
| 156 |
|
| 157 |
// newContextWithConfig creates a new ctxerr.Context using a specific |
| 158 |
// configuration. |
| 159 |
func newContextWithConfig(tb testing.TB, config ContextConfig) (ctx ctxerr.Context) { |
| 160 |
config.sanitize() |
| 161 |
|
| 162 |
ctx = ctxerr.Context{ |
| 163 |
Path: ctxerr.NewPath(config.RootName), |
| 164 |
Visited: visited.NewVisited(), |
| 165 |
MaxErrors: config.MaxErrors, |
| 166 |
Anchors: config.anchors, |
| 167 |
Hooks: config.hooks, |
| 168 |
OriginalTB: tb, |
| 169 |
FailureIsFatal: config.FailureIsFatal, |
| 170 |
UseEqual: config.UseEqual, |
| 171 |
BeLax: config.BeLax, |
| 172 |
IgnoreUnexported: config.IgnoreUnexported, |
| 173 |
TestDeepInGotOK: config.TestDeepInGotOK, |
| 174 |
} |
| 175 |
|
| 176 |
ctx.InitErrors() |
| 177 |
return |
| 178 |
} |
| 179 |
|
| 180 |
// newBooleanContext creates a new boolean ctxerr.Context. |
| 181 |
func newBooleanContext() ctxerr.Context { |
| 182 |
return ctxerr.Context{ |
| 183 |
Visited: visited.NewVisited(), |
| 184 |
BooleanError: true, |
| 185 |
UseEqual: DefaultContextConfig.UseEqual, |
| 186 |
BeLax: DefaultContextConfig.BeLax, |
| 187 |
IgnoreUnexported: DefaultContextConfig.IgnoreUnexported, |
| 188 |
TestDeepInGotOK: DefaultContextConfig.TestDeepInGotOK, |
| 189 |
} |
| 190 |
} |