Skip to content

Commit bef0e41

Browse files
committed
Added support for mock generation for function signatures
1 parent 6f71594 commit bef0e41

File tree

8 files changed

+1035
-24
lines changed

8 files changed

+1035
-24
lines changed

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ mockgen -archive=pkg.a database/sql/driver Conn,Driver
6060

6161
### Source mode
6262

63-
Source mode generates mock interfaces from a source file.
63+
Source mode generates mock interfaces and signatures from a source file.
6464
It is enabled by using the -source flag. Other flags that
6565
may be useful in this mode are -imports and -aux_files.
6666

@@ -72,7 +72,7 @@ mockgen -source=foo.go [other options]
7272

7373
### Package mode
7474

75-
Package mode works by specifying the package and interface names.
75+
Package mode works by specifying the package and interface or signature names.
7676
It is enabled by passing two non-flag arguments: an import path, and a
7777
comma-separated list of symbols.
7878

@@ -85,6 +85,9 @@ mockgen database/sql/driver Conn,Driver
8585

8686
# Convenient for `go:generate`.
8787
mockgen . Conn,Driver
88+
89+
# Function signatures can also be mocked
90+
mockgen iter Seq,Seq2
8891
```
8992

9093
### Flags
@@ -151,6 +154,8 @@ cases, you will need only the `-source` flag.
151154

152155
## Building Mocks
153156

157+
## Interfaces
158+
154159
```go
155160
type Foo interface {
156161
Bar(x int) int
@@ -179,6 +184,38 @@ func TestFoo(t *testing.T) {
179184
}
180185
```
181186

187+
### Signatures
188+
189+
```go
190+
import "iter"
191+
192+
type ToString[T any] func(T) string
193+
194+
func ToStringArray[T any](array []T, toString ToString[T]) []string {
195+
// ...
196+
}
197+
198+
```
199+
200+
```go
201+
func TestToStringArray(t *testing.T) {
202+
ctrl := gomock.NewController(t)
203+
204+
m := NewMockToString[int](ctrl)
205+
206+
// The Execute method is used as an implementation of the ToString signature.
207+
m.
208+
EXPECT().
209+
Execute(gomock.AnyOf(1, 2)).
210+
DoAndReturn(strconv.Itoa).
211+
Times(2)
212+
213+
strArray := ToStringArray([]int{1, 2}, m.Execute)
214+
215+
fmt.Println(strArray) // ["1", "2"]
216+
}
217+
```
218+
182219
## Building Stubs
183220

184221
```go
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
3+
import (
4+
"iter"
5+
6+
"go.uber.org/mock/mockgen/internal/tests/signature"
7+
"go.uber.org/mock/mockgen/internal/tests/signature/package_mode"
8+
"go.uber.org/mock/mockgen/internal/tests/signature/source_mode"
9+
)
10+
11+
func UseSome(fnc signature.Some) {}
12+
13+
func UseSome2(fnc signature.Some2) {}
14+
15+
func UseGeneric[T any](fnc signature.Generic[T]) {}
16+
17+
func UseWithMethod(fnc signature.WithMethod) {}
18+
19+
func UseSeq[T any](fnc iter.Seq[T]) {}
20+
21+
func packageModeChecks() {
22+
UseSome((&package_mode.MockSome{}).Execute)
23+
UseSome((&package_mode.MockSome2{}).Execute)
24+
UseSome((&package_mode.MockAlias{}).Execute)
25+
UseSome2((&package_mode.MockSome{}).Execute)
26+
UseSome2((&package_mode.MockSome2{}).Execute)
27+
UseSome2((&package_mode.MockAlias{}).Execute)
28+
29+
UseGeneric((&package_mode.MockGeneric[int]{}).Execute)
30+
UseGeneric[int]((&package_mode.MockIntGeneric{}).Execute)
31+
32+
UseWithMethod((&package_mode.MockWithMethod{}).Execute)
33+
34+
UseSeq((&package_mode.MockIntIter{}).Execute)
35+
UseSeq((&package_mode.MockSeq[bool]{}).Execute)
36+
}
37+
38+
func sourceModeChecks() {
39+
// Source Mode currently does not support aliases and derived types.
40+
// Please uncomment when the opportunity arises.
41+
//UseSome((&source_mode.MockSome2{}).Execute)
42+
//UseSome((&source_mode.MockAlias{}).Execute)
43+
//UseSome2((&source_mode.MockSome2{}).Execute)
44+
//UseSome2((&source_mode.MockAlias{}).Execute)
45+
//UseGeneric[int]((&source_mode.MockIntGeneric{}).Execute)
46+
47+
UseSome((&source_mode.MockSome{}).Execute)
48+
UseSome2((&source_mode.MockSome{}).Execute)
49+
UseGeneric((&source_mode.MockGeneric[int]{}).Execute)
50+
UseWithMethod((&source_mode.MockWithMethod{}).Execute)
51+
}
52+
53+
// We check in compile-time that mocks are generated correctly
54+
func main() {
55+
packageModeChecks()
56+
sourceModeChecks()
57+
}

mockgen/internal/tests/signature/package_mode/external_mock.go

Lines changed: 136 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)