Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ all: install test lint clean
generate:
go run ./cmd/minimock/minimock.go -i github.com/gojuno/minimock/v3.Tester -o ./tests
go run ./cmd/minimock/minimock.go -i ./tests.Formatter -o ./tests/formatter_mock.go
go run ./cmd/minimock/minimock.go -i ./tests.genericInout -o ./tests/generic/generic_inout.go
go run ./cmd/minimock/minimock.go -i ./tests.genericOut -o ./tests/generic/generic_out.go
go run ./cmd/minimock/minimock.go -i ./tests.genericIn -o ./tests/generic/generic_in.go

./bin:
mkdir ./bin
Expand All @@ -30,15 +33,24 @@ install:
clean:
[ -e ./tests/formatter_mock.go.test_origin ] && mv -f ./tests/formatter_mock.go.test_origin ./tests/formatter_mock.go
[ -e ./tests/tester_mock_test.go.test_origin ] && mv -f ./tests/tester_mock_test.go.test_origin ./tests/tester_mock_test.go
[ -e ./tests/generic/generic_inout.go.test_origin ] && mv -f ./tests/generic/generic_inout.go.test_origin ./tests/generic/generic_inout.go
[ -e ./tests/generic/generic_out.go.test_origin ] && mv -f ./tests/generic/generic_out.go.test_origin ./tests/generic/generic_out.go
[ -e ./tests/generic/generic_in.go.test_origin ]&& mv -f ./tests/generic/generic_in.go.test_origin ./tests/generic/generic_in.go
rm -Rf bin/ dist/

test_save_origin:
[ -e ./tests/formatter_mock.go.test_origin ] || cp ./tests/formatter_mock.go ./tests/formatter_mock.go.test_origin
[ -e ./tests/tester_mock_test.go.test_origin ] || cp ./tests/tester_mock_test.go ./tests/tester_mock_test.go.test_origin
[ -e ./tests/generic/generic_inout.go.test_origin ] || cp ./tests/generic/generic_inout.go ./tests/generic/generic_inout.go.test_origin
[ -e ./tests/generic/generic_out.go.test_origin ] || cp ./tests/generic/generic_out.go ./tests/generic/generic_out.go.test_origin
[ -e ./tests/generic/generic_in.go.test_origin ]|| cp ./tests/generic/generic_in.go ./tests/generic/generic_in.go.test_origin

test: test_save_origin generate
diff ./tests/formatter_mock.go ./tests/formatter_mock.go.test_origin
diff ./tests/tester_mock_test.go ./tests/tester_mock_test.go.test_origin
diff ./tests/generic/generic_inout.go ./tests/generic/generic_inout.go.test_origin
diff ./tests/generic/generic_out.go ./tests/generic/generic_out.go.test_origin
diff ./tests/generic/generic_in.go ./tests/generic/generic_in.go.test_origin
go test -race ./...

release: ./bin/goreleaser
Expand Down
103 changes: 92 additions & 11 deletions cmd/minimock/minimock.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func run(opts *options) (err error) {
"GenerateInstruction": !opts.noGenerate,
"Version": version,
},
Vars: map[string]interface{}{},
Vars: map[string]interface{}{},
Funcs: helpers,
}

Expand All @@ -125,20 +125,97 @@ func run(opts *options) (err error) {
return nil
}

func processPackage(opts generator.Options, interfaces []string, writeTo, suffix, mockName string) (err error) {
for _, name := range interfaces {
opts.InterfaceName = name
func getTypeParams(typeSpec *ast.TypeSpec) []interfaceSpecificationParam {
params := []interfaceSpecificationParam{}

opts.OutputFile, err = destinationFile(name, writeTo, suffix)
// Check whether node has any type params at all
if typeSpec == nil || typeSpec.TypeParams == nil {
return nil
}

// If node has any type params - store them in slice and return as a spec
for _, param := range typeSpec.TypeParams.List {
names := []string{}
for _, name := range param.Names {
names = append(names, name.Name)
}

paramType := ""

if ident, ok := param.Type.(*ast.Ident); ok {
paramType = ident.Name
}

params = append(params, interfaceSpecificationParam{
paramNames: names,
paramType: paramType,
})
}

return params
}

// interfaceSpecification represents abstraction over interface type. It contains all the metadata
// required to render a mock for given interface. One could deduce whether interface is generic
// by looking for type params
type interfaceSpecification struct {
interfaceName string
interfaceParams []interfaceSpecificationParam
}

// interfaceSpecificationParam represents a group of type param variables and their type
// I.e. [T,K any] would result in names "T","K" and type "any"
type interfaceSpecificationParam struct {
paramNames []string
paramType string
}

func processPackage(opts generator.Options, interfaces []interfaceSpecification, writeTo, suffix, mockName string) (err error) {
for _, iface := range interfaces {
opts.InterfaceName = iface.interfaceName

params := ""
paramsReferences := ""

for _, param := range iface.interfaceParams {
names := strings.Join(param.paramNames, ",")
params += fmt.Sprintf("%s %s", names, param.paramType)
if paramsReferences == "" {
paramsReferences = names
} else {
paramsReferences = strings.Join([]string{paramsReferences, names}, ",")
}
}

opts.OutputFile, err = destinationFile(iface.interfaceName, writeTo, suffix)
if err != nil {
return errors.Wrapf(err, "failed to generate mock for %s", name)
return errors.Wrapf(err, "failed to generate mock for %s", iface.interfaceName)
}

opts.Vars["MockName"] = fmt.Sprintf("%sMock", opts.InterfaceName)
if mockName != "" {
opts.Vars["MockName"] = mockName
}

// Due to limitations of the generator, type params render is done by additional functions
// params generates tokens for type param declarations, i.e. for declaring a generic function
opts.Funcs["params"] = func() string {
if params != "" {
return "[" + params + "]"
}
return ""
}

// Due to limitations of the generator, type params render is done by additional functions
// paramsRef generates cases when only a reference is needed, i.e. for instantiation
opts.Funcs["paramsRef"] = func() string {
if paramsReferences != "" {
return "[" + paramsReferences + "]"
}

return ""
}

if err := generate(opts); err != nil {
return err
}
Expand Down Expand Up @@ -207,27 +284,31 @@ func generate(o generator.Options) (err error) {
return ioutil.WriteFile(o.OutputFile, buf.Bytes(), 0644)
}

func findInterfaces(p *ast.Package, pattern string) ([]string, error) {
var names []string
func findInterfaces(p *ast.Package, pattern string) ([]interfaceSpecification, error) {
var interfaceSpecifications []interfaceSpecification

for _, f := range p.Files {
for _, d := range f.Decls {
if gd, ok := d.(*ast.GenDecl); ok && gd.Tok == token.TYPE {
for _, spec := range gd.Specs {
if ts, ok := spec.(*ast.TypeSpec); ok {
if _, ok := ts.Type.(*ast.InterfaceType); ok && match(ts.Name.Name, pattern) {
names = append(names, ts.Name.Name)
interfaceSpecifications = append(interfaceSpecifications, interfaceSpecification{
interfaceName: ts.Name.Name,
interfaceParams: getTypeParams(ts),
})
}
}
}
}
}
}

if len(names) == 0 {
if len(interfaceSpecifications) == 0 {
return nil, errors.Errorf("failed to find any interfaces matching %s in %s", pattern, p.Name)
}

return names, nil
return interfaceSpecifications, nil
}

func match(s, pattern string) bool {
Expand Down
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ module github.com/gojuno/minimock/v3
require (
github.com/davecgh/go-spew v1.1.1
github.com/hexdigest/gowrap v1.1.8
github.com/kr/text v0.2.0 // indirect
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0
github.com/stretchr/testify v1.7.0
golang.org/x/tools v0.1.3
)

require (
github.com/kr/text v0.2.0 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

go 1.13
go 1.18
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/twitchtv/twirp v5.8.0+incompatible/go.mod h1:RRJoFSAmTEh2weEqWtpPE3vFK5YBhA6bqp2l1kfCC5A=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand All @@ -79,26 +78,19 @@ golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
Expand Down
Loading