diff options
Diffstat (limited to 'vendor/github.com/golang/mock/mockgen/tests/custom_package_name')
6 files changed, 154 insertions, 0 deletions
diff --git a/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/README.md b/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/README.md new file mode 100644 index 0000000..b16da21 --- /dev/null +++ b/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/README.md @@ -0,0 +1,22 @@ +# Tests for custom package names + +This directory contains test for mockgen generating mocks when imported package +name does not match import path suffix. For example, package with name "client" +is located under import path "github.com/golang/mock/mockgen/tests/custom_package_name/client/v1". + +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 + +This can be fixed by manually providing `-imports` flag, like `-imports client=github.com/golang/mock/mockgen/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 + +Mockgen runs successfully, produced output is equal to [greeter_mock_test.go](greeter/greeter_mock_test.go) content. diff --git a/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/client/v1/client.go b/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/client/v1/client.go new file mode 100644 index 0000000..fc799d9 --- /dev/null +++ b/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/client/v1/client.go @@ -0,0 +1,13 @@ +package client + +import "fmt" + +type Client struct{} + +func (c *Client) Greet(in GreetInput) string { + return fmt.Sprintf("Hello, %s!", in.Name) +} + +type GreetInput struct { + Name string +} diff --git a/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/greeter/greeter.go b/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/greeter/greeter.go new file mode 100644 index 0000000..8c2d293 --- /dev/null +++ b/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/greeter/greeter.go @@ -0,0 +1,31 @@ +//go:generate mockgen -source greeter.go -destination greeter_mock_test.go -package greeter + +package greeter + +import ( + // stdlib import + "fmt" + + // non-matching import suffix and package name + "github.com/golang/mock/mockgen/tests/custom_package_name/client/v1" + + // matching import suffix and package name + "github.com/golang/mock/mockgen/tests/custom_package_name/validator" +) + +type InputMaker interface { + MakeInput() client.GreetInput +} + +type Greeter struct { + InputMaker InputMaker + Client *client.Client +} + +func (g *Greeter) Greet() (string, error) { + in := g.InputMaker.MakeInput() + if err := validator.Validate(in.Name); err != nil { + return "", fmt.Errorf("validation failed: %v", err) + } + return g.Client.Greet(in), nil +} diff --git a/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/greeter/greeter_mock_test.go b/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/greeter/greeter_mock_test.go new file mode 100644 index 0000000..0bf0f46 --- /dev/null +++ b/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/greeter/greeter_mock_test.go @@ -0,0 +1,46 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: greeter.go + +// Package greeter is a generated GoMock package. +package greeter + +import ( + gomock "github.com/golang/mock/gomock" + v1 "github.com/golang/mock/mockgen/tests/custom_package_name/client/v1" + reflect "reflect" +) + +// MockInputMaker is a mock of InputMaker interface +type MockInputMaker struct { + ctrl *gomock.Controller + recorder *MockInputMakerMockRecorder +} + +// MockInputMakerMockRecorder is the mock recorder for MockInputMaker +type MockInputMakerMockRecorder struct { + mock *MockInputMaker +} + +// NewMockInputMaker creates a new mock instance +func NewMockInputMaker(ctrl *gomock.Controller) *MockInputMaker { + mock := &MockInputMaker{ctrl: ctrl} + mock.recorder = &MockInputMakerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockInputMaker) EXPECT() *MockInputMakerMockRecorder { + return m.recorder +} + +// MakeInput mocks base method +func (m *MockInputMaker) MakeInput() v1.GreetInput { + ret := m.ctrl.Call(m, "MakeInput") + ret0, _ := ret[0].(v1.GreetInput) + return ret0 +} + +// MakeInput indicates an expected call of MakeInput +func (mr *MockInputMakerMockRecorder) MakeInput() *gomock.Call { + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MakeInput", reflect.TypeOf((*MockInputMaker)(nil).MakeInput)) +} diff --git a/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/greeter/greeter_test.go b/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/greeter/greeter_test.go new file mode 100644 index 0000000..f056ec8 --- /dev/null +++ b/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/greeter/greeter_test.go @@ -0,0 +1,37 @@ +package greeter + +import ( + "testing" + + "github.com/golang/mock/gomock" + "github.com/golang/mock/mockgen/tests/custom_package_name/client/v1" +) + +func TestGreeter_Greet(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + input := client.GreetInput{ + Name: "Foo", + } + + inputMaker := NewMockInputMaker(ctrl) + inputMaker.EXPECT(). + MakeInput(). + Return(input) + + g := &Greeter{ + InputMaker: inputMaker, + Client: &client.Client{}, + } + + greeting, err := g.Greet() + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + + expected := "Hello, Foo!" + if greeting != expected { + t.Fatalf("Expected greeting to be %v but got %v", expected, greeting) + } +} diff --git a/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/validator/validate.go b/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/validator/validate.go new file mode 100644 index 0000000..0617c4b --- /dev/null +++ b/vendor/github.com/golang/mock/mockgen/tests/custom_package_name/validator/validate.go @@ -0,0 +1,5 @@ +package validator + +func Validate(s string) error { + return nil +} |