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
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mockgen -archive=pkg.a database/sql/driver Conn,Driver

### Source mode

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

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

### Package mode

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

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

# Convenient for `go:generate`.
mockgen . Conn,Driver

# Function signatures can also be mocked
mockgen iter Seq,Seq2
```

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

## Building Mocks

## Interfaces

```go
type Foo interface {
Bar(x int) int
Expand Down Expand Up @@ -179,6 +184,38 @@ func TestFoo(t *testing.T) {
}
```

### Signatures

```go
import "iter"

type ToString[T any] func(T) string

func ToStringArray[T any](array []T, toString ToString[T]) []string {
// ...
}

```

```go
func TestToStringArray(t *testing.T) {
ctrl := gomock.NewController(t)

m := NewMockToString[int](ctrl)

// The Execute method is used as an implementation of the ToString signature.
m.
EXPECT().
Execute(gomock.AnyOf(1, 2)).
DoAndReturn(strconv.Itoa).
Times(2)

strArray := ToStringArray([]int{1, 2}, m.Execute)

fmt.Println(strArray) // ["1", "2"]
}
```

## Building Stubs

```go
Expand Down
57 changes: 57 additions & 0 deletions mockgen/internal/tests/signature/checks/checks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"iter"

"go.uber.org/mock/mockgen/internal/tests/signature"
"go.uber.org/mock/mockgen/internal/tests/signature/package_mode"
"go.uber.org/mock/mockgen/internal/tests/signature/source_mode"
)

func UseSome(fnc signature.Some) {}

func UseSome2(fnc signature.Some2) {}

func UseGeneric[T any](fnc signature.Generic[T]) {}

func UseWithMethod(fnc signature.WithMethod) {}

func UseSeq[T any](fnc iter.Seq[T]) {}

func packageModeChecks() {
UseSome((&package_mode.MockSome{}).Execute)
UseSome((&package_mode.MockSome2{}).Execute)
UseSome((&package_mode.MockAlias{}).Execute)
UseSome2((&package_mode.MockSome{}).Execute)
UseSome2((&package_mode.MockSome2{}).Execute)
UseSome2((&package_mode.MockAlias{}).Execute)

UseGeneric((&package_mode.MockGeneric[int]{}).Execute)
UseGeneric[int]((&package_mode.MockIntGeneric{}).Execute)

UseWithMethod((&package_mode.MockWithMethod{}).Execute)

UseSeq((&package_mode.MockIntIter{}).Execute)
UseSeq((&package_mode.MockSeq[bool]{}).Execute)
}

func sourceModeChecks() {
// Source Mode currently does not support aliases and derived types.
// Please uncomment when the opportunity arises.
//UseSome((&source_mode.MockSome2{}).Execute)
//UseSome((&source_mode.MockAlias{}).Execute)
//UseSome2((&source_mode.MockSome2{}).Execute)
//UseSome2((&source_mode.MockAlias{}).Execute)
//UseGeneric[int]((&source_mode.MockIntGeneric{}).Execute)

UseSome((&source_mode.MockSome{}).Execute)
UseSome2((&source_mode.MockSome{}).Execute)
UseGeneric((&source_mode.MockGeneric[int]{}).Execute)
UseWithMethod((&source_mode.MockWithMethod{}).Execute)
}

// We check in compile-time that mocks are generated correctly
func main() {
packageModeChecks()
sourceModeChecks()
}
136 changes: 136 additions & 0 deletions mockgen/internal/tests/signature/package_mode/external_mock.go

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

Loading