├── internal └── mockc │ ├── testdata │ ├── basic │ │ ├── testdata │ │ │ ├── input.json │ │ │ ├── output.json │ │ │ └── mockc_gen.go.gen │ │ ├── mockc.go │ │ └── cache.go │ ├── implements │ │ ├── testdata │ │ │ ├── input.json │ │ │ ├── output.json │ │ │ └── mockc_gen.go.gen │ │ ├── mockc.go │ │ └── cache.go │ ├── type-code │ │ ├── testdata │ │ │ ├── input.json │ │ │ ├── output.json │ │ │ └── mockc_gen.go.gen │ │ ├── mockc.go │ │ └── typecode.go │ ├── non-interface │ │ ├── testdata │ │ │ ├── input.json │ │ │ └── output.json │ │ ├── mockc.go │ │ └── cache.go │ ├── with-constructor │ │ ├── testdata │ │ │ ├── input.json │ │ │ ├── output.json │ │ │ └── mockc_gen.go.gen │ │ ├── cache.go │ │ └── mockc.go │ ├── custom-destination │ │ ├── testdata │ │ │ ├── input.json │ │ │ ├── output.json │ │ │ └── custom_destination.go.gen │ │ ├── cache.go │ │ └── mockc.go │ ├── custom-field-name │ │ ├── testdata │ │ │ ├── input.json │ │ │ ├── output.json │ │ │ └── mockc_gen.go.gen │ │ ├── cache.go │ │ └── mockc.go │ ├── invalid-destination │ │ ├── testdata │ │ │ ├── input.json │ │ │ └── output.json │ │ ├── cache.go │ │ └── mockc.go │ ├── invalid-field-name │ │ ├── testdata │ │ │ ├── input.json │ │ │ └── output.json │ │ ├── cache.go │ │ └── mockc.go │ ├── empty-string-destination │ │ ├── testdata │ │ │ ├── input.json │ │ │ └── output.json │ │ ├── cache.go │ │ └── mockc.go │ ├── invalid-mock-generator │ │ ├── testdata │ │ │ ├── input.json │ │ │ └── output.json │ │ ├── cache.go │ │ └── mockc.go │ └── with-custom-constructor │ │ ├── testdata │ │ ├── input.json │ │ ├── output.json │ │ └── mockc_gen.go.gen │ │ ├── mockc.go │ │ └── cache.go │ ├── utils.go │ ├── load_packages.go │ ├── mockc.go │ ├── mockc_test.go │ ├── generator.go │ ├── parser.go │ └── render.go ├── examples ├── basic │ ├── mockc.go │ ├── cache.go │ ├── cache_test.go │ └── mockc_gen.go ├── with-constructor │ ├── mockc.go │ ├── cache.go │ ├── cache_test.go │ └── mockc_gen.go └── with-custom-constructor │ ├── mockc.go │ ├── cache.go │ ├── cache_test.go │ └── mockc_gen.go ├── go.mod ├── .gitignore ├── cmd └── mockc │ ├── main.go │ └── config.go ├── LICENSE ├── mockc.go ├── go.sum └── README.md /internal/mockc/testdata/basic/testdata/input.json: -------------------------------------------------------------------------------- 1 | { 2 | "patterns": [] 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/implements/testdata/input.json: -------------------------------------------------------------------------------- 1 | { 2 | "patterns": [] 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/type-code/testdata/input.json: -------------------------------------------------------------------------------- 1 | { 2 | "patterns": [] 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/non-interface/testdata/input.json: -------------------------------------------------------------------------------- 1 | { 2 | "patterns": [] 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/with-constructor/testdata/input.json: -------------------------------------------------------------------------------- 1 | { 2 | "patterns": [] 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/custom-destination/testdata/input.json: -------------------------------------------------------------------------------- 1 | { 2 | "patterns": [] 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/custom-field-name/testdata/input.json: -------------------------------------------------------------------------------- 1 | { 2 | "patterns": [] 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/invalid-destination/testdata/input.json: -------------------------------------------------------------------------------- 1 | { 2 | "patterns": [] 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/invalid-field-name/testdata/input.json: -------------------------------------------------------------------------------- 1 | { 2 | "patterns": [] 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/empty-string-destination/testdata/input.json: -------------------------------------------------------------------------------- 1 | { 2 | "patterns": [] 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/invalid-mock-generator/testdata/input.json: -------------------------------------------------------------------------------- 1 | { 2 | "patterns": [] 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/with-custom-constructor/testdata/input.json: -------------------------------------------------------------------------------- 1 | { 2 | "patterns": [] 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/basic/testdata/output.json: -------------------------------------------------------------------------------- 1 | { 2 | "output": "^generated: /(.+?)/testdata/basic/mockc_gen\\.go\n$" 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/non-interface/testdata/output.json: -------------------------------------------------------------------------------- 1 | { 2 | "err": "non-interface:\n\tmock \"MockcCache\": string" 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/type-code/testdata/output.json: -------------------------------------------------------------------------------- 1 | { 2 | "output": "^generated: /(.+?)/testdata/type-code/mockc_gen\\.go\n$" 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/custom-field-name/testdata/output.json: -------------------------------------------------------------------------------- 1 | { 2 | "output": "^generated: /(.+?)/testdata/custom-field-name/mockc_gen\\.go\n$" 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/with-constructor/testdata/output.json: -------------------------------------------------------------------------------- 1 | { 2 | "output": "^generated: /(.+?)/testdata/with-constructor/mockc_gen\\.go\n$" 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/custom-destination/testdata/output.json: -------------------------------------------------------------------------------- 1 | { 2 | "output": "^generated: /(.+?)/testdata/custom-destination/custom_destination\\.go\n$" 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/with-custom-constructor/testdata/output.json: -------------------------------------------------------------------------------- 1 | { 2 | "output": "^generated: /(.+?)/testdata/with-custom-constructor/mockc_gen\\.go\n$" 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/invalid-destination/testdata/output.json: -------------------------------------------------------------------------------- 1 | { 2 | "err": "cannot set destination:\n\tmock \"MockcCache\": \"mockc_gen.txt\" is not a go file" 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/empty-string-destination/testdata/output.json: -------------------------------------------------------------------------------- 1 | { 2 | "err": "cannot set destination:\n\tmock \"MockcCache\": destination should not be an empty string" 3 | } 4 | -------------------------------------------------------------------------------- /examples/basic/mockc.go: -------------------------------------------------------------------------------- 1 | //+build mockc 2 | 3 | package basic 4 | 5 | import ( 6 | "github.com/KimMachineGun/mockc" 7 | ) 8 | 9 | func MockcCache() { 10 | mockc.Implement(Cache(nil)) 11 | } 12 | -------------------------------------------------------------------------------- /examples/basic/cache.go: -------------------------------------------------------------------------------- 1 | package basic 2 | 3 | type Cache interface { 4 | Get(key string) (val interface{}, err error) 5 | Set(key string, val interface{}) (err error) 6 | Del(key string) (err error) 7 | } 8 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/KimMachineGun/mockc 2 | 3 | go 1.13 4 | 5 | require ( 6 | github.com/dave/jennifer v1.4.1 7 | github.com/stretchr/testify v1.7.0 8 | golang.org/x/tools v0.19.0 9 | ) 10 | -------------------------------------------------------------------------------- /internal/mockc/testdata/invalid-mock-generator/testdata/output.json: -------------------------------------------------------------------------------- 1 | { 2 | "err": "cannot find mockc calls:\n\tmock \"InvalidMockGenerator\": mock generator should be consist of mockc function calls" 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/basic/mockc.go: -------------------------------------------------------------------------------- 1 | //+build mockc 2 | 3 | package basic 4 | 5 | import ( 6 | "github.com/KimMachineGun/mockc" 7 | ) 8 | 9 | func MockcCache() { 10 | mockc.Implement(Cache(nil)) 11 | } 12 | -------------------------------------------------------------------------------- /internal/mockc/testdata/implements/testdata/output.json: -------------------------------------------------------------------------------- 1 | { 2 | "output": "^mockc\\.Implements is deprecated\\. Please use mock\\.Implement instead\\.\\ngenerated: /(.+?)/testdata/implements/mockc_gen\\.go\n$" 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/basic/cache.go: -------------------------------------------------------------------------------- 1 | package basic 2 | 3 | type Cache interface { 4 | Get(key string) (val interface{}, err error) 5 | Set(key string, val interface{}) (err error) 6 | Del(key string) (err error) 7 | } 8 | -------------------------------------------------------------------------------- /internal/mockc/testdata/implements/mockc.go: -------------------------------------------------------------------------------- 1 | //+build mockc 2 | 3 | package basic 4 | 5 | import ( 6 | "github.com/KimMachineGun/mockc" 7 | ) 8 | 9 | func MockcCache() { 10 | mockc.Implements(Cache(nil)) 11 | } 12 | -------------------------------------------------------------------------------- /internal/mockc/testdata/implements/cache.go: -------------------------------------------------------------------------------- 1 | package basic 2 | 3 | type Cache interface { 4 | Get(key string) (val interface{}, err error) 5 | Set(key string, val interface{}) (err error) 6 | Del(key string) (err error) 7 | } 8 | -------------------------------------------------------------------------------- /internal/mockc/testdata/invalid-field-name/testdata/output.json: -------------------------------------------------------------------------------- 1 | { 2 | "err": "at least one of the field name prefix and field name suffix must not be an empty string:\n\tmock \"MockcCache\": prefix(\"\") suffix(\"\")" 3 | } 4 | -------------------------------------------------------------------------------- /internal/mockc/testdata/non-interface/mockc.go: -------------------------------------------------------------------------------- 1 | //+build mockc 2 | 3 | package basic 4 | 5 | import ( 6 | "github.com/KimMachineGun/mockc" 7 | ) 8 | 9 | func MockcCache() { 10 | mockc.Implement("Cache(nil)") 11 | } 12 | -------------------------------------------------------------------------------- /internal/mockc/testdata/type-code/mockc.go: -------------------------------------------------------------------------------- 1 | //+build mockc 2 | 3 | package basic 4 | 5 | import ( 6 | "github.com/KimMachineGun/mockc" 7 | ) 8 | 9 | func MockcTypeCode() { 10 | mockc.Implement(TypeCode(nil)) 11 | } 12 | -------------------------------------------------------------------------------- /internal/mockc/testdata/non-interface/cache.go: -------------------------------------------------------------------------------- 1 | package basic 2 | 3 | type Cache interface { 4 | Get(key string) (val interface{}, err error) 5 | Set(key string, val interface{}) (err error) 6 | Del(key string) (err error) 7 | } 8 | -------------------------------------------------------------------------------- /internal/mockc/testdata/custom-destination/cache.go: -------------------------------------------------------------------------------- 1 | package basic 2 | 3 | type Cache interface { 4 | Get(key string) (val interface{}, err error) 5 | Set(key string, val interface{}) (err error) 6 | Del(key string) (err error) 7 | } 8 | -------------------------------------------------------------------------------- /internal/mockc/testdata/custom-field-name/cache.go: -------------------------------------------------------------------------------- 1 | package basic 2 | 3 | type Cache interface { 4 | Get(key string) (val interface{}, err error) 5 | Set(key string, val interface{}) (err error) 6 | Del(key string) (err error) 7 | } 8 | -------------------------------------------------------------------------------- /internal/mockc/testdata/invalid-destination/cache.go: -------------------------------------------------------------------------------- 1 | package basic 2 | 3 | type Cache interface { 4 | Get(key string) (val interface{}, err error) 5 | Set(key string, val interface{}) (err error) 6 | Del(key string) (err error) 7 | } 8 | -------------------------------------------------------------------------------- /internal/mockc/testdata/invalid-field-name/cache.go: -------------------------------------------------------------------------------- 1 | package basic 2 | 3 | type Cache interface { 4 | Get(key string) (val interface{}, err error) 5 | Set(key string, val interface{}) (err error) 6 | Del(key string) (err error) 7 | } 8 | -------------------------------------------------------------------------------- /internal/mockc/testdata/empty-string-destination/cache.go: -------------------------------------------------------------------------------- 1 | package basic 2 | 3 | type Cache interface { 4 | Get(key string) (val interface{}, err error) 5 | Set(key string, val interface{}) (err error) 6 | Del(key string) (err error) 7 | } 8 | -------------------------------------------------------------------------------- /internal/mockc/testdata/invalid-mock-generator/cache.go: -------------------------------------------------------------------------------- 1 | package basic 2 | 3 | type Cache interface { 4 | Get(key string) (val interface{}, err error) 5 | Set(key string, val interface{}) (err error) 6 | Del(key string) (err error) 7 | } 8 | -------------------------------------------------------------------------------- /internal/mockc/testdata/with-constructor/cache.go: -------------------------------------------------------------------------------- 1 | package constructor 2 | 3 | type Cache interface { 4 | Get(key string) (val interface{}, err error) 5 | Set(key string, val interface{}) (err error) 6 | Del(key string) (err error) 7 | } 8 | -------------------------------------------------------------------------------- /examples/with-constructor/mockc.go: -------------------------------------------------------------------------------- 1 | //+build mockc 2 | 3 | package constructor 4 | 5 | import ( 6 | "github.com/KimMachineGun/mockc" 7 | ) 8 | 9 | func MockcCache() { 10 | mockc.Implement(Cache(nil)) 11 | mockc.WithConstructor() 12 | } 13 | -------------------------------------------------------------------------------- /internal/mockc/testdata/empty-string-destination/mockc.go: -------------------------------------------------------------------------------- 1 | //+build mockc 2 | 3 | package basic 4 | 5 | import ( 6 | "github.com/KimMachineGun/mockc" 7 | ) 8 | 9 | func MockcCache() { 10 | mockc.Implement(Cache(nil)) 11 | mockc.SetDestination("") 12 | } 13 | -------------------------------------------------------------------------------- /internal/mockc/testdata/with-constructor/mockc.go: -------------------------------------------------------------------------------- 1 | //+build mockc 2 | 3 | package constructor 4 | 5 | import ( 6 | "github.com/KimMachineGun/mockc" 7 | ) 8 | 9 | func MockcCache() { 10 | mockc.Implement(Cache(nil)) 11 | mockc.WithConstructor() 12 | } 13 | -------------------------------------------------------------------------------- /examples/with-custom-constructor/mockc.go: -------------------------------------------------------------------------------- 1 | //+build mockc 2 | 3 | package constructor 4 | 5 | import ( 6 | "github.com/KimMachineGun/mockc" 7 | ) 8 | 9 | func MockcCache() { 10 | mockc.Implement(Cache(nil)) 11 | mockc.SetConstructorName("newMockCache") 12 | } 13 | -------------------------------------------------------------------------------- /internal/mockc/testdata/invalid-destination/mockc.go: -------------------------------------------------------------------------------- 1 | //+build mockc 2 | 3 | package basic 4 | 5 | import ( 6 | "github.com/KimMachineGun/mockc" 7 | ) 8 | 9 | func MockcCache() { 10 | mockc.Implement(Cache(nil)) 11 | mockc.SetDestination("mockc_gen.txt") 12 | } 13 | -------------------------------------------------------------------------------- /internal/mockc/testdata/custom-destination/mockc.go: -------------------------------------------------------------------------------- 1 | //+build mockc 2 | 3 | package basic 4 | 5 | import ( 6 | "github.com/KimMachineGun/mockc" 7 | ) 8 | 9 | func MockcCache() { 10 | mockc.Implement(Cache(nil)) 11 | mockc.SetDestination("custom_destination.go") 12 | } 13 | -------------------------------------------------------------------------------- /internal/mockc/testdata/with-custom-constructor/mockc.go: -------------------------------------------------------------------------------- 1 | //+build mockc 2 | 3 | package constructor 4 | 5 | import ( 6 | "github.com/KimMachineGun/mockc" 7 | ) 8 | 9 | func MockcCache() { 10 | mockc.Implement(Cache(nil)) 11 | mockc.SetConstructorName("newMockCache") 12 | } 13 | -------------------------------------------------------------------------------- /internal/mockc/testdata/invalid-mock-generator/mockc.go: -------------------------------------------------------------------------------- 1 | //+build mockc 2 | 3 | package basic 4 | 5 | import ( 6 | "fmt" 7 | 8 | "github.com/KimMachineGun/mockc" 9 | ) 10 | 11 | func InvalidMockGenerator() { 12 | fmt.Println("Hello World") 13 | mockc.Implement(Cache(nil)) 14 | } 15 | -------------------------------------------------------------------------------- /internal/mockc/testdata/custom-field-name/mockc.go: -------------------------------------------------------------------------------- 1 | //+build mockc 2 | 3 | package basic 4 | 5 | import ( 6 | "github.com/KimMachineGun/mockc" 7 | ) 8 | 9 | func MockcCache() { 10 | mockc.Implement(Cache(nil)) 11 | mockc.SetFieldNamePrefix("") 12 | mockc.SetFieldNameSuffix("Func") 13 | } 14 | -------------------------------------------------------------------------------- /internal/mockc/testdata/invalid-field-name/mockc.go: -------------------------------------------------------------------------------- 1 | //+build mockc 2 | 3 | package basic 4 | 5 | import ( 6 | "github.com/KimMachineGun/mockc" 7 | ) 8 | 9 | func MockcCache() { 10 | mockc.Implement(Cache(nil)) 11 | mockc.SetFieldNamePrefix("") 12 | mockc.SetFieldNameSuffix("") 13 | } 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Swap 2 | [._]*.s[a-v][a-z] 3 | [._]*.sw[a-p] 4 | [._]s[a-v][a-z] 5 | [._]sw[a-p] 6 | 7 | # Session 8 | Session.vim 9 | 10 | # Temporary 11 | .netrwhist 12 | *~ 13 | # Auto-generated tag files 14 | tags 15 | 16 | # Binaries for programs and plugins 17 | *.exe 18 | *.exe~ 19 | *.dll 20 | *.so 21 | *.dylib 22 | 23 | # Test binary, build with `go test -c` 24 | *.test 25 | 26 | # Output of the go coverage tool, specifically when used with LiteIDE 27 | *.out 28 | 29 | # vscode 30 | .vscode 31 | 32 | # envrc 33 | .envrc 34 | 35 | .DS_Store 36 | 37 | .idea 38 | .idea/ 39 | .idea/* 40 | -------------------------------------------------------------------------------- /cmd/mockc/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "os" 7 | 8 | "github.com/KimMachineGun/mockc/internal/mockc" 9 | ) 10 | 11 | func main() { 12 | log.SetFlags(0) 13 | 14 | wd, err := os.Getwd() 15 | if err != nil { 16 | log.Fatalln("cannot get working directory:", err) 17 | } 18 | 19 | c := LoadConfig() 20 | if c.IsGeneratorMode() { 21 | err = mockc.Generate(context.Background(), wd, c.args) 22 | } else { 23 | err = c.ValidateFlags() 24 | if err == nil { 25 | err = mockc.GenerateWithFlags(context.Background(), wd, c.destination, c.name, c.withConstructor, c.fieldNamePrefix, c.fieldNameSuffix, c.args) 26 | } 27 | } 28 | if err != nil { 29 | log.Fatalln(err) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /examples/with-constructor/cache.go: -------------------------------------------------------------------------------- 1 | package constructor 2 | 3 | type Cache interface { 4 | Get(key string) (val interface{}, err error) 5 | Set(key string, val interface{}) (err error) 6 | Del(key string) (err error) 7 | } 8 | 9 | type MapCache struct { 10 | m map[string]interface{} 11 | } 12 | 13 | func (c MapCache) Get(key string) (interface{}, error) { 14 | if c.m != nil { 15 | c.m = map[string]interface{}{} 16 | } 17 | return c.m[key], nil 18 | } 19 | 20 | func (c MapCache) Set(key string, val interface{}) error { 21 | if c.m != nil { 22 | c.m = map[string]interface{}{} 23 | } 24 | c.m[key] = val 25 | return nil 26 | } 27 | 28 | func (c MapCache) Del(key string) error { 29 | if c.m != nil { 30 | c.m = map[string]interface{}{} 31 | } 32 | delete(c.m, key) 33 | return nil 34 | } 35 | -------------------------------------------------------------------------------- /examples/with-custom-constructor/cache.go: -------------------------------------------------------------------------------- 1 | package constructor 2 | 3 | type Cache interface { 4 | Get(key string) (val interface{}, err error) 5 | Set(key string, val interface{}) (err error) 6 | Del(key string) (err error) 7 | } 8 | 9 | type MapCache struct { 10 | m map[string]interface{} 11 | } 12 | 13 | func (c MapCache) Get(key string) (interface{}, error) { 14 | if c.m != nil { 15 | c.m = map[string]interface{}{} 16 | } 17 | return c.m[key], nil 18 | } 19 | 20 | func (c MapCache) Set(key string, val interface{}) error { 21 | if c.m != nil { 22 | c.m = map[string]interface{}{} 23 | } 24 | c.m[key] = val 25 | return nil 26 | } 27 | 28 | func (c MapCache) Del(key string) error { 29 | if c.m != nil { 30 | c.m = map[string]interface{}{} 31 | } 32 | delete(c.m, key) 33 | return nil 34 | } 35 | -------------------------------------------------------------------------------- /internal/mockc/testdata/with-custom-constructor/cache.go: -------------------------------------------------------------------------------- 1 | package constructor 2 | 3 | type Cache interface { 4 | Get(key string) (val interface{}, err error) 5 | Set(key string, val interface{}) (err error) 6 | Del(key string) (err error) 7 | } 8 | 9 | type MapCache struct { 10 | m map[string]interface{} 11 | } 12 | 13 | func (c MapCache) Get(key string) (interface{}, error) { 14 | if c.m != nil { 15 | c.m = map[string]interface{}{} 16 | } 17 | return c.m[key], nil 18 | } 19 | 20 | func (c MapCache) Set(key string, val interface{}) error { 21 | if c.m != nil { 22 | c.m = map[string]interface{}{} 23 | } 24 | c.m[key] = val 25 | return nil 26 | } 27 | 28 | func (c MapCache) Del(key string) error { 29 | if c.m != nil { 30 | c.m = map[string]interface{}{} 31 | } 32 | delete(c.m, key) 33 | return nil 34 | } 35 | -------------------------------------------------------------------------------- /internal/mockc/utils.go: -------------------------------------------------------------------------------- 1 | package mockc 2 | 3 | import ( 4 | "go/ast" 5 | "go/types" 6 | 7 | "golang.org/x/tools/go/packages" 8 | ) 9 | 10 | type interfaceFinder struct { 11 | pkg *packages.Package 12 | targets []string 13 | result map[string]*types.Interface 14 | } 15 | 16 | func newInterfaceFinder(pkg *packages.Package, targets []string) *interfaceFinder { 17 | return &interfaceFinder{ 18 | pkg: pkg, 19 | targets: targets, 20 | result: map[string]*types.Interface{}, 21 | } 22 | } 23 | 24 | func (f *interfaceFinder) Visit(node ast.Node) ast.Visitor { 25 | n, ok := node.(*ast.TypeSpec) 26 | if !ok { 27 | return f 28 | } 29 | 30 | inter, ok := f.pkg.TypesInfo.TypeOf(n.Type).(*types.Interface) 31 | if !ok { 32 | return f 33 | } 34 | 35 | for _, interfaceName := range f.targets { 36 | if interfaceName == n.Name.Name { 37 | f.result[interfaceName] = inter 38 | } 39 | } 40 | 41 | return f 42 | } 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 KimMachineGun 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /internal/mockc/load_packages.go: -------------------------------------------------------------------------------- 1 | package mockc 2 | 3 | import ( 4 | "context" 5 | "errors" 6 | "fmt" 7 | 8 | "golang.org/x/tools/go/packages" 9 | ) 10 | 11 | func loadPackages(ctx context.Context, wd string, patterns []string) ([]*packages.Package, error) { 12 | if len(patterns) == 0 { 13 | patterns = []string{"."} 14 | } 15 | 16 | patterns = append(make([]string, 0, len(patterns)), patterns...) 17 | for i, pattern := range patterns { 18 | patterns[i] = "pattern=" + pattern 19 | } 20 | 21 | cfg := &packages.Config{ 22 | Context: ctx, 23 | Mode: packages.LoadAllSyntax, 24 | Dir: wd, 25 | BuildFlags: []string{"-tags=mockc"}, 26 | } 27 | 28 | pkgs, err := packages.Load(cfg, patterns...) 29 | if err != nil { 30 | return nil, err 31 | } 32 | 33 | var errs []error 34 | for _, p := range pkgs { 35 | for _, e := range p.Errors { 36 | errs = append(errs, e) 37 | } 38 | } 39 | if len(errs) > 0 { 40 | var errMessage string 41 | for _, err := range errs { 42 | errMessage += fmt.Sprintf("\n\t%v", err) 43 | } 44 | 45 | return nil, errors.New(errMessage) 46 | } 47 | 48 | return pkgs, nil 49 | } 50 | -------------------------------------------------------------------------------- /internal/mockc/testdata/type-code/typecode.go: -------------------------------------------------------------------------------- 1 | package basic 2 | 3 | import ( 4 | "unsafe" 5 | ) 6 | 7 | type anon struct { 8 | C int 9 | D int8 10 | } 11 | 12 | type TypeCode interface { 13 | Bool(...bool) bool 14 | Int(...int) int 15 | Int8(...int8) int8 16 | Int16(...int16) int16 17 | Int32(...int32) int32 18 | Int64(...int64) int64 19 | Uint(...uint) uint 20 | Uint8(...uint8) uint8 21 | Uint16(...uint16) uint16 22 | Uint32(...uint32) uint32 23 | Uint64(...uint64) uint64 24 | Uintptr(...uintptr) uintptr 25 | Float32(...float32) float32 26 | Float64(...float64) float64 27 | Complex64(...complex64) complex64 28 | Complex128(...complex128) complex128 29 | String(...string) string 30 | Pointer(...unsafe.Pointer) unsafe.Pointer 31 | Byte(...byte) byte 32 | Rune(...rune) rune 33 | Array(...[0]bool) [0]bool 34 | Slice(...[]bool) []bool 35 | Struct(...struct{ 36 | A bool 37 | anon 38 | }) struct { 39 | B int 40 | } 41 | BoolP(...*bool) *bool 42 | Tuple() (bool, int, int8) 43 | Func(func(bool, int, ...int8) (int32, int64)) func() error 44 | Interface(...interface{}) interface{ 45 | Hello() string 46 | World() string 47 | } 48 | Map(...map[bool]int) map[bool]int 49 | Chan(...chan bool) (chan<- int, <-chan int8) 50 | } 51 | -------------------------------------------------------------------------------- /mockc.go: -------------------------------------------------------------------------------- 1 | package mockc 2 | 3 | // Implement designates the interfaces to be implemented. 4 | func Implement(i ...interface{}) {} 5 | 6 | // SetFieldNamePrefix sets the prefix of the mock's field names. 7 | func SetFieldNamePrefix(prefix string) {} 8 | 9 | // SetFieldNameSuffix sets the suffix of the mock's field names. 10 | func SetFieldNameSuffix(suffix string) {} 11 | 12 | // SetDestination sets the destination file where the mock will be generated. 13 | // SetDestination only uses the file name of the given destination. 14 | // If the destination is not a go file, the mock generation will fail. 15 | func SetDestination(destination string) {} 16 | 17 | // WithConstructor generates the constructor of mock. 18 | // You can set the underlying implementation by passing real implementation to the constructor. 19 | // 20 | // Internally, the WithConstructor is equivalent to the SetConstructorName("New" + MOCK_NAME) 21 | // 22 | // Check below example for details. 23 | // https://github.com/KimMachineGun/mockc/tree/master/examples/with-constructor 24 | func WithConstructor() {} 25 | 26 | // SetConstructorName sets the constructor name. 27 | // If the name is empty string, the constructor won't be generated. 28 | func SetConstructorName(name string) {} 29 | 30 | // Deprecated: Please use Implement instead. 31 | // Implements designates the interfaces to be implemented. 32 | func Implements(i ...interface{}) {} 33 | -------------------------------------------------------------------------------- /cmd/mockc/config.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "errors" 5 | "flag" 6 | ) 7 | 8 | type Config struct { 9 | destination string 10 | name string 11 | withConstructor bool 12 | fieldNamePrefix string 13 | fieldNameSuffix string 14 | args []string 15 | } 16 | 17 | func (c Config) IsGeneratorMode() bool { 18 | if c.name != "" || c.destination != "" { 19 | return false 20 | } 21 | 22 | return true 23 | } 24 | 25 | func (c Config) ValidateFlags() error { 26 | if c.name == "" { 27 | return errors.New("name flag is required in command line flags mode") 28 | } 29 | if c.destination == "" { 30 | return errors.New("destination flag is required in command line flags mode") 31 | } 32 | if c.fieldNamePrefix == "" && c.fieldNameSuffix == "" { 33 | return errors.New("at least one of the fieldNamePrefix and fieldNameSuffix must not be an empty string") 34 | } 35 | 36 | return nil 37 | } 38 | 39 | func LoadConfig() Config { 40 | var c Config 41 | 42 | flag.StringVar(&c.destination, "destination", "", "flag mode: mock file destination") 43 | flag.StringVar(&c.name, "name", "", "flag mode: name of the mock") 44 | flag.BoolVar(&c.withConstructor, "withConstructor", false, "flag mode: generate constructor") 45 | flag.StringVar(&c.fieldNamePrefix, "fieldNamePrefix", "_", "flag mode: prefix of the mock's field names") 46 | flag.StringVar(&c.fieldNameSuffix, "fieldNameSuffix", "", "flag mode: suffix of the mock's field names") 47 | 48 | flag.Parse() 49 | 50 | c.args = flag.Args() 51 | 52 | return c 53 | } 54 | -------------------------------------------------------------------------------- /examples/with-constructor/cache_test.go: -------------------------------------------------------------------------------- 1 | package constructor 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func HasKey(c Cache, key string) (bool, error) { 8 | val, err := c.Get(key) 9 | if err != nil { 10 | return false, err 11 | } 12 | 13 | return val != nil, nil 14 | } 15 | 16 | func TestHasKey_WithConstructor(t *testing.T) { 17 | m := NewMockcCache() 18 | 19 | // set return value 20 | m._Get.Results.R0 = struct{}{} 21 | 22 | // execute 23 | key := "test_key" 24 | result, err := HasKey(m, key) 25 | 26 | // assert 27 | if !result { 28 | t.Error("result should be true") 29 | } 30 | if err != nil { 31 | t.Error("err should be nil") 32 | } 33 | if m._Get.CallCount != 1 { 34 | t.Errorf("Cache.Get should be called once: actual(%d)", m._Get.CallCount) 35 | } 36 | if m._Get.Params.P0 != key { 37 | t.Errorf("Cache.Get should be called with %q: actual(%q)", key, m._Get.Params.P0) 38 | } 39 | } 40 | 41 | func TestHasKey_WithMapCache(t *testing.T) { 42 | // set the underlying implementation by passing real implementation to constructor 43 | m := NewMockcCache(MapCache{}) 44 | 45 | // execute 46 | key := "key" 47 | result, err := HasKey(m, key) 48 | 49 | // assert 50 | if result { 51 | t.Error("result should false") 52 | } 53 | if err != nil { 54 | t.Error("err should be nil") 55 | } 56 | if m._Get.CallCount != 1 { 57 | t.Errorf("Cache.Get should be called once: actual(%d)", m._Get.CallCount) 58 | } 59 | if m._Get.History[0].Params.P0 != key { 60 | t.Errorf("Cache.Get should be called with %q: actual(%q)", key, m._Get.History[0].Params.P0) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /examples/basic/cache_test.go: -------------------------------------------------------------------------------- 1 | package basic 2 | 3 | import ( 4 | "errors" 5 | "testing" 6 | ) 7 | 8 | func HasKey(c Cache, key string) (bool, error) { 9 | val, err := c.Get(key) 10 | if err != nil { 11 | return false, err 12 | } 13 | 14 | return val != nil, nil 15 | } 16 | 17 | func TestHasKey(t *testing.T) { 18 | m := &MockcCache{} 19 | 20 | // set return value 21 | m._Get.Results.R0 = struct{}{} 22 | 23 | // execute 24 | key := "test_key" 25 | result, err := HasKey(m, key) 26 | 27 | // assert 28 | if !result { 29 | t.Error("result should be true") 30 | } 31 | if err != nil { 32 | t.Error("err should be nil") 33 | } 34 | if m._Get.CallCount != 1 { 35 | t.Errorf("Cache.Get should be called once: actual(%d)", m._Get.CallCount) 36 | } 37 | if m._Get.Params.P0 != key { 38 | t.Errorf("Cache.Get should be called with %q: actual(%q)", key, m._Get.Params.P0) 39 | } 40 | } 41 | 42 | func TestHasKey_WithBodyInjection(t *testing.T) { 43 | m := &MockcCache{} 44 | 45 | // inject body 46 | key := "test_key" 47 | m._Get.Body = func(actualKey string) (interface{}, error) { 48 | if actualKey != key { 49 | t.Errorf("Cache.Get should be called with %q: actual(%q)", key, actualKey) 50 | } 51 | return nil, errors.New("error") 52 | } 53 | 54 | // execute 55 | result, err := HasKey(m, key) 56 | 57 | // assert 58 | if result { 59 | t.Error("result should be false") 60 | } 61 | if err == nil { 62 | t.Error("err should not be nil") 63 | } 64 | if m._Get.CallCount != 1 { 65 | t.Errorf("Cache.Get should be called once: actual(%d)", m._Get.CallCount) 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /examples/with-custom-constructor/cache_test.go: -------------------------------------------------------------------------------- 1 | package constructor 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func HasKey(c Cache, key string) (bool, error) { 8 | val, err := c.Get(key) 9 | if err != nil { 10 | return false, err 11 | } 12 | 13 | return val != nil, nil 14 | } 15 | 16 | func TestHasKey_WithConstructor(t *testing.T) { 17 | m := newMockCache() 18 | 19 | // set return value 20 | m._Get.Results.R0 = struct{}{} 21 | 22 | // execute 23 | key := "test_key" 24 | result, err := HasKey(m, key) 25 | 26 | // assert 27 | if !result { 28 | t.Error("result should be true") 29 | } 30 | if err != nil { 31 | t.Error("err should be nil") 32 | } 33 | if m._Get.CallCount != 1 { 34 | t.Errorf("Cache.Get should be called once: actual(%d)", m._Get.CallCount) 35 | } 36 | if m._Get.Params.P0 != key { 37 | t.Errorf("Cache.Get should be called with %q: actual(%q)", key, m._Get.Params.P0) 38 | } 39 | } 40 | 41 | func TestHasKey_WithMapCache(t *testing.T) { 42 | // set the underlying implementation by passing real implementation to constructor 43 | m := newMockCache(MapCache{}) 44 | 45 | // execute 46 | key := "key" 47 | result, err := HasKey(m, key) 48 | 49 | // assert 50 | if result { 51 | t.Error("result should false") 52 | } 53 | if err != nil { 54 | t.Error("err should be nil") 55 | } 56 | if m._Get.CallCount != 1 { 57 | t.Errorf("Cache.Get should be called once: actual(%d)", m._Get.CallCount) 58 | } 59 | if m._Get.History[0].Params.P0 != key { 60 | t.Errorf("Cache.Get should be called with %q: actual(%q)", key, m._Get.History[0].Params.P0) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /internal/mockc/mockc.go: -------------------------------------------------------------------------------- 1 | package mockc 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "path/filepath" 7 | "strings" 8 | ) 9 | 10 | const ( 11 | mockcPath = "github.com/KimMachineGun/mockc" 12 | defaultDestination = "mockc_gen.go" 13 | defaultFieldNamePrefix = "_" 14 | defaultFieldNameSuffix = "" 15 | ) 16 | 17 | func Generate(ctx context.Context, wd string, patterns []string) error { 18 | pkgs, err := loadPackages(ctx, wd, patterns) 19 | if err != nil { 20 | return fmt.Errorf("cannot load packages: %v", err) 21 | } 22 | 23 | for _, pkg := range pkgs { 24 | if _, ok := pkg.Imports[mockcPath]; !ok { 25 | continue 26 | } 27 | 28 | generators, err := newParser(pkg).parse() 29 | if err != nil { 30 | return err 31 | } 32 | 33 | for _, generator := range generators { 34 | err = generator.Generate("mockc") 35 | if err != nil { 36 | return fmt.Errorf("package %q: cannot generate mock: %v", pkg.PkgPath, err) 37 | } 38 | } 39 | } 40 | 41 | return nil 42 | } 43 | 44 | func GenerateWithFlags(ctx context.Context, wd string, destination string, name string, withConstructor bool, fieldNamePrefix string, fieldNameSuffix string, interfacePatterns []string) error { 45 | destination, err := filepath.Abs(destination) 46 | if err != nil { 47 | return fmt.Errorf("cannot convert destination into absolute path: %v", err) 48 | } 49 | 50 | destinationDir, fileName := filepath.Split(destination) 51 | if filepath.Ext(fileName) != ".go" { 52 | return fmt.Errorf("destination should be a go file: %s", fileName) 53 | } else if destinationDir == "" { 54 | destinationDir = "." 55 | } 56 | 57 | pkgs, err := loadPackages(ctx, wd, []string{destinationDir}) 58 | if err != nil { 59 | return fmt.Errorf("cannot load destination package: %v", err) 60 | } else if len(pkgs) != 1 { 61 | return fmt.Errorf("muptile destination packages are loaded: %v", pkgs) 62 | } 63 | 64 | generator := newGenerator(pkgs[0], destination) 65 | 66 | err = generator.addMockWithFlags(ctx, wd, name, withConstructor, fieldNamePrefix, fieldNameSuffix, interfacePatterns) 67 | if err != nil { 68 | return err 69 | } 70 | 71 | err = generator.Generate(fmt.Sprintf("mockc \"-destination=%s\" \"-name=%s\" \"-withConstructor=%t\" \"-fieldNamePrefix=%s\" \"-fieldNameSuffix=%s\" \"%s\"", fileName, name, withConstructor, fieldNamePrefix, fieldNameSuffix, strings.Join(interfacePatterns, " "))) 72 | if err != nil { 73 | return fmt.Errorf("cannot generate mock: %v", err) 74 | } 75 | 76 | return nil 77 | } 78 | -------------------------------------------------------------------------------- /internal/mockc/mockc_test.go: -------------------------------------------------------------------------------- 1 | package mockc 2 | 3 | import ( 4 | "bytes" 5 | "context" 6 | "encoding/json" 7 | "io/ioutil" 8 | "log" 9 | "os" 10 | "path/filepath" 11 | "regexp" 12 | "strings" 13 | "testing" 14 | 15 | "github.com/stretchr/testify/assert" 16 | ) 17 | 18 | const testRoot = "testdata" 19 | 20 | type testCase struct { 21 | name string 22 | path string 23 | goFiles map[string][]byte 24 | 25 | input struct { 26 | Patterns []string 27 | } 28 | output struct { 29 | Output string 30 | Err string 31 | } 32 | expectedFiles map[string][]byte 33 | } 34 | 35 | func TestMockc(t *testing.T) { 36 | a := assert.New(t) 37 | 38 | dirs, err := ioutil.ReadDir(testRoot) 39 | a.NoError(err) 40 | 41 | testCases := make([]testCase, 0, len(dirs)) 42 | for _, dir := range dirs { 43 | if !dir.IsDir() { 44 | continue 45 | } 46 | 47 | tc := testCase{ 48 | name: dir.Name(), 49 | path: filepath.Join(testRoot, dir.Name()), 50 | goFiles: map[string][]byte{}, 51 | expectedFiles: map[string][]byte{}, 52 | } 53 | 54 | goFiles, err := filepath.Glob(filepath.Join(tc.path, "*.go")) 55 | a.NoError(err) 56 | for _, f := range goFiles { 57 | b, err := ioutil.ReadFile(f) 58 | a.NoError(err) 59 | tc.goFiles[filepath.Base(f)] = b 60 | } 61 | 62 | input, err := ioutil.ReadFile(filepath.Join(tc.path, "testdata", "input.json")) 63 | a.NoError(err) 64 | 65 | err = json.Unmarshal(input, &tc.input) 66 | a.NoError(err) 67 | 68 | output, err := ioutil.ReadFile(filepath.Join(tc.path, "testdata", "output.json")) 69 | a.NoError(err) 70 | 71 | err = json.Unmarshal(output, &tc.output) 72 | a.NoError(err) 73 | 74 | expectedFiles, err := filepath.Glob(filepath.Join(tc.path, "testdata", "*.go.gen")) 75 | a.NoError(err) 76 | for _, f := range expectedFiles { 77 | b, err := ioutil.ReadFile(f) 78 | a.NoError(err) 79 | tc.expectedFiles[strings.TrimSuffix(filepath.Base(f), ".gen")] = b 80 | } 81 | 82 | testCases = append(testCases, tc) 83 | } 84 | 85 | for _, tc := range testCases { 86 | testMockc(t, tc) 87 | } 88 | } 89 | 90 | func testMockc(t *testing.T, tc testCase) { 91 | t.Run(tc.name, func(t *testing.T) { 92 | a := assert.New(t) 93 | 94 | buf := bytes.NewBuffer(nil) 95 | log.SetFlags(0) 96 | log.SetOutput(buf) 97 | 98 | err := Generate(context.Background(), tc.path, tc.input.Patterns) 99 | if tc.output.Err == "" { 100 | a.NoError(err) 101 | } else { 102 | a.EqualError(err, tc.output.Err) 103 | } 104 | a.Regexp(regexp.MustCompile(tc.output.Output), buf.String()) 105 | 106 | t.Cleanup(func() { 107 | for name := range tc.expectedFiles { 108 | err = os.Remove(filepath.Join(tc.path, name)) 109 | a.NoError(err) 110 | } 111 | }) 112 | 113 | for name, expected := range tc.expectedFiles { 114 | path := filepath.Join(tc.path, name) 115 | 116 | actual, err := ioutil.ReadFile(path) 117 | a.NoError(err) 118 | a.Equal(string(expected), string(actual)) 119 | } 120 | }) 121 | } 122 | -------------------------------------------------------------------------------- /examples/basic/mockc_gen.go: -------------------------------------------------------------------------------- 1 | // Code generated by Mockc. DO NOT EDIT. 2 | // repo: https://github.com/KimMachineGun/mockc 3 | 4 | //go:generate mockc 5 | // +build !mockc 6 | 7 | package basic 8 | 9 | import "sync" 10 | 11 | var _ interface { 12 | Cache 13 | } = &MockcCache{} 14 | 15 | type MockcCache struct { 16 | // method: Del 17 | _Del struct { 18 | mu sync.Mutex 19 | // basics 20 | Called bool 21 | CallCount int 22 | // call history 23 | History []struct { 24 | Params struct { 25 | P0 string 26 | } 27 | Results struct { 28 | R0 error 29 | } 30 | } 31 | // params 32 | Params struct { 33 | P0 string 34 | } 35 | // results 36 | Results struct { 37 | R0 error 38 | } 39 | // if it is not nil, it'll be called in the middle of the method. 40 | Body func(string) error 41 | } 42 | // method: Get 43 | _Get struct { 44 | mu sync.Mutex 45 | // basics 46 | Called bool 47 | CallCount int 48 | // call history 49 | History []struct { 50 | Params struct { 51 | P0 string 52 | } 53 | Results struct { 54 | R0 interface{} 55 | R1 error 56 | } 57 | } 58 | // params 59 | Params struct { 60 | P0 string 61 | } 62 | // results 63 | Results struct { 64 | R0 interface{} 65 | R1 error 66 | } 67 | // if it is not nil, it'll be called in the middle of the method. 68 | Body func(string) (interface{}, error) 69 | } 70 | // method: Set 71 | _Set struct { 72 | mu sync.Mutex 73 | // basics 74 | Called bool 75 | CallCount int 76 | // call history 77 | History []struct { 78 | Params struct { 79 | P0 string 80 | P1 interface{} 81 | } 82 | Results struct { 83 | R0 error 84 | } 85 | } 86 | // params 87 | Params struct { 88 | P0 string 89 | P1 interface{} 90 | } 91 | // results 92 | Results struct { 93 | R0 error 94 | } 95 | // if it is not nil, it'll be called in the middle of the method. 96 | Body func(string, interface{}) error 97 | } 98 | } 99 | 100 | func (recv *MockcCache) Del(p0 string) error { 101 | recv._Del.mu.Lock() 102 | defer recv._Del.mu.Unlock() 103 | // basics 104 | recv._Del.Called = true 105 | recv._Del.CallCount++ 106 | // params 107 | recv._Del.Params.P0 = p0 108 | // body 109 | if recv._Del.Body != nil { 110 | recv._Del.Results.R0 = recv._Del.Body(p0) 111 | } 112 | // call history 113 | recv._Del.History = append(recv._Del.History, struct { 114 | Params struct { 115 | P0 string 116 | } 117 | Results struct { 118 | R0 error 119 | } 120 | }{ 121 | Params: recv._Del.Params, 122 | Results: recv._Del.Results, 123 | }) 124 | // results 125 | return recv._Del.Results.R0 126 | } 127 | 128 | func (recv *MockcCache) Get(p0 string) (interface{}, error) { 129 | recv._Get.mu.Lock() 130 | defer recv._Get.mu.Unlock() 131 | // basics 132 | recv._Get.Called = true 133 | recv._Get.CallCount++ 134 | // params 135 | recv._Get.Params.P0 = p0 136 | // body 137 | if recv._Get.Body != nil { 138 | recv._Get.Results.R0, recv._Get.Results.R1 = recv._Get.Body(p0) 139 | } 140 | // call history 141 | recv._Get.History = append(recv._Get.History, struct { 142 | Params struct { 143 | P0 string 144 | } 145 | Results struct { 146 | R0 interface{} 147 | R1 error 148 | } 149 | }{ 150 | Params: recv._Get.Params, 151 | Results: recv._Get.Results, 152 | }) 153 | // results 154 | return recv._Get.Results.R0, recv._Get.Results.R1 155 | } 156 | 157 | func (recv *MockcCache) Set(p0 string, p1 interface{}) error { 158 | recv._Set.mu.Lock() 159 | defer recv._Set.mu.Unlock() 160 | // basics 161 | recv._Set.Called = true 162 | recv._Set.CallCount++ 163 | // params 164 | recv._Set.Params.P0 = p0 165 | recv._Set.Params.P1 = p1 166 | // body 167 | if recv._Set.Body != nil { 168 | recv._Set.Results.R0 = recv._Set.Body(p0, p1) 169 | } 170 | // call history 171 | recv._Set.History = append(recv._Set.History, struct { 172 | Params struct { 173 | P0 string 174 | P1 interface{} 175 | } 176 | Results struct { 177 | R0 error 178 | } 179 | }{ 180 | Params: recv._Set.Params, 181 | Results: recv._Set.Results, 182 | }) 183 | // results 184 | return recv._Set.Results.R0 185 | } 186 | -------------------------------------------------------------------------------- /internal/mockc/testdata/basic/testdata/mockc_gen.go.gen: -------------------------------------------------------------------------------- 1 | // Code generated by Mockc. DO NOT EDIT. 2 | // repo: https://github.com/KimMachineGun/mockc 3 | 4 | //go:generate mockc 5 | // +build !mockc 6 | 7 | package basic 8 | 9 | import "sync" 10 | 11 | var _ interface { 12 | Cache 13 | } = &MockcCache{} 14 | 15 | type MockcCache struct { 16 | // method: Del 17 | _Del struct { 18 | mu sync.Mutex 19 | // basics 20 | Called bool 21 | CallCount int 22 | // call history 23 | History []struct { 24 | Params struct { 25 | P0 string 26 | } 27 | Results struct { 28 | R0 error 29 | } 30 | } 31 | // params 32 | Params struct { 33 | P0 string 34 | } 35 | // results 36 | Results struct { 37 | R0 error 38 | } 39 | // if it is not nil, it'll be called in the middle of the method. 40 | Body func(string) error 41 | } 42 | // method: Get 43 | _Get struct { 44 | mu sync.Mutex 45 | // basics 46 | Called bool 47 | CallCount int 48 | // call history 49 | History []struct { 50 | Params struct { 51 | P0 string 52 | } 53 | Results struct { 54 | R0 interface{} 55 | R1 error 56 | } 57 | } 58 | // params 59 | Params struct { 60 | P0 string 61 | } 62 | // results 63 | Results struct { 64 | R0 interface{} 65 | R1 error 66 | } 67 | // if it is not nil, it'll be called in the middle of the method. 68 | Body func(string) (interface{}, error) 69 | } 70 | // method: Set 71 | _Set struct { 72 | mu sync.Mutex 73 | // basics 74 | Called bool 75 | CallCount int 76 | // call history 77 | History []struct { 78 | Params struct { 79 | P0 string 80 | P1 interface{} 81 | } 82 | Results struct { 83 | R0 error 84 | } 85 | } 86 | // params 87 | Params struct { 88 | P0 string 89 | P1 interface{} 90 | } 91 | // results 92 | Results struct { 93 | R0 error 94 | } 95 | // if it is not nil, it'll be called in the middle of the method. 96 | Body func(string, interface{}) error 97 | } 98 | } 99 | 100 | func (recv *MockcCache) Del(p0 string) error { 101 | recv._Del.mu.Lock() 102 | defer recv._Del.mu.Unlock() 103 | // basics 104 | recv._Del.Called = true 105 | recv._Del.CallCount++ 106 | // params 107 | recv._Del.Params.P0 = p0 108 | // body 109 | if recv._Del.Body != nil { 110 | recv._Del.Results.R0 = recv._Del.Body(p0) 111 | } 112 | // call history 113 | recv._Del.History = append(recv._Del.History, struct { 114 | Params struct { 115 | P0 string 116 | } 117 | Results struct { 118 | R0 error 119 | } 120 | }{ 121 | Params: recv._Del.Params, 122 | Results: recv._Del.Results, 123 | }) 124 | // results 125 | return recv._Del.Results.R0 126 | } 127 | 128 | func (recv *MockcCache) Get(p0 string) (interface{}, error) { 129 | recv._Get.mu.Lock() 130 | defer recv._Get.mu.Unlock() 131 | // basics 132 | recv._Get.Called = true 133 | recv._Get.CallCount++ 134 | // params 135 | recv._Get.Params.P0 = p0 136 | // body 137 | if recv._Get.Body != nil { 138 | recv._Get.Results.R0, recv._Get.Results.R1 = recv._Get.Body(p0) 139 | } 140 | // call history 141 | recv._Get.History = append(recv._Get.History, struct { 142 | Params struct { 143 | P0 string 144 | } 145 | Results struct { 146 | R0 interface{} 147 | R1 error 148 | } 149 | }{ 150 | Params: recv._Get.Params, 151 | Results: recv._Get.Results, 152 | }) 153 | // results 154 | return recv._Get.Results.R0, recv._Get.Results.R1 155 | } 156 | 157 | func (recv *MockcCache) Set(p0 string, p1 interface{}) error { 158 | recv._Set.mu.Lock() 159 | defer recv._Set.mu.Unlock() 160 | // basics 161 | recv._Set.Called = true 162 | recv._Set.CallCount++ 163 | // params 164 | recv._Set.Params.P0 = p0 165 | recv._Set.Params.P1 = p1 166 | // body 167 | if recv._Set.Body != nil { 168 | recv._Set.Results.R0 = recv._Set.Body(p0, p1) 169 | } 170 | // call history 171 | recv._Set.History = append(recv._Set.History, struct { 172 | Params struct { 173 | P0 string 174 | P1 interface{} 175 | } 176 | Results struct { 177 | R0 error 178 | } 179 | }{ 180 | Params: recv._Set.Params, 181 | Results: recv._Set.Results, 182 | }) 183 | // results 184 | return recv._Set.Results.R0 185 | } 186 | -------------------------------------------------------------------------------- /internal/mockc/testdata/implements/testdata/mockc_gen.go.gen: -------------------------------------------------------------------------------- 1 | // Code generated by Mockc. DO NOT EDIT. 2 | // repo: https://github.com/KimMachineGun/mockc 3 | 4 | //go:generate mockc 5 | // +build !mockc 6 | 7 | package basic 8 | 9 | import "sync" 10 | 11 | var _ interface { 12 | Cache 13 | } = &MockcCache{} 14 | 15 | type MockcCache struct { 16 | // method: Del 17 | _Del struct { 18 | mu sync.Mutex 19 | // basics 20 | Called bool 21 | CallCount int 22 | // call history 23 | History []struct { 24 | Params struct { 25 | P0 string 26 | } 27 | Results struct { 28 | R0 error 29 | } 30 | } 31 | // params 32 | Params struct { 33 | P0 string 34 | } 35 | // results 36 | Results struct { 37 | R0 error 38 | } 39 | // if it is not nil, it'll be called in the middle of the method. 40 | Body func(string) error 41 | } 42 | // method: Get 43 | _Get struct { 44 | mu sync.Mutex 45 | // basics 46 | Called bool 47 | CallCount int 48 | // call history 49 | History []struct { 50 | Params struct { 51 | P0 string 52 | } 53 | Results struct { 54 | R0 interface{} 55 | R1 error 56 | } 57 | } 58 | // params 59 | Params struct { 60 | P0 string 61 | } 62 | // results 63 | Results struct { 64 | R0 interface{} 65 | R1 error 66 | } 67 | // if it is not nil, it'll be called in the middle of the method. 68 | Body func(string) (interface{}, error) 69 | } 70 | // method: Set 71 | _Set struct { 72 | mu sync.Mutex 73 | // basics 74 | Called bool 75 | CallCount int 76 | // call history 77 | History []struct { 78 | Params struct { 79 | P0 string 80 | P1 interface{} 81 | } 82 | Results struct { 83 | R0 error 84 | } 85 | } 86 | // params 87 | Params struct { 88 | P0 string 89 | P1 interface{} 90 | } 91 | // results 92 | Results struct { 93 | R0 error 94 | } 95 | // if it is not nil, it'll be called in the middle of the method. 96 | Body func(string, interface{}) error 97 | } 98 | } 99 | 100 | func (recv *MockcCache) Del(p0 string) error { 101 | recv._Del.mu.Lock() 102 | defer recv._Del.mu.Unlock() 103 | // basics 104 | recv._Del.Called = true 105 | recv._Del.CallCount++ 106 | // params 107 | recv._Del.Params.P0 = p0 108 | // body 109 | if recv._Del.Body != nil { 110 | recv._Del.Results.R0 = recv._Del.Body(p0) 111 | } 112 | // call history 113 | recv._Del.History = append(recv._Del.History, struct { 114 | Params struct { 115 | P0 string 116 | } 117 | Results struct { 118 | R0 error 119 | } 120 | }{ 121 | Params: recv._Del.Params, 122 | Results: recv._Del.Results, 123 | }) 124 | // results 125 | return recv._Del.Results.R0 126 | } 127 | 128 | func (recv *MockcCache) Get(p0 string) (interface{}, error) { 129 | recv._Get.mu.Lock() 130 | defer recv._Get.mu.Unlock() 131 | // basics 132 | recv._Get.Called = true 133 | recv._Get.CallCount++ 134 | // params 135 | recv._Get.Params.P0 = p0 136 | // body 137 | if recv._Get.Body != nil { 138 | recv._Get.Results.R0, recv._Get.Results.R1 = recv._Get.Body(p0) 139 | } 140 | // call history 141 | recv._Get.History = append(recv._Get.History, struct { 142 | Params struct { 143 | P0 string 144 | } 145 | Results struct { 146 | R0 interface{} 147 | R1 error 148 | } 149 | }{ 150 | Params: recv._Get.Params, 151 | Results: recv._Get.Results, 152 | }) 153 | // results 154 | return recv._Get.Results.R0, recv._Get.Results.R1 155 | } 156 | 157 | func (recv *MockcCache) Set(p0 string, p1 interface{}) error { 158 | recv._Set.mu.Lock() 159 | defer recv._Set.mu.Unlock() 160 | // basics 161 | recv._Set.Called = true 162 | recv._Set.CallCount++ 163 | // params 164 | recv._Set.Params.P0 = p0 165 | recv._Set.Params.P1 = p1 166 | // body 167 | if recv._Set.Body != nil { 168 | recv._Set.Results.R0 = recv._Set.Body(p0, p1) 169 | } 170 | // call history 171 | recv._Set.History = append(recv._Set.History, struct { 172 | Params struct { 173 | P0 string 174 | P1 interface{} 175 | } 176 | Results struct { 177 | R0 error 178 | } 179 | }{ 180 | Params: recv._Set.Params, 181 | Results: recv._Set.Results, 182 | }) 183 | // results 184 | return recv._Set.Results.R0 185 | } 186 | -------------------------------------------------------------------------------- /internal/mockc/testdata/custom-destination/testdata/custom_destination.go.gen: -------------------------------------------------------------------------------- 1 | // Code generated by Mockc. DO NOT EDIT. 2 | // repo: https://github.com/KimMachineGun/mockc 3 | 4 | //go:generate mockc 5 | // +build !mockc 6 | 7 | package basic 8 | 9 | import "sync" 10 | 11 | var _ interface { 12 | Cache 13 | } = &MockcCache{} 14 | 15 | type MockcCache struct { 16 | // method: Del 17 | _Del struct { 18 | mu sync.Mutex 19 | // basics 20 | Called bool 21 | CallCount int 22 | // call history 23 | History []struct { 24 | Params struct { 25 | P0 string 26 | } 27 | Results struct { 28 | R0 error 29 | } 30 | } 31 | // params 32 | Params struct { 33 | P0 string 34 | } 35 | // results 36 | Results struct { 37 | R0 error 38 | } 39 | // if it is not nil, it'll be called in the middle of the method. 40 | Body func(string) error 41 | } 42 | // method: Get 43 | _Get struct { 44 | mu sync.Mutex 45 | // basics 46 | Called bool 47 | CallCount int 48 | // call history 49 | History []struct { 50 | Params struct { 51 | P0 string 52 | } 53 | Results struct { 54 | R0 interface{} 55 | R1 error 56 | } 57 | } 58 | // params 59 | Params struct { 60 | P0 string 61 | } 62 | // results 63 | Results struct { 64 | R0 interface{} 65 | R1 error 66 | } 67 | // if it is not nil, it'll be called in the middle of the method. 68 | Body func(string) (interface{}, error) 69 | } 70 | // method: Set 71 | _Set struct { 72 | mu sync.Mutex 73 | // basics 74 | Called bool 75 | CallCount int 76 | // call history 77 | History []struct { 78 | Params struct { 79 | P0 string 80 | P1 interface{} 81 | } 82 | Results struct { 83 | R0 error 84 | } 85 | } 86 | // params 87 | Params struct { 88 | P0 string 89 | P1 interface{} 90 | } 91 | // results 92 | Results struct { 93 | R0 error 94 | } 95 | // if it is not nil, it'll be called in the middle of the method. 96 | Body func(string, interface{}) error 97 | } 98 | } 99 | 100 | func (recv *MockcCache) Del(p0 string) error { 101 | recv._Del.mu.Lock() 102 | defer recv._Del.mu.Unlock() 103 | // basics 104 | recv._Del.Called = true 105 | recv._Del.CallCount++ 106 | // params 107 | recv._Del.Params.P0 = p0 108 | // body 109 | if recv._Del.Body != nil { 110 | recv._Del.Results.R0 = recv._Del.Body(p0) 111 | } 112 | // call history 113 | recv._Del.History = append(recv._Del.History, struct { 114 | Params struct { 115 | P0 string 116 | } 117 | Results struct { 118 | R0 error 119 | } 120 | }{ 121 | Params: recv._Del.Params, 122 | Results: recv._Del.Results, 123 | }) 124 | // results 125 | return recv._Del.Results.R0 126 | } 127 | 128 | func (recv *MockcCache) Get(p0 string) (interface{}, error) { 129 | recv._Get.mu.Lock() 130 | defer recv._Get.mu.Unlock() 131 | // basics 132 | recv._Get.Called = true 133 | recv._Get.CallCount++ 134 | // params 135 | recv._Get.Params.P0 = p0 136 | // body 137 | if recv._Get.Body != nil { 138 | recv._Get.Results.R0, recv._Get.Results.R1 = recv._Get.Body(p0) 139 | } 140 | // call history 141 | recv._Get.History = append(recv._Get.History, struct { 142 | Params struct { 143 | P0 string 144 | } 145 | Results struct { 146 | R0 interface{} 147 | R1 error 148 | } 149 | }{ 150 | Params: recv._Get.Params, 151 | Results: recv._Get.Results, 152 | }) 153 | // results 154 | return recv._Get.Results.R0, recv._Get.Results.R1 155 | } 156 | 157 | func (recv *MockcCache) Set(p0 string, p1 interface{}) error { 158 | recv._Set.mu.Lock() 159 | defer recv._Set.mu.Unlock() 160 | // basics 161 | recv._Set.Called = true 162 | recv._Set.CallCount++ 163 | // params 164 | recv._Set.Params.P0 = p0 165 | recv._Set.Params.P1 = p1 166 | // body 167 | if recv._Set.Body != nil { 168 | recv._Set.Results.R0 = recv._Set.Body(p0, p1) 169 | } 170 | // call history 171 | recv._Set.History = append(recv._Set.History, struct { 172 | Params struct { 173 | P0 string 174 | P1 interface{} 175 | } 176 | Results struct { 177 | R0 error 178 | } 179 | }{ 180 | Params: recv._Set.Params, 181 | Results: recv._Set.Results, 182 | }) 183 | // results 184 | return recv._Set.Results.R0 185 | } 186 | -------------------------------------------------------------------------------- /internal/mockc/testdata/custom-field-name/testdata/mockc_gen.go.gen: -------------------------------------------------------------------------------- 1 | // Code generated by Mockc. DO NOT EDIT. 2 | // repo: https://github.com/KimMachineGun/mockc 3 | 4 | //go:generate mockc 5 | // +build !mockc 6 | 7 | package basic 8 | 9 | import "sync" 10 | 11 | var _ interface { 12 | Cache 13 | } = &MockcCache{} 14 | 15 | type MockcCache struct { 16 | // method: Del 17 | DelFunc struct { 18 | mu sync.Mutex 19 | // basics 20 | Called bool 21 | CallCount int 22 | // call history 23 | History []struct { 24 | Params struct { 25 | P0 string 26 | } 27 | Results struct { 28 | R0 error 29 | } 30 | } 31 | // params 32 | Params struct { 33 | P0 string 34 | } 35 | // results 36 | Results struct { 37 | R0 error 38 | } 39 | // if it is not nil, it'll be called in the middle of the method. 40 | Body func(string) error 41 | } 42 | // method: Get 43 | GetFunc struct { 44 | mu sync.Mutex 45 | // basics 46 | Called bool 47 | CallCount int 48 | // call history 49 | History []struct { 50 | Params struct { 51 | P0 string 52 | } 53 | Results struct { 54 | R0 interface{} 55 | R1 error 56 | } 57 | } 58 | // params 59 | Params struct { 60 | P0 string 61 | } 62 | // results 63 | Results struct { 64 | R0 interface{} 65 | R1 error 66 | } 67 | // if it is not nil, it'll be called in the middle of the method. 68 | Body func(string) (interface{}, error) 69 | } 70 | // method: Set 71 | SetFunc struct { 72 | mu sync.Mutex 73 | // basics 74 | Called bool 75 | CallCount int 76 | // call history 77 | History []struct { 78 | Params struct { 79 | P0 string 80 | P1 interface{} 81 | } 82 | Results struct { 83 | R0 error 84 | } 85 | } 86 | // params 87 | Params struct { 88 | P0 string 89 | P1 interface{} 90 | } 91 | // results 92 | Results struct { 93 | R0 error 94 | } 95 | // if it is not nil, it'll be called in the middle of the method. 96 | Body func(string, interface{}) error 97 | } 98 | } 99 | 100 | func (recv *MockcCache) Del(p0 string) error { 101 | recv.DelFunc.mu.Lock() 102 | defer recv.DelFunc.mu.Unlock() 103 | // basics 104 | recv.DelFunc.Called = true 105 | recv.DelFunc.CallCount++ 106 | // params 107 | recv.DelFunc.Params.P0 = p0 108 | // body 109 | if recv.DelFunc.Body != nil { 110 | recv.DelFunc.Results.R0 = recv.DelFunc.Body(p0) 111 | } 112 | // call history 113 | recv.DelFunc.History = append(recv.DelFunc.History, struct { 114 | Params struct { 115 | P0 string 116 | } 117 | Results struct { 118 | R0 error 119 | } 120 | }{ 121 | Params: recv.DelFunc.Params, 122 | Results: recv.DelFunc.Results, 123 | }) 124 | // results 125 | return recv.DelFunc.Results.R0 126 | } 127 | 128 | func (recv *MockcCache) Get(p0 string) (interface{}, error) { 129 | recv.GetFunc.mu.Lock() 130 | defer recv.GetFunc.mu.Unlock() 131 | // basics 132 | recv.GetFunc.Called = true 133 | recv.GetFunc.CallCount++ 134 | // params 135 | recv.GetFunc.Params.P0 = p0 136 | // body 137 | if recv.GetFunc.Body != nil { 138 | recv.GetFunc.Results.R0, recv.GetFunc.Results.R1 = recv.GetFunc.Body(p0) 139 | } 140 | // call history 141 | recv.GetFunc.History = append(recv.GetFunc.History, struct { 142 | Params struct { 143 | P0 string 144 | } 145 | Results struct { 146 | R0 interface{} 147 | R1 error 148 | } 149 | }{ 150 | Params: recv.GetFunc.Params, 151 | Results: recv.GetFunc.Results, 152 | }) 153 | // results 154 | return recv.GetFunc.Results.R0, recv.GetFunc.Results.R1 155 | } 156 | 157 | func (recv *MockcCache) Set(p0 string, p1 interface{}) error { 158 | recv.SetFunc.mu.Lock() 159 | defer recv.SetFunc.mu.Unlock() 160 | // basics 161 | recv.SetFunc.Called = true 162 | recv.SetFunc.CallCount++ 163 | // params 164 | recv.SetFunc.Params.P0 = p0 165 | recv.SetFunc.Params.P1 = p1 166 | // body 167 | if recv.SetFunc.Body != nil { 168 | recv.SetFunc.Results.R0 = recv.SetFunc.Body(p0, p1) 169 | } 170 | // call history 171 | recv.SetFunc.History = append(recv.SetFunc.History, struct { 172 | Params struct { 173 | P0 string 174 | P1 interface{} 175 | } 176 | Results struct { 177 | R0 error 178 | } 179 | }{ 180 | Params: recv.SetFunc.Params, 181 | Results: recv.SetFunc.Results, 182 | }) 183 | // results 184 | return recv.SetFunc.Results.R0 185 | } 186 | -------------------------------------------------------------------------------- /examples/with-constructor/mockc_gen.go: -------------------------------------------------------------------------------- 1 | // Code generated by Mockc. DO NOT EDIT. 2 | // repo: https://github.com/KimMachineGun/mockc 3 | 4 | //go:generate mockc 5 | // +build !mockc 6 | 7 | package constructor 8 | 9 | import "sync" 10 | 11 | var _ interface { 12 | Cache 13 | } = &MockcCache{} 14 | 15 | type MockcCache struct { 16 | // method: Del 17 | _Del struct { 18 | mu sync.Mutex 19 | // basics 20 | Called bool 21 | CallCount int 22 | // call history 23 | History []struct { 24 | Params struct { 25 | P0 string 26 | } 27 | Results struct { 28 | R0 error 29 | } 30 | } 31 | // params 32 | Params struct { 33 | P0 string 34 | } 35 | // results 36 | Results struct { 37 | R0 error 38 | } 39 | // if it is not nil, it'll be called in the middle of the method. 40 | Body func(string) error 41 | } 42 | // method: Get 43 | _Get struct { 44 | mu sync.Mutex 45 | // basics 46 | Called bool 47 | CallCount int 48 | // call history 49 | History []struct { 50 | Params struct { 51 | P0 string 52 | } 53 | Results struct { 54 | R0 interface{} 55 | R1 error 56 | } 57 | } 58 | // params 59 | Params struct { 60 | P0 string 61 | } 62 | // results 63 | Results struct { 64 | R0 interface{} 65 | R1 error 66 | } 67 | // if it is not nil, it'll be called in the middle of the method. 68 | Body func(string) (interface{}, error) 69 | } 70 | // method: Set 71 | _Set struct { 72 | mu sync.Mutex 73 | // basics 74 | Called bool 75 | CallCount int 76 | // call history 77 | History []struct { 78 | Params struct { 79 | P0 string 80 | P1 interface{} 81 | } 82 | Results struct { 83 | R0 error 84 | } 85 | } 86 | // params 87 | Params struct { 88 | P0 string 89 | P1 interface{} 90 | } 91 | // results 92 | Results struct { 93 | R0 error 94 | } 95 | // if it is not nil, it'll be called in the middle of the method. 96 | Body func(string, interface{}) error 97 | } 98 | } 99 | 100 | func NewMockcCache(v ...interface { 101 | Cache 102 | }) *MockcCache { 103 | m := &MockcCache{} 104 | if len(v) > 0 { 105 | m._Del.Body = v[0].Del 106 | m._Get.Body = v[0].Get 107 | m._Set.Body = v[0].Set 108 | } 109 | return m 110 | } 111 | 112 | func (recv *MockcCache) Del(p0 string) error { 113 | recv._Del.mu.Lock() 114 | defer recv._Del.mu.Unlock() 115 | // basics 116 | recv._Del.Called = true 117 | recv._Del.CallCount++ 118 | // params 119 | recv._Del.Params.P0 = p0 120 | // body 121 | if recv._Del.Body != nil { 122 | recv._Del.Results.R0 = recv._Del.Body(p0) 123 | } 124 | // call history 125 | recv._Del.History = append(recv._Del.History, struct { 126 | Params struct { 127 | P0 string 128 | } 129 | Results struct { 130 | R0 error 131 | } 132 | }{ 133 | Params: recv._Del.Params, 134 | Results: recv._Del.Results, 135 | }) 136 | // results 137 | return recv._Del.Results.R0 138 | } 139 | 140 | func (recv *MockcCache) Get(p0 string) (interface{}, error) { 141 | recv._Get.mu.Lock() 142 | defer recv._Get.mu.Unlock() 143 | // basics 144 | recv._Get.Called = true 145 | recv._Get.CallCount++ 146 | // params 147 | recv._Get.Params.P0 = p0 148 | // body 149 | if recv._Get.Body != nil { 150 | recv._Get.Results.R0, recv._Get.Results.R1 = recv._Get.Body(p0) 151 | } 152 | // call history 153 | recv._Get.History = append(recv._Get.History, struct { 154 | Params struct { 155 | P0 string 156 | } 157 | Results struct { 158 | R0 interface{} 159 | R1 error 160 | } 161 | }{ 162 | Params: recv._Get.Params, 163 | Results: recv._Get.Results, 164 | }) 165 | // results 166 | return recv._Get.Results.R0, recv._Get.Results.R1 167 | } 168 | 169 | func (recv *MockcCache) Set(p0 string, p1 interface{}) error { 170 | recv._Set.mu.Lock() 171 | defer recv._Set.mu.Unlock() 172 | // basics 173 | recv._Set.Called = true 174 | recv._Set.CallCount++ 175 | // params 176 | recv._Set.Params.P0 = p0 177 | recv._Set.Params.P1 = p1 178 | // body 179 | if recv._Set.Body != nil { 180 | recv._Set.Results.R0 = recv._Set.Body(p0, p1) 181 | } 182 | // call history 183 | recv._Set.History = append(recv._Set.History, struct { 184 | Params struct { 185 | P0 string 186 | P1 interface{} 187 | } 188 | Results struct { 189 | R0 error 190 | } 191 | }{ 192 | Params: recv._Set.Params, 193 | Results: recv._Set.Results, 194 | }) 195 | // results 196 | return recv._Set.Results.R0 197 | } 198 | -------------------------------------------------------------------------------- /examples/with-custom-constructor/mockc_gen.go: -------------------------------------------------------------------------------- 1 | // Code generated by Mockc. DO NOT EDIT. 2 | // repo: https://github.com/KimMachineGun/mockc 3 | 4 | //go:generate mockc 5 | // +build !mockc 6 | 7 | package constructor 8 | 9 | import "sync" 10 | 11 | var _ interface { 12 | Cache 13 | } = &MockcCache{} 14 | 15 | type MockcCache struct { 16 | // method: Del 17 | _Del struct { 18 | mu sync.Mutex 19 | // basics 20 | Called bool 21 | CallCount int 22 | // call history 23 | History []struct { 24 | Params struct { 25 | P0 string 26 | } 27 | Results struct { 28 | R0 error 29 | } 30 | } 31 | // params 32 | Params struct { 33 | P0 string 34 | } 35 | // results 36 | Results struct { 37 | R0 error 38 | } 39 | // if it is not nil, it'll be called in the middle of the method. 40 | Body func(string) error 41 | } 42 | // method: Get 43 | _Get struct { 44 | mu sync.Mutex 45 | // basics 46 | Called bool 47 | CallCount int 48 | // call history 49 | History []struct { 50 | Params struct { 51 | P0 string 52 | } 53 | Results struct { 54 | R0 interface{} 55 | R1 error 56 | } 57 | } 58 | // params 59 | Params struct { 60 | P0 string 61 | } 62 | // results 63 | Results struct { 64 | R0 interface{} 65 | R1 error 66 | } 67 | // if it is not nil, it'll be called in the middle of the method. 68 | Body func(string) (interface{}, error) 69 | } 70 | // method: Set 71 | _Set struct { 72 | mu sync.Mutex 73 | // basics 74 | Called bool 75 | CallCount int 76 | // call history 77 | History []struct { 78 | Params struct { 79 | P0 string 80 | P1 interface{} 81 | } 82 | Results struct { 83 | R0 error 84 | } 85 | } 86 | // params 87 | Params struct { 88 | P0 string 89 | P1 interface{} 90 | } 91 | // results 92 | Results struct { 93 | R0 error 94 | } 95 | // if it is not nil, it'll be called in the middle of the method. 96 | Body func(string, interface{}) error 97 | } 98 | } 99 | 100 | func newMockCache(v ...interface { 101 | Cache 102 | }) *MockcCache { 103 | m := &MockcCache{} 104 | if len(v) > 0 { 105 | m._Del.Body = v[0].Del 106 | m._Get.Body = v[0].Get 107 | m._Set.Body = v[0].Set 108 | } 109 | return m 110 | } 111 | 112 | func (recv *MockcCache) Del(p0 string) error { 113 | recv._Del.mu.Lock() 114 | defer recv._Del.mu.Unlock() 115 | // basics 116 | recv._Del.Called = true 117 | recv._Del.CallCount++ 118 | // params 119 | recv._Del.Params.P0 = p0 120 | // body 121 | if recv._Del.Body != nil { 122 | recv._Del.Results.R0 = recv._Del.Body(p0) 123 | } 124 | // call history 125 | recv._Del.History = append(recv._Del.History, struct { 126 | Params struct { 127 | P0 string 128 | } 129 | Results struct { 130 | R0 error 131 | } 132 | }{ 133 | Params: recv._Del.Params, 134 | Results: recv._Del.Results, 135 | }) 136 | // results 137 | return recv._Del.Results.R0 138 | } 139 | 140 | func (recv *MockcCache) Get(p0 string) (interface{}, error) { 141 | recv._Get.mu.Lock() 142 | defer recv._Get.mu.Unlock() 143 | // basics 144 | recv._Get.Called = true 145 | recv._Get.CallCount++ 146 | // params 147 | recv._Get.Params.P0 = p0 148 | // body 149 | if recv._Get.Body != nil { 150 | recv._Get.Results.R0, recv._Get.Results.R1 = recv._Get.Body(p0) 151 | } 152 | // call history 153 | recv._Get.History = append(recv._Get.History, struct { 154 | Params struct { 155 | P0 string 156 | } 157 | Results struct { 158 | R0 interface{} 159 | R1 error 160 | } 161 | }{ 162 | Params: recv._Get.Params, 163 | Results: recv._Get.Results, 164 | }) 165 | // results 166 | return recv._Get.Results.R0, recv._Get.Results.R1 167 | } 168 | 169 | func (recv *MockcCache) Set(p0 string, p1 interface{}) error { 170 | recv._Set.mu.Lock() 171 | defer recv._Set.mu.Unlock() 172 | // basics 173 | recv._Set.Called = true 174 | recv._Set.CallCount++ 175 | // params 176 | recv._Set.Params.P0 = p0 177 | recv._Set.Params.P1 = p1 178 | // body 179 | if recv._Set.Body != nil { 180 | recv._Set.Results.R0 = recv._Set.Body(p0, p1) 181 | } 182 | // call history 183 | recv._Set.History = append(recv._Set.History, struct { 184 | Params struct { 185 | P0 string 186 | P1 interface{} 187 | } 188 | Results struct { 189 | R0 error 190 | } 191 | }{ 192 | Params: recv._Set.Params, 193 | Results: recv._Set.Results, 194 | }) 195 | // results 196 | return recv._Set.Results.R0 197 | } 198 | -------------------------------------------------------------------------------- /internal/mockc/testdata/with-constructor/testdata/mockc_gen.go.gen: -------------------------------------------------------------------------------- 1 | // Code generated by Mockc. DO NOT EDIT. 2 | // repo: https://github.com/KimMachineGun/mockc 3 | 4 | //go:generate mockc 5 | // +build !mockc 6 | 7 | package constructor 8 | 9 | import "sync" 10 | 11 | var _ interface { 12 | Cache 13 | } = &MockcCache{} 14 | 15 | type MockcCache struct { 16 | // method: Del 17 | _Del struct { 18 | mu sync.Mutex 19 | // basics 20 | Called bool 21 | CallCount int 22 | // call history 23 | History []struct { 24 | Params struct { 25 | P0 string 26 | } 27 | Results struct { 28 | R0 error 29 | } 30 | } 31 | // params 32 | Params struct { 33 | P0 string 34 | } 35 | // results 36 | Results struct { 37 | R0 error 38 | } 39 | // if it is not nil, it'll be called in the middle of the method. 40 | Body func(string) error 41 | } 42 | // method: Get 43 | _Get struct { 44 | mu sync.Mutex 45 | // basics 46 | Called bool 47 | CallCount int 48 | // call history 49 | History []struct { 50 | Params struct { 51 | P0 string 52 | } 53 | Results struct { 54 | R0 interface{} 55 | R1 error 56 | } 57 | } 58 | // params 59 | Params struct { 60 | P0 string 61 | } 62 | // results 63 | Results struct { 64 | R0 interface{} 65 | R1 error 66 | } 67 | // if it is not nil, it'll be called in the middle of the method. 68 | Body func(string) (interface{}, error) 69 | } 70 | // method: Set 71 | _Set struct { 72 | mu sync.Mutex 73 | // basics 74 | Called bool 75 | CallCount int 76 | // call history 77 | History []struct { 78 | Params struct { 79 | P0 string 80 | P1 interface{} 81 | } 82 | Results struct { 83 | R0 error 84 | } 85 | } 86 | // params 87 | Params struct { 88 | P0 string 89 | P1 interface{} 90 | } 91 | // results 92 | Results struct { 93 | R0 error 94 | } 95 | // if it is not nil, it'll be called in the middle of the method. 96 | Body func(string, interface{}) error 97 | } 98 | } 99 | 100 | func NewMockcCache(v ...interface { 101 | Cache 102 | }) *MockcCache { 103 | m := &MockcCache{} 104 | if len(v) > 0 { 105 | m._Del.Body = v[0].Del 106 | m._Get.Body = v[0].Get 107 | m._Set.Body = v[0].Set 108 | } 109 | return m 110 | } 111 | 112 | func (recv *MockcCache) Del(p0 string) error { 113 | recv._Del.mu.Lock() 114 | defer recv._Del.mu.Unlock() 115 | // basics 116 | recv._Del.Called = true 117 | recv._Del.CallCount++ 118 | // params 119 | recv._Del.Params.P0 = p0 120 | // body 121 | if recv._Del.Body != nil { 122 | recv._Del.Results.R0 = recv._Del.Body(p0) 123 | } 124 | // call history 125 | recv._Del.History = append(recv._Del.History, struct { 126 | Params struct { 127 | P0 string 128 | } 129 | Results struct { 130 | R0 error 131 | } 132 | }{ 133 | Params: recv._Del.Params, 134 | Results: recv._Del.Results, 135 | }) 136 | // results 137 | return recv._Del.Results.R0 138 | } 139 | 140 | func (recv *MockcCache) Get(p0 string) (interface{}, error) { 141 | recv._Get.mu.Lock() 142 | defer recv._Get.mu.Unlock() 143 | // basics 144 | recv._Get.Called = true 145 | recv._Get.CallCount++ 146 | // params 147 | recv._Get.Params.P0 = p0 148 | // body 149 | if recv._Get.Body != nil { 150 | recv._Get.Results.R0, recv._Get.Results.R1 = recv._Get.Body(p0) 151 | } 152 | // call history 153 | recv._Get.History = append(recv._Get.History, struct { 154 | Params struct { 155 | P0 string 156 | } 157 | Results struct { 158 | R0 interface{} 159 | R1 error 160 | } 161 | }{ 162 | Params: recv._Get.Params, 163 | Results: recv._Get.Results, 164 | }) 165 | // results 166 | return recv._Get.Results.R0, recv._Get.Results.R1 167 | } 168 | 169 | func (recv *MockcCache) Set(p0 string, p1 interface{}) error { 170 | recv._Set.mu.Lock() 171 | defer recv._Set.mu.Unlock() 172 | // basics 173 | recv._Set.Called = true 174 | recv._Set.CallCount++ 175 | // params 176 | recv._Set.Params.P0 = p0 177 | recv._Set.Params.P1 = p1 178 | // body 179 | if recv._Set.Body != nil { 180 | recv._Set.Results.R0 = recv._Set.Body(p0, p1) 181 | } 182 | // call history 183 | recv._Set.History = append(recv._Set.History, struct { 184 | Params struct { 185 | P0 string 186 | P1 interface{} 187 | } 188 | Results struct { 189 | R0 error 190 | } 191 | }{ 192 | Params: recv._Set.Params, 193 | Results: recv._Set.Results, 194 | }) 195 | // results 196 | return recv._Set.Results.R0 197 | } 198 | -------------------------------------------------------------------------------- /internal/mockc/testdata/with-custom-constructor/testdata/mockc_gen.go.gen: -------------------------------------------------------------------------------- 1 | // Code generated by Mockc. DO NOT EDIT. 2 | // repo: https://github.com/KimMachineGun/mockc 3 | 4 | //go:generate mockc 5 | // +build !mockc 6 | 7 | package constructor 8 | 9 | import "sync" 10 | 11 | var _ interface { 12 | Cache 13 | } = &MockcCache{} 14 | 15 | type MockcCache struct { 16 | // method: Del 17 | _Del struct { 18 | mu sync.Mutex 19 | // basics 20 | Called bool 21 | CallCount int 22 | // call history 23 | History []struct { 24 | Params struct { 25 | P0 string 26 | } 27 | Results struct { 28 | R0 error 29 | } 30 | } 31 | // params 32 | Params struct { 33 | P0 string 34 | } 35 | // results 36 | Results struct { 37 | R0 error 38 | } 39 | // if it is not nil, it'll be called in the middle of the method. 40 | Body func(string) error 41 | } 42 | // method: Get 43 | _Get struct { 44 | mu sync.Mutex 45 | // basics 46 | Called bool 47 | CallCount int 48 | // call history 49 | History []struct { 50 | Params struct { 51 | P0 string 52 | } 53 | Results struct { 54 | R0 interface{} 55 | R1 error 56 | } 57 | } 58 | // params 59 | Params struct { 60 | P0 string 61 | } 62 | // results 63 | Results struct { 64 | R0 interface{} 65 | R1 error 66 | } 67 | // if it is not nil, it'll be called in the middle of the method. 68 | Body func(string) (interface{}, error) 69 | } 70 | // method: Set 71 | _Set struct { 72 | mu sync.Mutex 73 | // basics 74 | Called bool 75 | CallCount int 76 | // call history 77 | History []struct { 78 | Params struct { 79 | P0 string 80 | P1 interface{} 81 | } 82 | Results struct { 83 | R0 error 84 | } 85 | } 86 | // params 87 | Params struct { 88 | P0 string 89 | P1 interface{} 90 | } 91 | // results 92 | Results struct { 93 | R0 error 94 | } 95 | // if it is not nil, it'll be called in the middle of the method. 96 | Body func(string, interface{}) error 97 | } 98 | } 99 | 100 | func newMockCache(v ...interface { 101 | Cache 102 | }) *MockcCache { 103 | m := &MockcCache{} 104 | if len(v) > 0 { 105 | m._Del.Body = v[0].Del 106 | m._Get.Body = v[0].Get 107 | m._Set.Body = v[0].Set 108 | } 109 | return m 110 | } 111 | 112 | func (recv *MockcCache) Del(p0 string) error { 113 | recv._Del.mu.Lock() 114 | defer recv._Del.mu.Unlock() 115 | // basics 116 | recv._Del.Called = true 117 | recv._Del.CallCount++ 118 | // params 119 | recv._Del.Params.P0 = p0 120 | // body 121 | if recv._Del.Body != nil { 122 | recv._Del.Results.R0 = recv._Del.Body(p0) 123 | } 124 | // call history 125 | recv._Del.History = append(recv._Del.History, struct { 126 | Params struct { 127 | P0 string 128 | } 129 | Results struct { 130 | R0 error 131 | } 132 | }{ 133 | Params: recv._Del.Params, 134 | Results: recv._Del.Results, 135 | }) 136 | // results 137 | return recv._Del.Results.R0 138 | } 139 | 140 | func (recv *MockcCache) Get(p0 string) (interface{}, error) { 141 | recv._Get.mu.Lock() 142 | defer recv._Get.mu.Unlock() 143 | // basics 144 | recv._Get.Called = true 145 | recv._Get.CallCount++ 146 | // params 147 | recv._Get.Params.P0 = p0 148 | // body 149 | if recv._Get.Body != nil { 150 | recv._Get.Results.R0, recv._Get.Results.R1 = recv._Get.Body(p0) 151 | } 152 | // call history 153 | recv._Get.History = append(recv._Get.History, struct { 154 | Params struct { 155 | P0 string 156 | } 157 | Results struct { 158 | R0 interface{} 159 | R1 error 160 | } 161 | }{ 162 | Params: recv._Get.Params, 163 | Results: recv._Get.Results, 164 | }) 165 | // results 166 | return recv._Get.Results.R0, recv._Get.Results.R1 167 | } 168 | 169 | func (recv *MockcCache) Set(p0 string, p1 interface{}) error { 170 | recv._Set.mu.Lock() 171 | defer recv._Set.mu.Unlock() 172 | // basics 173 | recv._Set.Called = true 174 | recv._Set.CallCount++ 175 | // params 176 | recv._Set.Params.P0 = p0 177 | recv._Set.Params.P1 = p1 178 | // body 179 | if recv._Set.Body != nil { 180 | recv._Set.Results.R0 = recv._Set.Body(p0, p1) 181 | } 182 | // call history 183 | recv._Set.History = append(recv._Set.History, struct { 184 | Params struct { 185 | P0 string 186 | P1 interface{} 187 | } 188 | Results struct { 189 | R0 error 190 | } 191 | }{ 192 | Params: recv._Set.Params, 193 | Results: recv._Set.Results, 194 | }) 195 | // results 196 | return recv._Set.Results.R0 197 | } 198 | -------------------------------------------------------------------------------- /internal/mockc/generator.go: -------------------------------------------------------------------------------- 1 | package mockc 2 | 3 | import ( 4 | "context" 5 | "errors" 6 | "fmt" 7 | "go/ast" 8 | "go/types" 9 | "io/ioutil" 10 | "log" 11 | "sort" 12 | "strings" 13 | 14 | "golang.org/x/tools/go/packages" 15 | ) 16 | 17 | type generator struct { 18 | pkg *packages.Package 19 | path string 20 | imports map[string]string 21 | importConflicts map[string]int 22 | mocks []mockInfo 23 | } 24 | 25 | func newGenerator(pkg *packages.Package, path string) *generator { 26 | return &generator{ 27 | pkg: pkg, 28 | path: path, 29 | imports: map[string]string{}, 30 | importConflicts: map[string]int{}, 31 | } 32 | } 33 | 34 | func (g *generator) Generate(gogenerate string) error { 35 | if len(g.mocks) == 0 { 36 | return nil 37 | } 38 | 39 | g.sortMocks() 40 | 41 | b, err := render(g.pkg, g.mocks, gogenerate) 42 | if err != nil { 43 | return fmt.Errorf("cannot execute template: %v", err) 44 | } 45 | 46 | err = ioutil.WriteFile(g.path, b, 0666) 47 | if err != nil { 48 | return fmt.Errorf("cannot write %s: %v", g.path, err) 49 | } 50 | 51 | log.Println("generated:", g.path) 52 | 53 | return nil 54 | } 55 | 56 | func (g *generator) sortMocks() { 57 | sort.Slice(g.mocks, func(i, j int) bool { 58 | return g.mocks[i].name < g.mocks[j].name 59 | }) 60 | for _, m := range g.mocks { 61 | sort.Slice(m.methods, func(i, j int) bool { 62 | return m.methods[i].typ.Name() < m.methods[j].typ.Name() 63 | }) 64 | } 65 | } 66 | 67 | func (g *generator) addMockWithFlags(ctx context.Context, wd string, name string, withConstructor bool, fieldNamePrefix string, fieldNameSuffix string, interfacePatterns []string) error { 68 | targetInterfaces := map[string][]string{} 69 | for _, inter := range interfacePatterns { 70 | idx := strings.LastIndex(inter, ".") 71 | if idx == -1 { 72 | errorMessage := "invalid interface pattern:" 73 | errorMessage += fmt.Sprintf("\n\texpected interface pattern {package_path}.{interface_name}: actual %s", inter) 74 | 75 | return errors.New(errorMessage) 76 | } 77 | 78 | pkgPath, interfaceName := inter[:idx], inter[idx+1:] 79 | if pkgPath == "" || interfaceName == "" { 80 | errorMessage := "invalid interface pattern:" 81 | errorMessage += fmt.Sprintf("\n\texpected interface pattern {package-path}.{interface-name}: actual %s", inter) 82 | 83 | return errors.New(errorMessage) 84 | } 85 | 86 | targetInterfaces[pkgPath] = append(targetInterfaces[pkgPath], interfaceName) 87 | } 88 | 89 | patterns := make([]string, 0, len(targetInterfaces)) 90 | for pkgPath := range targetInterfaces { 91 | patterns = append(patterns, pkgPath) 92 | } 93 | 94 | pkgs, err := loadPackages(ctx, wd, patterns) 95 | if err != nil { 96 | return fmt.Errorf("cannot load packages: %v", err) 97 | } 98 | 99 | interfaces := make([]types.Type, 0, len(interfacePatterns)) 100 | for _, pkg := range pkgs { 101 | interfaceNames := targetInterfaces[pkg.PkgPath] 102 | if len(interfaceNames) == 0 { 103 | continue 104 | } 105 | 106 | f := newInterfaceFinder(pkg, interfaceNames) 107 | for _, syntax := range pkg.Syntax { 108 | ast.Walk(f, syntax) 109 | } 110 | 111 | for _, interfaceName := range interfaceNames { 112 | inter, ok := f.result[interfaceName] 113 | if !ok { 114 | return fmt.Errorf("package %q: cannot load interface: %s", pkg.PkgPath, interfaceName) 115 | } 116 | 117 | interfaces = append(interfaces, inter) 118 | } 119 | } 120 | 121 | var constructor string 122 | if withConstructor { 123 | constructor = "New" + name 124 | } 125 | 126 | err = g.addMock(name, constructor, interfaces, newFieldNameFormatter(fieldNamePrefix, fieldNameSuffix)) 127 | if err != nil { 128 | return err 129 | } 130 | 131 | return nil 132 | } 133 | 134 | func (g *generator) addMock(name string, constructor string, interfaces []types.Type, fieldNameFormatter func(string) string) error { 135 | iface, err := overlapInterfaces(interfaces) 136 | if err != nil { 137 | errorMessage := err.Error() 138 | errorMessage += fmt.Sprintf("\n\tmock %q", name) 139 | 140 | return errors.New(errorMessage) 141 | } 142 | 143 | methods := make([]methodInfo, iface.NumMethods()) 144 | for i := 0; i < iface.NumMethods(); i++ { 145 | method := iface.Method(i) 146 | sig := method.Type().(*types.Signature) 147 | 148 | params := make([]paramInfo, sig.Params().Len()) 149 | for i := 0; i < sig.Params().Len(); i++ { 150 | params[i] = paramInfo{ 151 | typ: sig.Params().At(i), 152 | isVariadic: i+1 == sig.Params().Len() && sig.Variadic(), 153 | } 154 | } 155 | 156 | results := make([]resultInfo, sig.Results().Len()) 157 | for i := 0; i < sig.Results().Len(); i++ { 158 | results[i] = resultInfo{ 159 | typ: sig.Results().At(i), 160 | } 161 | } 162 | 163 | methods[i] = methodInfo{ 164 | typ: method, 165 | fieldName: fieldNameFormatter(method.Name()), 166 | params: params, 167 | results: results, 168 | } 169 | } 170 | 171 | g.mocks = append(g.mocks, mockInfo{ 172 | typ: iface, 173 | name: name, 174 | constructor: constructor, 175 | methods: methods, 176 | }) 177 | 178 | return nil 179 | } 180 | 181 | func overlapInterfaces(interfaces []types.Type) (iface *types.Interface, err error) { 182 | var ( 183 | methods []*types.Func 184 | embeddeds []types.Type 185 | ) 186 | for _, inter := range interfaces { 187 | switch inter := inter.(type) { 188 | case *types.Named: 189 | embeddeds = append(embeddeds, inter) 190 | case *types.Interface: 191 | for i := 0; i < inter.NumEmbeddeds(); i++ { 192 | embeddeds = append(embeddeds, inter.EmbeddedType(i)) 193 | } 194 | for i := 0; i < inter.NumExplicitMethods(); i++ { 195 | methods = append(methods, inter.ExplicitMethod(i)) 196 | } 197 | } 198 | } 199 | 200 | iface = types.NewInterfaceType(methods, embeddeds) 201 | defer func() { 202 | rec := recover() 203 | if rec != nil { 204 | err = fmt.Errorf("%v", rec) 205 | } 206 | }() 207 | iface.Complete() 208 | 209 | return iface, err 210 | } 211 | 212 | func newFieldNameFormatter(prefix, suffix string) func(string) string { 213 | return func(field string) string { 214 | return prefix + field + suffix 215 | } 216 | } 217 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/dave/jennifer v1.4.1 h1:XyqG6cn5RQsTj3qlWQTKlRGAyrTcsk1kUmWdZBzRjDw= 2 | github.com/dave/jennifer v1.4.1/go.mod h1:7jEdnm+qBcxl8PC0zyp7vxcpSRnzXSt9r39tpTVGlwA= 3 | github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 4 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 5 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 6 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 7 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 8 | github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= 9 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 10 | github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 11 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 12 | golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 13 | golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= 14 | golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= 15 | golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= 16 | golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= 17 | golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= 18 | golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= 19 | golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= 20 | golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic= 21 | golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= 22 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 23 | golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 24 | golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= 25 | golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= 26 | golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= 27 | golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= 28 | golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= 29 | golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= 30 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 31 | golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 32 | golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 33 | golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= 34 | golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= 35 | golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 36 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 37 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 38 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 39 | golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 40 | golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 41 | golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 42 | golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 43 | golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 44 | golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 45 | golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 46 | golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE= 47 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 48 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 49 | golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= 50 | golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= 51 | golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= 52 | golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= 53 | golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= 54 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 55 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 56 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 57 | golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= 58 | golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= 59 | golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= 60 | golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 61 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 62 | golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 63 | golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= 64 | golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= 65 | golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= 66 | golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw= 67 | golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc= 68 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 69 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 70 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 71 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= 72 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 73 | -------------------------------------------------------------------------------- /internal/mockc/parser.go: -------------------------------------------------------------------------------- 1 | package mockc 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "go/ast" 7 | "go/types" 8 | "log" 9 | "path/filepath" 10 | "strconv" 11 | 12 | "golang.org/x/tools/go/packages" 13 | ) 14 | 15 | type parser struct { 16 | pkg *packages.Package 17 | } 18 | 19 | func newParser(pkg *packages.Package) *parser { 20 | return &parser{ 21 | pkg: pkg, 22 | } 23 | } 24 | 25 | func (p *parser) parse() ([]*generator, error) { 26 | destinationsAndGenerators := map[string]*generator{} 27 | for _, syntax := range p.pkg.Syntax { 28 | for _, decl := range syntax.Decls { 29 | fun, ok := decl.(*ast.FuncDecl) 30 | if !ok { 31 | continue 32 | } 33 | 34 | calls, err := p.findMockcCalls(fun.Body.List) 35 | if err != nil { 36 | errorMessage := "cannot find mockc calls:" 37 | errorMessage += fmt.Sprintf("\n\tmock %q: %v", fun.Name.Name, err) 38 | 39 | return nil, errors.New(errorMessage) 40 | } else if len(calls) == 0 { 41 | continue 42 | } 43 | 44 | var ( 45 | pkgDir = filepath.Dir(p.pkg.Fset.File(decl.Pos()).Name()) 46 | destination = defaultDestination 47 | name = fun.Name.Name 48 | constructor string 49 | fieldNamePrefix = defaultFieldNamePrefix 50 | fieldNameSuffix = defaultFieldNameSuffix 51 | interfaces []types.Type 52 | ) 53 | 54 | for _, call := range calls { 55 | sel, ok := call.Fun.(*ast.SelectorExpr) 56 | if !ok { 57 | continue 58 | } 59 | 60 | obj := p.pkg.TypesInfo.ObjectOf(sel.Sel) 61 | switch obj.Name() { 62 | case "Implements": 63 | log.Println("mockc.Implements is deprecated. Please use mock.Implement instead.") 64 | fallthrough 65 | case "Implement": 66 | for _, arg := range call.Args { 67 | t := p.pkg.TypesInfo.TypeOf(arg) 68 | 69 | _, ok := t.Underlying().(*types.Interface) 70 | if !ok { 71 | errorMessage := "non-interface:" 72 | errorMessage += fmt.Sprintf("\n\tmock %q: %v", fun.Name.Name, t) 73 | 74 | return nil, errors.New(errorMessage) 75 | } 76 | 77 | named, ok := t.(*types.Named) 78 | if ok { 79 | interfaces = append(interfaces, named) 80 | } else { 81 | interfaces = append(interfaces, t.Underlying()) 82 | } 83 | } 84 | case "SetFieldNamePrefix": 85 | arg := call.Args[0] 86 | res, err := types.Eval(p.pkg.Fset, p.pkg.Types, arg.Pos(), types.ExprString(arg)) 87 | if err != nil { 88 | errorMessage := "cannot set field name prefix:" 89 | errorMessage += fmt.Sprintf("\n\tmock %q: %v", fun.Name.Name, err) 90 | 91 | return nil, errors.New(errorMessage) 92 | } 93 | 94 | fieldNamePrefix, err = strconv.Unquote(res.Value.ExactString()) 95 | if err != nil { 96 | errorMessage := "cannot set field name prefix:" 97 | errorMessage += fmt.Sprintf("\n\tmock %q: %v", fun.Name.Name, err) 98 | 99 | return nil, errors.New(errorMessage) 100 | } 101 | case "SetFieldNameSuffix": 102 | arg := call.Args[0] 103 | res, err := types.Eval(p.pkg.Fset, p.pkg.Types, arg.Pos(), types.ExprString(arg)) 104 | if err != nil { 105 | errorMessage := "cannot set field name suffix:" 106 | errorMessage += fmt.Sprintf("\n\tmock %q: %v", fun.Name.Name, err) 107 | 108 | return nil, errors.New(errorMessage) 109 | } 110 | 111 | fieldNameSuffix, err = strconv.Unquote(res.Value.ExactString()) 112 | if err != nil { 113 | errorMessage := "cannot set field name suffix:" 114 | errorMessage += fmt.Sprintf("\n\tmock %q: %v", fun.Name.Name, err) 115 | 116 | return nil, errors.New(errorMessage) 117 | } 118 | case "SetDestination": 119 | arg := call.Args[0] 120 | res, err := types.Eval(p.pkg.Fset, p.pkg.Types, arg.Pos(), types.ExprString(arg)) 121 | if err != nil { 122 | errorMessage := "cannot set destination:" 123 | errorMessage += fmt.Sprintf("\n\tmock %q: %v", fun.Name.Name, err) 124 | 125 | return nil, errors.New(errorMessage) 126 | } 127 | 128 | val, err := strconv.Unquote(res.Value.ExactString()) 129 | if err != nil { 130 | errorMessage := "cannot set destination:" 131 | errorMessage += fmt.Sprintf("\n\tmock %q: %v", fun.Name.Name, err) 132 | 133 | return nil, errors.New(errorMessage) 134 | } else if val == "" { 135 | errorMessage := "cannot set destination:" 136 | errorMessage += fmt.Sprintf("\n\tmock %q: destination should not be an empty string", fun.Name.Name) 137 | 138 | return nil, errors.New(errorMessage) 139 | } else if filepath.Ext(val) != ".go" { 140 | errorMessage := "cannot set destination:" 141 | errorMessage += fmt.Sprintf("\n\tmock %q: %q is not a go file", fun.Name.Name, val) 142 | 143 | return nil, errors.New(errorMessage) 144 | } 145 | 146 | destination = val 147 | case "WithConstructor": 148 | constructor = "New" + name 149 | case "SetConstructorName": 150 | arg := call.Args[0] 151 | res, err := types.Eval(p.pkg.Fset, p.pkg.Types, arg.Pos(), types.ExprString(arg)) 152 | if err != nil { 153 | errorMessage := "cannot set constructor name:" 154 | errorMessage += fmt.Sprintf("\n\tmock %q: %v", fun.Name.Name, err) 155 | 156 | return nil, errors.New(errorMessage) 157 | } 158 | 159 | constructor, err = strconv.Unquote(res.Value.ExactString()) 160 | if err != nil { 161 | errorMessage := "cannot set constructor name:" 162 | errorMessage += fmt.Sprintf("\n\tmock %q: %v", fun.Name.Name, err) 163 | 164 | return nil, errors.New(errorMessage) 165 | } 166 | default: 167 | errorMessage := "unknown mockc function call:" 168 | errorMessage += fmt.Sprintf("\n\tmock %q: mockc.%s", fun.Name.Name, obj.Name()) 169 | 170 | return nil, errors.New(errorMessage) 171 | } 172 | } 173 | 174 | if fieldNamePrefix == "" && fieldNameSuffix == "" { 175 | errorMessage := "at least one of the field name prefix and field name suffix must not be an empty string:" 176 | errorMessage += fmt.Sprintf( 177 | "\n\tmock %q: prefix(%q) suffix(%q)", fun.Name.Name, fieldNamePrefix, fieldNameSuffix, 178 | ) 179 | 180 | return nil, errors.New(errorMessage) 181 | } 182 | 183 | destination = filepath.Join(pkgDir, destination) 184 | if destinationsAndGenerators[destination] == nil { 185 | destinationsAndGenerators[destination] = newGenerator(p.pkg, destination) 186 | } 187 | 188 | err = destinationsAndGenerators[destination].addMock( 189 | name, 190 | constructor, 191 | interfaces, 192 | newFieldNameFormatter(fieldNamePrefix, fieldNameSuffix), 193 | ) 194 | if err != nil { 195 | return nil, err 196 | } 197 | } 198 | } 199 | if len(destinationsAndGenerators) == 0 { 200 | return nil, nil 201 | } 202 | 203 | generators := make([]*generator, 0, len(destinationsAndGenerators)) 204 | for _, generator := range destinationsAndGenerators { 205 | generators = append(generators, generator) 206 | } 207 | 208 | return generators, nil 209 | } 210 | 211 | func (p *parser) findMockcCalls(stmts []ast.Stmt) ([]*ast.CallExpr, error) { 212 | var ( 213 | calls []*ast.CallExpr 214 | invalid bool 215 | ) 216 | for _, stmt := range stmts { 217 | switch stmt := stmt.(type) { 218 | case *ast.ExprStmt: 219 | call, ok := stmt.X.(*ast.CallExpr) 220 | if !ok { 221 | invalid = true 222 | continue 223 | } 224 | 225 | sel, ok := call.Fun.(*ast.SelectorExpr) 226 | if !ok { 227 | invalid = true 228 | continue 229 | } 230 | 231 | if p.pkg.TypesInfo.ObjectOf(sel.Sel).Pkg().Path() != mockcPath { 232 | invalid = true 233 | continue 234 | } 235 | 236 | calls = append(calls, call) 237 | case *ast.EmptyStmt, *ast.ReturnStmt: 238 | default: 239 | invalid = true 240 | } 241 | } 242 | 243 | if len(calls) == 0 { 244 | return nil, nil 245 | } else if invalid { 246 | return nil, errors.New("mock generator should be consist of mockc function calls") 247 | } 248 | 249 | return calls, nil 250 | } 251 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![PkgGoDev](https://pkg.go.dev/badge/github.com/KimMachineGun/mockc)](https://pkg.go.dev/github.com/KimMachineGun/mockc) 2 | [![Go Report Card](https://goreportcard.com/badge/github.com/KimMachineGun/mockc)](https://goreportcard.com/report/github.com/KimMachineGun/mockc) 3 | # Mockc 4 | 5 | Mockc is a completely type-safe compile-time mock generator for Go. You can use it just by writing the mock generators with `mockc.Implement()` or using it with command like flags. 6 | 7 | Check out [my blog post](https://blog.kimmachinegun.dev/2020/08/go-interface-mocking-with-mockc.html) for more details. 8 | 9 | ## Features 10 | 11 | - Tools 12 | - [x] Generating mock with mock generators 13 | - [x] Generating mock with command line flags (experimental feature) 14 | - Generated Mock 15 | - [x] Capturing params and results of the method 16 | - [x] Capturing method calls 17 | - [x] Injecting method body 18 | - [x] Customizing mock's field names with the prefix and the suffix 19 | - default: `prefix:"_"`, `suffix:""` 20 | - [x] Generating mock constructor 21 | 22 | ## Installation 23 | 24 | ``` 25 | go get github.com/KimMachineGun/mockc/cmd/mockc 26 | ``` 27 | 28 | ## Look and Feel 29 | 30 | _You can see more examples [here](https://github.com/KimMachineGun/mockc/tree/master/examples)._ 31 | 32 | ### Target Interface 33 | 34 | ```go 35 | package basic 36 | 37 | type Cache interface { 38 | Get(key string) (val interface{}, err error) 39 | Set(key string, val interface{}) (err error) 40 | Del(key string) (err error) 41 | } 42 | ``` 43 | If you want to generate mock that implements the above interface, follow the steps below. 44 | 45 | ### With Mock Generator 46 | 47 | #### 1. Write Mock Generator 48 | 49 | If you want to generate mock with mock generator, write a mock generator first. The mock will be generated in its generator path, and it'll be named its generator's name. You can write multiple generators in one file, and multiple mocks will be generated. The mock generator should be consisted of function calls of the `mockc` package. 50 | 51 | ```go 52 | //+build mockc 53 | 54 | package basic 55 | 56 | import ( 57 | "github.com/KimMachineGun/mockc" 58 | ) 59 | 60 | func MockcCache() { 61 | mockc.Implement(Cache(nil)) 62 | } 63 | ``` 64 | 65 | If you want to customize the field names of the mock, use `mockc.SetFieldNamePrefix()` or `mockc.SetFieldNameSuffix()`. (Notice: These functions only work with constant string value.) 66 | 67 | #### 2. Generate Mock 68 | 69 | This command will generate mock with your mock generator. The `` argument will be used for loading mock generator with [golang.org/x/tools/go/packages#Load](https://godoc.org/golang.org/x/tools/go/packages#Load). If it's not provided, `.` will be used. 70 | 71 | ```sh 72 | mockc [] 73 | Ex: mock ./example 74 | ``` 75 | 76 | ### With Command Line Flags 77 | 78 | #### 1. Generate Mock 79 | 80 | This command will generate mock with its command line flags. If you generate mock with this command, you don't need to write the mock generator. The `` should follow `{package_path}.{interface_name}` format. 81 | 82 | ```sh 83 | mockc -destination= -name= [-withConstructor] [-fieldNamePrefix=] [-fieldNameSuffix=] [] 84 | Ex: mockc -destination=./example/mockc_gen.go -name=MockcCache github.com/KimMachineGun/mockc/example.Cache 85 | ``` 86 | 87 | If you want to customize the field names of the mock, pass string value to the `-fieldNamePrefix` or `-fieldNameSuffix`. 88 | 89 | ### Generated Mock 90 | 91 | The `//go:generate` directive may vary depending on your mock generation command. 92 | 93 | ```go 94 | // Code generated by Mockc. DO NOT EDIT. 95 | // repo: https://github.com/KimMachineGun/mockc 96 | 97 | //go:generate mockc 98 | // +build !mockc 99 | 100 | package basic 101 | 102 | import "sync" 103 | 104 | var _ interface { 105 | Cache 106 | } = &MockcCache{} 107 | 108 | type MockcCache struct { 109 | // method: Del 110 | _Del struct { 111 | mu sync.Mutex 112 | // basics 113 | Called bool 114 | CallCount int 115 | // call history 116 | History []struct { 117 | Params struct { 118 | P0 string 119 | } 120 | Results struct { 121 | R0 error 122 | } 123 | } 124 | // params 125 | Params struct { 126 | P0 string 127 | } 128 | // results 129 | Results struct { 130 | R0 error 131 | } 132 | // if it is not nil, it'll be called in the middle of the method. 133 | Body func(string) error 134 | } 135 | // method: Get 136 | _Get struct { 137 | mu sync.Mutex 138 | // basics 139 | Called bool 140 | CallCount int 141 | // call history 142 | History []struct { 143 | Params struct { 144 | P0 string 145 | } 146 | Results struct { 147 | R0 interface{} 148 | R1 error 149 | } 150 | } 151 | // params 152 | Params struct { 153 | P0 string 154 | } 155 | // results 156 | Results struct { 157 | R0 interface{} 158 | R1 error 159 | } 160 | // if it is not nil, it'll be called in the middle of the method. 161 | Body func(string) (interface{}, error) 162 | } 163 | // method: Set 164 | _Set struct { 165 | mu sync.Mutex 166 | // basics 167 | Called bool 168 | CallCount int 169 | // call history 170 | History []struct { 171 | Params struct { 172 | P0 string 173 | P1 interface{} 174 | } 175 | Results struct { 176 | R0 error 177 | } 178 | } 179 | // params 180 | Params struct { 181 | P0 string 182 | P1 interface{} 183 | } 184 | // results 185 | Results struct { 186 | R0 error 187 | } 188 | // if it is not nil, it'll be called in the middle of the method. 189 | Body func(string, interface{}) error 190 | } 191 | } 192 | 193 | func (recv *MockcCache) Del(p0 string) error { 194 | recv._Del.mu.Lock() 195 | defer recv._Del.mu.Unlock() 196 | // basics 197 | recv._Del.Called = true 198 | recv._Del.CallCount++ 199 | // params 200 | recv._Del.Params.P0 = p0 201 | // body 202 | if recv._Del.Body != nil { 203 | recv._Del.Results.R0 = recv._Del.Body(p0) 204 | } 205 | // call history 206 | recv._Del.History = append(recv._Del.History, struct { 207 | Params struct { 208 | P0 string 209 | } 210 | Results struct { 211 | R0 error 212 | } 213 | }{ 214 | Params: recv._Del.Params, 215 | Results: recv._Del.Results, 216 | }) 217 | // results 218 | return recv._Del.Results.R0 219 | } 220 | 221 | func (recv *MockcCache) Get(p0 string) (interface{}, error) { 222 | recv._Get.mu.Lock() 223 | defer recv._Get.mu.Unlock() 224 | // basics 225 | recv._Get.Called = true 226 | recv._Get.CallCount++ 227 | // params 228 | recv._Get.Params.P0 = p0 229 | // body 230 | if recv._Get.Body != nil { 231 | recv._Get.Results.R0, recv._Get.Results.R1 = recv._Get.Body(p0) 232 | } 233 | // call history 234 | recv._Get.History = append(recv._Get.History, struct { 235 | Params struct { 236 | P0 string 237 | } 238 | Results struct { 239 | R0 interface{} 240 | R1 error 241 | } 242 | }{ 243 | Params: recv._Get.Params, 244 | Results: recv._Get.Results, 245 | }) 246 | // results 247 | return recv._Get.Results.R0, recv._Get.Results.R1 248 | } 249 | 250 | func (recv *MockcCache) Set(p0 string, p1 interface{}) error { 251 | recv._Set.mu.Lock() 252 | defer recv._Set.mu.Unlock() 253 | // basics 254 | recv._Set.Called = true 255 | recv._Set.CallCount++ 256 | // params 257 | recv._Set.Params.P0 = p0 258 | recv._Set.Params.P1 = p1 259 | // body 260 | if recv._Set.Body != nil { 261 | recv._Set.Results.R0 = recv._Set.Body(p0, p1) 262 | } 263 | // call history 264 | recv._Set.History = append(recv._Set.History, struct { 265 | Params struct { 266 | P0 string 267 | P1 interface{} 268 | } 269 | Results struct { 270 | R0 error 271 | } 272 | }{ 273 | Params: recv._Set.Params, 274 | Results: recv._Set.Results, 275 | }) 276 | // results 277 | return recv._Set.Results.R0 278 | } 279 | ``` 280 | 281 | ### Feel Free to Use the Generated Mock 282 | 283 | ```go 284 | package basic 285 | 286 | import ( 287 | "errors" 288 | "testing" 289 | ) 290 | 291 | func HasKey(c Cache, key string) (bool, error) { 292 | val, err := c.Get(key) 293 | if err != nil { 294 | return false, err 295 | } 296 | 297 | return val != nil, nil 298 | } 299 | 300 | func TestHasKey(t *testing.T) { 301 | m := &MockcCache{} 302 | 303 | // set return value 304 | m._Get.Results.R0 = struct{}{} 305 | 306 | // execute 307 | key := "test_key" 308 | result, err := HasKey(m, key) 309 | 310 | // assert 311 | if !result { 312 | t.Error("result should be true") 313 | } 314 | if err != nil { 315 | t.Error("err should be nil") 316 | } 317 | if m._Get.CallCount != 1 { 318 | t.Errorf("Cache.Get should be called once: actual(%d)", m._Get.CallCount) 319 | } 320 | if m._Get.Params.P0 != key { 321 | t.Errorf("Cache.Get should be called with %q: actual(%q)", key, m._Get.Params.P0) 322 | } 323 | } 324 | 325 | func TestHasKey_WithBodyInjection(t *testing.T) { 326 | m := &MockcCache{} 327 | 328 | // inject body 329 | key := "test_key" 330 | m._Get.Body = func(actualKey string) (interface{}, error) { 331 | if actualKey != key { 332 | t.Errorf("Cache.Get should be called with %q: actual(%q)", key, actualKey) 333 | } 334 | return nil, errors.New("error") 335 | } 336 | 337 | // execute 338 | result, err := HasKey(m, key) 339 | 340 | // assert 341 | if result { 342 | t.Error("result should be false") 343 | } 344 | if err == nil { 345 | t.Error("err should not be nil") 346 | } 347 | if m._Get.CallCount != 1 { 348 | t.Errorf("Cache.Get should be called once: actual(%d)", m._Get.CallCount) 349 | } 350 | } 351 | ``` 352 | 353 | ## Inspired by 🙏 354 | 355 | - [github.com/google/wire](https://github.com/google/wire) 356 | - [github.com/sasha-s/goimpl](https://github.com/sasha-s/goimpl) 357 | - [github.com/vektra/mockery](https://github.com/vektra/mockery) 358 | -------------------------------------------------------------------------------- /internal/mockc/render.go: -------------------------------------------------------------------------------- 1 | package mockc 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "go/types" 7 | "strings" 8 | 9 | "golang.org/x/tools/go/packages" 10 | 11 | "github.com/dave/jennifer/jen" 12 | ) 13 | 14 | func render(pkg *packages.Package, mocks []mockInfo, gogenerate string) ([]byte, error) { 15 | f := jen.NewFilePathName(pkg.PkgPath, pkg.Name) 16 | 17 | f.PackageComment("// Code generated by Mockc. DO NOT EDIT.") 18 | f.PackageComment("// repo: https://github.com/KimMachineGun/mockc\n") 19 | f.PackageComment(fmt.Sprintf("//go:generate %s", gogenerate)) 20 | f.PackageComment("// +build !mockc\n") 21 | 22 | for _, mock := range mocks { 23 | f.Var().Id("_").Do(func(s *jen.Statement) { 24 | typeCode(s, mock.typ) 25 | }).Op("=").Op("&").Id(mock.name).Values() 26 | f.Type().Id(mock.name).StructFunc(func(g *jen.Group) { 27 | for _, method := range mock.methods { 28 | g.Commentf("method: %s", method.typ.Name()) 29 | g.Id(method.fieldName).StructFunc(func(g *jen.Group) { 30 | g.Id("mu").Qual("sync", "Mutex") 31 | g.Comment("basics") 32 | g.Id("Called").Bool() 33 | g.Id("CallCount").Int() 34 | if len(method.params)+len(method.results) > 0 { 35 | g.Comment("call history") 36 | g.Id("History").Index().StructFunc(func(g *jen.Group) { 37 | if len(method.params) > 0 { 38 | g.Id("Params").StructFunc(func(g *jen.Group) { 39 | for i, param := range method.params { 40 | param := param 41 | g.Do(func(s *jen.Statement) { 42 | typeCode(s.Id(fmt.Sprintf("P%d", i)), param.typ.Type()) 43 | }) 44 | } 45 | }) 46 | } 47 | if len(method.results) > 0 { 48 | g.Id("Results").StructFunc(func(g *jen.Group) { 49 | for i, result := range method.results { 50 | result := result 51 | g.Do(func(s *jen.Statement) { 52 | typeCode(s.Id(fmt.Sprintf("R%d", i)), result.typ.Type()) 53 | }) 54 | } 55 | }) 56 | } 57 | }) 58 | } 59 | if len(method.params) > 0 { 60 | g.Comment("params") 61 | g.Id("Params").StructFunc(func(g *jen.Group) { 62 | for i, param := range method.params { 63 | param := param 64 | g.Do(func(s *jen.Statement) { 65 | typeCode(s.Id(fmt.Sprintf("P%d", i)), param.typ.Type()) 66 | }) 67 | } 68 | }) 69 | } 70 | if len(method.results) > 0 { 71 | g.Comment("results") 72 | g.Id("Results").StructFunc(func(g *jen.Group) { 73 | for i, result := range method.results { 74 | result := result 75 | g.Do(func(s *jen.Statement) { 76 | typeCode(s.Id(fmt.Sprintf("R%d", i)), result.typ.Type()) 77 | }) 78 | } 79 | }) 80 | } 81 | g.Comment("if it is not nil, it'll be called in the middle of the method.") 82 | g.Id("Body").Do(func(s *jen.Statement) { 83 | typeCode(s, method.typ.Type()) 84 | }) 85 | }) 86 | } 87 | }) 88 | 89 | if mock.constructor != "" { 90 | f.Func().Id(mock.constructor).Params( 91 | jen.Id("v").Op("...").Do(func(s *jen.Statement) { 92 | typeCode(s, mock.typ) 93 | }), 94 | ).Op("*").Id(mock.name).Block( 95 | jen.Id("m").Op(":=").Op("&").Id(mock.name).Values(), 96 | jen.If(jen.Len(jen.Id("v")).Op(">").Lit(0)).BlockFunc(func(g *jen.Group) { 97 | for _, method := range mock.methods { 98 | g.Id("m").Dot(method.fieldName).Dot("Body").Op("=").Id("v").Index(jen.Lit(0)).Dot(method.typ.Name()) 99 | } 100 | }), 101 | jen.Return(jen.Id("m")), 102 | ).Line() 103 | } 104 | 105 | for _, method := range mock.methods { 106 | f.Func().Params(jen.Id("recv").Op("*").Id(mock.name)).Id(method.typ.Name()).ParamsFunc(func(g *jen.Group) { 107 | for i, param := range method.params { 108 | param := param 109 | g.Do(func(s *jen.Statement) { 110 | s.Id(fmt.Sprintf("p%d", i)) 111 | if param.isVariadic { 112 | typeCode(s.Op("..."), param.typ.Type().(*types.Slice).Elem()) 113 | } else { 114 | typeCode(s, param.typ.Type()) 115 | } 116 | }) 117 | } 118 | }).ParamsFunc(func(g *jen.Group) { 119 | for _, result := range method.results { 120 | result := result 121 | g.Do(func(s *jen.Statement) { 122 | typeCode(s, result.typ.Type()) 123 | }) 124 | } 125 | }).BlockFunc(func(g *jen.Group) { 126 | fieldName := jen.Id("recv").Dot(method.fieldName) 127 | 128 | g.Add(fieldName).Dot("mu").Dot("Lock").Call() 129 | g.Defer().Add(fieldName).Dot("mu").Dot("Unlock").Call() 130 | 131 | g.Comment("basics") 132 | g.Add(fieldName).Dot("Called").Op("=").True() 133 | g.Add(fieldName).Dot("CallCount").Op("++") 134 | 135 | if len(method.params) > 0 { 136 | g.Comment("params") 137 | for i := range method.params { 138 | g.Add(fieldName).Dot("Params").Dot(fmt.Sprintf("P%d", i)).Op("=").Id(fmt.Sprintf("p%d", i)) 139 | } 140 | } 141 | 142 | g.Comment("body") 143 | g.If(jen.Add(fieldName).Dot("Body").Op("!=").Nil()).BlockFunc(func(g *jen.Group) { 144 | g.Do(func(s *jen.Statement) { 145 | if len(method.results) > 0 { 146 | s.ListFunc(func(g *jen.Group) { 147 | for i := range method.results { 148 | g.Add(fieldName).Dot("Results").Dot(fmt.Sprintf("R%d", i)) 149 | } 150 | }).Op("=") 151 | } 152 | s.Add(fieldName).Dot("Body").CallFunc(func(g *jen.Group) { 153 | for i, param := range method.params { 154 | g.Do(func(s *jen.Statement) { 155 | s.Id(fmt.Sprintf("p%d", i)) 156 | if param.isVariadic { 157 | s.Op("...") 158 | } 159 | }) 160 | } 161 | }) 162 | }) 163 | }) 164 | 165 | if len(method.params)+len(method.results) > 0 { 166 | g.Comment("call history") 167 | g.Id("recv").Dot(method.fieldName).Dot("History").Op("=").Append( 168 | jen.Id("recv").Dot(method.fieldName).Dot("History"), 169 | jen.StructFunc(func(g *jen.Group) { 170 | if len(method.params) > 0 { 171 | g.Id("Params").StructFunc(func(g *jen.Group) { 172 | for i, param := range method.params { 173 | param := param 174 | g.Do(func(s *jen.Statement) { 175 | typeCode(s.Id(fmt.Sprintf("P%d", i)), param.typ.Type()) 176 | }) 177 | } 178 | }) 179 | } 180 | if len(method.results) > 0 { 181 | g.Id("Results").StructFunc(func(g *jen.Group) { 182 | for i, result := range method.results { 183 | result := result 184 | g.Do(func(s *jen.Statement) { 185 | typeCode(s.Id(fmt.Sprintf("R%d", i)), result.typ.Type()) 186 | }) 187 | } 188 | }) 189 | } 190 | }).Values(jen.DictFunc(func(d jen.Dict) { 191 | if len(method.params) > 0 { 192 | d[jen.Id("Params")] = jen.Add(fieldName).Dot("Params") 193 | } 194 | if len(method.results) > 0 { 195 | d[jen.Id("Results")] = jen.Add(fieldName).Dot("Results") 196 | } 197 | })), 198 | ) 199 | } 200 | 201 | if len(method.results) > 0 { 202 | g.Comment("results") 203 | g.ReturnFunc(func(g *jen.Group) { 204 | for i := range method.results { 205 | g.Add(fieldName).Dot("Results").Dot(fmt.Sprintf("R%d", i)) 206 | } 207 | }) 208 | } 209 | }).Line() 210 | } 211 | } 212 | 213 | b := bytes.NewBuffer(nil) 214 | err := f.Render(b) 215 | if err != nil { 216 | return nil, err 217 | } 218 | 219 | return b.Bytes(), nil 220 | } 221 | 222 | func typeCode(stmt *jen.Statement, t types.Type) jen.Code { 223 | if stmt == nil { 224 | stmt = &jen.Statement{} 225 | } 226 | 227 | switch t := t.(type) { 228 | case *types.Basic: 229 | switch t.Name() { 230 | case "bool": 231 | return stmt.Bool() 232 | case "int": 233 | return stmt.Int() 234 | case "int8": 235 | return stmt.Int8() 236 | case "int16": 237 | return stmt.Int16() 238 | case "int32": 239 | return stmt.Int32() 240 | case "int64": 241 | return stmt.Int64() 242 | case "uint": 243 | return stmt.Uint() 244 | case "uint8": 245 | return stmt.Uint8() 246 | case "uint16": 247 | return stmt.Uint16() 248 | case "uint32": 249 | return stmt.Uint32() 250 | case "uint64": 251 | return stmt.Uint64() 252 | case "uintptr": 253 | return stmt.Uintptr() 254 | case "float32": 255 | return stmt.Float32() 256 | case "float64": 257 | return stmt.Float64() 258 | case "complex64": 259 | return stmt.Complex64() 260 | case "complex128": 261 | return stmt.Complex128() 262 | case "string": 263 | return stmt.String() 264 | case "Pointer": 265 | return stmt.Qual("unsafe", "Pointer") 266 | case "byte": 267 | return stmt.Byte() 268 | case "rune": 269 | return stmt.Rune() 270 | } 271 | case *types.Array: 272 | return typeCode(stmt.Index(jen.Lit(int(t.Len()))), t.Elem()) 273 | case *types.Slice: 274 | return typeCode(stmt.Index(), t.Elem()) 275 | case *types.Struct: 276 | return stmt.StructFunc(func(g *jen.Group) { 277 | for i := 0; i < t.NumFields(); i++ { 278 | f := t.Field(i) 279 | g.Do(func(s *jen.Statement) { 280 | if f.Anonymous() { 281 | typeCode(s, f.Type()) 282 | } else { 283 | typeCode(s.Id(f.Name()), f.Type()) 284 | } 285 | }) 286 | } 287 | }) 288 | case *types.Pointer: 289 | return typeCode(stmt.Op("*"), t.Elem()) 290 | case *types.Tuple: 291 | return stmt.ValuesFunc(func(g *jen.Group) { 292 | typeTupleCode(g, t, false) 293 | }) 294 | case *types.Signature: 295 | return stmt.Func().ParamsFunc(func(g *jen.Group) { 296 | typeTupleCode(g, t.Params(), t.Variadic()) 297 | }).ParamsFunc(func(g *jen.Group) { 298 | typeTupleCode(g, t.Results(), false) 299 | }) 300 | case *types.Interface: 301 | return stmt.InterfaceFunc(func(g *jen.Group) { 302 | for i := 0; i < t.NumEmbeddeds(); i++ { 303 | e := t.EmbeddedType(i) 304 | g.Do(func(s *jen.Statement) { 305 | typeCode(s, e) 306 | }) 307 | } 308 | for i := 0; i < t.NumExplicitMethods(); i++ { 309 | m := t.ExplicitMethod(i) 310 | sig := m.Type().(*types.Signature) 311 | 312 | g.Do(func(s *jen.Statement) { 313 | s.Id(m.Name()).ParamsFunc(func(g *jen.Group) { 314 | typeTupleCode(g, sig.Params(), sig.Variadic()) 315 | }).ParamsFunc(func(g *jen.Group) { 316 | typeTupleCode(g, sig.Results(), false) 317 | }) 318 | }) 319 | } 320 | }) 321 | case *types.Map: 322 | return typeCode(stmt.Map(typeCode(nil, t.Key())), t.Elem()) 323 | case *types.Chan: 324 | switch t.Dir() { 325 | case types.SendRecv: 326 | return typeCode(stmt.Chan(), t.Elem()) 327 | case types.RecvOnly: 328 | return typeCode(stmt.Op("<-").Chan(), t.Elem()) 329 | default: 330 | return typeCode(stmt.Chan().Op("<-"), t.Elem()) 331 | } 332 | case *types.Named: 333 | if i := strings.LastIndex(t.String(), "."); i >= 0 { 334 | return stmt.Qual(t.String()[:i], t.String()[i+1:]) 335 | } 336 | return stmt.Id(t.String()) 337 | } 338 | return stmt 339 | } 340 | 341 | func typeTupleCode(g *jen.Group, t *types.Tuple, variadic bool) { 342 | for i := 0; i < t.Len(); i++ { 343 | g.Do(func(s *jen.Statement) { 344 | v := t.At(i) 345 | if variadic && i+1 == t.Len() { 346 | typeCode(s.Id("").Op("..."), v.Type().(*types.Slice).Elem()) 347 | } else { 348 | typeCode(s.Id(""), v.Type()) 349 | } 350 | }) 351 | } 352 | } 353 | 354 | type mockInfo struct { 355 | typ *types.Interface 356 | name string 357 | constructor string 358 | methods []methodInfo 359 | } 360 | 361 | type methodInfo struct { 362 | typ *types.Func 363 | fieldName string 364 | params []paramInfo 365 | results []resultInfo 366 | } 367 | 368 | type paramInfo struct { 369 | typ *types.Var 370 | isVariadic bool 371 | } 372 | 373 | type resultInfo struct { 374 | typ *types.Var 375 | } 376 | -------------------------------------------------------------------------------- /internal/mockc/testdata/type-code/testdata/mockc_gen.go.gen: -------------------------------------------------------------------------------- 1 | // Code generated by Mockc. DO NOT EDIT. 2 | // repo: https://github.com/KimMachineGun/mockc 3 | 4 | //go:generate mockc 5 | // +build !mockc 6 | 7 | package basic 8 | 9 | import ( 10 | "sync" 11 | "unsafe" 12 | ) 13 | 14 | var _ interface { 15 | TypeCode 16 | } = &MockcTypeCode{} 17 | 18 | type MockcTypeCode struct { 19 | // method: Array 20 | _Array struct { 21 | mu sync.Mutex 22 | // basics 23 | Called bool 24 | CallCount int 25 | // call history 26 | History []struct { 27 | Params struct { 28 | P0 [][0]bool 29 | } 30 | Results struct { 31 | R0 [0]bool 32 | } 33 | } 34 | // params 35 | Params struct { 36 | P0 [][0]bool 37 | } 38 | // results 39 | Results struct { 40 | R0 [0]bool 41 | } 42 | // if it is not nil, it'll be called in the middle of the method. 43 | Body func(...[0]bool) [0]bool 44 | } 45 | // method: Bool 46 | _Bool struct { 47 | mu sync.Mutex 48 | // basics 49 | Called bool 50 | CallCount int 51 | // call history 52 | History []struct { 53 | Params struct { 54 | P0 []bool 55 | } 56 | Results struct { 57 | R0 bool 58 | } 59 | } 60 | // params 61 | Params struct { 62 | P0 []bool 63 | } 64 | // results 65 | Results struct { 66 | R0 bool 67 | } 68 | // if it is not nil, it'll be called in the middle of the method. 69 | Body func(...bool) bool 70 | } 71 | // method: BoolP 72 | _BoolP struct { 73 | mu sync.Mutex 74 | // basics 75 | Called bool 76 | CallCount int 77 | // call history 78 | History []struct { 79 | Params struct { 80 | P0 []*bool 81 | } 82 | Results struct { 83 | R0 *bool 84 | } 85 | } 86 | // params 87 | Params struct { 88 | P0 []*bool 89 | } 90 | // results 91 | Results struct { 92 | R0 *bool 93 | } 94 | // if it is not nil, it'll be called in the middle of the method. 95 | Body func(...*bool) *bool 96 | } 97 | // method: Byte 98 | _Byte struct { 99 | mu sync.Mutex 100 | // basics 101 | Called bool 102 | CallCount int 103 | // call history 104 | History []struct { 105 | Params struct { 106 | P0 []byte 107 | } 108 | Results struct { 109 | R0 byte 110 | } 111 | } 112 | // params 113 | Params struct { 114 | P0 []byte 115 | } 116 | // results 117 | Results struct { 118 | R0 byte 119 | } 120 | // if it is not nil, it'll be called in the middle of the method. 121 | Body func(...byte) byte 122 | } 123 | // method: Chan 124 | _Chan struct { 125 | mu sync.Mutex 126 | // basics 127 | Called bool 128 | CallCount int 129 | // call history 130 | History []struct { 131 | Params struct { 132 | P0 []chan bool 133 | } 134 | Results struct { 135 | R0 chan<- int 136 | R1 <-chan int8 137 | } 138 | } 139 | // params 140 | Params struct { 141 | P0 []chan bool 142 | } 143 | // results 144 | Results struct { 145 | R0 chan<- int 146 | R1 <-chan int8 147 | } 148 | // if it is not nil, it'll be called in the middle of the method. 149 | Body func(...chan bool) (chan<- int, <-chan int8) 150 | } 151 | // method: Complex128 152 | _Complex128 struct { 153 | mu sync.Mutex 154 | // basics 155 | Called bool 156 | CallCount int 157 | // call history 158 | History []struct { 159 | Params struct { 160 | P0 []complex128 161 | } 162 | Results struct { 163 | R0 complex128 164 | } 165 | } 166 | // params 167 | Params struct { 168 | P0 []complex128 169 | } 170 | // results 171 | Results struct { 172 | R0 complex128 173 | } 174 | // if it is not nil, it'll be called in the middle of the method. 175 | Body func(...complex128) complex128 176 | } 177 | // method: Complex64 178 | _Complex64 struct { 179 | mu sync.Mutex 180 | // basics 181 | Called bool 182 | CallCount int 183 | // call history 184 | History []struct { 185 | Params struct { 186 | P0 []complex64 187 | } 188 | Results struct { 189 | R0 complex64 190 | } 191 | } 192 | // params 193 | Params struct { 194 | P0 []complex64 195 | } 196 | // results 197 | Results struct { 198 | R0 complex64 199 | } 200 | // if it is not nil, it'll be called in the middle of the method. 201 | Body func(...complex64) complex64 202 | } 203 | // method: Float32 204 | _Float32 struct { 205 | mu sync.Mutex 206 | // basics 207 | Called bool 208 | CallCount int 209 | // call history 210 | History []struct { 211 | Params struct { 212 | P0 []float32 213 | } 214 | Results struct { 215 | R0 float32 216 | } 217 | } 218 | // params 219 | Params struct { 220 | P0 []float32 221 | } 222 | // results 223 | Results struct { 224 | R0 float32 225 | } 226 | // if it is not nil, it'll be called in the middle of the method. 227 | Body func(...float32) float32 228 | } 229 | // method: Float64 230 | _Float64 struct { 231 | mu sync.Mutex 232 | // basics 233 | Called bool 234 | CallCount int 235 | // call history 236 | History []struct { 237 | Params struct { 238 | P0 []float64 239 | } 240 | Results struct { 241 | R0 float64 242 | } 243 | } 244 | // params 245 | Params struct { 246 | P0 []float64 247 | } 248 | // results 249 | Results struct { 250 | R0 float64 251 | } 252 | // if it is not nil, it'll be called in the middle of the method. 253 | Body func(...float64) float64 254 | } 255 | // method: Func 256 | _Func struct { 257 | mu sync.Mutex 258 | // basics 259 | Called bool 260 | CallCount int 261 | // call history 262 | History []struct { 263 | Params struct { 264 | P0 func(bool, int, ...int8) (int32, int64) 265 | } 266 | Results struct { 267 | R0 func() error 268 | } 269 | } 270 | // params 271 | Params struct { 272 | P0 func(bool, int, ...int8) (int32, int64) 273 | } 274 | // results 275 | Results struct { 276 | R0 func() error 277 | } 278 | // if it is not nil, it'll be called in the middle of the method. 279 | Body func(func(bool, int, ...int8) (int32, int64)) func() error 280 | } 281 | // method: Int 282 | _Int struct { 283 | mu sync.Mutex 284 | // basics 285 | Called bool 286 | CallCount int 287 | // call history 288 | History []struct { 289 | Params struct { 290 | P0 []int 291 | } 292 | Results struct { 293 | R0 int 294 | } 295 | } 296 | // params 297 | Params struct { 298 | P0 []int 299 | } 300 | // results 301 | Results struct { 302 | R0 int 303 | } 304 | // if it is not nil, it'll be called in the middle of the method. 305 | Body func(...int) int 306 | } 307 | // method: Int16 308 | _Int16 struct { 309 | mu sync.Mutex 310 | // basics 311 | Called bool 312 | CallCount int 313 | // call history 314 | History []struct { 315 | Params struct { 316 | P0 []int16 317 | } 318 | Results struct { 319 | R0 int16 320 | } 321 | } 322 | // params 323 | Params struct { 324 | P0 []int16 325 | } 326 | // results 327 | Results struct { 328 | R0 int16 329 | } 330 | // if it is not nil, it'll be called in the middle of the method. 331 | Body func(...int16) int16 332 | } 333 | // method: Int32 334 | _Int32 struct { 335 | mu sync.Mutex 336 | // basics 337 | Called bool 338 | CallCount int 339 | // call history 340 | History []struct { 341 | Params struct { 342 | P0 []int32 343 | } 344 | Results struct { 345 | R0 int32 346 | } 347 | } 348 | // params 349 | Params struct { 350 | P0 []int32 351 | } 352 | // results 353 | Results struct { 354 | R0 int32 355 | } 356 | // if it is not nil, it'll be called in the middle of the method. 357 | Body func(...int32) int32 358 | } 359 | // method: Int64 360 | _Int64 struct { 361 | mu sync.Mutex 362 | // basics 363 | Called bool 364 | CallCount int 365 | // call history 366 | History []struct { 367 | Params struct { 368 | P0 []int64 369 | } 370 | Results struct { 371 | R0 int64 372 | } 373 | } 374 | // params 375 | Params struct { 376 | P0 []int64 377 | } 378 | // results 379 | Results struct { 380 | R0 int64 381 | } 382 | // if it is not nil, it'll be called in the middle of the method. 383 | Body func(...int64) int64 384 | } 385 | // method: Int8 386 | _Int8 struct { 387 | mu sync.Mutex 388 | // basics 389 | Called bool 390 | CallCount int 391 | // call history 392 | History []struct { 393 | Params struct { 394 | P0 []int8 395 | } 396 | Results struct { 397 | R0 int8 398 | } 399 | } 400 | // params 401 | Params struct { 402 | P0 []int8 403 | } 404 | // results 405 | Results struct { 406 | R0 int8 407 | } 408 | // if it is not nil, it'll be called in the middle of the method. 409 | Body func(...int8) int8 410 | } 411 | // method: Interface 412 | _Interface struct { 413 | mu sync.Mutex 414 | // basics 415 | Called bool 416 | CallCount int 417 | // call history 418 | History []struct { 419 | Params struct { 420 | P0 []interface{} 421 | } 422 | Results struct { 423 | R0 interface { 424 | Hello() string 425 | World() string 426 | } 427 | } 428 | } 429 | // params 430 | Params struct { 431 | P0 []interface{} 432 | } 433 | // results 434 | Results struct { 435 | R0 interface { 436 | Hello() string 437 | World() string 438 | } 439 | } 440 | // if it is not nil, it'll be called in the middle of the method. 441 | Body func(...interface{}) interface { 442 | Hello() string 443 | World() string 444 | } 445 | } 446 | // method: Map 447 | _Map struct { 448 | mu sync.Mutex 449 | // basics 450 | Called bool 451 | CallCount int 452 | // call history 453 | History []struct { 454 | Params struct { 455 | P0 []map[bool]int 456 | } 457 | Results struct { 458 | R0 map[bool]int 459 | } 460 | } 461 | // params 462 | Params struct { 463 | P0 []map[bool]int 464 | } 465 | // results 466 | Results struct { 467 | R0 map[bool]int 468 | } 469 | // if it is not nil, it'll be called in the middle of the method. 470 | Body func(...map[bool]int) map[bool]int 471 | } 472 | // method: Pointer 473 | _Pointer struct { 474 | mu sync.Mutex 475 | // basics 476 | Called bool 477 | CallCount int 478 | // call history 479 | History []struct { 480 | Params struct { 481 | P0 []unsafe.Pointer 482 | } 483 | Results struct { 484 | R0 unsafe.Pointer 485 | } 486 | } 487 | // params 488 | Params struct { 489 | P0 []unsafe.Pointer 490 | } 491 | // results 492 | Results struct { 493 | R0 unsafe.Pointer 494 | } 495 | // if it is not nil, it'll be called in the middle of the method. 496 | Body func(...unsafe.Pointer) unsafe.Pointer 497 | } 498 | // method: Rune 499 | _Rune struct { 500 | mu sync.Mutex 501 | // basics 502 | Called bool 503 | CallCount int 504 | // call history 505 | History []struct { 506 | Params struct { 507 | P0 []rune 508 | } 509 | Results struct { 510 | R0 rune 511 | } 512 | } 513 | // params 514 | Params struct { 515 | P0 []rune 516 | } 517 | // results 518 | Results struct { 519 | R0 rune 520 | } 521 | // if it is not nil, it'll be called in the middle of the method. 522 | Body func(...rune) rune 523 | } 524 | // method: Slice 525 | _Slice struct { 526 | mu sync.Mutex 527 | // basics 528 | Called bool 529 | CallCount int 530 | // call history 531 | History []struct { 532 | Params struct { 533 | P0 [][]bool 534 | } 535 | Results struct { 536 | R0 []bool 537 | } 538 | } 539 | // params 540 | Params struct { 541 | P0 [][]bool 542 | } 543 | // results 544 | Results struct { 545 | R0 []bool 546 | } 547 | // if it is not nil, it'll be called in the middle of the method. 548 | Body func(...[]bool) []bool 549 | } 550 | // method: String 551 | _String struct { 552 | mu sync.Mutex 553 | // basics 554 | Called bool 555 | CallCount int 556 | // call history 557 | History []struct { 558 | Params struct { 559 | P0 []string 560 | } 561 | Results struct { 562 | R0 string 563 | } 564 | } 565 | // params 566 | Params struct { 567 | P0 []string 568 | } 569 | // results 570 | Results struct { 571 | R0 string 572 | } 573 | // if it is not nil, it'll be called in the middle of the method. 574 | Body func(...string) string 575 | } 576 | // method: Struct 577 | _Struct struct { 578 | mu sync.Mutex 579 | // basics 580 | Called bool 581 | CallCount int 582 | // call history 583 | History []struct { 584 | Params struct { 585 | P0 []struct { 586 | A bool 587 | anon 588 | } 589 | } 590 | Results struct { 591 | R0 struct { 592 | B int 593 | } 594 | } 595 | } 596 | // params 597 | Params struct { 598 | P0 []struct { 599 | A bool 600 | anon 601 | } 602 | } 603 | // results 604 | Results struct { 605 | R0 struct { 606 | B int 607 | } 608 | } 609 | // if it is not nil, it'll be called in the middle of the method. 610 | Body func(...struct { 611 | A bool 612 | anon 613 | }) struct { 614 | B int 615 | } 616 | } 617 | // method: Tuple 618 | _Tuple struct { 619 | mu sync.Mutex 620 | // basics 621 | Called bool 622 | CallCount int 623 | // call history 624 | History []struct { 625 | Results struct { 626 | R0 bool 627 | R1 int 628 | R2 int8 629 | } 630 | } 631 | // results 632 | Results struct { 633 | R0 bool 634 | R1 int 635 | R2 int8 636 | } 637 | // if it is not nil, it'll be called in the middle of the method. 638 | Body func() (bool, int, int8) 639 | } 640 | // method: Uint 641 | _Uint struct { 642 | mu sync.Mutex 643 | // basics 644 | Called bool 645 | CallCount int 646 | // call history 647 | History []struct { 648 | Params struct { 649 | P0 []uint 650 | } 651 | Results struct { 652 | R0 uint 653 | } 654 | } 655 | // params 656 | Params struct { 657 | P0 []uint 658 | } 659 | // results 660 | Results struct { 661 | R0 uint 662 | } 663 | // if it is not nil, it'll be called in the middle of the method. 664 | Body func(...uint) uint 665 | } 666 | // method: Uint16 667 | _Uint16 struct { 668 | mu sync.Mutex 669 | // basics 670 | Called bool 671 | CallCount int 672 | // call history 673 | History []struct { 674 | Params struct { 675 | P0 []uint16 676 | } 677 | Results struct { 678 | R0 uint16 679 | } 680 | } 681 | // params 682 | Params struct { 683 | P0 []uint16 684 | } 685 | // results 686 | Results struct { 687 | R0 uint16 688 | } 689 | // if it is not nil, it'll be called in the middle of the method. 690 | Body func(...uint16) uint16 691 | } 692 | // method: Uint32 693 | _Uint32 struct { 694 | mu sync.Mutex 695 | // basics 696 | Called bool 697 | CallCount int 698 | // call history 699 | History []struct { 700 | Params struct { 701 | P0 []uint32 702 | } 703 | Results struct { 704 | R0 uint32 705 | } 706 | } 707 | // params 708 | Params struct { 709 | P0 []uint32 710 | } 711 | // results 712 | Results struct { 713 | R0 uint32 714 | } 715 | // if it is not nil, it'll be called in the middle of the method. 716 | Body func(...uint32) uint32 717 | } 718 | // method: Uint64 719 | _Uint64 struct { 720 | mu sync.Mutex 721 | // basics 722 | Called bool 723 | CallCount int 724 | // call history 725 | History []struct { 726 | Params struct { 727 | P0 []uint64 728 | } 729 | Results struct { 730 | R0 uint64 731 | } 732 | } 733 | // params 734 | Params struct { 735 | P0 []uint64 736 | } 737 | // results 738 | Results struct { 739 | R0 uint64 740 | } 741 | // if it is not nil, it'll be called in the middle of the method. 742 | Body func(...uint64) uint64 743 | } 744 | // method: Uint8 745 | _Uint8 struct { 746 | mu sync.Mutex 747 | // basics 748 | Called bool 749 | CallCount int 750 | // call history 751 | History []struct { 752 | Params struct { 753 | P0 []uint8 754 | } 755 | Results struct { 756 | R0 uint8 757 | } 758 | } 759 | // params 760 | Params struct { 761 | P0 []uint8 762 | } 763 | // results 764 | Results struct { 765 | R0 uint8 766 | } 767 | // if it is not nil, it'll be called in the middle of the method. 768 | Body func(...uint8) uint8 769 | } 770 | // method: Uintptr 771 | _Uintptr struct { 772 | mu sync.Mutex 773 | // basics 774 | Called bool 775 | CallCount int 776 | // call history 777 | History []struct { 778 | Params struct { 779 | P0 []uintptr 780 | } 781 | Results struct { 782 | R0 uintptr 783 | } 784 | } 785 | // params 786 | Params struct { 787 | P0 []uintptr 788 | } 789 | // results 790 | Results struct { 791 | R0 uintptr 792 | } 793 | // if it is not nil, it'll be called in the middle of the method. 794 | Body func(...uintptr) uintptr 795 | } 796 | } 797 | 798 | func (recv *MockcTypeCode) Array(p0 ...[0]bool) [0]bool { 799 | recv._Array.mu.Lock() 800 | defer recv._Array.mu.Unlock() 801 | // basics 802 | recv._Array.Called = true 803 | recv._Array.CallCount++ 804 | // params 805 | recv._Array.Params.P0 = p0 806 | // body 807 | if recv._Array.Body != nil { 808 | recv._Array.Results.R0 = recv._Array.Body(p0...) 809 | } 810 | // call history 811 | recv._Array.History = append(recv._Array.History, struct { 812 | Params struct { 813 | P0 [][0]bool 814 | } 815 | Results struct { 816 | R0 [0]bool 817 | } 818 | }{ 819 | Params: recv._Array.Params, 820 | Results: recv._Array.Results, 821 | }) 822 | // results 823 | return recv._Array.Results.R0 824 | } 825 | 826 | func (recv *MockcTypeCode) Bool(p0 ...bool) bool { 827 | recv._Bool.mu.Lock() 828 | defer recv._Bool.mu.Unlock() 829 | // basics 830 | recv._Bool.Called = true 831 | recv._Bool.CallCount++ 832 | // params 833 | recv._Bool.Params.P0 = p0 834 | // body 835 | if recv._Bool.Body != nil { 836 | recv._Bool.Results.R0 = recv._Bool.Body(p0...) 837 | } 838 | // call history 839 | recv._Bool.History = append(recv._Bool.History, struct { 840 | Params struct { 841 | P0 []bool 842 | } 843 | Results struct { 844 | R0 bool 845 | } 846 | }{ 847 | Params: recv._Bool.Params, 848 | Results: recv._Bool.Results, 849 | }) 850 | // results 851 | return recv._Bool.Results.R0 852 | } 853 | 854 | func (recv *MockcTypeCode) BoolP(p0 ...*bool) *bool { 855 | recv._BoolP.mu.Lock() 856 | defer recv._BoolP.mu.Unlock() 857 | // basics 858 | recv._BoolP.Called = true 859 | recv._BoolP.CallCount++ 860 | // params 861 | recv._BoolP.Params.P0 = p0 862 | // body 863 | if recv._BoolP.Body != nil { 864 | recv._BoolP.Results.R0 = recv._BoolP.Body(p0...) 865 | } 866 | // call history 867 | recv._BoolP.History = append(recv._BoolP.History, struct { 868 | Params struct { 869 | P0 []*bool 870 | } 871 | Results struct { 872 | R0 *bool 873 | } 874 | }{ 875 | Params: recv._BoolP.Params, 876 | Results: recv._BoolP.Results, 877 | }) 878 | // results 879 | return recv._BoolP.Results.R0 880 | } 881 | 882 | func (recv *MockcTypeCode) Byte(p0 ...byte) byte { 883 | recv._Byte.mu.Lock() 884 | defer recv._Byte.mu.Unlock() 885 | // basics 886 | recv._Byte.Called = true 887 | recv._Byte.CallCount++ 888 | // params 889 | recv._Byte.Params.P0 = p0 890 | // body 891 | if recv._Byte.Body != nil { 892 | recv._Byte.Results.R0 = recv._Byte.Body(p0...) 893 | } 894 | // call history 895 | recv._Byte.History = append(recv._Byte.History, struct { 896 | Params struct { 897 | P0 []byte 898 | } 899 | Results struct { 900 | R0 byte 901 | } 902 | }{ 903 | Params: recv._Byte.Params, 904 | Results: recv._Byte.Results, 905 | }) 906 | // results 907 | return recv._Byte.Results.R0 908 | } 909 | 910 | func (recv *MockcTypeCode) Chan(p0 ...chan bool) (chan<- int, <-chan int8) { 911 | recv._Chan.mu.Lock() 912 | defer recv._Chan.mu.Unlock() 913 | // basics 914 | recv._Chan.Called = true 915 | recv._Chan.CallCount++ 916 | // params 917 | recv._Chan.Params.P0 = p0 918 | // body 919 | if recv._Chan.Body != nil { 920 | recv._Chan.Results.R0, recv._Chan.Results.R1 = recv._Chan.Body(p0...) 921 | } 922 | // call history 923 | recv._Chan.History = append(recv._Chan.History, struct { 924 | Params struct { 925 | P0 []chan bool 926 | } 927 | Results struct { 928 | R0 chan<- int 929 | R1 <-chan int8 930 | } 931 | }{ 932 | Params: recv._Chan.Params, 933 | Results: recv._Chan.Results, 934 | }) 935 | // results 936 | return recv._Chan.Results.R0, recv._Chan.Results.R1 937 | } 938 | 939 | func (recv *MockcTypeCode) Complex128(p0 ...complex128) complex128 { 940 | recv._Complex128.mu.Lock() 941 | defer recv._Complex128.mu.Unlock() 942 | // basics 943 | recv._Complex128.Called = true 944 | recv._Complex128.CallCount++ 945 | // params 946 | recv._Complex128.Params.P0 = p0 947 | // body 948 | if recv._Complex128.Body != nil { 949 | recv._Complex128.Results.R0 = recv._Complex128.Body(p0...) 950 | } 951 | // call history 952 | recv._Complex128.History = append(recv._Complex128.History, struct { 953 | Params struct { 954 | P0 []complex128 955 | } 956 | Results struct { 957 | R0 complex128 958 | } 959 | }{ 960 | Params: recv._Complex128.Params, 961 | Results: recv._Complex128.Results, 962 | }) 963 | // results 964 | return recv._Complex128.Results.R0 965 | } 966 | 967 | func (recv *MockcTypeCode) Complex64(p0 ...complex64) complex64 { 968 | recv._Complex64.mu.Lock() 969 | defer recv._Complex64.mu.Unlock() 970 | // basics 971 | recv._Complex64.Called = true 972 | recv._Complex64.CallCount++ 973 | // params 974 | recv._Complex64.Params.P0 = p0 975 | // body 976 | if recv._Complex64.Body != nil { 977 | recv._Complex64.Results.R0 = recv._Complex64.Body(p0...) 978 | } 979 | // call history 980 | recv._Complex64.History = append(recv._Complex64.History, struct { 981 | Params struct { 982 | P0 []complex64 983 | } 984 | Results struct { 985 | R0 complex64 986 | } 987 | }{ 988 | Params: recv._Complex64.Params, 989 | Results: recv._Complex64.Results, 990 | }) 991 | // results 992 | return recv._Complex64.Results.R0 993 | } 994 | 995 | func (recv *MockcTypeCode) Float32(p0 ...float32) float32 { 996 | recv._Float32.mu.Lock() 997 | defer recv._Float32.mu.Unlock() 998 | // basics 999 | recv._Float32.Called = true 1000 | recv._Float32.CallCount++ 1001 | // params 1002 | recv._Float32.Params.P0 = p0 1003 | // body 1004 | if recv._Float32.Body != nil { 1005 | recv._Float32.Results.R0 = recv._Float32.Body(p0...) 1006 | } 1007 | // call history 1008 | recv._Float32.History = append(recv._Float32.History, struct { 1009 | Params struct { 1010 | P0 []float32 1011 | } 1012 | Results struct { 1013 | R0 float32 1014 | } 1015 | }{ 1016 | Params: recv._Float32.Params, 1017 | Results: recv._Float32.Results, 1018 | }) 1019 | // results 1020 | return recv._Float32.Results.R0 1021 | } 1022 | 1023 | func (recv *MockcTypeCode) Float64(p0 ...float64) float64 { 1024 | recv._Float64.mu.Lock() 1025 | defer recv._Float64.mu.Unlock() 1026 | // basics 1027 | recv._Float64.Called = true 1028 | recv._Float64.CallCount++ 1029 | // params 1030 | recv._Float64.Params.P0 = p0 1031 | // body 1032 | if recv._Float64.Body != nil { 1033 | recv._Float64.Results.R0 = recv._Float64.Body(p0...) 1034 | } 1035 | // call history 1036 | recv._Float64.History = append(recv._Float64.History, struct { 1037 | Params struct { 1038 | P0 []float64 1039 | } 1040 | Results struct { 1041 | R0 float64 1042 | } 1043 | }{ 1044 | Params: recv._Float64.Params, 1045 | Results: recv._Float64.Results, 1046 | }) 1047 | // results 1048 | return recv._Float64.Results.R0 1049 | } 1050 | 1051 | func (recv *MockcTypeCode) Func(p0 func(bool, int, ...int8) (int32, int64)) func() error { 1052 | recv._Func.mu.Lock() 1053 | defer recv._Func.mu.Unlock() 1054 | // basics 1055 | recv._Func.Called = true 1056 | recv._Func.CallCount++ 1057 | // params 1058 | recv._Func.Params.P0 = p0 1059 | // body 1060 | if recv._Func.Body != nil { 1061 | recv._Func.Results.R0 = recv._Func.Body(p0) 1062 | } 1063 | // call history 1064 | recv._Func.History = append(recv._Func.History, struct { 1065 | Params struct { 1066 | P0 func(bool, int, ...int8) (int32, int64) 1067 | } 1068 | Results struct { 1069 | R0 func() error 1070 | } 1071 | }{ 1072 | Params: recv._Func.Params, 1073 | Results: recv._Func.Results, 1074 | }) 1075 | // results 1076 | return recv._Func.Results.R0 1077 | } 1078 | 1079 | func (recv *MockcTypeCode) Int(p0 ...int) int { 1080 | recv._Int.mu.Lock() 1081 | defer recv._Int.mu.Unlock() 1082 | // basics 1083 | recv._Int.Called = true 1084 | recv._Int.CallCount++ 1085 | // params 1086 | recv._Int.Params.P0 = p0 1087 | // body 1088 | if recv._Int.Body != nil { 1089 | recv._Int.Results.R0 = recv._Int.Body(p0...) 1090 | } 1091 | // call history 1092 | recv._Int.History = append(recv._Int.History, struct { 1093 | Params struct { 1094 | P0 []int 1095 | } 1096 | Results struct { 1097 | R0 int 1098 | } 1099 | }{ 1100 | Params: recv._Int.Params, 1101 | Results: recv._Int.Results, 1102 | }) 1103 | // results 1104 | return recv._Int.Results.R0 1105 | } 1106 | 1107 | func (recv *MockcTypeCode) Int16(p0 ...int16) int16 { 1108 | recv._Int16.mu.Lock() 1109 | defer recv._Int16.mu.Unlock() 1110 | // basics 1111 | recv._Int16.Called = true 1112 | recv._Int16.CallCount++ 1113 | // params 1114 | recv._Int16.Params.P0 = p0 1115 | // body 1116 | if recv._Int16.Body != nil { 1117 | recv._Int16.Results.R0 = recv._Int16.Body(p0...) 1118 | } 1119 | // call history 1120 | recv._Int16.History = append(recv._Int16.History, struct { 1121 | Params struct { 1122 | P0 []int16 1123 | } 1124 | Results struct { 1125 | R0 int16 1126 | } 1127 | }{ 1128 | Params: recv._Int16.Params, 1129 | Results: recv._Int16.Results, 1130 | }) 1131 | // results 1132 | return recv._Int16.Results.R0 1133 | } 1134 | 1135 | func (recv *MockcTypeCode) Int32(p0 ...int32) int32 { 1136 | recv._Int32.mu.Lock() 1137 | defer recv._Int32.mu.Unlock() 1138 | // basics 1139 | recv._Int32.Called = true 1140 | recv._Int32.CallCount++ 1141 | // params 1142 | recv._Int32.Params.P0 = p0 1143 | // body 1144 | if recv._Int32.Body != nil { 1145 | recv._Int32.Results.R0 = recv._Int32.Body(p0...) 1146 | } 1147 | // call history 1148 | recv._Int32.History = append(recv._Int32.History, struct { 1149 | Params struct { 1150 | P0 []int32 1151 | } 1152 | Results struct { 1153 | R0 int32 1154 | } 1155 | }{ 1156 | Params: recv._Int32.Params, 1157 | Results: recv._Int32.Results, 1158 | }) 1159 | // results 1160 | return recv._Int32.Results.R0 1161 | } 1162 | 1163 | func (recv *MockcTypeCode) Int64(p0 ...int64) int64 { 1164 | recv._Int64.mu.Lock() 1165 | defer recv._Int64.mu.Unlock() 1166 | // basics 1167 | recv._Int64.Called = true 1168 | recv._Int64.CallCount++ 1169 | // params 1170 | recv._Int64.Params.P0 = p0 1171 | // body 1172 | if recv._Int64.Body != nil { 1173 | recv._Int64.Results.R0 = recv._Int64.Body(p0...) 1174 | } 1175 | // call history 1176 | recv._Int64.History = append(recv._Int64.History, struct { 1177 | Params struct { 1178 | P0 []int64 1179 | } 1180 | Results struct { 1181 | R0 int64 1182 | } 1183 | }{ 1184 | Params: recv._Int64.Params, 1185 | Results: recv._Int64.Results, 1186 | }) 1187 | // results 1188 | return recv._Int64.Results.R0 1189 | } 1190 | 1191 | func (recv *MockcTypeCode) Int8(p0 ...int8) int8 { 1192 | recv._Int8.mu.Lock() 1193 | defer recv._Int8.mu.Unlock() 1194 | // basics 1195 | recv._Int8.Called = true 1196 | recv._Int8.CallCount++ 1197 | // params 1198 | recv._Int8.Params.P0 = p0 1199 | // body 1200 | if recv._Int8.Body != nil { 1201 | recv._Int8.Results.R0 = recv._Int8.Body(p0...) 1202 | } 1203 | // call history 1204 | recv._Int8.History = append(recv._Int8.History, struct { 1205 | Params struct { 1206 | P0 []int8 1207 | } 1208 | Results struct { 1209 | R0 int8 1210 | } 1211 | }{ 1212 | Params: recv._Int8.Params, 1213 | Results: recv._Int8.Results, 1214 | }) 1215 | // results 1216 | return recv._Int8.Results.R0 1217 | } 1218 | 1219 | func (recv *MockcTypeCode) Interface(p0 ...interface{}) interface { 1220 | Hello() string 1221 | World() string 1222 | } { 1223 | recv._Interface.mu.Lock() 1224 | defer recv._Interface.mu.Unlock() 1225 | // basics 1226 | recv._Interface.Called = true 1227 | recv._Interface.CallCount++ 1228 | // params 1229 | recv._Interface.Params.P0 = p0 1230 | // body 1231 | if recv._Interface.Body != nil { 1232 | recv._Interface.Results.R0 = recv._Interface.Body(p0...) 1233 | } 1234 | // call history 1235 | recv._Interface.History = append(recv._Interface.History, struct { 1236 | Params struct { 1237 | P0 []interface{} 1238 | } 1239 | Results struct { 1240 | R0 interface { 1241 | Hello() string 1242 | World() string 1243 | } 1244 | } 1245 | }{ 1246 | Params: recv._Interface.Params, 1247 | Results: recv._Interface.Results, 1248 | }) 1249 | // results 1250 | return recv._Interface.Results.R0 1251 | } 1252 | 1253 | func (recv *MockcTypeCode) Map(p0 ...map[bool]int) map[bool]int { 1254 | recv._Map.mu.Lock() 1255 | defer recv._Map.mu.Unlock() 1256 | // basics 1257 | recv._Map.Called = true 1258 | recv._Map.CallCount++ 1259 | // params 1260 | recv._Map.Params.P0 = p0 1261 | // body 1262 | if recv._Map.Body != nil { 1263 | recv._Map.Results.R0 = recv._Map.Body(p0...) 1264 | } 1265 | // call history 1266 | recv._Map.History = append(recv._Map.History, struct { 1267 | Params struct { 1268 | P0 []map[bool]int 1269 | } 1270 | Results struct { 1271 | R0 map[bool]int 1272 | } 1273 | }{ 1274 | Params: recv._Map.Params, 1275 | Results: recv._Map.Results, 1276 | }) 1277 | // results 1278 | return recv._Map.Results.R0 1279 | } 1280 | 1281 | func (recv *MockcTypeCode) Pointer(p0 ...unsafe.Pointer) unsafe.Pointer { 1282 | recv._Pointer.mu.Lock() 1283 | defer recv._Pointer.mu.Unlock() 1284 | // basics 1285 | recv._Pointer.Called = true 1286 | recv._Pointer.CallCount++ 1287 | // params 1288 | recv._Pointer.Params.P0 = p0 1289 | // body 1290 | if recv._Pointer.Body != nil { 1291 | recv._Pointer.Results.R0 = recv._Pointer.Body(p0...) 1292 | } 1293 | // call history 1294 | recv._Pointer.History = append(recv._Pointer.History, struct { 1295 | Params struct { 1296 | P0 []unsafe.Pointer 1297 | } 1298 | Results struct { 1299 | R0 unsafe.Pointer 1300 | } 1301 | }{ 1302 | Params: recv._Pointer.Params, 1303 | Results: recv._Pointer.Results, 1304 | }) 1305 | // results 1306 | return recv._Pointer.Results.R0 1307 | } 1308 | 1309 | func (recv *MockcTypeCode) Rune(p0 ...rune) rune { 1310 | recv._Rune.mu.Lock() 1311 | defer recv._Rune.mu.Unlock() 1312 | // basics 1313 | recv._Rune.Called = true 1314 | recv._Rune.CallCount++ 1315 | // params 1316 | recv._Rune.Params.P0 = p0 1317 | // body 1318 | if recv._Rune.Body != nil { 1319 | recv._Rune.Results.R0 = recv._Rune.Body(p0...) 1320 | } 1321 | // call history 1322 | recv._Rune.History = append(recv._Rune.History, struct { 1323 | Params struct { 1324 | P0 []rune 1325 | } 1326 | Results struct { 1327 | R0 rune 1328 | } 1329 | }{ 1330 | Params: recv._Rune.Params, 1331 | Results: recv._Rune.Results, 1332 | }) 1333 | // results 1334 | return recv._Rune.Results.R0 1335 | } 1336 | 1337 | func (recv *MockcTypeCode) Slice(p0 ...[]bool) []bool { 1338 | recv._Slice.mu.Lock() 1339 | defer recv._Slice.mu.Unlock() 1340 | // basics 1341 | recv._Slice.Called = true 1342 | recv._Slice.CallCount++ 1343 | // params 1344 | recv._Slice.Params.P0 = p0 1345 | // body 1346 | if recv._Slice.Body != nil { 1347 | recv._Slice.Results.R0 = recv._Slice.Body(p0...) 1348 | } 1349 | // call history 1350 | recv._Slice.History = append(recv._Slice.History, struct { 1351 | Params struct { 1352 | P0 [][]bool 1353 | } 1354 | Results struct { 1355 | R0 []bool 1356 | } 1357 | }{ 1358 | Params: recv._Slice.Params, 1359 | Results: recv._Slice.Results, 1360 | }) 1361 | // results 1362 | return recv._Slice.Results.R0 1363 | } 1364 | 1365 | func (recv *MockcTypeCode) String(p0 ...string) string { 1366 | recv._String.mu.Lock() 1367 | defer recv._String.mu.Unlock() 1368 | // basics 1369 | recv._String.Called = true 1370 | recv._String.CallCount++ 1371 | // params 1372 | recv._String.Params.P0 = p0 1373 | // body 1374 | if recv._String.Body != nil { 1375 | recv._String.Results.R0 = recv._String.Body(p0...) 1376 | } 1377 | // call history 1378 | recv._String.History = append(recv._String.History, struct { 1379 | Params struct { 1380 | P0 []string 1381 | } 1382 | Results struct { 1383 | R0 string 1384 | } 1385 | }{ 1386 | Params: recv._String.Params, 1387 | Results: recv._String.Results, 1388 | }) 1389 | // results 1390 | return recv._String.Results.R0 1391 | } 1392 | 1393 | func (recv *MockcTypeCode) Struct(p0 ...struct { 1394 | A bool 1395 | anon 1396 | }) struct { 1397 | B int 1398 | } { 1399 | recv._Struct.mu.Lock() 1400 | defer recv._Struct.mu.Unlock() 1401 | // basics 1402 | recv._Struct.Called = true 1403 | recv._Struct.CallCount++ 1404 | // params 1405 | recv._Struct.Params.P0 = p0 1406 | // body 1407 | if recv._Struct.Body != nil { 1408 | recv._Struct.Results.R0 = recv._Struct.Body(p0...) 1409 | } 1410 | // call history 1411 | recv._Struct.History = append(recv._Struct.History, struct { 1412 | Params struct { 1413 | P0 []struct { 1414 | A bool 1415 | anon 1416 | } 1417 | } 1418 | Results struct { 1419 | R0 struct { 1420 | B int 1421 | } 1422 | } 1423 | }{ 1424 | Params: recv._Struct.Params, 1425 | Results: recv._Struct.Results, 1426 | }) 1427 | // results 1428 | return recv._Struct.Results.R0 1429 | } 1430 | 1431 | func (recv *MockcTypeCode) Tuple() (bool, int, int8) { 1432 | recv._Tuple.mu.Lock() 1433 | defer recv._Tuple.mu.Unlock() 1434 | // basics 1435 | recv._Tuple.Called = true 1436 | recv._Tuple.CallCount++ 1437 | // body 1438 | if recv._Tuple.Body != nil { 1439 | recv._Tuple.Results.R0, recv._Tuple.Results.R1, recv._Tuple.Results.R2 = recv._Tuple.Body() 1440 | } 1441 | // call history 1442 | recv._Tuple.History = append(recv._Tuple.History, struct { 1443 | Results struct { 1444 | R0 bool 1445 | R1 int 1446 | R2 int8 1447 | } 1448 | }{Results: recv._Tuple.Results}) 1449 | // results 1450 | return recv._Tuple.Results.R0, recv._Tuple.Results.R1, recv._Tuple.Results.R2 1451 | } 1452 | 1453 | func (recv *MockcTypeCode) Uint(p0 ...uint) uint { 1454 | recv._Uint.mu.Lock() 1455 | defer recv._Uint.mu.Unlock() 1456 | // basics 1457 | recv._Uint.Called = true 1458 | recv._Uint.CallCount++ 1459 | // params 1460 | recv._Uint.Params.P0 = p0 1461 | // body 1462 | if recv._Uint.Body != nil { 1463 | recv._Uint.Results.R0 = recv._Uint.Body(p0...) 1464 | } 1465 | // call history 1466 | recv._Uint.History = append(recv._Uint.History, struct { 1467 | Params struct { 1468 | P0 []uint 1469 | } 1470 | Results struct { 1471 | R0 uint 1472 | } 1473 | }{ 1474 | Params: recv._Uint.Params, 1475 | Results: recv._Uint.Results, 1476 | }) 1477 | // results 1478 | return recv._Uint.Results.R0 1479 | } 1480 | 1481 | func (recv *MockcTypeCode) Uint16(p0 ...uint16) uint16 { 1482 | recv._Uint16.mu.Lock() 1483 | defer recv._Uint16.mu.Unlock() 1484 | // basics 1485 | recv._Uint16.Called = true 1486 | recv._Uint16.CallCount++ 1487 | // params 1488 | recv._Uint16.Params.P0 = p0 1489 | // body 1490 | if recv._Uint16.Body != nil { 1491 | recv._Uint16.Results.R0 = recv._Uint16.Body(p0...) 1492 | } 1493 | // call history 1494 | recv._Uint16.History = append(recv._Uint16.History, struct { 1495 | Params struct { 1496 | P0 []uint16 1497 | } 1498 | Results struct { 1499 | R0 uint16 1500 | } 1501 | }{ 1502 | Params: recv._Uint16.Params, 1503 | Results: recv._Uint16.Results, 1504 | }) 1505 | // results 1506 | return recv._Uint16.Results.R0 1507 | } 1508 | 1509 | func (recv *MockcTypeCode) Uint32(p0 ...uint32) uint32 { 1510 | recv._Uint32.mu.Lock() 1511 | defer recv._Uint32.mu.Unlock() 1512 | // basics 1513 | recv._Uint32.Called = true 1514 | recv._Uint32.CallCount++ 1515 | // params 1516 | recv._Uint32.Params.P0 = p0 1517 | // body 1518 | if recv._Uint32.Body != nil { 1519 | recv._Uint32.Results.R0 = recv._Uint32.Body(p0...) 1520 | } 1521 | // call history 1522 | recv._Uint32.History = append(recv._Uint32.History, struct { 1523 | Params struct { 1524 | P0 []uint32 1525 | } 1526 | Results struct { 1527 | R0 uint32 1528 | } 1529 | }{ 1530 | Params: recv._Uint32.Params, 1531 | Results: recv._Uint32.Results, 1532 | }) 1533 | // results 1534 | return recv._Uint32.Results.R0 1535 | } 1536 | 1537 | func (recv *MockcTypeCode) Uint64(p0 ...uint64) uint64 { 1538 | recv._Uint64.mu.Lock() 1539 | defer recv._Uint64.mu.Unlock() 1540 | // basics 1541 | recv._Uint64.Called = true 1542 | recv._Uint64.CallCount++ 1543 | // params 1544 | recv._Uint64.Params.P0 = p0 1545 | // body 1546 | if recv._Uint64.Body != nil { 1547 | recv._Uint64.Results.R0 = recv._Uint64.Body(p0...) 1548 | } 1549 | // call history 1550 | recv._Uint64.History = append(recv._Uint64.History, struct { 1551 | Params struct { 1552 | P0 []uint64 1553 | } 1554 | Results struct { 1555 | R0 uint64 1556 | } 1557 | }{ 1558 | Params: recv._Uint64.Params, 1559 | Results: recv._Uint64.Results, 1560 | }) 1561 | // results 1562 | return recv._Uint64.Results.R0 1563 | } 1564 | 1565 | func (recv *MockcTypeCode) Uint8(p0 ...uint8) uint8 { 1566 | recv._Uint8.mu.Lock() 1567 | defer recv._Uint8.mu.Unlock() 1568 | // basics 1569 | recv._Uint8.Called = true 1570 | recv._Uint8.CallCount++ 1571 | // params 1572 | recv._Uint8.Params.P0 = p0 1573 | // body 1574 | if recv._Uint8.Body != nil { 1575 | recv._Uint8.Results.R0 = recv._Uint8.Body(p0...) 1576 | } 1577 | // call history 1578 | recv._Uint8.History = append(recv._Uint8.History, struct { 1579 | Params struct { 1580 | P0 []uint8 1581 | } 1582 | Results struct { 1583 | R0 uint8 1584 | } 1585 | }{ 1586 | Params: recv._Uint8.Params, 1587 | Results: recv._Uint8.Results, 1588 | }) 1589 | // results 1590 | return recv._Uint8.Results.R0 1591 | } 1592 | 1593 | func (recv *MockcTypeCode) Uintptr(p0 ...uintptr) uintptr { 1594 | recv._Uintptr.mu.Lock() 1595 | defer recv._Uintptr.mu.Unlock() 1596 | // basics 1597 | recv._Uintptr.Called = true 1598 | recv._Uintptr.CallCount++ 1599 | // params 1600 | recv._Uintptr.Params.P0 = p0 1601 | // body 1602 | if recv._Uintptr.Body != nil { 1603 | recv._Uintptr.Results.R0 = recv._Uintptr.Body(p0...) 1604 | } 1605 | // call history 1606 | recv._Uintptr.History = append(recv._Uintptr.History, struct { 1607 | Params struct { 1608 | P0 []uintptr 1609 | } 1610 | Results struct { 1611 | R0 uintptr 1612 | } 1613 | }{ 1614 | Params: recv._Uintptr.Params, 1615 | Results: recv._Uintptr.Results, 1616 | }) 1617 | // results 1618 | return recv._Uintptr.Results.R0 1619 | } 1620 | --------------------------------------------------------------------------------