-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Contains rework to handle maps, arrays and slices
Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
- Loading branch information
Showing
4 changed files
with
113 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright (c) 2018, 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 testdeep | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
type tdContains struct { | ||
Base | ||
expectedValue reflect.Value | ||
} | ||
|
||
var _ TestDeep = &tdContains{} | ||
|
||
// Contains operator allows to check the presence of a string (or | ||
// convertible), error or fmt.Stringer interface (error interface is | ||
// tested before fmt.Stringer.) | ||
// | ||
// type Foobar string | ||
// CmpDeeply(t, Foobar("foobar"), Contains("ooba")) // succeeds | ||
// | ||
// err := errors.New("error!") | ||
// CmpDeeply(t, err, Contains("ror")) // succeeds | ||
// | ||
// bstr := bytes.NewBufferString("fmt.Stringer!") | ||
// CmpDeeply(t, bstr, Contains("String")) // succeeds | ||
func Contains(expectedValue interface{}) TestDeep { | ||
return &tdContains{ | ||
Base: NewBase(3), | ||
expectedValue: reflect.ValueOf(expectedValue), | ||
} | ||
} | ||
|
||
func (t *tdContains) doesNotContains(ctx Context, got interface{}) *Error { | ||
if ctx.booleanError { | ||
return booleanError | ||
} | ||
return ctx.CollectError(&Error{ | ||
Message: "does not contain", | ||
Got: got, | ||
Expected: t, | ||
}) | ||
} | ||
|
||
func (t *tdContains) Match(ctx Context, got reflect.Value) *Error { | ||
switch got.Kind() { | ||
// case reflect.Ptr: | ||
case reflect.Array, reflect.Slice: | ||
for index := got.Len() - 1; index >= 0; index-- { | ||
if deepValueEqualOK(got.Index(index), t.expectedValue) { | ||
return nil | ||
} | ||
} | ||
return t.doesNotContains(ctx, got) | ||
|
||
case reflect.Map: | ||
for _, vkey := range got.MapKeys() { | ||
if deepValueEqualOK(got.MapIndex(vkey), t.expectedValue) { | ||
return nil | ||
} | ||
} | ||
return t.doesNotContains(ctx, got) | ||
} | ||
|
||
switch expectedKind := t.expectedValue.Kind(); expectedKind { | ||
case reflect.String, reflect.Int32, reflect.Uint8: // string, rune & byte | ||
str, err := getString(ctx, got) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
switch expectedKind { | ||
case reflect.String: | ||
if strings.Contains(str, t.expectedValue.String()) { | ||
return nil | ||
} | ||
case reflect.Int32: | ||
if strings.ContainsRune(str, rune(t.expectedValue.Int())) { | ||
return nil | ||
} | ||
default: // = case reflect.Uint8: | ||
if strings.IndexByte(str, byte(t.expectedValue.Uint())) >= 0 { | ||
return nil | ||
} | ||
} | ||
return t.doesNotContains(ctx, str) | ||
} | ||
|
||
if ctx.booleanError { | ||
return booleanError | ||
} | ||
return ctx.CollectError(&Error{ | ||
Message: "cannot check contains", | ||
Got: got, | ||
Expected: t.expectedValue, | ||
}) | ||
} | ||
|
||
func (t *tdContains) String() string { | ||
return "Contains(" + toString(t.expectedValue) + ")" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters