Debian Codegraph @

td/td_set_base.go

raw
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 "reflect"
11 "strings"
12
13 "github.com/maxatome/go-testdeep/internal/ctxerr"
14 "github.com/maxatome/go-testdeep/internal/flat"
15 "github.com/maxatome/go-testdeep/internal/util"
16 )
17
18 type setKind uint8
19
20 const (
21 allSet setKind = iota
22 subSet
23 superSet
24 noneSet
25 )
26
27 type tdSetBase struct {
28 baseOKNil
29 kind setKind
30 ignoreDups bool
31
32 expectedItems []reflect.Value
33 }
34
35 func newSetBase(kind setKind, ignoreDups bool, expectedItems []any) *tdSetBase {
36 return &tdSetBase{
37 baseOKNil: newBaseOKNil(4),
38 kind: kind,
39 ignoreDups: ignoreDups,
40 expectedItems: flat.Values(expectedItems),
41 }
42 }
43
44 func (s *tdSetBase) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {
45 switch got.Kind() {
46 case reflect.Ptr:
47 gotElem := got.Elem()
48 if !gotElem.IsValid() {
49 if ctx.BooleanError {
50 return ctxerr.BooleanError
51 }
52 return ctx.CollectError(ctxerr.NilPointer(got, "non-nil *slice OR *array"))
53 }
54
55 if gotElem.Kind() != reflect.Array && gotElem.Kind() != reflect.Slice {
56 break
57 }
58 got = gotElem
59 fallthrough
60
61 case reflect.Array, reflect.Slice:
62 var (
63 gotLen = got.Len()
64
65 foundItems []reflect.Value
66 missingItems []reflect.Value
67 foundGotIdxes = map[int]bool{}
68 )
69
70 for _, expected := range s.expectedItems {
71 found := false
72
73 for idx := 0; len(foundGotIdxes) < gotLen && idx < gotLen; idx++ {
74 if foundGotIdxes[idx] {
75 continue
76 }
77
78 ok, err := deepValueEqualFinalOK(ctx, got.Index(idx), expected)
79 if err != nil { // user error, stop asap
80 return err
81 }
82 if ok {
83 foundItems = append(foundItems, expected)
84
85 foundGotIdxes[idx] = true
86 found = true
87
88 if !s.ignoreDups {
89 break
90 }
91 }
92 }
93
94 if !found {
95 missingItems = append(missingItems, expected)
96 }
97 }
98
99 res := tdSetResult{
100 Kind: itemsSetResult,
101 Sort: true,
102 }
103
104 if s.kind != noneSet {
105 if s.kind != subSet {
106 // In Set* cases with missing items, try a second pass. Perhaps
107 // an already matching got item, matches another expected item?
108 if s.ignoreDups && len(missingItems) > 0 {
109 var newMissingItems []reflect.Value
110
111 nextExpected:
112 for _, expected := range missingItems {
113 for idxGot := range foundGotIdxes {
114 ok, _ := deepValueEqualFinalOK(ctx, got.Index(idxGot), expected)
115 if ok {
116 continue nextExpected
117 }
118 }
119
120 newMissingItems = append(newMissingItems, expected)
121 }
122
123 missingItems = newMissingItems
124 }
125
126 if len(missingItems) > 0 {
127 if ctx.BooleanError {
128 return ctxerr.BooleanError
129 }
130 res.Missing = missingItems
131 }
132 }
133
134 if len(foundGotIdxes) < gotLen && s.kind != superSet {
135 if ctx.BooleanError {
136 return ctxerr.BooleanError
137 }
138 notFoundRemain := gotLen - len(foundGotIdxes)
139 res.Extra = make([]reflect.Value, 0, notFoundRemain)
140 for idx := 0; notFoundRemain > 0; idx++ {
141 if !foundGotIdxes[idx] {
142 res.Extra = append(res.Extra, got.Index(idx))
143 notFoundRemain--
144 }
145 }
146 }
147 } else if len(foundItems) > 0 {
148 if ctx.BooleanError {
149 return ctxerr.BooleanError
150 }
151 res.Extra = foundItems
152 }
153
154 if res.IsEmpty() {
155 return nil
156 }
157 return ctx.CollectError(&ctxerr.Error{
158 Message: "comparing %% as a " + s.GetLocation().Func,
159 Summary: res.Summary(),
160 })
161 }
162
163 if ctx.BooleanError {
164 return ctxerr.BooleanError
165 }
166 return ctx.CollectError(ctxerr.BadKind(got, "slice OR array OR *slice OR *array"))
167 }
168
169 func (s *tdSetBase) String() string {
170 var b strings.Builder
171 b.WriteString(s.GetLocation().Func)
172 return util.SliceToString(&b, s.expectedItems).String()
173 }
174
175 func (s *tdSetBase) TypeBehind() reflect.Type {
176 typ := uniqTypeBehindSlice(s.expectedItems)
177 if typ == nil {
178 return nil
179 }
180 return reflect.SliceOf(typ)
181 }