Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
91 changes: 91 additions & 0 deletions mock/functional_options_nil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package mock

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// Diff with a FunctionalOptions expectation against a nil actual argument
// must report a difference instead of panicking on the nil type.
func Test_Arguments_Diff_FunctionalOptions_NilActual(t *testing.T) {
t.Parallel()

args := Arguments{FunctionalOptions(OpNum(1))}
diff, count := args.Diff([]interface{}{nil})

require.Equal(t, 1, count)
assert.Contains(t, diff, "type <nil>")
}

// Diff with an empty FunctionalOptions expectation against a nil actual
// argument must report a difference instead of panicking in assertOpts.
func Test_Arguments_Diff_FunctionalOptions_EmptyExpected_NilActual(t *testing.T) {
t.Parallel()

args := Arguments{FunctionalOptions()}
diff, count := args.Diff([]interface{}{nil})

require.Equal(t, 1, count)
assert.Contains(t, diff, "type <nil>")
}

// Diff with a nil option value in the expectation must report a
// difference instead of panicking when rendering the expected type.
func Test_Arguments_Diff_FunctionalOptions_NilExpectedValue(t *testing.T) {
t.Parallel()

args := Arguments{FunctionalOptions(nil)}
diff, count := args.Diff([]interface{}{OpNum(1)})

require.Equal(t, 1, count)
assert.Contains(t, diff, "FAIL")
}

// A FunctionalOptions holding a nil value must render without panicking.
func Test_FunctionalOptions_NilValue_String(t *testing.T) {
t.Parallel()

assert.Contains(t, FunctionalOptions(nil).String(), "<nil>")
}

// Full mock flow: expectation uses FunctionalOptions, the call passes nil.
// The call must fail as an unexpected call, not crash the test run.
func Test_Mock_FunctionalOptions_NilActual(t *testing.T) {
t.Parallel()

mockT := &MockTestingT{}
m := &Mock{}
m.Test(mockT)
m.On("Fn", FunctionalOptions(OpNum(1))).Return()

assert.PanicsWithValue(t, mockTestingTFailNowCalled, func() {
m.MethodCalled("Fn", nil)
})
assert.Equal(t, 1, mockT.errorfCount)
}

// Diff where the actual options slice holds a nil func element must
// report a difference instead of panicking in funcName/isFuncSame.
func Test_Arguments_Diff_FunctionalOptions_NilFuncElement(t *testing.T) {
t.Parallel()

args := Arguments{FunctionalOptions(OpNum(1))}
diff, count := args.Diff([]interface{}{[]OptionFn{nil}})

require.Equal(t, 1, count)
assert.Contains(t, diff, "FAIL")
}

// Diff where a nil sits among the expected option values must report a
// difference instead of panicking in runtimeFunc.
func Test_Arguments_Diff_FunctionalOptions_NilInExpectedValues(t *testing.T) {
t.Parallel()

args := Arguments{FunctionalOptions(OpNum(1), nil)}
diff, count := args.Diff([]interface{}{[]OptionFn{OpNum(1), OpNum(2)}})

require.Equal(t, 1, count)
assert.Contains(t, diff, "FAIL")
}
35 changes: 30 additions & 5 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ type FunctionalOptionsArgument struct {
func (f *FunctionalOptionsArgument) String() string {
var name string
if len(f.values) > 0 {
name = "[]" + reflect.TypeOf(f.values[0]).String()
name = "[]" + safeTypeString(reflect.TypeOf(f.values[0]))
}

return strings.Replace(fmt.Sprintf("%#v", f.values), "[]interface {}", name, 1)
Expand Down Expand Up @@ -1045,13 +1045,18 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
case *FunctionalOptionsArgument:
var name string
if len(expected.values) > 0 {
name = "[]" + reflect.TypeOf(expected.values[0]).String()
name = "[]" + safeTypeString(reflect.TypeOf(expected.values[0]))
}

const tName = "[]interface{}"
if name != reflect.TypeOf(actual).String() && len(expected.values) != 0 {
actualType := reflect.TypeOf(actual)
// A nil actual argument is never a match: it has no type
// to compare, and assertOpts below cannot inspect it
// (reflect.ValueOf(nil) has no Len). Report the
// difference instead of panicking on the nil type.
if actual == nil || (name != safeTypeString(actualType) && 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)
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, tName, safeTypeName(actualType), actualFmt)
} else {
if ef, af := assertOpts(expected.values, actual); ef == "" && af == "" {
// match
Expand Down Expand Up @@ -1178,6 +1183,16 @@ func safeTypeName(t reflect.Type) string {
return t.Name()
}

// safeTypeString returns the reflect.Type's string representation without
// causing a panic. If the provided reflect.Type is nil, it returns the
// placeholder string "<nil>"
func safeTypeString(t reflect.Type) string {
if t == nil {
return "<nil>"
}
return t.String()
}

func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) {
t := reflect.TypeOf(v)
k := t.Kind()
Expand Down Expand Up @@ -1312,10 +1327,17 @@ func assertOpts(expected, actual interface{}) (expectedFmt, actualFmt string) {
}

func runtimeFunc(opt interface{}) *runtime.Func {
return runtime.FuncForPC(reflect.ValueOf(opt).Pointer())
v := reflect.ValueOf(opt)
if !v.IsValid() {
return nil
}
return runtime.FuncForPC(v.Pointer())
}

func funcName(f *runtime.Func) string {
if f == nil {
return "<nil>"
}
name := f.Name()
trimmed := strings.TrimSuffix(path.Base(name), path.Ext(name))
splitted := strings.Split(trimmed, ".")
Expand All @@ -1328,6 +1350,9 @@ func funcName(f *runtime.Func) string {
}

func isFuncSame(f1, f2 *runtime.Func) bool {
if f1 == nil || f2 == nil {
return f1 == f2
}
f1File, f1Loc := f1.FileLine(f1.Entry())
f2File, f2Loc := f2.FileLine(f2.Entry())

Expand Down