Skip to content

Commit

Permalink
Merge pull request #90 from zcolleen/master
Browse files Browse the repository at this point in the history
Added ExpectParams methods to template
  • Loading branch information
hexdigest authored Apr 20, 2024
2 parents 8a13ad2 + a075ec5 commit 6fbb32e
Show file tree
Hide file tree
Showing 18 changed files with 1,517 additions and 81 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ generate:
go run ./cmd/minimock/minimock.go -i ./tests.genericInlineUnion -o ./tests/generic_inline_union.go
go run ./cmd/minimock/minimock.go -i ./tests.contextAccepter -o ./tests/context_accepter_mock.go
go run ./cmd/minimock/minimock.go -i github.com/gojuno/minimock/v3.Tester -o ./tests/package_name_specified_test.go -p tests_test
go run ./cmd/minimock/minimock.go -i ./tests.actor -o ./tests/actor_mock.go

./bin:
mkdir ./bin
Expand Down
3 changes: 3 additions & 0 deletions cmd/minimock/minimock.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ var helpers = template.FuncMap{
}
return false
},
"inc": func(a int) int {
return a + 1
},
}

type (
Expand Down
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ module github.com/gojuno/minimock/v3

require (
github.com/davecgh/go-spew v1.1.1
github.com/hexdigest/gowrap v1.3.2
github.com/hexdigest/gowrap v1.3.6
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0
github.com/stretchr/testify v1.7.1
github.com/stretchr/testify v1.8.4
golang.org/x/tools v0.1.11-0.20220316014157-77aa08bb151a
google.golang.org/protobuf v1.30.0
)

require (
github.com/kr/text v0.2.0 // indirect
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

go 1.19
go 1.22

toolchain go1.22.0
24 changes: 12 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/hexdigest/gowrap v1.3.2 h1:ZDhDFhrbAHYRdt9ZnULKZyggC/3+W9EpfX6R8DjlggY=
github.com/hexdigest/gowrap v1.3.2/go.mod h1:g8N2jI4n9AKrf843erksNTrt4sdkG+TGVfhWe8dWrJQ=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hexdigest/gowrap v1.3.6 h1:nW91fC0tjW9X6MjittAK1EmI/9lAXibjqL4ebA6y8F8=
github.com/hexdigest/gowrap v1.3.6/go.mod h1:5KTYxPjK1RRfD+9L4Oo9gjP3XNAs4rkoVK2E7eAEFyM=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand All @@ -18,13 +19,13 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.1.11-0.20220316014157-77aa08bb151a h1:ofrrl6c6NG5/IOSx/R1cyiQxxjqlur0h/TvbUhkH0II=
golang.org/x/tools v0.1.11-0.20220316014157-77aa08bb151a/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand All @@ -36,6 +37,5 @@ google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
44 changes: 43 additions & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const (
type {{$mock}}{{$method.Name}}Expectation{{(params)}} struct {
mock *{{$mock}}{{(paramsRef)}}
{{ if $method.HasParams }} params *{{$mock}}{{$method.Name}}Params{{(paramsRef)}} {{end}}
{{ if $method.HasParams }} paramPtrs *{{$mock}}{{$method.Name}}ParamPtrs{{(paramsRef)}} {{end}}
{{ if $method.HasResults }} results *{{$mock}}{{$method.Name}}Results{{(paramsRef)}} {{end}}
Counter uint64
}
Expand All @@ -87,6 +88,11 @@ const (
type {{$mock}}{{$method.Name}}Params{{(params)}} {{$method.ParamsStruct}}
{{end}}
{{if $method.HasParams }}
// {{$mock}}{{$method.Name}}ParamPtrs contains pointers to parameters of the {{$.Interface.Name}}.{{$method.Name}}
type {{$mock}}{{$method.Name}}ParamPtrs{{(params)}} {{$method.ParamPtrsStruct}}
{{end}}
{{if $method.HasResults }}
// {{$mock}}{{$method.Name}}Results contains results of the {{$.Interface.Name}}.{{$method.Name}}
type {{$mock}}{{$method.Name}}Results{{(params)}} {{$method.ResultsStruct}}
Expand All @@ -103,6 +109,10 @@ const (
}
{{if $method.HasParams }}
if {{$m}}.defaultExpectation.paramPtrs != nil {
{{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by ExpectParams functions")
}
{{$m}}.defaultExpectation.params = &{{$mock}}{{$method.Name}}Params{{(paramsRef)}}{ {{ $method.ParamsNames }} }
for _, e := range {{$m}}.expectations {
if minimock.Equal(e.params, {{$m}}.defaultExpectation.params) {
Expand All @@ -113,6 +123,29 @@ const (
return {{$m}}
}
{{ range $idx, $param := $method.Params }}
// Expect{{ $param.Name | title }}Param{{$idx | inc}} sets up expected param {{ $param.Name }} for {{$.Interface.Name}}.{{$method.Name}}
func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Expect{{ $param.Name | title }}Param{{$idx | inc}}({{$param.Name}} {{$param.Type}}) *m{{$mock}}{{$method.Name}}{{(paramsRef)}} {
if {{$m}}.mock.func{{$method.Name}} != nil {
{{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Set")
}
if {{$m}}.defaultExpectation == nil {
{{$m}}.defaultExpectation = &{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}}{}
}
if {{$m}}.defaultExpectation.params != nil {
{{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Expect")
}
if {{$m}}.defaultExpectation.paramPtrs == nil {
{{$m}}.defaultExpectation.paramPtrs = &{{$mock}}{{$method.Name}}ParamPtrs{{(paramsRef)}}{}
}
{{$m}}.defaultExpectation.paramPtrs.{{$param.Name}} = &{{$param.Name}}
return {{$m}}
}
{{ end }}
// Inspect accepts an inspector function that has same arguments as the {{$.Interface.Name}}.{{$method.Name}}
func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Inspect(f func({{$method.Params}})) *m{{$mock}}{{$method.Name}}{{(paramsRef)}} {
if {{$m}}.mock.inspectFunc{{$method.Name}} != nil {
Expand Down Expand Up @@ -203,8 +236,17 @@ const (
mm_atomic.AddUint64(&{{$m}}.{{$method.Name}}Mock.defaultExpectation.Counter, 1)
{{- if $method.HasParams }}
mm_want := {{$m}}.{{$method.Name}}Mock.defaultExpectation.params
mm_want_ptrs := {{$m}}.{{$method.Name}}Mock.defaultExpectation.paramPtrs
mm_got := {{$mock}}{{$method.Name}}Params{{(paramsRef)}}{ {{$method.ParamsNames}} }
if mm_want != nil && !minimock.Equal(*mm_want, mm_got) {
if mm_want_ptrs != nil {
{{ range $idx, $param := $method.Params }}
if mm_want_ptrs.{{$param.Name}} != nil && !minimock.Equal(*mm_want_ptrs.{{$param.Name}}, mm_got.{{$param.Name}}) {
{{$m}}.t.Errorf("{{$mock}}.{{$method.Name}} got unexpected parameter {{$param.Name}}, want: %#v, got: %#v%s\n", *mm_want_ptrs.{{$param.Name}}, mm_got.{{$param.Name}}, minimock.Diff(*mm_want_ptrs.{{$param.Name}}, mm_got.{{$param.Name}}))
}
{{ end }}
} else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) {
{{$m}}.t.Errorf("{{$mock}}.{{$method.Name}} got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got))
}
{{ end }}
Expand Down
Loading

0 comments on commit 6fbb32e

Please sign in to comment.