| 1 |
// Copyright (c) 2018-2025, 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 |
"fmt" |
| 11 |
"reflect" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 15 |
"github.com/maxatome/go-testdeep/internal/flat" |
| 16 |
"github.com/maxatome/go-testdeep/internal/util" |
| 17 |
) |
| 18 |
|
| 19 |
type tdListBase struct { |
| 20 |
baseOKNil |
| 21 |
items []reflect.Value |
| 22 |
} |
| 23 |
|
| 24 |
func newListBase(items ...any) tdListBase { |
| 25 |
return tdListBase{ |
| 26 |
baseOKNil: newBaseOKNil(4), |
| 27 |
items: flat.Values(items), |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func (l *tdListBase) String() string { |
| 32 |
var b strings.Builder |
| 33 |
b.WriteString(l.GetLocation().Func) |
| 34 |
return util.SliceToString(&b, l.items).String() |
| 35 |
} |
| 36 |
|
| 37 |
type tdList struct { |
| 38 |
tdListBase |
| 39 |
} |
| 40 |
|
| 41 |
var _ TestDeep = &tdList{} |
| 42 |
|
| 43 |
// summary(List): compares the contents of an array or a slice with taking |
| 44 |
// care of the order of items |
| 45 |
// input(List): array,slice,ptr(ptr on array/slice) |
| 46 |
|
| 47 |
// List operator compares the contents of an array or a slice (or a |
| 48 |
// pointer on array/slice) with taking care of the order of items. |
| 49 |
// |
| 50 |
// [Array] and [Slice] need to specify the type of array/slice being |
| 51 |
// compared then to index all expected items. List does not. It acts |
| 52 |
// as comparing a literal array/slice, but without having to specify |
| 53 |
// the type and allowing to easily use TestDeep operators: |
| 54 |
// |
| 55 |
// td.Cmp(t, []int{1, 9, 5}, td.List(1, 9, 5)) // succeeds |
| 56 |
// td.Cmp(t, []int{1, 9, 5}, td.List(td.Gt(0), td.Between(8, 9), td.Lt(5))) // succeeds |
| 57 |
// td.Cmp(t, []int{1, 9, 5}, td.List(1, 9)) // fails, 5 is extra |
| 58 |
// td.Cmp(t, []int{1, 9, 5}, td.List(1, 9, 5, 4)) // fails, 4 is missing |
| 59 |
// |
| 60 |
// // works with slices/arrays of any type |
| 61 |
// td.Cmp(t, personSlice, td.List( |
| 62 |
// Person{Name: "Bob", Age: 32}, |
| 63 |
// Person{Name: "Alice", Age: 26}, |
| 64 |
// )) |
| 65 |
// |
| 66 |
// To flatten a non-[]any slice/array, use [Flatten] function |
| 67 |
// and so avoid boring and inefficient copies: |
| 68 |
// |
| 69 |
// expected := []int{1, 2, 1} |
| 70 |
// td.Cmp(t, []int{1, 1, 2}, td.List(td.Flatten(expected))) // succeeds |
| 71 |
// // = td.Cmp(t, []int{1, 1, 2}, td.List(1, 2, 1)) |
| 72 |
// |
| 73 |
// // Compare only Name field of a slice of Person structs |
| 74 |
// td.Cmp(t, personSlice, td.List(td.Flatten([]string{"Bob", "Alice"}, "Smuggle:Name"))) |
| 75 |
// |
| 76 |
// TypeBehind method can return a non-nil [reflect.Type] if all items |
| 77 |
// known non-interface types are equal, or if only interface types |
| 78 |
// are found (mostly issued from Isa()) and they are equal. |
| 79 |
// |
| 80 |
// See also [Bag], [Set] and [Sort]. |
| 81 |
func List(expectedValues ...any) TestDeep { |
| 82 |
return &tdList{ |
| 83 |
tdListBase: newListBase(expectedValues...), |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func (l *tdList) Match(ctx ctxerr.Context, got reflect.Value) (err *ctxerr.Error) { |
| 88 |
switch got.Kind() { |
| 89 |
case reflect.Ptr: |
| 90 |
gotElem := got.Elem() |
| 91 |
if !gotElem.IsValid() { |
| 92 |
if ctx.BooleanError { |
| 93 |
return ctxerr.BooleanError |
| 94 |
} |
| 95 |
return ctx.CollectError(ctxerr.NilPointer(got, "non-nil *slice OR *array")) |
| 96 |
} |
| 97 |
|
| 98 |
if gotElem.Kind() != reflect.Array && gotElem.Kind() != reflect.Slice { |
| 99 |
break |
| 100 |
} |
| 101 |
got = gotElem |
| 102 |
fallthrough |
| 103 |
|
| 104 |
case reflect.Array, reflect.Slice: |
| 105 |
gotLen, expectedLen := got.Len(), len(l.items) |
| 106 |
if ctx.BooleanError && gotLen != expectedLen { |
| 107 |
return ctxerr.BooleanError // shortcut in boolean context |
| 108 |
} |
| 109 |
|
| 110 |
var maxLen int |
| 111 |
if gotLen >= expectedLen { |
| 112 |
maxLen = expectedLen |
| 113 |
} else { |
| 114 |
maxLen = gotLen |
| 115 |
} |
| 116 |
|
| 117 |
for i := 0; i < maxLen; i++ { |
| 118 |
err = deepValueEqual(ctx.AddArrayIndex(i), got.Index(i), l.items[i]) |
| 119 |
if err != nil { |
| 120 |
return err |
| 121 |
} |
| 122 |
} |
| 123 |
if gotLen == expectedLen { |
| 124 |
return |
| 125 |
} |
| 126 |
|
| 127 |
res := tdSetResult{ |
| 128 |
Kind: itemsSetResult, |
| 129 |
// do not sort Extra/Mising here |
| 130 |
} |
| 131 |
|
| 132 |
if gotLen > expectedLen { |
| 133 |
res.Extra = make([]reflect.Value, gotLen-expectedLen) |
| 134 |
for i := expectedLen; i < gotLen; i++ { |
| 135 |
res.Extra[i-expectedLen] = got.Index(i) |
| 136 |
} |
| 137 |
} else { |
| 138 |
res.Missing = l.items[gotLen:] |
| 139 |
} |
| 140 |
return ctx.CollectError(&ctxerr.Error{ |
| 141 |
Message: fmt.Sprintf("comparing %s, from index #%d", got.Kind(), maxLen), |
| 142 |
Summary: res.Summary(), |
| 143 |
}) |
| 144 |
} |
| 145 |
|
| 146 |
if ctx.BooleanError { |
| 147 |
return ctxerr.BooleanError |
| 148 |
} |
| 149 |
return ctx.CollectError(ctxerr.BadKind(got, "slice OR array OR *slice OR *array")) |
| 150 |
} |
| 151 |
|
| 152 |
func (l *tdList) TypeBehind() reflect.Type { |
| 153 |
typ := uniqTypeBehindSlice(l.items) |
| 154 |
if typ == nil { |
| 155 |
return nil |
| 156 |
} |
| 157 |
return reflect.SliceOf(typ) |
| 158 |
} |