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

support type-safe return values #11

Merged
merged 4 commits into from
Jul 6, 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ It supports the following flags:

- `-write_package_comment`: Writes package documentation comment (godoc) if true. (default true)

- `-typed`: Generate Type-safe 'Return', 'Do', 'DoAndReturn' function. (default false)

For an example of the use of `mockgen`, see the `sample/` directory. In simple
cases, you will need only the `-source` flag.

Expand Down
18 changes: 18 additions & 0 deletions mockgen/internal/tests/typed/bugreport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package typed

//go:generate mockgen -typed -aux_files faux=faux/faux.go -destination bugreport_mock.go -package typed -source=bugreport.go Example

import (
"log"

"go.uber.org/mock/mockgen/internal/tests/typed/faux"
)

// Source is an interface w/ an embedded foreign interface
type Source interface {
faux.Foreign
}

func CallForeignMethod(s Source) {
log.Println(s.Method())
}
111 changes: 111 additions & 0 deletions mockgen/internal/tests/typed/bugreport_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions mockgen/internal/tests/typed/bugreport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package typed

import (
"testing"

"go.uber.org/mock/gomock"
)

// TestValidInterface assesses whether or not the generated mock is valid
func TestValidInterface(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

s := NewMockSource(ctrl)
s.EXPECT().Method().Return("")

CallForeignMethod(s)
}
21 changes: 21 additions & 0 deletions mockgen/internal/tests/typed/external.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package typed

import (
"go.uber.org/mock/mockgen/internal/tests/typed/other"
"golang.org/x/exp/constraints"
)

//go:generate mockgen --source=external.go --destination=source/mock_external_test.go --package source -typed

type ExternalConstraint[I constraints.Integer, F constraints.Float] interface {
One(string) string
Two(I) string
Three(I) F
Four(I) Foo[I, F]
Five(I) Baz[F]
Six(I) *Baz[F]
Seven(I) other.One[I]
Eight(F) other.Two[I, F]
Nine(Iface[I])
Ten(*I)
}
11 changes: 11 additions & 0 deletions mockgen/internal/tests/typed/faux/faux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package faux

type Foreign interface {
Method() Return
Embedded
error
}

type Embedded interface{}

type Return interface{}
40 changes: 40 additions & 0 deletions mockgen/internal/tests/typed/generics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package typed

import "go.uber.org/mock/mockgen/internal/tests/typed/other"

//go:generate mockgen --source=generics.go --destination=source/mock_generics_test.go --package source -typed
////go:generate mockgen --destination=reflect/mock_test.go --package reflect . Bar,Bar2

type Bar[T any, R any] interface {
One(string) string
Two(T) string
Three(T) R
Four(T) Foo[T, R]
Five(T) Baz[T]
Six(T) *Baz[T]
Seven(T) other.One[T]
Eight(T) other.Two[T, R]
Nine(Iface[T])
Ten(*T)
Eleven() (*other.One[T], error)
Twelve() (*other.Two[T, R], error)
Thirteen() (Baz[StructType], error)
Fourteen() (*Foo[StructType, StructType2], error)
Fifteen() (Iface[StructType], error)
Sixteen() (Baz[other.Three], error)
Seventeen() (*Foo[other.Three, other.Four], error)
Eighteen() (Iface[*other.Five], error)
Nineteen() AliasType
}

type Foo[T any, R any] struct{}

type Baz[T any] struct{}

type Iface[T any] interface{}

type StructType struct{}

type StructType2 struct{}

type AliasType Baz[other.Three]
10 changes: 10 additions & 0 deletions mockgen/internal/tests/typed/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module go.uber.org/mock/mockgen/internal/tests/typed

go 1.18

replace go.uber.org/mock => ../../../..

require (
go.uber.org/mock v0.0.0-00010101000000-000000000000
golang.org/x/exp v0.0.0-20220609121020-a51bd0440498
)
26 changes: 26 additions & 0 deletions mockgen/internal/tests/typed/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20220609121020-a51bd0440498 h1:TF0FvLUGEq/8wOt/9AV1nj6D4ViZGUIGCMQfCv7VRXY=
golang.org/x/exp v0.0.0-20220609121020-a51bd0440498/go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
11 changes: 11 additions & 0 deletions mockgen/internal/tests/typed/other/other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package other

type One[T any] struct{}

type Two[T any, R any] struct{}

type Three struct{}

type Four struct{}

type Five interface{}
Loading