| 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 |
| 8 |
|
| 9 |
import ( |
| 10 |
"reflect" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"github.com/maxatome/go-testdeep/internal/color" |
| 14 |
"github.com/maxatome/go-testdeep/internal/location" |
| 15 |
"github.com/maxatome/go-testdeep/internal/types" |
| 16 |
"github.com/maxatome/go-testdeep/internal/util" |
| 17 |
) |
| 18 |
|
| 19 |
// Error represents errors generated by td (go-testdeep) functions. |
| 20 |
type Error struct { |
| 21 |
// Context when the error occurred |
| 22 |
Context Context |
| 23 |
// Message describes the error |
| 24 |
Message string |
| 25 |
// Got value |
| 26 |
Got any |
| 27 |
// Expected value |
| 28 |
Expected any |
| 29 |
// If not nil, Summary is used to display summary instead of using |
| 30 |
// Got + Expected fields |
| 31 |
Summary ErrorSummary |
| 32 |
// If initialized, location of TestDeep operator originator of the error |
| 33 |
Location location.Location |
| 34 |
// If defined, the current Error comes from this Error |
| 35 |
Origin *Error |
| 36 |
// If defined, points to the next Error |
| 37 |
Next *Error |
| 38 |
// true if the error is a user mistake |
| 39 |
User bool |
| 40 |
} |
| 41 |
|
| 42 |
// BooleanError is the [*Error] returned when an error occurs in a |
| 43 |
// boolean context. |
| 44 |
var BooleanError = &Error{} |
| 45 |
|
| 46 |
// ErrTooManyErrors is chained to the last error encountered when |
| 47 |
// the maximum number of errors has been reached. |
| 48 |
var ErrTooManyErrors = &Error{ |
| 49 |
Message: "Too many errors (use TESTDEEP_MAX_ERRORS=-1 to see all)", |
| 50 |
} |
| 51 |
|
| 52 |
// TypeMismatch returns a "type mismatch" error. It is the caller |
| 53 |
// responsibility to check that both types differ. |
| 54 |
// |
| 55 |
// If they resolve to the same name (via their String method), it |
| 56 |
// tries to deeply dump the full package name of each type. |
| 57 |
// |
| 58 |
// It works pretty well with the exception of identical anomymous |
| 59 |
// structs in 2 different packages with the same last name: in this |
| 60 |
// case reflect does not allow us to retrieve the package from which |
| 61 |
// each type comes. |
| 62 |
// |
| 63 |
// package foo // in a/ |
| 64 |
// var Foo struct { a int } |
| 65 |
// |
| 66 |
// package foo // in b/ |
| 67 |
// var Foo struct { a int } |
| 68 |
// |
| 69 |
// package ctxerr |
| 70 |
// import( |
| 71 |
// a_foo "a/foo" |
| 72 |
// b_foo "b/foo" |
| 73 |
// ) |
| 74 |
// … |
| 75 |
// TypeMismatch(reflect.TypeOf(a_foo.Foo), reflect.TypeOf(b_foo.Foo)) |
| 76 |
// |
| 77 |
// returns an error producing: |
| 78 |
// |
| 79 |
// type mismatch |
| 80 |
// got: struct { a int } |
| 81 |
// expected: struct { a int } |
| 82 |
func TypeMismatch(got, expected reflect.Type) *Error { |
| 83 |
gs, es := got.String(), expected.String() |
| 84 |
if gs == es { |
| 85 |
gs, es = util.TypeFullName(got), util.TypeFullName(expected) |
| 86 |
} |
| 87 |
return &Error{ |
| 88 |
Message: "type mismatch", |
| 89 |
Got: types.RawString(gs), |
| 90 |
Expected: types.RawString(es), |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// Error implements error interface. |
| 95 |
func (e *Error) Error() string { |
| 96 |
buf := strings.Builder{} |
| 97 |
|
| 98 |
e.Append(&buf, "", true) |
| 99 |
|
| 100 |
return buf.String() |
| 101 |
} |
| 102 |
|
| 103 |
// ErrorWithoutColors is the same as [Error.Error] but guarantees the |
| 104 |
// resulting string does not contain any ANSI color escape sequences. |
| 105 |
func (e *Error) ErrorWithoutColors() string { |
| 106 |
buf := strings.Builder{} |
| 107 |
|
| 108 |
e.Append(&buf, "", false) |
| 109 |
|
| 110 |
return buf.String() |
| 111 |
} |
| 112 |
|
| 113 |
// Append appends the a contents to buf using prefix prefix for each |
| 114 |
// line. |
| 115 |
func (e *Error) Append(buf *strings.Builder, prefix string, colorized bool) { |
| 116 |
if e == BooleanError { |
| 117 |
return |
| 118 |
} |
| 119 |
|
| 120 |
var badOn, badOff, okOn, okOff string |
| 121 |
if colorized { |
| 122 |
color.Init() |
| 123 |
badOn, badOff = color.BadOn, color.BadOff |
| 124 |
okOn, okOff = color.OKOn, color.OKOff |
| 125 |
} |
| 126 |
|
| 127 |
var writeEolPrefix func() |
| 128 |
if prefix != "" { |
| 129 |
eolPrefix := make([]byte, 1+len(prefix)) |
| 130 |
eolPrefix[0] = '\n' |
| 131 |
copy(eolPrefix[1:], prefix) |
| 132 |
|
| 133 |
writeEolPrefix = func() { |
| 134 |
buf.Write(eolPrefix) |
| 135 |
} |
| 136 |
buf.WriteString(prefix) |
| 137 |
} else { |
| 138 |
writeEolPrefix = func() { |
| 139 |
buf.WriteByte('\n') |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if e == ErrTooManyErrors { |
| 144 |
if colorized { |
| 145 |
buf.WriteString(color.TitleOn) |
| 146 |
} |
| 147 |
buf.WriteString(e.Message) |
| 148 |
if colorized { |
| 149 |
buf.WriteString(color.TitleOff) |
| 150 |
} |
| 151 |
return |
| 152 |
} |
| 153 |
|
| 154 |
if colorized { |
| 155 |
buf.WriteString(color.TitleOn) |
| 156 |
} |
| 157 |
if pos := strings.Index(e.Message, "%%"); pos >= 0 { |
| 158 |
buf.WriteString(e.Message[:pos]) |
| 159 |
buf.WriteString(e.Context.Path.String()) |
| 160 |
buf.WriteString(e.Message[pos+2:]) |
| 161 |
} else { |
| 162 |
buf.WriteString(e.Context.Path.String()) |
| 163 |
buf.WriteString(": ") |
| 164 |
buf.WriteString(e.Message) |
| 165 |
} |
| 166 |
if colorized { |
| 167 |
buf.WriteString(color.TitleOff) |
| 168 |
} |
| 169 |
|
| 170 |
if e.Summary != nil { |
| 171 |
buf.WriteByte('\n') |
| 172 |
e.Summary.AppendSummary(buf, prefix+"\t", colorized) |
| 173 |
} else { |
| 174 |
writeEolPrefix() |
| 175 |
if colorized { |
| 176 |
buf.WriteString(color.BadOnBold) |
| 177 |
} |
| 178 |
buf.WriteString("\t got: ") |
| 179 |
util.IndentColorizeStringIn(buf, e.GotString(), prefix+"\t ", badOn, badOff) |
| 180 |
writeEolPrefix() |
| 181 |
if colorized { |
| 182 |
buf.WriteString(color.OKOnBold) |
| 183 |
} |
| 184 |
buf.WriteString("\texpected: ") |
| 185 |
util.IndentColorizeStringIn(buf, e.ExpectedString(), prefix+"\t ", okOn, okOff) |
| 186 |
} |
| 187 |
|
| 188 |
// This error comes from another one |
| 189 |
if e.Origin != nil { |
| 190 |
writeEolPrefix() |
| 191 |
buf.WriteString("Originates from following error:\n") |
| 192 |
|
| 193 |
e.Origin.Append(buf, prefix+"\t", colorized) |
| 194 |
} |
| 195 |
|
| 196 |
if e.Location.IsInitialized() && |
| 197 |
!e.Location.BehindCmp && // no need to log Cmp* func |
| 198 |
(e.Next == nil || e.Next.Location != e.Location) { |
| 199 |
writeEolPrefix() |
| 200 |
buf.WriteString("[under operator ") |
| 201 |
buf.WriteString(e.Location.String()) |
| 202 |
buf.WriteByte(']') |
| 203 |
} |
| 204 |
|
| 205 |
if e.Next != nil { |
| 206 |
buf.WriteByte('\n') |
| 207 |
e.Next.Append(buf, prefix, colorized) // next error at same level |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
// GotString returns the string corresponding to the Got |
| 212 |
// field. Returns the empty string if the e Summary field is not nil. |
| 213 |
func (e *Error) GotString() string { |
| 214 |
if e.Summary != nil { |
| 215 |
return "" |
| 216 |
} |
| 217 |
return util.ToString(e.Got) |
| 218 |
} |
| 219 |
|
| 220 |
// ExpectedString returns the string corresponding to the Expected |
| 221 |
// field. Returns the empty string if the e Summary field is not nil. |
| 222 |
func (e *Error) ExpectedString() string { |
| 223 |
if e.Summary != nil { |
| 224 |
return "" |
| 225 |
} |
| 226 |
return util.ToString(e.Expected) |
| 227 |
} |
| 228 |
|
| 229 |
// SummaryString returns the string corresponding to the Summary field |
| 230 |
// without any ANSI color escape sequences. Returns the empty string |
| 231 |
// if the e Summary field is nil. |
| 232 |
func (e *Error) SummaryString() string { |
| 233 |
if e.Summary == nil { |
| 234 |
return "" |
| 235 |
} |
| 236 |
|
| 237 |
var buf strings.Builder |
| 238 |
e.Summary.AppendSummary(&buf, "", false) |
| 239 |
return buf.String() |
| 240 |
} |