Skip to content

Commit a706293

Browse files
use reflect magic to obtain FunctionalOption name
Co-authored-by: dillonstreator <dillonstreator@gmail.com>
1 parent 3b93fae commit a706293

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

mock/mock.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -668,23 +668,27 @@ func IsType(t interface{}) *IsTypeArgument {
668668
// FunctionalOptionsArgument is a struct that contains the type and value of an functional option argument
669669
// for use when type checking.
670670
type FunctionalOptionsArgument struct {
671-
name string
672671
value interface{}
673672
}
674673

675674
// String returns the string representation of FunctionalOptionsArgument
676675
func (f *FunctionalOptionsArgument) String() string {
677-
return strings.Replace(fmt.Sprintf("%#v", f.value), "[]interface {}", f.name, 1)
676+
var name string
677+
tValue := reflect.ValueOf(f.value)
678+
if tValue.Len() > 0 {
679+
name = "[]" + reflect.TypeOf(tValue.Index(0).Interface()).String()
680+
}
681+
682+
return strings.Replace(fmt.Sprintf("%#v", f.value), "[]interface {}", name, 1)
678683
}
679684

680685
// FunctionalOptions returns an FunctionalOptionsArgument object containing the functional option type
681686
// and the values to check of
682687
//
683688
// For example:
684689
// Assert(t, FunctionalOptions("[]foo.FunctionalOption", foo.Opt1(), foo.Opt2()))
685-
func FunctionalOptions(name string, value ...interface{}) *FunctionalOptionsArgument {
690+
func FunctionalOptions(value ...interface{}) *FunctionalOptionsArgument {
686691
return &FunctionalOptionsArgument{
687-
name: name,
688692
value: value,
689693
}
690694
}
@@ -829,10 +833,16 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
829833
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, reflect.TypeOf(t).Name(), reflect.TypeOf(actual).Name(), actualFmt)
830834
}
831835
} else if reflect.TypeOf(expected) == reflect.TypeOf((*FunctionalOptionsArgument)(nil)) {
832-
name := expected.(*FunctionalOptionsArgument).name
833836
t := expected.(*FunctionalOptionsArgument).value
837+
838+
var name string
839+
tValue := reflect.ValueOf(t)
840+
if tValue.Len() > 0 {
841+
name = "[]" + reflect.TypeOf(tValue.Index(0).Interface()).String()
842+
}
843+
834844
tName := reflect.TypeOf(t).Name()
835-
if name != reflect.TypeOf(actual).String() {
845+
if name != reflect.TypeOf(actual).String() && tValue.Len() != 0 {
836846
differences++
837847
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, tName, reflect.TypeOf(actual).Name(), actualFmt)
838848
} else {

mock/mock_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ func Test_Mock_AssertExpectationsFunctionalOptionsType(t *testing.T) {
10711071

10721072
var mockedService = new(TestExampleImplementation)
10731073

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

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

10851085
}
10861086

1087+
func Test_Mock_AssertExpectationsFunctionalOptionsType_Empty(t *testing.T) {
1088+
1089+
var mockedService = new(TestExampleImplementation)
1090+
1091+
mockedService.On("TheExampleMethodFunctionalOptions", "test", FunctionalOptions()).Return(nil).Once()
1092+
1093+
tt := new(testing.T)
1094+
assert.False(t, mockedService.AssertExpectations(tt))
1095+
1096+
// make the call now
1097+
mockedService.TheExampleMethodFunctionalOptions("test")
1098+
1099+
// now assert expectations
1100+
assert.True(t, mockedService.AssertExpectations(tt))
1101+
1102+
}
1103+
10871104
func Test_Mock_AssertExpectations_With_Repeatability(t *testing.T) {
10881105

10891106
var mockedService = new(TestExampleImplementation)

0 commit comments

Comments
 (0)