| 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 |
"bytes" |
| 11 |
"reflect" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"github.com/maxatome/go-testdeep/helpers/tdutil" |
| 15 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 16 |
"github.com/maxatome/go-testdeep/internal/types" |
| 17 |
"github.com/maxatome/go-testdeep/internal/util" |
| 18 |
) |
| 19 |
|
| 20 |
type tdContains struct { |
| 21 |
tdSmugglerBase |
| 22 |
} |
| 23 |
|
| 24 |
var _ TestDeep = &tdContains{} |
| 25 |
|
| 26 |
// summary(Contains): checks that a string, []byte, error or |
| 27 |
// fmt.Stringer interfaces contain a rune, byte or a sub-string; or a |
| 28 |
// slice contains a single value or a sub-slice; or an array or map |
| 29 |
// contain a single value |
| 30 |
// input(Contains): str,array,slice,map,if(✓ + fmt.Stringer/error) |
| 31 |
|
| 32 |
// Contains is a smuggler operator to check if something is contained |
| 33 |
// in another thing. Contains has to be applied on arrays, slices, maps or |
| 34 |
// strings. It tries to be as smarter as possible. |
| 35 |
// |
| 36 |
// If expectedValue is a [TestDeep] operator, each item of data |
| 37 |
// array/slice/map/string (rune for strings) is compared to it. The |
| 38 |
// use of a [TestDeep] operator as expectedValue works only in this |
| 39 |
// way: item per item. |
| 40 |
// |
| 41 |
// If data is a slice, and expectedValue has the same type, then |
| 42 |
// expectedValue is searched as a sub-slice, otherwise |
| 43 |
// expectedValue is compared to each slice value. |
| 44 |
// |
| 45 |
// list := []int{12, 34, 28} |
| 46 |
// td.Cmp(t, list, td.Contains(34)) // succeeds |
| 47 |
// td.Cmp(t, list, td.Contains(td.Between(30, 35))) // succeeds too |
| 48 |
// td.Cmp(t, list, td.Contains(35)) // fails |
| 49 |
// td.Cmp(t, list, td.Contains([]int{34, 28})) // succeeds |
| 50 |
// |
| 51 |
// If data is an array or a map, each value is compared to |
| 52 |
// expectedValue. Map keys are not checked: see [ContainsKey] to check |
| 53 |
// map keys existence. |
| 54 |
// |
| 55 |
// hash := map[string]int{"foo": 12, "bar": 34, "zip": 28} |
| 56 |
// td.Cmp(t, hash, td.Contains(34)) // succeeds |
| 57 |
// td.Cmp(t, hash, td.Contains(td.Between(30, 35))) // succeeds too |
| 58 |
// td.Cmp(t, hash, td.Contains(35)) // fails |
| 59 |
// |
| 60 |
// array := [...]int{12, 34, 28} |
| 61 |
// td.Cmp(t, array, td.Contains(34)) // succeeds |
| 62 |
// td.Cmp(t, array, td.Contains(td.Between(30, 35))) // succeeds too |
| 63 |
// td.Cmp(t, array, td.Contains(35)) // fails |
| 64 |
// |
| 65 |
// If data is a string (or convertible), []byte (or convertible), |
| 66 |
// error or [fmt.Stringer] interface (error interface is tested before |
| 67 |
// [fmt.Stringer]), expectedValue can be a string, a []byte, a rune or |
| 68 |
// a byte. In this case, it tests if the got string contains this |
| 69 |
// expected string, []byte, rune or byte. |
| 70 |
// |
| 71 |
// got := "foo bar" |
| 72 |
// td.Cmp(t, got, td.Contains('o')) // succeeds |
| 73 |
// td.Cmp(t, got, td.Contains(rune('o'))) // succeeds |
| 74 |
// td.Cmp(t, got, td.Contains(td.Between('n', 'p'))) // succeeds |
| 75 |
// td.Cmp(t, got, td.Contains("bar")) // succeeds |
| 76 |
// td.Cmp(t, got, td.Contains([]byte("bar"))) // succeeds |
| 77 |
// |
| 78 |
// td.Cmp(t, []byte("foobar"), td.Contains("ooba")) // succeeds |
| 79 |
// |
| 80 |
// type Foobar string |
| 81 |
// td.Cmp(t, Foobar("foobar"), td.Contains("ooba")) // succeeds |
| 82 |
// |
| 83 |
// err := errors.New("error!") |
| 84 |
// td.Cmp(t, err, td.Contains("ror")) // succeeds |
| 85 |
// |
| 86 |
// bstr := bytes.NewBufferString("fmt.Stringer!") |
| 87 |
// td.Cmp(t, bstr, td.Contains("String")) // succeeds |
| 88 |
// |
| 89 |
// Pitfall: if you want to check if 2 words are contained in got, don't do: |
| 90 |
// |
| 91 |
// td.Cmp(t, "foobar", td.Contains(td.All("foo", "bar"))) // Bad! |
| 92 |
// |
| 93 |
// as [TestDeep] operator [All] in Contains operates on each rune, so it |
| 94 |
// does not work as expected, but do:: |
| 95 |
// |
| 96 |
// td.Cmp(t, "foobar", td.All(td.Contains("foo"), td.Contains("bar"))) |
| 97 |
// |
| 98 |
// When Contains(nil) is used, nil is automatically converted to a |
| 99 |
// typed nil on the fly to avoid confusion (if the array/slice/map |
| 100 |
// item type allows it of course.) So all following [Cmp] calls |
| 101 |
// are equivalent (except the (*byte)(nil) one): |
| 102 |
// |
| 103 |
// num := 123 |
| 104 |
// list := []*int{&num, nil} |
| 105 |
// td.Cmp(t, list, td.Contains(nil)) // succeeds → (*int)(nil) |
| 106 |
// td.Cmp(t, list, td.Contains((*int)(nil))) // succeeds |
| 107 |
// td.Cmp(t, list, td.Contains(td.Nil())) // succeeds |
| 108 |
// // But... |
| 109 |
// td.Cmp(t, list, td.Contains((*byte)(nil))) // fails: (*byte)(nil) ≠ (*int)(nil) |
| 110 |
// |
| 111 |
// As well as these ones: |
| 112 |
// |
| 113 |
// hash := map[string]*int{"foo": nil, "bar": &num} |
| 114 |
// td.Cmp(t, hash, td.Contains(nil)) // succeeds → (*int)(nil) |
| 115 |
// td.Cmp(t, hash, td.Contains((*int)(nil))) // succeeds |
| 116 |
// td.Cmp(t, hash, td.Contains(td.Nil())) // succeeds |
| 117 |
// |
| 118 |
// See also [ContainsKey]. |
| 119 |
func Contains(expectedValue any) TestDeep { |
| 120 |
c := tdContains{ |
| 121 |
tdSmugglerBase: newSmugglerBase(expectedValue), |
| 122 |
} |
| 123 |
|
| 124 |
if !c.isTestDeeper { |
| 125 |
c.expectedValue = reflect.ValueOf(expectedValue) |
| 126 |
} |
| 127 |
return &c |
| 128 |
} |
| 129 |
|
| 130 |
func (c *tdContains) doesNotContainErr(ctx ctxerr.Context, got any) *ctxerr.Error { |
| 131 |
if ctx.BooleanError { |
| 132 |
return ctxerr.BooleanError |
| 133 |
} |
| 134 |
return ctx.CollectError(&ctxerr.Error{ |
| 135 |
Message: "does not contain", |
| 136 |
Got: got, |
| 137 |
Expected: c, |
| 138 |
}) |
| 139 |
} |
| 140 |
|
| 141 |
// getExpectedValue returns the expected value handling the |
| 142 |
// Contains(nil) case: in this case it returns a typed nil (same type |
| 143 |
// as the items of got). |
| 144 |
// got is an array, a slice or a map (it's the caller responsibility to check). |
| 145 |
func (c *tdContains) getExpectedValue(got reflect.Value) reflect.Value { |
| 146 |
// If the expectValue is non-typed nil |
| 147 |
if !c.expectedValue.IsValid() { |
| 148 |
// AND the kind of items in got is... |
| 149 |
switch got.Type().Elem().Kind() { |
| 150 |
case reflect.Chan, reflect.Func, reflect.Interface, |
| 151 |
reflect.Map, reflect.Ptr, reflect.Slice: |
| 152 |
// returns a typed nil |
| 153 |
return reflect.Zero(got.Type().Elem()) |
| 154 |
} |
| 155 |
} |
| 156 |
return c.expectedValue |
| 157 |
} |
| 158 |
|
| 159 |
func (c *tdContains) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 160 |
switch got.Kind() { |
| 161 |
case reflect.Slice: |
| 162 |
if !c.isTestDeeper && c.expectedValue.IsValid() { |
| 163 |
// Special case for []byte & expected []byte or string |
| 164 |
if got.Type().Elem() == types.Uint8 { |
| 165 |
switch c.expectedValue.Kind() { |
| 166 |
case reflect.String: |
| 167 |
if bytes.Contains(got.Bytes(), []byte(c.expectedValue.String())) { |
| 168 |
return nil |
| 169 |
} |
| 170 |
return c.doesNotContainErr(ctx, got) |
| 171 |
|
| 172 |
case reflect.Slice: |
| 173 |
if c.expectedValue.Type().Elem() == types.Uint8 { |
| 174 |
if bytes.Contains(got.Bytes(), c.expectedValue.Bytes()) { |
| 175 |
return nil |
| 176 |
} |
| 177 |
return c.doesNotContainErr(ctx, got) |
| 178 |
} |
| 179 |
|
| 180 |
case reflect.Int32: // rune |
| 181 |
if bytes.ContainsRune(got.Bytes(), rune(c.expectedValue.Int())) { |
| 182 |
return nil |
| 183 |
} |
| 184 |
return c.doesNotContainErr(ctx, got) |
| 185 |
|
| 186 |
case reflect.Uint8: // byte |
| 187 |
if bytes.ContainsRune(got.Bytes(), rune(c.expectedValue.Uint())) { |
| 188 |
return nil |
| 189 |
} |
| 190 |
return c.doesNotContainErr(ctx, got) |
| 191 |
} |
| 192 |
|
| 193 |
// fall back on string conversion |
| 194 |
break |
| 195 |
} |
| 196 |
|
| 197 |
// Search slice in slice |
| 198 |
if got.Type() == c.expectedValue.Type() { |
| 199 |
gotLen, expectedLen := got.Len(), c.expectedValue.Len() |
| 200 |
if expectedLen == 0 { |
| 201 |
return nil |
| 202 |
} |
| 203 |
if expectedLen > gotLen { |
| 204 |
return c.doesNotContainErr(ctx, got) |
| 205 |
} |
| 206 |
for i := 0; i <= gotLen-expectedLen; i++ { |
| 207 |
ok, err := deepValueEqualFinalOK(ctx, got.Slice(i, i+expectedLen), c.expectedValue) |
| 208 |
if err != nil || ok { |
| 209 |
return err |
| 210 |
} |
| 211 |
} |
| 212 |
return c.doesNotContainErr(ctx, got) |
| 213 |
} |
| 214 |
} |
| 215 |
fallthrough |
| 216 |
|
| 217 |
case reflect.Array: |
| 218 |
expectedValue := c.getExpectedValue(got) |
| 219 |
for index := got.Len() - 1; index >= 0; index-- { |
| 220 |
ok, err := deepValueEqualFinalOK(ctx, got.Index(index), expectedValue) |
| 221 |
if err != nil || ok { |
| 222 |
return err |
| 223 |
} |
| 224 |
} |
| 225 |
return c.doesNotContainErr(ctx, got) |
| 226 |
|
| 227 |
case reflect.Map: |
| 228 |
expectedValue := c.getExpectedValue(got) |
| 229 |
var err *ctxerr.Error |
| 230 |
var ok bool |
| 231 |
if !tdutil.MapEachValue(got, func(v reflect.Value) bool { |
| 232 |
ok, err = deepValueEqualFinalOK(ctx, v, expectedValue) |
| 233 |
return err == nil && !ok |
| 234 |
}) { |
| 235 |
return err |
| 236 |
} |
| 237 |
return c.doesNotContainErr(ctx, got) |
| 238 |
} |
| 239 |
|
| 240 |
str, err := getString(ctx, got) |
| 241 |
if err != nil { |
| 242 |
return ctx.CollectError(err) |
| 243 |
} |
| 244 |
|
| 245 |
// If a TestDeep operator is expected, applies this operator on |
| 246 |
// each character of the string |
| 247 |
if c.isTestDeeper { |
| 248 |
// If the type behind the operator is known *and* is not rune, |
| 249 |
// then no need to go further, but return an explicit error to |
| 250 |
// help our user to fix his probably bogus code |
| 251 |
op := c.expectedValue.Interface().(TestDeep) |
| 252 |
if typeBehind := op.TypeBehind(); typeBehind != nil && typeBehind != types.Rune && !ctx.BeLax { |
| 253 |
if ctx.BooleanError { |
| 254 |
return ctxerr.BooleanError |
| 255 |
} |
| 256 |
return ctx.CollectError(&ctxerr.Error{ |
| 257 |
Message: op.GetLocation().Func + " operator has to match rune in string, but it does not", |
| 258 |
Got: types.RawString(typeBehind.String()), |
| 259 |
Expected: types.RawString("rune"), |
| 260 |
}) |
| 261 |
} |
| 262 |
|
| 263 |
for _, chr := range str { |
| 264 |
ok, err := deepValueEqualFinalOK(ctx, reflect.ValueOf(chr), c.expectedValue) |
| 265 |
if err != nil || ok { |
| 266 |
return err |
| 267 |
} |
| 268 |
} |
| 269 |
return c.doesNotContainErr(ctx, got) |
| 270 |
} |
| 271 |
|
| 272 |
// If expectedValue is a []byte, a string, a rune or a byte, we |
| 273 |
// check whether it is contained in the string or not |
| 274 |
var contains bool |
| 275 |
switch expectedKind := c.expectedValue.Kind(); expectedKind { |
| 276 |
case reflect.String: |
| 277 |
contains = strings.Contains(str, c.expectedValue.String()) |
| 278 |
|
| 279 |
case reflect.Int32: // rune |
| 280 |
contains = strings.ContainsRune(str, rune(c.expectedValue.Int())) |
| 281 |
|
| 282 |
case reflect.Uint8: // byte |
| 283 |
contains = strings.ContainsRune(str, rune(c.expectedValue.Uint())) |
| 284 |
|
| 285 |
case reflect.Slice: |
| 286 |
// Only []byte |
| 287 |
if c.expectedValue.Type().Elem() == types.Uint8 { |
| 288 |
contains = strings.Contains(str, string(c.expectedValue.Bytes())) |
| 289 |
break |
| 290 |
} |
| 291 |
fallthrough |
| 292 |
|
| 293 |
default: |
| 294 |
if ctx.BooleanError { |
| 295 |
return ctxerr.BooleanError |
| 296 |
} |
| 297 |
var expectedType any |
| 298 |
if c.expectedValue.IsValid() { |
| 299 |
expectedType = types.RawString(c.expectedValue.Type().String()) |
| 300 |
} else { |
| 301 |
expectedType = c |
| 302 |
} |
| 303 |
|
| 304 |
return ctx.CollectError(&ctxerr.Error{ |
| 305 |
Message: "cannot check contains", |
| 306 |
Got: types.RawString(got.Type().String()), |
| 307 |
Expected: expectedType, |
| 308 |
}) |
| 309 |
} |
| 310 |
|
| 311 |
if contains { |
| 312 |
return nil |
| 313 |
} |
| 314 |
return c.doesNotContainErr(ctx, str) |
| 315 |
} |
| 316 |
|
| 317 |
func (c *tdContains) String() string { |
| 318 |
return "Contains(" + util.ToString(c.expectedValue) + ")" |
| 319 |
} |