Skip to content

Commit

Permalink
mock: simplify implementation of FunctionalOptions
Browse files Browse the repository at this point in the history
Remove unnecessary use of reflect in the implementation of
mock.FunctionalOptions().
  • Loading branch information
dolmen committed Apr 22, 2024
1 parent f32ff5b commit d4a63f5
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions mock/mock.go
Expand Up @@ -803,32 +803,31 @@ func IsType(t interface{}) *IsTypeArgument {
return &IsTypeArgument{t: reflect.TypeOf(t)}
}

// FunctionalOptionsArgument is a struct that contains the type and value of an functional option argument
// for use when type checking.
// FunctionalOptionsArgument contains a list of functional options arguments
// expected for use when matching a list of arguments.
type FunctionalOptionsArgument struct {
value interface{}
values []interface{}
}

// String returns the string representation of FunctionalOptionsArgument
func (f *FunctionalOptionsArgument) String() string {
var name string
tValue := reflect.ValueOf(f.value)
if tValue.Len() > 0 {
name = "[]" + reflect.TypeOf(tValue.Index(0).Interface()).String()
if len(f.values) > 0 {
name = "[]" + reflect.TypeOf(f.values[0]).String()
}

return strings.Replace(fmt.Sprintf("%#v", f.value), "[]interface {}", name, 1)
return strings.Replace(fmt.Sprintf("%#v", f.values), "[]interface {}", name, 1)
}

// FunctionalOptions returns an [FunctionalOptionsArgument] object containing
// the expected functional-options to check for.
//
// For example:
//
// Assert(t, FunctionalOptions(foo.Opt1("strValue"), foo.Opt2(613)))
func FunctionalOptions(value ...interface{}) *FunctionalOptionsArgument {
// args.Assert(t, FunctionalOptions(foo.Opt1("strValue"), foo.Opt2(613)))
func FunctionalOptions(values ...interface{}) *FunctionalOptionsArgument {
return &FunctionalOptionsArgument{
value: value,
values: values,
}
}

Expand Down Expand Up @@ -982,20 +981,17 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected.t.Name(), actualT.Name(), actualFmt)
}
case *FunctionalOptionsArgument:
t := expected.value

var name string
tValue := reflect.ValueOf(t)
if tValue.Len() > 0 {
name = "[]" + reflect.TypeOf(tValue.Index(0).Interface()).String()
if len(expected.values) > 0 {
name = "[]" + reflect.TypeOf(expected.values[0]).String()
}

tName := reflect.TypeOf(t).Name()
if name != reflect.TypeOf(actual).String() && tValue.Len() != 0 {
const tName = "[]interface{}"
if name != reflect.TypeOf(actual).String() && len(expected.values) != 0 {
differences++
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, tName, reflect.TypeOf(actual).Name(), actualFmt)
} else {
if ef, af := assertOpts(t, actual); ef == "" && af == "" {
if ef, af := assertOpts(expected.values, actual); ef == "" && af == "" {
// match
output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, tName, tName)
} else {
Expand Down

0 comments on commit d4a63f5

Please sign in to comment.