go.uber.org/yarpc@v1.72.1/encoding/thrift/thriftrw-plugin-yarpc/gomock.go (about) 1 // Copyright (c) 2022 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package main 22 23 import ( 24 "path/filepath" 25 26 "go.uber.org/thriftrw/plugin" 27 ) 28 29 const gomockTemplate = ` 30 // Code generated by thriftrw-plugin-yarpc 31 // @generated 32 33 <$pkgname := printf "%stest" (lower .Name)> 34 package <$pkgname> 35 36 <$gomock := import "github.com/golang/mock/gomock"> 37 38 // MockClient implements a gomock-compatible mock client for service 39 // <.Name>. 40 type MockClient struct { 41 ctrl *<$gomock>.Controller 42 recorder *_MockClientRecorder 43 } 44 45 var _ <import .ClientPackagePath>.Interface = (*MockClient)(nil) 46 47 type _MockClientRecorder struct { 48 mock *MockClient 49 } 50 51 // Build a new mock client for service <.Name>. 52 // 53 // mockCtrl := gomock.NewController(t) 54 // client := <$pkgname>.NewMockClient(mockCtrl) 55 // 56 // Use EXPECT() to set expectations on the mock. 57 func NewMockClient(ctrl *<$gomock>.Controller) *MockClient { 58 mock := &MockClient{ctrl: ctrl} 59 mock.recorder = &_MockClientRecorder{mock} 60 return mock 61 } 62 63 // EXPECT returns an object that allows you to define an expectation on the 64 // <.Name> mock client. 65 func (m *MockClient) EXPECT() *_MockClientRecorder { 66 return m.recorder 67 } 68 69 <range .AllFunctions> 70 <$context := import "context"> 71 <$yarpc := import "go.uber.org/yarpc"> 72 73 // <.Name> responds to a <.Name> call based on the mock expectations. This 74 // call will fail if the mock does not expect this call. Use EXPECT to expect 75 // a call to this function. 76 // 77 // client.EXPECT().<.Name>(gomock.Any(), ...).Return(...) 78 // ... := client.<.Name>(...) 79 func (m *MockClient) <.Name>( 80 ctx <$context>.Context, <range .Arguments> 81 _<.Name> <formatType .Type>,<end> 82 opts ...<$yarpc>.CallOption, 83 ) <if .OneWay> (ack <$yarpc>.Ack, err error) { 84 <else> (<if .ReturnType>success <formatType .ReturnType>,<end> err error) { 85 <end> 86 args := []interface{}{ctx,<range .Arguments> _<.Name>,<end>} 87 for _, o := range opts { 88 args = append(args, o) 89 } 90 i := 0 91 ret := m.ctrl.Call(m, "<.Name>", args...) 92 <if .OneWay> ack, _ = ret[i].(<$yarpc>.Ack); i++ 93 <else if .ReturnType> success, _ = ret[i].(<formatType .ReturnType>); i++ 94 <end> err, _ = ret[i].(error) 95 return 96 } 97 98 func (mr *_MockClientRecorder) <.Name>( 99 ctx interface{}, <range .Arguments> 100 _<.Name> interface{},<end> 101 opts ...interface{}, 102 ) *gomock.Call { 103 args := append([]interface{}{ctx,<range .Arguments> _<.Name>,<end>}, opts...) 104 return mr.mock.ctrl.RecordCall(mr.mock, "<.Name>", args...) 105 } 106 <end> 107 ` 108 109 func gomockGenerator(data *serviceTemplateData, files map[string][]byte) (err error) { 110 packageName := filepath.Base(data.TestPackagePath()) 111 // kv.thrift => .../kv/keyvaluetest/client.go 112 path := filepath.Join(data.Module.Directory, packageName, "client.go") 113 files[path], err = plugin.GoFileFromTemplate(path, gomockTemplate, data, templateOptions...) 114 return 115 }