зеркало из https://github.com/golang/mock.git
refactor to unify README styles (#338)
This commit is contained in:
Родитель
577071b755
Коммит
0b73a1dbc3
|
@ -15,3 +15,6 @@ mockgen/mockgen
|
|||
|
||||
# A binary produced by gotest.
|
||||
#gomock/[568]\.out
|
||||
|
||||
# Editors
|
||||
.vscode
|
||||
|
|
43
README.md
43
README.md
|
@ -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._
|
||||
|
@ -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
|
||||
---------------
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
--------------
|
||||
|
||||
|
|
|
@ -98,6 +98,7 @@ func (m assignableToTypeOfMatcher) String() string {
|
|||
}
|
||||
|
||||
// Constructors
|
||||
|
||||
// Any returns a matcher that always matches.
|
||||
func Any() Matcher { return anyMatcher{} }
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
@ -11,7 +14,7 @@ type Source interface {
|
|||
}
|
||||
```
|
||||
|
||||
```
|
||||
```go
|
||||
// some.org/package/imported
|
||||
type Foreign interface {
|
||||
Embedded
|
||||
|
@ -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()]
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
@ -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)
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
```
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
# Unexported Method
|
||||
|
||||
From #52, this tests an unexported method in the mocked interface.
|
||||
|
|
|
@ -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).
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
# Vendor Pkg
|
||||
|
||||
Test for [Issue#4](https://github.com/golang/mock/issues/4).
|
||||
|
|
|
@ -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
|
||||
```
|
||||
|
|
Загрузка…
Ссылка в новой задаче