| 1 |
// Copyright (c) 2022-2023, 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 |
"reflect" |
| 11 |
|
| 12 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 13 |
"github.com/maxatome/go-testdeep/internal/types" |
| 14 |
"github.com/maxatome/go-testdeep/internal/util" |
| 15 |
) |
| 16 |
|
| 17 |
const grepUsage = "(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE)" |
| 18 |
|
| 19 |
type tdGrepBase struct { |
| 20 |
tdSmugglerBase |
| 21 |
filter reflect.Value // func (argType ≠ nil) OR TestDeep operator |
| 22 |
argType reflect.Type |
| 23 |
} |
| 24 |
|
| 25 |
func (g *tdGrepBase) initGrepBase(filter, expectedValue any) { |
| 26 |
g.tdSmugglerBase = newSmugglerBase(expectedValue, 1) |
| 27 |
|
| 28 |
if !g.isTestDeeper { |
| 29 |
g.expectedValue = reflect.ValueOf(expectedValue) |
| 30 |
} |
| 31 |
|
| 32 |
if op, ok := filter.(TestDeep); ok { |
| 33 |
g.filter = reflect.ValueOf(op) |
| 34 |
return |
| 35 |
} |
| 36 |
|
| 37 |
vfilter := reflect.ValueOf(filter) |
| 38 |
if vfilter.Kind() != reflect.Func { |
| 39 |
g.err = ctxerr.OpBad(g.GetLocation().Func, |
| 40 |
"usage: %s%s, FILTER_FUNC must be a function or FILTER_TESTDEEP_OPERATOR a TestDeep operator", |
| 41 |
g.GetLocation().Func, grepUsage) |
| 42 |
return |
| 43 |
} |
| 44 |
|
| 45 |
filterType := vfilter.Type() |
| 46 |
if filterType.IsVariadic() || filterType.NumIn() != 1 { |
| 47 |
g.err = ctxerr.OpBad(g.GetLocation().Func, |
| 48 |
"usage: %s%s, FILTER_FUNC must take only one non-variadic argument", |
| 49 |
g.GetLocation().Func, grepUsage) |
| 50 |
return |
| 51 |
} |
| 52 |
if filterType.NumOut() != 1 || filterType.Out(0) != types.Bool { |
| 53 |
g.err = ctxerr.OpBad(g.GetLocation().Func, |
| 54 |
"usage: %s%s, FILTER_FUNC must return bool", |
| 55 |
g.GetLocation().Func, grepUsage) |
| 56 |
return |
| 57 |
} |
| 58 |
|
| 59 |
g.argType = filterType.In(0) |
| 60 |
g.filter = vfilter |
| 61 |
} |
| 62 |
|
| 63 |
func (g *tdGrepBase) matchItem(ctx ctxerr.Context, item reflect.Value) (bool, *ctxerr.Error) { |
| 64 |
if g.argType == nil { |
| 65 |
// g.filter is a TestDeep operator |
| 66 |
return deepValueEqualFinalOK(ctx, item, g.filter) |
| 67 |
} |
| 68 |
|
| 69 |
// item is an interface, but the filter function does not expect an |
| 70 |
// interface, resolve it |
| 71 |
if item.Kind() == reflect.Interface && g.argType.Kind() != reflect.Interface { |
| 72 |
item = item.Elem() |
| 73 |
} |
| 74 |
|
| 75 |
if !item.Type().AssignableTo(g.argType) { |
| 76 |
if !types.IsConvertible(item, g.argType) { |
| 77 |
if ctx.BooleanError { |
| 78 |
return false, ctxerr.BooleanError |
| 79 |
} |
| 80 |
return false, &ctxerr.Error{ |
| 81 |
Message: "incompatible parameter type", |
| 82 |
Got: types.RawString(item.Type().String()), |
| 83 |
Expected: types.RawString(g.argType.String()), |
| 84 |
} |
| 85 |
} |
| 86 |
item = item.Convert(g.argType) |
| 87 |
} |
| 88 |
|
| 89 |
return g.filter.Call([]reflect.Value{item})[0].Bool(), nil |
| 90 |
} |
| 91 |
|
| 92 |
func (g *tdGrepBase) HandleInvalid() bool { |
| 93 |
return true // Knows how to handle untyped nil values (aka invalid values) |
| 94 |
} |
| 95 |
|
| 96 |
func (g *tdGrepBase) String() string { |
| 97 |
if g.err != nil { |
| 98 |
return g.stringError() |
| 99 |
} |
| 100 |
if g.argType == nil { |
| 101 |
return S("%s(%s, %s)", |
| 102 |
g.GetLocation().Func, |
| 103 |
g.filter.Interface().(TestDeep), |
| 104 |
util.ToString(g.expectedValue)) |
| 105 |
} |
| 106 |
return S("%s(%s, %s)", |
| 107 |
g.GetLocation().Func, |
| 108 |
g.filter.Type(), |
| 109 |
util.ToString(g.expectedValue)) |
| 110 |
} |
| 111 |
|
| 112 |
func (g *tdGrepBase) TypeBehind() reflect.Type { |
| 113 |
if g.err != nil { |
| 114 |
return nil |
| 115 |
} |
| 116 |
return g.internalTypeBehind() |
| 117 |
} |
| 118 |
|
| 119 |
// sliceTypeBehind is used by First & Last TypeBehind method. |
| 120 |
func (g *tdGrepBase) sliceTypeBehind() reflect.Type { |
| 121 |
typ := g.TypeBehind() |
| 122 |
if typ == nil { |
| 123 |
return nil |
| 124 |
} |
| 125 |
return reflect.SliceOf(typ) |
| 126 |
} |
| 127 |
|
| 128 |
func (g *tdGrepBase) notFound(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 129 |
if ctx.BooleanError { |
| 130 |
return ctxerr.BooleanError |
| 131 |
} |
| 132 |
return ctx.CollectError(&ctxerr.Error{ |
| 133 |
Message: "item not found", |
| 134 |
Got: got, |
| 135 |
Expected: types.RawString(g.String()), |
| 136 |
}) |
| 137 |
} |
| 138 |
|
| 139 |
func grepResolvePtr(ctx ctxerr.Context, got *reflect.Value) *ctxerr.Error { |
| 140 |
if got.Kind() == reflect.Ptr { |
| 141 |
gotElem := got.Elem() |
| 142 |
if !gotElem.IsValid() { |
| 143 |
if ctx.BooleanError { |
| 144 |
return ctxerr.BooleanError |
| 145 |
} |
| 146 |
return ctxerr.NilPointer(*got, "non-nil *slice OR *array") |
| 147 |
} |
| 148 |
switch gotElem.Kind() { |
| 149 |
case reflect.Slice, reflect.Array: |
| 150 |
*got = gotElem |
| 151 |
} |
| 152 |
} |
| 153 |
return nil |
| 154 |
} |
| 155 |
|
| 156 |
func grepBadKind(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 157 |
if ctx.BooleanError { |
| 158 |
return ctxerr.BooleanError |
| 159 |
} |
| 160 |
return ctx.CollectError(ctxerr.BadKind(got, "slice OR array OR *slice OR *array")) |
| 161 |
} |
| 162 |
|
| 163 |
type tdGrep struct { |
| 164 |
tdGrepBase |
| 165 |
} |
| 166 |
|
| 167 |
var _ TestDeep = &tdGrep{} |
| 168 |
|
| 169 |
// summary(Grep): reduces a slice or an array before comparing its content |
| 170 |
// input(Grep): array,slice,ptr(ptr on array/slice) |
| 171 |
|
| 172 |
// Grep is a smuggler operator. It takes an array, a slice or a |
| 173 |
// pointer on array/slice. For each item it applies filter, a |
| 174 |
// [TestDeep] operator or a function returning a bool, and produces a |
| 175 |
// slice consisting of those items for which the filter matched and |
| 176 |
// compares it to expectedValue. The filter matches when it is a: |
| 177 |
// - [TestDeep] operator and it matches for the item; |
| 178 |
// - function receiving the item and it returns true. |
| 179 |
// |
| 180 |
// expectedValue can be a [TestDeep] operator or a slice (but never an |
| 181 |
// array nor a pointer on a slice/array nor any other kind). |
| 182 |
// |
| 183 |
// got := []int{-3, -2, -1, 0, 1, 2, 3} |
| 184 |
// td.Cmp(t, got, td.Grep(td.Gt(0), []int{1, 2, 3})) // succeeds |
| 185 |
// td.Cmp(t, got, td.Grep( |
| 186 |
// func(x int) bool { return x%2 == 0 }, |
| 187 |
// []int{-2, 0, 2})) // succeeds |
| 188 |
// td.Cmp(t, got, td.Grep( |
| 189 |
// func(x int) bool { return x%2 == 0 }, |
| 190 |
// td.Set(0, 2, -2))) // succeeds |
| 191 |
// |
| 192 |
// If Grep receives a nil slice or a pointer on a nil slice, it always |
| 193 |
// returns a nil slice: |
| 194 |
// |
| 195 |
// var got []int |
| 196 |
// td.Cmp(t, got, td.Grep(td.Gt(0), ([]int)(nil))) // succeeds |
| 197 |
// td.Cmp(t, got, td.Grep(td.Gt(0), td.Nil())) // succeeds |
| 198 |
// td.Cmp(t, got, td.Grep(td.Gt(0), []int{})) // fails |
| 199 |
// |
| 200 |
// See also [First], [Last] and [Flatten]. |
| 201 |
func Grep(filter, expectedValue any) TestDeep { |
| 202 |
g := tdGrep{} |
| 203 |
g.initGrepBase(filter, expectedValue) |
| 204 |
|
| 205 |
if g.err == nil && !g.isTestDeeper && g.expectedValue.Kind() != reflect.Slice { |
| 206 |
g.err = ctxerr.OpBad("Grep", |
| 207 |
"usage: Grep%s, EXPECTED_VALUE must be a slice not a %s", |
| 208 |
grepUsage, types.KindType(g.expectedValue)) |
| 209 |
} |
| 210 |
return &g |
| 211 |
} |
| 212 |
|
| 213 |
func (g *tdGrep) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 214 |
if g.err != nil { |
| 215 |
return ctx.CollectError(g.err) |
| 216 |
} |
| 217 |
|
| 218 |
if rErr := grepResolvePtr(ctx, &got); rErr != nil { |
| 219 |
return ctx.CollectError(rErr) |
| 220 |
} |
| 221 |
|
| 222 |
switch got.Kind() { |
| 223 |
case reflect.Slice, reflect.Array: |
| 224 |
default: |
| 225 |
return grepBadKind(ctx, got) |
| 226 |
} |
| 227 |
|
| 228 |
const grepped = "<grepped>" |
| 229 |
|
| 230 |
l := got.Len() |
| 231 |
if l == 0 { |
| 232 |
return deepValueEqual(ctx.AddCustomLevel(grepped), got, g.expectedValue) |
| 233 |
} |
| 234 |
|
| 235 |
outType := got.Type() |
| 236 |
if got.Kind() == reflect.Array { |
| 237 |
outType = reflect.SliceOf(outType.Elem()) |
| 238 |
} |
| 239 |
|
| 240 |
out := reflect.MakeSlice(outType, 0, l) |
| 241 |
|
| 242 |
for idx := 0; idx < l; idx++ { |
| 243 |
item := got.Index(idx) |
| 244 |
ok, rErr := g.matchItem(ctx, item) |
| 245 |
if rErr != nil { |
| 246 |
if !rErr.User { |
| 247 |
ctx = ctx.AddArrayIndex(idx) |
| 248 |
} |
| 249 |
return ctx.CollectError(rErr) |
| 250 |
} |
| 251 |
if ok { |
| 252 |
out = reflect.Append(out, item) |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
return deepValueEqual(ctx.AddCustomLevel(grepped), out, g.expectedValue) |
| 257 |
} |
| 258 |
|
| 259 |
type tdFirst struct { |
| 260 |
tdGrepBase |
| 261 |
} |
| 262 |
|
| 263 |
var _ TestDeep = &tdFirst{} |
| 264 |
|
| 265 |
// summary(First): find the first matching item of a slice or an array |
| 266 |
// then compare its content |
| 267 |
// input(First): array,slice,ptr(ptr on array/slice) |
| 268 |
|
| 269 |
// First is a smuggler operator. It takes an array, a slice or a |
| 270 |
// pointer on array/slice. For each item it applies filter, a |
| 271 |
// [TestDeep] operator or a function returning a bool. It takes the |
| 272 |
// first item for which the filter matched and compares it to |
| 273 |
// expectedValue. The filter matches when it is a: |
| 274 |
// - [TestDeep] operator and it matches for the item; |
| 275 |
// - function receiving the item and it returns true. |
| 276 |
// |
| 277 |
// expectedValue can of course be a [TestDeep] operator. |
| 278 |
// |
| 279 |
// got := []int{-3, -2, -1, 0, 1, 2, 3} |
| 280 |
// td.Cmp(t, got, td.First(td.Gt(0), 1)) // succeeds |
| 281 |
// td.Cmp(t, got, td.First(func(x int) bool { return x%2 == 0 }, -2)) // succeeds |
| 282 |
// td.Cmp(t, got, td.First(func(x int) bool { return x%2 == 0 }, td.Lt(0))) // succeeds |
| 283 |
// |
| 284 |
// If the input is empty (and/or nil for a slice), an "item not found" |
| 285 |
// error is raised before comparing to expectedValue. |
| 286 |
// |
| 287 |
// var got []int |
| 288 |
// td.Cmp(t, got, td.First(td.Gt(0), td.Gt(0))) // fails |
| 289 |
// td.Cmp(t, []int{}, td.First(td.Gt(0), td.Gt(0))) // fails |
| 290 |
// td.Cmp(t, [0]int{}, td.First(td.Gt(0), td.Gt(0))) // fails |
| 291 |
// |
| 292 |
// See also [Last] and [Grep]. |
| 293 |
func First(filter, expectedValue any) TestDeep { |
| 294 |
g := tdFirst{} |
| 295 |
g.initGrepBase(filter, expectedValue) |
| 296 |
return &g |
| 297 |
} |
| 298 |
|
| 299 |
func (g *tdFirst) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 300 |
if g.err != nil { |
| 301 |
return ctx.CollectError(g.err) |
| 302 |
} |
| 303 |
|
| 304 |
if rErr := grepResolvePtr(ctx, &got); rErr != nil { |
| 305 |
return ctx.CollectError(rErr) |
| 306 |
} |
| 307 |
|
| 308 |
switch got.Kind() { |
| 309 |
case reflect.Slice, reflect.Array: |
| 310 |
for idx, l := 0, got.Len(); idx < l; idx++ { |
| 311 |
item := got.Index(idx) |
| 312 |
ok, rErr := g.matchItem(ctx, item) |
| 313 |
if rErr != nil { |
| 314 |
if !rErr.User { |
| 315 |
ctx = ctx.AddArrayIndex(idx) |
| 316 |
} |
| 317 |
return ctx.CollectError(rErr) |
| 318 |
} |
| 319 |
if ok { |
| 320 |
return deepValueEqual( |
| 321 |
ctx.AddCustomLevel(S("<first#%d>", idx)), |
| 322 |
item, |
| 323 |
g.expectedValue, |
| 324 |
) |
| 325 |
} |
| 326 |
} |
| 327 |
return g.notFound(ctx, got) |
| 328 |
} |
| 329 |
|
| 330 |
return grepBadKind(ctx, got) |
| 331 |
} |
| 332 |
|
| 333 |
func (g *tdFirst) TypeBehind() reflect.Type { |
| 334 |
return g.sliceTypeBehind() |
| 335 |
} |
| 336 |
|
| 337 |
type tdLast struct { |
| 338 |
tdGrepBase |
| 339 |
} |
| 340 |
|
| 341 |
var _ TestDeep = &tdLast{} |
| 342 |
|
| 343 |
// summary(Last): find the last matching item of a slice or an array |
| 344 |
// then compare its content |
| 345 |
// input(Last): array,slice,ptr(ptr on array/slice) |
| 346 |
|
| 347 |
// Last is a smuggler operator. It takes an array, a slice or a |
| 348 |
// pointer on array/slice. For each item it applies filter, a |
| 349 |
// [TestDeep] operator or a function returning a bool. It takes the |
| 350 |
// last item for which the filter matched and compares it to |
| 351 |
// expectedValue. The filter matches when it is a: |
| 352 |
// - [TestDeep] operator and it matches for the item; |
| 353 |
// - function receiving the item and it returns true. |
| 354 |
// |
| 355 |
// expectedValue can of course be a [TestDeep] operator. |
| 356 |
// |
| 357 |
// got := []int{-3, -2, -1, 0, 1, 2, 3} |
| 358 |
// td.Cmp(t, got, td.Last(td.Lt(0), -1)) // succeeds |
| 359 |
// td.Cmp(t, got, td.Last(func(x int) bool { return x%2 == 0 }, 2)) // succeeds |
| 360 |
// td.Cmp(t, got, td.Last(func(x int) bool { return x%2 == 0 }, td.Gt(0))) // succeeds |
| 361 |
// |
| 362 |
// If the input is empty (and/or nil for a slice), an "item not found" |
| 363 |
// error is raised before comparing to expectedValue. |
| 364 |
// |
| 365 |
// var got []int |
| 366 |
// td.Cmp(t, got, td.Last(td.Gt(0), td.Gt(0))) // fails |
| 367 |
// td.Cmp(t, []int{}, td.Last(td.Gt(0), td.Gt(0))) // fails |
| 368 |
// td.Cmp(t, [0]int{}, td.Last(td.Gt(0), td.Gt(0))) // fails |
| 369 |
// |
| 370 |
// See also [First] and [Grep]. |
| 371 |
func Last(filter, expectedValue any) TestDeep { |
| 372 |
g := tdLast{} |
| 373 |
g.initGrepBase(filter, expectedValue) |
| 374 |
return &g |
| 375 |
} |
| 376 |
|
| 377 |
func (g *tdLast) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 378 |
if g.err != nil { |
| 379 |
return ctx.CollectError(g.err) |
| 380 |
} |
| 381 |
|
| 382 |
if rErr := grepResolvePtr(ctx, &got); rErr != nil { |
| 383 |
return ctx.CollectError(rErr) |
| 384 |
} |
| 385 |
|
| 386 |
switch got.Kind() { |
| 387 |
case reflect.Slice, reflect.Array: |
| 388 |
for idx := got.Len() - 1; idx >= 0; idx-- { |
| 389 |
item := got.Index(idx) |
| 390 |
ok, rErr := g.matchItem(ctx, item) |
| 391 |
if rErr != nil { |
| 392 |
if !rErr.User { |
| 393 |
ctx = ctx.AddArrayIndex(idx) |
| 394 |
} |
| 395 |
return ctx.CollectError(rErr) |
| 396 |
} |
| 397 |
if ok { |
| 398 |
return deepValueEqual( |
| 399 |
ctx.AddCustomLevel(S("<last#%d>", idx)), |
| 400 |
item, |
| 401 |
g.expectedValue, |
| 402 |
) |
| 403 |
} |
| 404 |
} |
| 405 |
return g.notFound(ctx, got) |
| 406 |
} |
| 407 |
|
| 408 |
return grepBadKind(ctx, got) |
| 409 |
} |
| 410 |
|
| 411 |
func (g *tdLast) TypeBehind() reflect.Type { |
| 412 |
return g.sliceTypeBehind() |
| 413 |
} |