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