From c7fd6ba2115907ee279af9d11dd6fe19797d614f Mon Sep 17 00:00:00 2001 From: LandonTClipp <11232769+LandonTClipp@users.noreply.github.com> Date: Mon, 30 Dec 2024 14:09:39 -0600 Subject: [PATCH] More bug fixes to template --- .../mockery/v2/pkg/fixtures/mockery_mock.go | 336 +++++++----------- pkg/template/mockery.templ | 68 ++-- 2 files changed, 176 insertions(+), 228 deletions(-) diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/mockery_mock.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/mockery_mock.go index 12cd6eb4..24f12bd7 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/mockery_mock.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/mockery_mock.go @@ -35,122 +35,99 @@ type ExpecterAndRolledVariadic struct { // ManyArgsReturns provides a mock function for the type ExpecterAndRolledVariadic func (_mock *ExpecterAndRolledVariadic) ManyArgsReturns(str string, i int) ([]string, error) { - var retArgs mock.Arguments + ret := _mock.Called(str, i) - if retArgs == nil { - retArgs = _mock.Called(str, i) + if len(ret) == 0 { + panic("no return value specified for ManyArgsReturns") } - if len(retArgs) == 0 { - panic("no return value specified for ExpecterAndRolledVariadic.ManyArgsReturns") - } - - var ( - retVal0 []string - retVal1 error - ) - if returnFunc, ok := retArgs.Get(0).(func(string, int) ([]string, error)); ok { + var r0 []string + var r1 error + if returnFunc, ok := ret.Get(0).(func(string, int) ([]string, error)); ok { return returnFunc(str, i) } - if returnFunc, ok := retArgs.Get(0).(func(string, int) []string); ok { - retVal0 = returnFunc(str, i) + if returnFunc, ok := ret.Get(0).(func(string, int) []string); ok { + r0 = returnFunc(str, i) } else { - if retArgs.Get(0) != nil { - retVal0 = retArgs.Get(0).([]string) + if ret.Get(0) != nil { + r0 = ret.Get(0).([]string) } } - if returnFunc, ok := retArgs.Get(1).(func(string, int) error); ok { - retVal1 = returnFunc(str, i) + if returnFunc, ok := ret.Get(1).(func(string, int) error); ok { + r1 = returnFunc(str, i) } else { - retVal1 = retArgs.Error(1) + r1 = ret.Error(1) } - return retVal0, retVal1 + return r0, r1 } // NoArg provides a mock function for the type ExpecterAndRolledVariadic func (_mock *ExpecterAndRolledVariadic) NoArg() string { - var retArgs mock.Arguments - - if retArgs == nil { - retArgs = _mock.Called() - } + ret := _mock.Called() - if len(retArgs) == 0 { - panic("no return value specified for ExpecterAndRolledVariadic.NoArg") + if len(ret) == 0 { + panic("no return value specified for NoArg") } - var ( - retVal0 string - ) - if returnFunc, ok := retArgs.Get(0).(func() string); ok { - retVal0 = returnFunc() + var r0 string + if returnFunc, ok := ret.Get(0).(func() string); ok { + r0 = returnFunc() } else { - retVal0 = retArgs.Get(0).(string) + r0 = ret.Get(0).(string) } - return retVal0 + return r0 } // NoReturn provides a mock function for the type ExpecterAndRolledVariadic func (_mock *ExpecterAndRolledVariadic) NoReturn(str string) { - _mock.Called(str) return } // Variadic provides a mock function for the type ExpecterAndRolledVariadic func (_mock *ExpecterAndRolledVariadic) Variadic(ints ...int) error { - var retArgs mock.Arguments + var tmpRet mock.Arguments if len(ints) > 0 { - retArgs = _mock.Called(ints) + tmpRet = _mock.Called(ints) } else { - retArgs = _mock.Called() - } - - if retArgs == nil { - retArgs = _mock.Called(ints) + tmpRet = _mock.Called() } + ret := tmpRet - if len(retArgs) == 0 { - panic("no return value specified for ExpecterAndRolledVariadic.Variadic") + if len(ret) == 0 { + panic("no return value specified for Variadic") } - var ( - retVal0 error - ) - if returnFunc, ok := retArgs.Get(0).(func(...int) error); ok { - retVal0 = returnFunc(ints...) + var r0 error + if returnFunc, ok := ret.Get(0).(func(...int) error); ok { + r0 = returnFunc(ints...) } else { - retVal0 = retArgs.Error(0) + r0 = ret.Error(0) } - return retVal0 + return r0 } // VariadicMany provides a mock function for the type ExpecterAndRolledVariadic func (_mock *ExpecterAndRolledVariadic) VariadicMany(i int, a string, intfs ...interface{}) error { - var retArgs mock.Arguments + var tmpRet mock.Arguments if len(intfs) > 0 { - retArgs = _mock.Called(i, a, intfs) + tmpRet = _mock.Called(i, a, intfs) } else { - retArgs = _mock.Called(i, a) + tmpRet = _mock.Called(i, a) } + ret := tmpRet - if retArgs == nil { - retArgs = _mock.Called(i, a, intfs) + if len(ret) == 0 { + panic("no return value specified for VariadicMany") } - if len(retArgs) == 0 { - panic("no return value specified for ExpecterAndRolledVariadic.VariadicMany") - } - - var ( - retVal0 error - ) - if returnFunc, ok := retArgs.Get(0).(func(int, string, ...interface{}) error); ok { - retVal0 = returnFunc(i, a, intfs...) + var r0 error + if returnFunc, ok := ret.Get(0).(func(int, string, ...interface{}) error); ok { + r0 = returnFunc(i, a, intfs...) } else { - retVal0 = retArgs.Error(0) + r0 = ret.Error(0) } - return retVal0 + return r0 } type ExpecterAndRolledVariadic_expecter struct { @@ -182,114 +159,97 @@ type Expecter struct { // ManyArgsReturns provides a mock function for the type Expecter func (_mock *Expecter) ManyArgsReturns(str string, i int) ([]string, error) { - var retArgs mock.Arguments - - if retArgs == nil { - retArgs = _mock.Called(str, i) - } + ret := _mock.Called(str, i) - if len(retArgs) == 0 { - panic("no return value specified for Expecter.ManyArgsReturns") + if len(ret) == 0 { + panic("no return value specified for ManyArgsReturns") } - var ( - retVal0 []string - retVal1 error - ) - if returnFunc, ok := retArgs.Get(0).(func(string, int) ([]string, error)); ok { + var r0 []string + var r1 error + if returnFunc, ok := ret.Get(0).(func(string, int) ([]string, error)); ok { return returnFunc(str, i) } - if returnFunc, ok := retArgs.Get(0).(func(string, int) []string); ok { - retVal0 = returnFunc(str, i) + if returnFunc, ok := ret.Get(0).(func(string, int) []string); ok { + r0 = returnFunc(str, i) } else { - if retArgs.Get(0) != nil { - retVal0 = retArgs.Get(0).([]string) + if ret.Get(0) != nil { + r0 = ret.Get(0).([]string) } } - if returnFunc, ok := retArgs.Get(1).(func(string, int) error); ok { - retVal1 = returnFunc(str, i) + if returnFunc, ok := ret.Get(1).(func(string, int) error); ok { + r1 = returnFunc(str, i) } else { - retVal1 = retArgs.Error(1) + r1 = ret.Error(1) } - return retVal0, retVal1 + return r0, r1 } // NoArg provides a mock function for the type Expecter func (_mock *Expecter) NoArg() string { - var retArgs mock.Arguments + ret := _mock.Called() - if retArgs == nil { - retArgs = _mock.Called() + if len(ret) == 0 { + panic("no return value specified for NoArg") } - if len(retArgs) == 0 { - panic("no return value specified for Expecter.NoArg") - } - - var ( - retVal0 string - ) - if returnFunc, ok := retArgs.Get(0).(func() string); ok { - retVal0 = returnFunc() + var r0 string + if returnFunc, ok := ret.Get(0).(func() string); ok { + r0 = returnFunc() } else { - retVal0 = retArgs.Get(0).(string) + r0 = ret.Get(0).(string) } - return retVal0 + return r0 } // NoReturn provides a mock function for the type Expecter func (_mock *Expecter) NoReturn(str string) { - _mock.Called(str) return } // Variadic provides a mock function for the type Expecter func (_mock *Expecter) Variadic(ints ...int) error { - var retArgs mock.Arguments - /* TODO: finish https://github.com/vektra/mockery/blob/ede4f2c90f854f51c8618c934930f7bf6cdb5a5d/pkg/generator.go#L1109-L1153 */ - - if retArgs == nil { - retArgs = _mock.Called(ints...) + // int + _va := make([]interface{}, len(ints)) + for _i := range ints { + _va[_i] = ints[_i] } + var _ca []interface{} + _ca = append(_ca, _va...) + ret := _mock.Called(_ca...) - if len(retArgs) == 0 { - panic("no return value specified for Expecter.Variadic") + if len(ret) == 0 { + panic("no return value specified for Variadic") } - var ( - retVal0 error - ) - if returnFunc, ok := retArgs.Get(0).(func(...int) error); ok { - retVal0 = returnFunc(ints...) + var r0 error + if returnFunc, ok := ret.Get(0).(func(...int) error); ok { + r0 = returnFunc(ints...) } else { - retVal0 = retArgs.Error(0) + r0 = ret.Error(0) } - return retVal0 + return r0 } // VariadicMany provides a mock function for the type Expecter func (_mock *Expecter) VariadicMany(i int, a string, intfs ...interface{}) error { - var retArgs mock.Arguments - /* TODO: finish https://github.com/vektra/mockery/blob/ede4f2c90f854f51c8618c934930f7bf6cdb5a5d/pkg/generator.go#L1109-L1153 */ - - if retArgs == nil { - retArgs = _mock.Called(i, a, intfs...) - } + var _ca []interface{} + _ca = append(_ca, i, a) + _ca = append(_ca, intfs...) + ret := _mock.Called(_ca...) - if len(retArgs) == 0 { - panic("no return value specified for Expecter.VariadicMany") + if len(ret) == 0 { + panic("no return value specified for VariadicMany") } - var ( - retVal0 error - ) - if returnFunc, ok := retArgs.Get(0).(func(int, string, ...interface{}) error); ok { - retVal0 = returnFunc(i, a, intfs...) + var r0 error + if returnFunc, ok := ret.Get(0).(func(int, string, ...interface{}) error); ok { + r0 = returnFunc(i, a, intfs...) } else { - retVal0 = retArgs.Error(0) + r0 = ret.Error(0) } - return retVal0 + return r0 } type Expecter_expecter struct { @@ -329,90 +289,72 @@ type MockRequesterGenerics[TAny any, TComparable comparable, TSigned constraints func (_mock *MockRequesterGenerics[TAny, TComparable, TSigned, TIntf, TExternalIntf, TGenIntf, TInlineType, TInlineTypeGeneric]) GenericAnonymousStructs(val struct{ Type1 TExternalIntf }) struct { Type2 test.GenericType[string, test.EmbeddedGet[int]] } { - var retArgs mock.Arguments + ret := _mock.Called(val) - if retArgs == nil { - retArgs = _mock.Called(val) + if len(ret) == 0 { + panic("no return value specified for GenericAnonymousStructs") } - if len(retArgs) == 0 { - panic("no return value specified for MockRequesterGenerics.GenericAnonymousStructs") + var r0 struct { + Type2 test.GenericType[string, test.EmbeddedGet[int]] } - - var ( - retVal0 struct { - Type2 test.GenericType[string, test.EmbeddedGet[int]] - } - ) - if returnFunc, ok := retArgs.Get(0).(func(struct{ Type1 TExternalIntf }) struct { + if returnFunc, ok := ret.Get(0).(func(struct{ Type1 TExternalIntf }) struct { Type2 test.GenericType[string, test.EmbeddedGet[int]] }); ok { - retVal0 = returnFunc(val) + r0 = returnFunc(val) } else { - retVal0 = retArgs.Get(0).(struct { + r0 = ret.Get(0).(struct { Type2 test.GenericType[string, test.EmbeddedGet[int]] }) } - return retVal0 + return r0 } // GenericArguments provides a mock function for the type MockRequesterGenerics func (_mock *MockRequesterGenerics[TAny, TComparable, TSigned, TIntf, TExternalIntf, TGenIntf, TInlineType, TInlineTypeGeneric]) GenericArguments(v1 TAny, v2 TComparable) (TSigned, TIntf) { - var retArgs mock.Arguments + ret := _mock.Called(v1, v2) - if retArgs == nil { - retArgs = _mock.Called(v1, v2) + if len(ret) == 0 { + panic("no return value specified for GenericArguments") } - if len(retArgs) == 0 { - panic("no return value specified for MockRequesterGenerics.GenericArguments") - } - - var ( - retVal0 TSigned - retVal1 TIntf - ) - if returnFunc, ok := retArgs.Get(0).(func(TAny, TComparable) (TSigned, TIntf)); ok { + var r0 TSigned + var r1 TIntf + if returnFunc, ok := ret.Get(0).(func(TAny, TComparable) (TSigned, TIntf)); ok { return returnFunc(v1, v2) } - if returnFunc, ok := retArgs.Get(0).(func(TAny, TComparable) TSigned); ok { - retVal0 = returnFunc(v1, v2) + if returnFunc, ok := ret.Get(0).(func(TAny, TComparable) TSigned); ok { + r0 = returnFunc(v1, v2) } else { - if retArgs.Get(0) != nil { - retVal0 = retArgs.Get(0).(TSigned) + if ret.Get(0) != nil { + r0 = ret.Get(0).(TSigned) } } - if returnFunc, ok := retArgs.Get(1).(func(TAny, TComparable) TIntf); ok { - retVal1 = returnFunc(v1, v2) + if returnFunc, ok := ret.Get(1).(func(TAny, TComparable) TIntf); ok { + r1 = returnFunc(v1, v2) } else { - if retArgs.Get(1) != nil { - retVal1 = retArgs.Get(1).(TIntf) + if ret.Get(1) != nil { + r1 = ret.Get(1).(TIntf) } } - return retVal0, retVal1 + return r0, r1 } // GenericStructs provides a mock function for the type MockRequesterGenerics func (_mock *MockRequesterGenerics[TAny, TComparable, TSigned, TIntf, TExternalIntf, TGenIntf, TInlineType, TInlineTypeGeneric]) GenericStructs(genericType test.GenericType[TAny, TIntf]) test.GenericType[TSigned, TIntf] { - var retArgs mock.Arguments - - if retArgs == nil { - retArgs = _mock.Called(genericType) - } + ret := _mock.Called(genericType) - if len(retArgs) == 0 { - panic("no return value specified for MockRequesterGenerics.GenericStructs") + if len(ret) == 0 { + panic("no return value specified for GenericStructs") } - var ( - retVal0 test.GenericType[TSigned, TIntf] - ) - if returnFunc, ok := retArgs.Get(0).(func(test.GenericType[TAny, TIntf]) test.GenericType[TSigned, TIntf]); ok { - retVal0 = returnFunc(genericType) + var r0 test.GenericType[TSigned, TIntf] + if returnFunc, ok := ret.Get(0).(func(test.GenericType[TAny, TIntf]) test.GenericType[TSigned, TIntf]); ok { + r0 = returnFunc(genericType) } else { - retVal0 = retArgs.Get(0).(test.GenericType[TSigned, TIntf]) + r0 = ret.Get(0).(test.GenericType[TSigned, TIntf]) } - return retVal0 + return r0 } type MockRequesterGenerics_expecter[TAny any, TComparable comparable, TSigned constraints.Signed, TIntf test.GetInt, TExternalIntf io.Writer, TGenIntf test.GetGeneric[TSigned], TInlineType interface{ ~int | ~uint }, TInlineTypeGeneric interface { @@ -447,34 +389,28 @@ type MockA struct { // Call provides a mock function for the type MockA func (_mock *MockA) Call() (test.B, error) { - var retArgs mock.Arguments - - if retArgs == nil { - retArgs = _mock.Called() - } + ret := _mock.Called() - if len(retArgs) == 0 { - panic("no return value specified for MockA.Call") + if len(ret) == 0 { + panic("no return value specified for Call") } - var ( - retVal0 test.B - retVal1 error - ) - if returnFunc, ok := retArgs.Get(0).(func() (test.B, error)); ok { + var r0 test.B + var r1 error + if returnFunc, ok := ret.Get(0).(func() (test.B, error)); ok { return returnFunc() } - if returnFunc, ok := retArgs.Get(0).(func() test.B); ok { - retVal0 = returnFunc() + if returnFunc, ok := ret.Get(0).(func() test.B); ok { + r0 = returnFunc() } else { - retVal0 = retArgs.Get(0).(test.B) + r0 = ret.Get(0).(test.B) } - if returnFunc, ok := retArgs.Get(1).(func() error); ok { - retVal1 = returnFunc() + if returnFunc, ok := ret.Get(1).(func() error); ok { + r1 = returnFunc() } else { - retVal1 = retArgs.Error(1) + r1 = ret.Error(1) } - return retVal0, retVal1 + return r0, r1 } type MockA_expecter struct { diff --git a/pkg/template/mockery.templ b/pkg/template/mockery.templ index 67eeb794..9ebebbbb 100644 --- a/pkg/template/mockery.templ +++ b/pkg/template/mockery.templ @@ -1,7 +1,6 @@ {{ .Boilerplate }} // Code generated by mockery; DO NOT EDIT. // github.com/vektra/mockery - {{- if .BuildTags }} //go:build {{ .BuildTags }} @@ -41,9 +40,7 @@ type {{ .MockName }}{{ $mock | TypeConstraint }} struct { // {{ $method.Name }} provides a mock function for the type {{ $mock.MockName }} func (_mock *{{$mock.MockName}}{{ $mock | TypeInstantiation }}) {{$method.Name}}({{$method.ArgList}}) {{$method.ReturnArgTypeList}} { - {{- if ne (len $method.Returns) 0 }} - var retArgs mock.Arguments - {{- end }} + {{- $calledString := "" }} {{- if or (eq (len $method.ArgList) 0) (not $method.IsVariadic) @@ -53,58 +50,73 @@ func (_mock *{{$mock.MockName}}{{ $mock | TypeInstantiation }}) {{$method.Name}} ($method.IsVariadic) (not (index $mock.TemplateData "unroll-variadic")) }} + var tmpRet mock.Arguments {{- $lastParam := index $method.Params (len $method.Params | Add -1 )}} if len({{ $lastParam.Var.Name }}) > 0 { - retArgs = _mock.Called({{- if (index $mock.TemplateData "unroll-variadic") }}{{ $method.ArgCallList }}{{- else }}{{ $method.ArgCallListNoEllipsis }}{{- end }}) + tmpRet = _mock.Called({{- if (index $mock.TemplateData "unroll-variadic") }}{{ $method.ArgCallList }}{{- else }}{{ $method.ArgCallListNoEllipsis }}{{- end }}) } else { - retArgs = _mock.Called({{- if (index $mock.TemplateData "unroll-variadic") }}{{ $method.ArgCallListSlice 0 (len $method.Params | Add -1 )}}{{- else }}{{ $method.ArgCallListSliceNoEllipsis 0 (len $method.Params | Add -1 )}}{{- end }}) + tmpRet = _mock.Called({{- if (index $mock.TemplateData "unroll-variadic") }}{{ $method.ArgCallListSlice 0 (len $method.Params | Add -1 )}}{{- else }}{{ $method.ArgCallListSliceNoEllipsis 0 (len $method.Params | Add -1 )}}{{- end }}) } + {{- $calledString = "tmpRet" }} + {{- else }} + {{- $calledString = printf "_mock.Called(%s)" $method.ArgCallList }} {{- end }} {{- else }} - /* TODO: finish https://github.com/vektra/mockery/blob/ede4f2c90f854f51c8618c934930f7bf6cdb5a5d/pkg/generator.go#L1109-L1153 */ -{{- end }} -{{/* END PREAMBLE */}} + {{- $lastParam := (index $method.Params (len $method.Params | Add -1)) }} + {{- $variadicArgsName := $lastParam.Var.Name }} + {{- $strippedTypeString := TrimPrefix "..." $lastParam.TypeStringEllipsis }} + + {{- if and (ne $strippedTypeString "interface{}") (ne $strippedTypeString "any") }} + // {{ $strippedTypeString }} + _va := make([]interface{}, len({{- $lastParam.Var.Name }})) + for _i := range {{ $lastParam.Var.Name }} { + _va[_i] = {{ $lastParam.Var.Name }}[_i] + } + {{- $variadicArgsName = "_va" }} + {{- end }} + var _ca []interface{} + {{- if gt (len $method.Params) 1 }} + _ca = append(_ca, {{ $method.ArgCallListSlice 0 (len $method.Params | Add -1) }}) + {{- end }} + _ca = append(_ca, {{ $variadicArgsName }}...) + {{- $calledString = "_mock.Called(_ca...)" }} +{{- end }} {{/* END PREAMBLE */}} {{- if eq (len $method.Returns) 0 }} - _mock.Called({{- if (index $mock.TemplateData "unroll-variadic") }}{{ $method.ArgCallList }}{{- else }}{{ $method.ArgCallListNoEllipsis }}{{- end }}) + {{- $calledString }} {{- else }} - if retArgs == nil { - retArgs = _mock.Called({{- if (index $mock.TemplateData "unroll-variadic") }}{{ $method.ArgCallList }}{{- else }}{{ $method.ArgCallListNoEllipsis }}{{- end }}) - } - + ret := {{ $calledString }} - if len(retArgs) == 0 { - panic("no return value specified for {{$mock.MockName}}.{{$method.Name}}") + if len(ret) == 0 { + panic("no return value specified for {{$method.Name}}") } - var ( - {{- range $retIdx, $ret := $method.Returns }} - retVal{{ $retIdx }} {{ (index $method.Returns $retIdx).TypeString }} + {{ range $retIdx, $ret := $method.Returns }} + var r{{ $retIdx }} {{ (index $method.Returns $retIdx).TypeString }} {{- end }} - ) {{- if gt (len $method.Returns) 1 }} - if returnFunc, ok := retArgs.Get(0).(func({{ $method.ArgTypeList }}) {{ $method.ReturnArgTypeList }}); ok { + if returnFunc, ok := ret.Get(0).(func({{ $method.ArgTypeList }}) {{ $method.ReturnArgTypeList }}); ok { return returnFunc({{- if (index $mock.TemplateData "unroll-variadic") }}{{ $method.ArgCallList }}{{- else }}{{ $method.ArgCallListNoEllipsis }}{{- end }}) } {{- end }} {{- range $retIdx, $ret := $method.Returns }} {{/* START RETURN RANGE */}} - if returnFunc, ok := retArgs.Get({{ $retIdx }}).(func({{$method.ArgTypeListEllipsis }}) {{ (index $method.Returns $retIdx).TypeString }}); ok { - retVal{{ $retIdx }} = returnFunc({{ $method.ArgCallList }}) + if returnFunc, ok := ret.Get({{ $retIdx }}).(func({{$method.ArgTypeListEllipsis }}) {{ (index $method.Returns $retIdx).TypeString }}); ok { + r{{ $retIdx }} = returnFunc({{ $method.ArgCallList }}) } else { {{- if eq "error" (index $method.Returns $retIdx).TypeString }} - retVal{{ $retIdx }} = retArgs.Error({{ $retIdx }}) + r{{ $retIdx }} = ret.Error({{ $retIdx }}) {{- else if (index $method.Returns $retIdx).Var.Nillable }} - if retArgs.Get({{ $retIdx }}) != nil { - retVal{{ $retIdx }} = retArgs.Get({{ $retIdx }}).({{ (index $method.Returns $retIdx).TypeString }}) + if ret.Get({{ $retIdx }}) != nil { + r{{ $retIdx }} = ret.Get({{ $retIdx }}).({{ (index $method.Returns $retIdx).TypeString }}) } {{- else }} - retVal{{ $retIdx }} = retArgs.Get({{ $retIdx }}).({{ (index $method.Returns $retIdx).TypeString }}) + r{{ $retIdx }} = ret.Get({{ $retIdx }}).({{ (index $method.Returns $retIdx).TypeString }}) {{- end }} } {{- end }} {{/* END RETURN RANGE */}} {{- end }} - return {{ range $retIdx, $ret := $method.Returns }}retVal{{ $retIdx }}{{ if ne $retIdx (len $method.Returns | Add -1) }}, {{ end }}{{ end }} + return {{ range $retIdx, $ret := $method.Returns }}r{{ $retIdx }}{{ if ne $retIdx (len $method.Returns | Add -1) }}, {{ end }}{{ end }} } {{- end }} {{/* END METHOD RANGE */}}