Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
refactor to unify README styles (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
codyoss committed Oct 24, 2019
1 parent 577071b commit 0b73a1d
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 44 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ mockgen/mockgen

# A binary produced by gotest.
#gomock/[568]\.out

# Editors
.vscode
43 changes: 24 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ GoMock is a mocking framework for the [Go programming language][golang]. It
integrates well with Go's built-in `testing` package, but can be used in other
contexts too.


Installation
------------

Once you have [installed Go][golang-install], install the `mockgen` tool:

go get github.com/golang/mock/mockgen
```bash
go get github.com/golang/mock/mockgen
```

_Note: It is recommended to have `GO111MODULE=on` to ensure the correct
dependencies are used._
Expand All @@ -21,12 +22,13 @@ Documentation

After installing, you can use `go doc` to get documentation:

go doc github.com/golang/mock/gomock
```bash
go doc github.com/golang/mock/gomock
```

Alternatively, there is an online reference for the package hosted on GoPkgDoc
[here][gomock-ref].


Running mockgen
---------------

Expand All @@ -37,7 +39,9 @@ may be useful in this mode are -imports and -aux_files.

Example:

mockgen -source=foo.go [other options]
```bash
mockgen -source=foo.go [other options]
```

Reflect mode generates mock interfaces by building a program
that uses reflection to understand interfaces. It is enabled
Expand All @@ -46,48 +50,49 @@ comma-separated list of symbols.

Example:

mockgen database/sql/driver Conn,Driver
```bash
mockgen database/sql/driver Conn,Driver
```

The `mockgen` command is used to generate source code for a mock
class given a Go source file containing interfaces to be mocked.
It supports the following flags:

* `-source`: A file containing interfaces to be mocked.
* `-source`: A file containing interfaces to be mocked.

* `-destination`: A file to which to write the resulting source code. If you
* `-destination`: A file to which to write the resulting source code. If you
don't set this, the code is printed to standard output.

* `-package`: The package to use for the resulting mock class
* `-package`: The package to use for the resulting mock class
source code. If you don't set this, the package name is `mock_` concatenated
with the package of the input file.

* `-imports`: A list of explicit imports that should be used in the resulting
* `-imports`: A list of explicit imports that should be used in the resulting
source code, specified as a comma-separated list of elements of the form
`foo=bar/baz`, where `bar/baz` is the package being imported and `foo` is
the identifier to use for the package in the generated source code.

* `-aux_files`: A list of additional files that should be consulted to
* `-aux_files`: A list of additional files that should be consulted to
resolve e.g. embedded interfaces defined in a different file. This is
specified as a comma-separated list of elements of the form
`foo=bar/baz.go`, where `bar/baz.go` is the source file and `foo` is the
package name of that file used by the -source file.

* `-build_flags`: (reflect mode only) Flags passed verbatim to `go build`.
* `-build_flags`: (reflect mode only) Flags passed verbatim to `go build`.

* `-mock_names`: A list of custom names for generated mocks. This is specified
as a comma-separated list of elements of the form
`Repository=MockSensorRepository,Endpoint=MockSensorEndpoint`, where
`Repository` is the interface name and `MockSensorRepository` is the desired
mock name (mock factory method and mock recorder will be named after the mock).
If one of the interfaces has no custom name specified, then default naming
convention will be used.
as a comma-separated list of elements of the form
`Repository=MockSensorRepository,Endpoint=MockSensorEndpoint`, where
`Repository` is the interface name and `MockSensorRepository` is the desired
mock name (mock factory method and mock recorder will be named after the mock).
If one of the interfaces has no custom name specified, then default naming
convention will be used.

* `-copyright_file`: Copyright file used to add copyright header to the resulting source code.

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


Building Mocks
--------------

Expand Down
1 change: 1 addition & 0 deletions gomock/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (m assignableToTypeOfMatcher) String() string {
}

// Constructors

// Any returns a matcher that always matches.
func Any() Matcher { return anyMatcher{} }

Expand Down
10 changes: 7 additions & 3 deletions mockgen/internal/tests/aux_imports_embedded_interface/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Embedded Interfaces in aux_files

Embedded interfaces in `aux_files` generate `unknown embedded interface XXX` errors.
See below for example of the problem:
```

```go
// source
import (
alias "some.org/package/imported"
Expand All @@ -11,7 +14,7 @@ type Source interface {
}
```

```
```go
// some.org/package/imported
type Foreign interface {
Embedded
Expand All @@ -26,7 +29,8 @@ explicitly specified in the `aux_files` flag.

In the `parseInterface` method, there is an incorrect assumption about an embedded interface
always being in the source file.
```

```go
case *ast.Ident:
// Embedded interface in this package.
ei := p.auxInterfaces[""][v.String()]
Expand Down
16 changes: 10 additions & 6 deletions mockgen/internal/tests/custom_package_name/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ is located under import path "github.com/golang/mock/mockgen/internal/tests/cust

Prior to this patch:

$ go generate greeter/greeter.go
2018/03/05 22:44:52 Loading input failed: greeter.go:17:11: failed parsing returns: greeter.go:17:14: unknown package "client"
greeter/greeter.go:1: running "mockgen": exit status 1
```bash
$ go generate greeter/greeter.go
2018/03/05 22:44:52 Loading input failed: greeter.go:17:11: failed parsing returns: greeter.go:17:14: unknown package "client"
greeter/greeter.go:1: running "mockgen": exit status 1
```

This can be fixed by manually providing `-imports` flag, like `-imports client=github.com/golang/mock/mockgen/internal/tests/custom_package_name/client/v1`.
But, mockgen should be able to automatically resolve package names in such situations.

With this patch applied:

$ go generate greeter/greeter.go
$ echo $?
0
```bash
$ go generate greeter/greeter.go
$ echo $?
0
```

Mockgen runs successfully, produced output is equal to [greeter_mock_test.go](greeter/greeter_mock_test.go) content.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Generated Identifier Conflict

The generated mock methods use some hardcoded variable/receiver names that can
have conflicts with the argument names that are defined by the code for which
the mock is generated when using the source generation method.
Expand All @@ -6,14 +8,14 @@ Example:

```go
type Example interface {
Method(_m, _mr, m, mr int)
Method(_m, _mr, m, mr int)
}
```

```go
// Method mocks base method
func (_m *MockExample) Method(_m int, _mr int, m int, mr int) {
_m.ctrl.Call(_m, "Method", _m, _mr, m, mr)
_m.ctrl.Call(_m, "Method", _m, _mr, m, mr)
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Import Source

Test the case where the generated code uses a type defined in the source package (in source mode). There are two test cases:

- the output is in a new package
- the output is in the same package as the input
- the output is in the same package as the input
24 changes: 15 additions & 9 deletions mockgen/internal/tests/mock_in_test_package/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# Mock in Test Package

Test the case where the package has the `_test` suffix.

Prior to patch:

$ go generate
$ go test
# github.com/golang/mock/mockgen/internal/tests/mock_in_test_package_test [github.com/golang/mock/mockgen/internal/tests/mock_in_test_package.test]
./mock_test.go:36:44: undefined: User
./mock_test.go:38:21: undefined: User
FAIL github.com/golang/mock/mockgen/internal/tests/mock_in_test_package [build failed]
```bash
$ go generate
$ go test
# github.com/golang/mock/mockgen/internal/tests/mock_in_test_package_test [github.com/golang/mock/mockgen/internal/tests/mock_in_test_package.test]
./mock_test.go:36:44: undefined: User
./mock_test.go:38:21: undefined: User
FAIL github.com/golang/mock/mockgen/internal/tests/mock_in_test_package [build failed]
```

With this patch applied:

$ go generate
$ go test
ok github.com/golang/mock/mockgen/internal/tests/mock_in_test_package 0.031s
```bash
$ go generate
$ go test
ok github.com/golang/mock/mockgen/internal/tests/mock_in_test_package 0.031s
```
2 changes: 2 additions & 0 deletions mockgen/internal/tests/unexported_method/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# Unexported Method

From #52, this tests an unexported method in the mocked interface.
2 changes: 2 additions & 0 deletions mockgen/internal/tests/vendor_dep/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Vendor Dep

Test for [Issue#4](https://github.com/golang/mock/issues/4).
Also see discussion on [#28](https://github.com/golang/mock/pull/28).
2 changes: 2 additions & 0 deletions mockgen/internal/tests/vendor_pkg/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# Vendor Pkg

Test for [Issue#4](https://github.com/golang/mock/issues/4).
12 changes: 8 additions & 4 deletions sample/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
# Samples

This directory contains an example of a package containing a non-trivial
interface that can be mocked with GoMock. The interesting files are:

* `user.go`: Source code for the sample package, containing interfaces to be
* `user.go`: Source code for the sample package, containing interfaces to be
mocked. This file depends on the packages named imp[1-4] for various things.

* `user_test.go`: A test for the sample package, in which mocks of the
* `user_test.go`: A test for the sample package, in which mocks of the
interfaces from `user.go` are used. This demonstrates how to create mock
objects, set up expectations, and so on.

* `mock_user/mock_user.go`: The generated mock code. See ../gomock/matchers.go
* `mock_user/mock_user.go`: The generated mock code. See ../gomock/matchers.go
for the `go:generate` command used to generate it.

To run the test,

go test github.com/golang/mock/sample
```bash
go test github.com/golang/mock/sample
```

0 comments on commit 0b73a1d

Please sign in to comment.