Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mock: allow testing for functional options #1023

Merged
merged 3 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
use reflect magic to obtain FunctionalOption name
Co-authored-by: dillonstreator <dillonstreator@gmail.com>
  • Loading branch information
nbaztec and dillonstreator authored Jan 17, 2023
commit a706293304275448e7eeca780f9617e95018f2d9
22 changes: 16 additions & 6 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,23 +668,27 @@ func IsType(t interface{}) *IsTypeArgument {
// FunctionalOptionsArgument is a struct that contains the type and value of an functional option argument
// for use when type checking.
type FunctionalOptionsArgument struct {
name string
value interface{}
}

// String returns the string representation of FunctionalOptionsArgument
func (f *FunctionalOptionsArgument) String() string {
return strings.Replace(fmt.Sprintf("%#v", f.value), "[]interface {}", f.name, 1)
var name string
tValue := reflect.ValueOf(f.value)
if tValue.Len() > 0 {
name = "[]" + reflect.TypeOf(tValue.Index(0).Interface()).String()
}

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

// FunctionalOptions returns an FunctionalOptionsArgument object containing the functional option type
// and the values to check of
//
// For example:
// Assert(t, FunctionalOptions("[]foo.FunctionalOption", foo.Opt1(), foo.Opt2()))
func FunctionalOptions(name string, value ...interface{}) *FunctionalOptionsArgument {
func FunctionalOptions(value ...interface{}) *FunctionalOptionsArgument {
return &FunctionalOptionsArgument{
name: name,
value: value,
}
}
Expand Down Expand Up @@ -829,10 +833,16 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, reflect.TypeOf(t).Name(), reflect.TypeOf(actual).Name(), actualFmt)
}
} else if reflect.TypeOf(expected) == reflect.TypeOf((*FunctionalOptionsArgument)(nil)) {
name := expected.(*FunctionalOptionsArgument).name
t := expected.(*FunctionalOptionsArgument).value

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

tName := reflect.TypeOf(t).Name()
if name != reflect.TypeOf(actual).String() {
if name != reflect.TypeOf(actual).String() && tValue.Len() != 0 {
differences++
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, tName, reflect.TypeOf(actual).Name(), actualFmt)
} else {
Expand Down
19 changes: 18 additions & 1 deletion mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ func Test_Mock_AssertExpectationsFunctionalOptionsType(t *testing.T) {

var mockedService = new(TestExampleImplementation)

mockedService.On("TheExampleMethodFunctionalOptions", "test", FunctionalOptions("[]mock.OptionFn", OpNum(1), OpStr("foo"))).Return(nil).Once()
mockedService.On("TheExampleMethodFunctionalOptions", "test", FunctionalOptions(OpNum(1), OpStr("foo"))).Return(nil).Once()

tt := new(testing.T)
assert.False(t, mockedService.AssertExpectations(tt))
Expand All @@ -1084,6 +1084,23 @@ func Test_Mock_AssertExpectationsFunctionalOptionsType(t *testing.T) {

}

nbaztec marked this conversation as resolved.
Show resolved Hide resolved
func Test_Mock_AssertExpectationsFunctionalOptionsType_Empty(t *testing.T) {

var mockedService = new(TestExampleImplementation)

mockedService.On("TheExampleMethodFunctionalOptions", "test", FunctionalOptions()).Return(nil).Once()

tt := new(testing.T)
assert.False(t, mockedService.AssertExpectations(tt))

// make the call now
mockedService.TheExampleMethodFunctionalOptions("test")

// now assert expectations
assert.True(t, mockedService.AssertExpectations(tt))

}

func Test_Mock_AssertExpectations_With_Repeatability(t *testing.T) {

var mockedService = new(TestExampleImplementation)
Expand Down