diff --git a/Makefile b/Makefile index 8998be8..b58a188 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,10 @@ generate: 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 + go run ./cmd/minimock/minimock.go -i ./tests.genericSpecific -o ./tests/generic/generic_specific.go + go run ./cmd/minimock/minimock.go -i ./tests.genericSimpleUnion -o ./tests/generic/generic_simple_union.go + go run ./cmd/minimock/minimock.go -i ./tests.genericComplexUnion -o ./tests/generic/generic_complex_union.go + go run ./cmd/minimock/minimock.go -i ./tests.genericInlineUnion -o ./tests/generic/generic_inline_union.go ./bin: mkdir ./bin diff --git a/cmd/minimock/minimock.go b/cmd/minimock/minimock.go index 08e5ddc..6bd45b3 100644 --- a/cmd/minimock/minimock.go +++ b/cmd/minimock/minimock.go @@ -15,6 +15,7 @@ import ( "time" minimock "github.com/gojuno/minimock/v3" + "github.com/gojuno/minimock/v3/internal/types" "github.com/hexdigest/gowrap/generator" "github.com/hexdigest/gowrap/pkg" "github.com/pkg/errors" @@ -95,10 +96,7 @@ func run(opts *options) (err error) { } } - interfaces, err := findInterfaces(astPackage, in.Type) - if err != nil { - return err - } + interfaces := types.FindAllInterfaces(astPackage, in.Type) gopts := generator.Options{ SourcePackage: sourcePackage.PkgPath, @@ -125,61 +123,16 @@ func run(opts *options) (err error) { return nil } -func getTypeParams(typeSpec *ast.TypeSpec) []interfaceSpecificationParam { - params := []interfaceSpecificationParam{} - - // 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) { +func processPackage(opts generator.Options, interfaces []types.InterfaceSpecification, writeTo, suffix, mockName string) (err error) { for _, iface := range interfaces { - opts.InterfaceName = iface.interfaceName + opts.InterfaceName = iface.InterfaceName params := "" paramsReferences := "" - for _, param := range iface.interfaceParams { - names := strings.Join(param.paramNames, ",") - params += fmt.Sprintf("%s %s", names, param.paramType) + for _, param := range iface.InterfaceParams { + names := strings.Join(param.ParamNames, ",") + params += fmt.Sprintf("%s %s", names, param.ParamType) if paramsReferences == "" { paramsReferences = names } else { @@ -187,9 +140,9 @@ func processPackage(opts generator.Options, interfaces []interfaceSpecification, } } - opts.OutputFile, err = destinationFile(iface.interfaceName, writeTo, suffix) + opts.OutputFile, err = destinationFile(iface.InterfaceName, writeTo, suffix) if err != nil { - return errors.Wrapf(err, "failed to generate mock for %s", iface.interfaceName) + return errors.Wrapf(err, "failed to generate mock for %s", iface.InterfaceName) } opts.Vars["MockName"] = fmt.Sprintf("%sMock", opts.InterfaceName) @@ -284,33 +237,6 @@ func generate(o generator.Options) (err error) { return ioutil.WriteFile(o.OutputFile, buf.Bytes(), 0644) } -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) { - interfaceSpecifications = append(interfaceSpecifications, interfaceSpecification{ - interfaceName: ts.Name.Name, - interfaceParams: getTypeParams(ts), - }) - } - } - } - } - } - } - - if len(interfaceSpecifications) == 0 { - return nil, errors.Errorf("failed to find any interfaces matching %s in %s", pattern, p.Name) - } - - return interfaceSpecifications, nil -} - func match(s, pattern string) bool { return pattern == "*" || s == pattern } diff --git a/go.mod b/go.mod index d0e8ce2..6c4ffdd 100644 --- a/go.mod +++ b/go.mod @@ -7,9 +7,11 @@ require ( github.com/pmezard/go-difflib v1.0.0 github.com/stretchr/testify v1.7.0 golang.org/x/tools v0.1.3 + google.golang.org/protobuf v1.30.0 ) require ( + github.com/bradleyjkemp/cupaloy v2.3.0+incompatible // indirect github.com/kr/text v0.2.0 // indirect golang.org/x/mod v0.4.2 // indirect golang.org/x/sys v0.1.0 // indirect diff --git a/go.sum b/go.sum index 59081a4..13f311a 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/bradleyjkemp/cupaloy v2.3.0+incompatible h1:UafIjBvWQmS9i/xRg+CamMrnLTKNzo+bdmT/oH34c2Y= +github.com/bradleyjkemp/cupaloy v2.3.0+incompatible/go.mod h1:Au1Xw1sgaJ5iSFktEhYsS0dbQiS1B0/XMXl+42y9Ilk= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= @@ -22,7 +24,10 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/hexdigest/gowrap v1.1.7/go.mod h1:Z+nBFUDLa01iaNM+/jzoOA1JJ7sm51rnYFauKFUB5fs= github.com/hexdigest/gowrap v1.1.8 h1:xGTnuMvHou3sa+PSHphOCxPJTJyqNRvGl21t/p3eLes= github.com/hexdigest/gowrap v1.1.8/go.mod h1:H/JiFmQMp//tedlV8qt2xBdGzmne6bpbaSuiHmygnMw= @@ -101,6 +106,7 @@ golang.org/x/tools v0.1.3 h1:L69ShwSZEyCsLKoAxDKeMvLDZkumEe8gXUZAjab0tX8= golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= @@ -110,6 +116,9 @@ google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98 google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= +google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= diff --git a/internal/types/interface.go b/internal/types/interface.go new file mode 100644 index 0000000..94fd2ec --- /dev/null +++ b/internal/types/interface.go @@ -0,0 +1,122 @@ +package types + +import ( + "bytes" + "go/ast" + "go/printer" + "go/token" +) + +// 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 FindAllInterfaces(p *ast.Package, pattern string) []InterfaceSpecification { + // Find all declared types in a single package + types := []*ast.TypeSpec{} + for _, file := range p.Files { + types = append(types, findAllTypeSpecsInFile(file)...) + } + + // Filter interfaces from all the declarations + interfaces := []*ast.TypeSpec{} + for _, typeSpec := range types { + if isInterface(typeSpec) { + interfaces = append(interfaces, typeSpec) + } + } + + // Filter interfaces with the given pattern + filteredInterfaces := []*ast.TypeSpec{} + for _, iface := range interfaces { + if match(iface.Name.Name, pattern) { + filteredInterfaces = append(filteredInterfaces, iface) + } + } + + // Transform AST nodes into specifications + interfaceSpecifications := make([]InterfaceSpecification, 0, len(filteredInterfaces)) + for _, iface := range filteredInterfaces { + interfaceSpecifications = append(interfaceSpecifications, InterfaceSpecification{ + InterfaceName: iface.Name.Name, + InterfaceParams: getTypeParams(iface), + }) + } + + return interfaceSpecifications +} + +func isInterface(typeSpec *ast.TypeSpec) bool { + // Check if this type declaration is specifically an interface declaration + _, ok := typeSpec.Type.(*ast.InterfaceType) + return ok +} + +// findAllInterfaceNodesInFile ranges over file's AST nodes and extracts all interfaces inside +// returned *ast.TypeSpecs can be safely interpreted as interface declaration nodes +func findAllTypeSpecsInFile(f *ast.File) []*ast.TypeSpec { + typeSpecs := []*ast.TypeSpec{} + + // Range over all declarations in a single file + for _, declaration := range f.Decls { + // Check if declaration is an import, constant, type or variable declaration. + // If it is, check specifically if it's a TYPE as all interfaces are types + if genericDeclaration, ok := declaration.(*ast.GenDecl); ok && genericDeclaration.Tok == token.TYPE { + // Range over all specifications and find ones that are Type declarations + // This is mostly a precaution + for _, spec := range genericDeclaration.Specs { + // Check directly for a type spec declaration + if typeSpec, ok := spec.(*ast.TypeSpec); ok { + typeSpecs = append(typeSpecs, typeSpec) + } + } + } + } + + return typeSpecs +} + +// match returns true if pattern is a wildcard or directly matches the given name +func match(name, pattern string) bool { + return pattern == "*" || name == pattern +} + +func getTypeParams(typeSpec *ast.TypeSpec) []InterfaceSpecificationParam { + params := []InterfaceSpecificationParam{} + + // 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) + } + + var out bytes.Buffer + printer.Fprint(&out, token.NewFileSet(), param.Type) + + paramType := out.String() + + params = append(params, InterfaceSpecificationParam{ + ParamNames: names, + ParamType: paramType, + }) + } + + return params +} diff --git a/snapshot_test.go b/snapshot_test.go new file mode 100644 index 0000000..6c0fbe8 --- /dev/null +++ b/snapshot_test.go @@ -0,0 +1,139 @@ +package minimock_test + +import ( + "bytes" + "context" + "os" + "os/exec" + "testing" + "time" + + "github.com/bradleyjkemp/cupaloy" +) + +func mustRunMinimockWithParams( + t *testing.T, + interfacePattern string, + outputFile string, +) { + t.Helper() + + var outBuffer bytes.Buffer + + timeoutContext, cancel := context.WithTimeout(context.Background(), time.Second*10) + defer cancel() + + cmd := exec.CommandContext(timeoutContext, "go", "run", "cmd/minimock/minimock.go", "-o", outputFile, "-i", interfacePattern) + cmd.Stdout = &outBuffer + cmd.Stderr = &outBuffer + + t.Log(cmd.String()) + + if err := cmd.Run(); err != nil { + t.Log(outBuffer.String()) + t.Fail() + } +} + +func mustReadFile( + t *testing.T, + filename string, +) []byte { + t.Helper() + + contents, err := os.ReadFile(filename) + if err != nil { + t.Errorf("failed to read the file: %v", err) + t.Fail() + } + + return contents +} + +func TestSnapshot(t *testing.T) { + snapshotter := cupaloy.New( + cupaloy.SnapshotSubdirectory("snapshots"), + cupaloy.SnapshotFileExtension(".go"), + ) + + type testCase struct { + name string + outputFile string + inputInterface string + expectedOutputFile string + } + + testCases := []testCase{ + { + name: "package reference", + outputFile: "./tests", + inputInterface: "github.com/gojuno/minimock/v3.Tester", + expectedOutputFile: "tests/tester_mock_test.go", + }, + { + name: "relative reference", + inputInterface: "./tests.Formatter", + outputFile: "./tests/formatter_mock.go", + expectedOutputFile: "./tests/formatter_mock.go", + }, + { + name: "generics with any used as param and return type", + inputInterface: "./tests.genericInout", + outputFile: "./tests/generic_inout.go", + expectedOutputFile: "./tests/generic_inout.go", + }, + { + name: "generics with any used as return type", + inputInterface: "./tests.genericOut", + outputFile: "./tests/generic_out.go", + expectedOutputFile: "./tests/generic_out.go", + }, + { + name: "generics with any used as param type", + inputInterface: "./tests.genericIn", + outputFile: "./tests/generic_in.go", + expectedOutputFile: "./tests/generic_in.go", + }, + { + name: "generics with specific type used as a generic constraint", + inputInterface: "./tests.genericSpecific", + outputFile: "./tests/generic_specific.go", + expectedOutputFile: "./tests/generic_specific.go", + }, + { + name: "generics with simple union used as a generic constraint", + inputInterface: "./tests.genericSimpleUnion", + outputFile: "./tests/generic_simple_union.go", + expectedOutputFile: "./tests/generic_simple_union.go", + }, + { + name: "generics with complex union used as a generic constraint", + inputInterface: "./tests.genericComplexUnion", + outputFile: "./tests/generic_complex_union.go", + expectedOutputFile: "./tests/generic_complex_union.go", + }, + { + name: "generics with complex inline union used as a generic constraint", + inputInterface: "./tests.genericInlineUnion", + outputFile: "./tests/generic_inline_union.go", + expectedOutputFile: "./tests/generic_inline_union.go", + }, + { + name: "generics with complex inline union with many types", + inputInterface: "./tests.genericInlineUnionWithManyTypes", + outputFile: "./tests/generic_inline_with_many_options.go", + expectedOutputFile: "./tests/generic_inline_with_many_options.go", + }, + } + + for _, testCase := range testCases { + testCase := testCase + + t.Run(testCase.name, func(t *testing.T) { + t.Parallel() + + mustRunMinimockWithParams(t, testCase.inputInterface, testCase.outputFile) + snapshotter.SnapshotT(t, mustReadFile(t, testCase.expectedOutputFile)) + }) + } +} diff --git a/snapshots/TestSnapshot-generics_with_any_used_as_param_and_return_type.go b/snapshots/TestSnapshot-generics_with_any_used_as_param_and_return_type.go new file mode 100644 index 0000000..185c923 --- /dev/null +++ b/snapshots/TestSnapshot-generics_with_any_used_as_param_and_return_type.go @@ -0,0 +1,283 @@ +package tests + +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInout -o ./tests/generic_inout.go -n GenericInoutMock + +import ( + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" +) + +// GenericInoutMock implements genericInout +type GenericInoutMock[T any] struct { + t minimock.Tester + + funcName func(t1 T) (t2 T) + inspectFuncName func(t1 T) + afterNameCounter uint64 + beforeNameCounter uint64 + NameMock mGenericInoutMockName[T] +} + +// NewGenericInoutMock returns a mock for genericInout +func NewGenericInoutMock[T any](t minimock.Tester) *GenericInoutMock[T] { + m := &GenericInoutMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.NameMock = mGenericInoutMockName[T]{mock: m} + m.NameMock.callArgs = []*GenericInoutMockNameParams[T]{} + + return m +} + +type mGenericInoutMockName[T any] struct { + mock *GenericInoutMock[T] + defaultExpectation *GenericInoutMockNameExpectation[T] + expectations []*GenericInoutMockNameExpectation[T] + + callArgs []*GenericInoutMockNameParams[T] + mutex sync.RWMutex +} + +// GenericInoutMockNameExpectation specifies expectation struct of the genericInout.Name +type GenericInoutMockNameExpectation[T any] struct { + mock *GenericInoutMock[T] + params *GenericInoutMockNameParams[T] + results *GenericInoutMockNameResults[T] + Counter uint64 +} + +// GenericInoutMockNameParams contains parameters of the genericInout.Name +type GenericInoutMockNameParams[T any] struct { + t1 T +} + +// GenericInoutMockNameResults contains results of the genericInout.Name +type GenericInoutMockNameResults[T any] struct { + t2 T +} + +// Expect sets up expected params for genericInout.Name +func (mmName *mGenericInoutMockName[T]) Expect(t1 T) *mGenericInoutMockName[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericInoutMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericInoutMockNameExpectation[T]{} + } + + mmName.defaultExpectation.params = &GenericInoutMockNameParams[T]{t1} + for _, e := range mmName.expectations { + if minimock.Equal(e.params, mmName.defaultExpectation.params) { + mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) + } + } + + return mmName +} + +// Inspect accepts an inspector function that has same arguments as the genericInout.Name +func (mmName *mGenericInoutMockName[T]) Inspect(f func(t1 T)) *mGenericInoutMockName[T] { + if mmName.mock.inspectFuncName != nil { + mmName.mock.t.Fatalf("Inspect function is already set for GenericInoutMock.Name") + } + + mmName.mock.inspectFuncName = f + + return mmName +} + +// Return sets up results that will be returned by genericInout.Name +func (mmName *mGenericInoutMockName[T]) Return(t2 T) *GenericInoutMock[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericInoutMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericInoutMockNameExpectation[T]{mock: mmName.mock} + } + mmName.defaultExpectation.results = &GenericInoutMockNameResults[T]{t2} + return mmName.mock +} + +// Set uses given function f to mock the genericInout.Name method +func (mmName *mGenericInoutMockName[T]) Set(f func(t1 T) (t2 T)) *GenericInoutMock[T] { + if mmName.defaultExpectation != nil { + mmName.mock.t.Fatalf("Default expectation is already set for the genericInout.Name method") + } + + if len(mmName.expectations) > 0 { + mmName.mock.t.Fatalf("Some expectations are already set for the genericInout.Name method") + } + + mmName.mock.funcName = f + return mmName.mock +} + +// When sets expectation for the genericInout.Name which will trigger the result defined by the following +// Then helper +func (mmName *mGenericInoutMockName[T]) When(t1 T) *GenericInoutMockNameExpectation[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericInoutMock.Name mock is already set by Set") + } + + expectation := &GenericInoutMockNameExpectation[T]{ + mock: mmName.mock, + params: &GenericInoutMockNameParams[T]{t1}, + } + mmName.expectations = append(mmName.expectations, expectation) + return expectation +} + +// Then sets up genericInout.Name return parameters for the expectation previously defined by the When method +func (e *GenericInoutMockNameExpectation[T]) Then(t2 T) *GenericInoutMock[T] { + e.results = &GenericInoutMockNameResults[T]{t2} + return e.mock +} + +// Name implements genericInout +func (mmName *GenericInoutMock[T]) Name(t1 T) (t2 T) { + mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) + defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + + if mmName.inspectFuncName != nil { + mmName.inspectFuncName(t1) + } + + mm_params := &GenericInoutMockNameParams[T]{t1} + + // Record call args + mmName.NameMock.mutex.Lock() + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.mutex.Unlock() + + for _, e := range mmName.NameMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return e.results.t2 + } + } + + if mmName.NameMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) + mm_want := mmName.NameMock.defaultExpectation.params + mm_got := GenericInoutMockNameParams[T]{t1} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmName.t.Errorf("GenericInoutMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + mm_results := mmName.NameMock.defaultExpectation.results + if mm_results == nil { + mmName.t.Fatal("No results are set for the GenericInoutMock.Name") + } + return (*mm_results).t2 + } + if mmName.funcName != nil { + return mmName.funcName(t1) + } + mmName.t.Fatalf("Unexpected call to GenericInoutMock.Name. %v", t1) + return +} + +// NameAfterCounter returns a count of finished GenericInoutMock.Name invocations +func (mmName *GenericInoutMock[T]) NameAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.afterNameCounter) +} + +// NameBeforeCounter returns a count of GenericInoutMock.Name invocations +func (mmName *GenericInoutMock[T]) NameBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.beforeNameCounter) +} + +// Calls returns a list of arguments used in each call to GenericInoutMock.Name. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmName *mGenericInoutMockName[T]) Calls() []*GenericInoutMockNameParams[T] { + mmName.mutex.RLock() + + argCopy := make([]*GenericInoutMockNameParams[T], len(mmName.callArgs)) + copy(argCopy, mmName.callArgs) + + mmName.mutex.RUnlock() + + return argCopy +} + +// MinimockNameDone returns true if the count of the Name invocations corresponds +// the number of defined expectations +func (m *GenericInoutMock[T]) MinimockNameDone() bool { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + return true +} + +// MinimockNameInspect logs each unmet expectation +func (m *GenericInoutMock[T]) MinimockNameInspect() { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to GenericInoutMock.Name with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation.params == nil { + m.t.Error("Expected call to GenericInoutMock.Name") + } else { + m.t.Errorf("Expected call to GenericInoutMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + m.t.Error("Expected call to GenericInoutMock.Name") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *GenericInoutMock[T]) MinimockFinish() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *GenericInoutMock[T]) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *GenericInoutMock[T]) minimockDone() bool { + done := true + return done && + m.MinimockNameDone() +} + diff --git a/snapshots/TestSnapshot-generics_with_any_used_as_param_type.go b/snapshots/TestSnapshot-generics_with_any_used_as_param_type.go new file mode 100644 index 0000000..7aba884 --- /dev/null +++ b/snapshots/TestSnapshot-generics_with_any_used_as_param_type.go @@ -0,0 +1,255 @@ +package tests + +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericIn -o ./tests/generic_in.go -n GenericInMock + +import ( + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" +) + +// GenericInMock implements genericIn +type GenericInMock[T any] struct { + t minimock.Tester + + funcName func(t1 T) + inspectFuncName func(t1 T) + afterNameCounter uint64 + beforeNameCounter uint64 + NameMock mGenericInMockName[T] +} + +// NewGenericInMock returns a mock for genericIn +func NewGenericInMock[T any](t minimock.Tester) *GenericInMock[T] { + m := &GenericInMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.NameMock = mGenericInMockName[T]{mock: m} + m.NameMock.callArgs = []*GenericInMockNameParams[T]{} + + return m +} + +type mGenericInMockName[T any] struct { + mock *GenericInMock[T] + defaultExpectation *GenericInMockNameExpectation[T] + expectations []*GenericInMockNameExpectation[T] + + callArgs []*GenericInMockNameParams[T] + mutex sync.RWMutex +} + +// GenericInMockNameExpectation specifies expectation struct of the genericIn.Name +type GenericInMockNameExpectation[T any] struct { + mock *GenericInMock[T] + params *GenericInMockNameParams[T] + + Counter uint64 +} + +// GenericInMockNameParams contains parameters of the genericIn.Name +type GenericInMockNameParams[T any] struct { + t1 T +} + +// Expect sets up expected params for genericIn.Name +func (mmName *mGenericInMockName[T]) Expect(t1 T) *mGenericInMockName[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericInMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericInMockNameExpectation[T]{} + } + + mmName.defaultExpectation.params = &GenericInMockNameParams[T]{t1} + for _, e := range mmName.expectations { + if minimock.Equal(e.params, mmName.defaultExpectation.params) { + mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) + } + } + + return mmName +} + +// Inspect accepts an inspector function that has same arguments as the genericIn.Name +func (mmName *mGenericInMockName[T]) Inspect(f func(t1 T)) *mGenericInMockName[T] { + if mmName.mock.inspectFuncName != nil { + mmName.mock.t.Fatalf("Inspect function is already set for GenericInMock.Name") + } + + mmName.mock.inspectFuncName = f + + return mmName +} + +// Return sets up results that will be returned by genericIn.Name +func (mmName *mGenericInMockName[T]) Return() *GenericInMock[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericInMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericInMockNameExpectation[T]{mock: mmName.mock} + } + + return mmName.mock +} + +// Set uses given function f to mock the genericIn.Name method +func (mmName *mGenericInMockName[T]) Set(f func(t1 T)) *GenericInMock[T] { + if mmName.defaultExpectation != nil { + mmName.mock.t.Fatalf("Default expectation is already set for the genericIn.Name method") + } + + if len(mmName.expectations) > 0 { + mmName.mock.t.Fatalf("Some expectations are already set for the genericIn.Name method") + } + + mmName.mock.funcName = f + return mmName.mock +} + +// Name implements genericIn +func (mmName *GenericInMock[T]) Name(t1 T) { + mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) + defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + + if mmName.inspectFuncName != nil { + mmName.inspectFuncName(t1) + } + + mm_params := &GenericInMockNameParams[T]{t1} + + // Record call args + mmName.NameMock.mutex.Lock() + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.mutex.Unlock() + + for _, e := range mmName.NameMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmName.NameMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) + mm_want := mmName.NameMock.defaultExpectation.params + mm_got := GenericInMockNameParams[T]{t1} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmName.t.Errorf("GenericInMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmName.funcName != nil { + mmName.funcName(t1) + return + } + mmName.t.Fatalf("Unexpected call to GenericInMock.Name. %v", t1) + +} + +// NameAfterCounter returns a count of finished GenericInMock.Name invocations +func (mmName *GenericInMock[T]) NameAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.afterNameCounter) +} + +// NameBeforeCounter returns a count of GenericInMock.Name invocations +func (mmName *GenericInMock[T]) NameBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.beforeNameCounter) +} + +// Calls returns a list of arguments used in each call to GenericInMock.Name. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmName *mGenericInMockName[T]) Calls() []*GenericInMockNameParams[T] { + mmName.mutex.RLock() + + argCopy := make([]*GenericInMockNameParams[T], len(mmName.callArgs)) + copy(argCopy, mmName.callArgs) + + mmName.mutex.RUnlock() + + return argCopy +} + +// MinimockNameDone returns true if the count of the Name invocations corresponds +// the number of defined expectations +func (m *GenericInMock[T]) MinimockNameDone() bool { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + return true +} + +// MinimockNameInspect logs each unmet expectation +func (m *GenericInMock[T]) MinimockNameInspect() { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to GenericInMock.Name with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation.params == nil { + m.t.Error("Expected call to GenericInMock.Name") + } else { + m.t.Errorf("Expected call to GenericInMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + m.t.Error("Expected call to GenericInMock.Name") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *GenericInMock[T]) MinimockFinish() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *GenericInMock[T]) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *GenericInMock[T]) minimockDone() bool { + done := true + return done && + m.MinimockNameDone() +} + diff --git a/snapshots/TestSnapshot-generics_with_any_used_as_return_type.go b/snapshots/TestSnapshot-generics_with_any_used_as_return_type.go new file mode 100644 index 0000000..0b46107 --- /dev/null +++ b/snapshots/TestSnapshot-generics_with_any_used_as_return_type.go @@ -0,0 +1,209 @@ +package tests + +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericOut -o ./tests/generic_out.go -n GenericOutMock + +import ( + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" +) + +// GenericOutMock implements genericOut +type GenericOutMock[T any] struct { + t minimock.Tester + + funcName func() (t1 T) + inspectFuncName func() + afterNameCounter uint64 + beforeNameCounter uint64 + NameMock mGenericOutMockName[T] +} + +// NewGenericOutMock returns a mock for genericOut +func NewGenericOutMock[T any](t minimock.Tester) *GenericOutMock[T] { + m := &GenericOutMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.NameMock = mGenericOutMockName[T]{mock: m} + + return m +} + +type mGenericOutMockName[T any] struct { + mock *GenericOutMock[T] + defaultExpectation *GenericOutMockNameExpectation[T] + expectations []*GenericOutMockNameExpectation[T] +} + +// GenericOutMockNameExpectation specifies expectation struct of the genericOut.Name +type GenericOutMockNameExpectation[T any] struct { + mock *GenericOutMock[T] + + results *GenericOutMockNameResults[T] + Counter uint64 +} + +// GenericOutMockNameResults contains results of the genericOut.Name +type GenericOutMockNameResults[T any] struct { + t1 T +} + +// Expect sets up expected params for genericOut.Name +func (mmName *mGenericOutMockName[T]) Expect() *mGenericOutMockName[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericOutMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericOutMockNameExpectation[T]{} + } + + return mmName +} + +// Inspect accepts an inspector function that has same arguments as the genericOut.Name +func (mmName *mGenericOutMockName[T]) Inspect(f func()) *mGenericOutMockName[T] { + if mmName.mock.inspectFuncName != nil { + mmName.mock.t.Fatalf("Inspect function is already set for GenericOutMock.Name") + } + + mmName.mock.inspectFuncName = f + + return mmName +} + +// Return sets up results that will be returned by genericOut.Name +func (mmName *mGenericOutMockName[T]) Return(t1 T) *GenericOutMock[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericOutMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericOutMockNameExpectation[T]{mock: mmName.mock} + } + mmName.defaultExpectation.results = &GenericOutMockNameResults[T]{t1} + return mmName.mock +} + +// Set uses given function f to mock the genericOut.Name method +func (mmName *mGenericOutMockName[T]) Set(f func() (t1 T)) *GenericOutMock[T] { + if mmName.defaultExpectation != nil { + mmName.mock.t.Fatalf("Default expectation is already set for the genericOut.Name method") + } + + if len(mmName.expectations) > 0 { + mmName.mock.t.Fatalf("Some expectations are already set for the genericOut.Name method") + } + + mmName.mock.funcName = f + return mmName.mock +} + +// Name implements genericOut +func (mmName *GenericOutMock[T]) Name() (t1 T) { + mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) + defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + + if mmName.inspectFuncName != nil { + mmName.inspectFuncName() + } + + if mmName.NameMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) + + mm_results := mmName.NameMock.defaultExpectation.results + if mm_results == nil { + mmName.t.Fatal("No results are set for the GenericOutMock.Name") + } + return (*mm_results).t1 + } + if mmName.funcName != nil { + return mmName.funcName() + } + mmName.t.Fatalf("Unexpected call to GenericOutMock.Name.") + return +} + +// NameAfterCounter returns a count of finished GenericOutMock.Name invocations +func (mmName *GenericOutMock[T]) NameAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.afterNameCounter) +} + +// NameBeforeCounter returns a count of GenericOutMock.Name invocations +func (mmName *GenericOutMock[T]) NameBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.beforeNameCounter) +} + +// MinimockNameDone returns true if the count of the Name invocations corresponds +// the number of defined expectations +func (m *GenericOutMock[T]) MinimockNameDone() bool { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + return true +} + +// MinimockNameInspect logs each unmet expectation +func (m *GenericOutMock[T]) MinimockNameInspect() { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Error("Expected call to GenericOutMock.Name") + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + m.t.Error("Expected call to GenericOutMock.Name") + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + m.t.Error("Expected call to GenericOutMock.Name") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *GenericOutMock[T]) MinimockFinish() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *GenericOutMock[T]) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *GenericOutMock[T]) minimockDone() bool { + done := true + return done && + m.MinimockNameDone() +} + diff --git a/snapshots/TestSnapshot-generics_with_complex_inline_union_used_as_a_generic_constraint.go b/snapshots/TestSnapshot-generics_with_complex_inline_union_used_as_a_generic_constraint.go new file mode 100644 index 0000000..4ce08c8 --- /dev/null +++ b/snapshots/TestSnapshot-generics_with_complex_inline_union_used_as_a_generic_constraint.go @@ -0,0 +1,255 @@ +package tests + +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInlineUnion -o ./tests/generic_inline_union.go -n GenericInlineUnionMock + +import ( + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" +) + +// GenericInlineUnionMock implements genericInlineUnion +type GenericInlineUnionMock[T int | float64] struct { + t minimock.Tester + + funcName func(t1 T) + inspectFuncName func(t1 T) + afterNameCounter uint64 + beforeNameCounter uint64 + NameMock mGenericInlineUnionMockName[T] +} + +// NewGenericInlineUnionMock returns a mock for genericInlineUnion +func NewGenericInlineUnionMock[T int | float64](t minimock.Tester) *GenericInlineUnionMock[T] { + m := &GenericInlineUnionMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.NameMock = mGenericInlineUnionMockName[T]{mock: m} + m.NameMock.callArgs = []*GenericInlineUnionMockNameParams[T]{} + + return m +} + +type mGenericInlineUnionMockName[T int | float64] struct { + mock *GenericInlineUnionMock[T] + defaultExpectation *GenericInlineUnionMockNameExpectation[T] + expectations []*GenericInlineUnionMockNameExpectation[T] + + callArgs []*GenericInlineUnionMockNameParams[T] + mutex sync.RWMutex +} + +// GenericInlineUnionMockNameExpectation specifies expectation struct of the genericInlineUnion.Name +type GenericInlineUnionMockNameExpectation[T int | float64] struct { + mock *GenericInlineUnionMock[T] + params *GenericInlineUnionMockNameParams[T] + + Counter uint64 +} + +// GenericInlineUnionMockNameParams contains parameters of the genericInlineUnion.Name +type GenericInlineUnionMockNameParams[T int | float64] struct { + t1 T +} + +// Expect sets up expected params for genericInlineUnion.Name +func (mmName *mGenericInlineUnionMockName[T]) Expect(t1 T) *mGenericInlineUnionMockName[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericInlineUnionMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericInlineUnionMockNameExpectation[T]{} + } + + mmName.defaultExpectation.params = &GenericInlineUnionMockNameParams[T]{t1} + for _, e := range mmName.expectations { + if minimock.Equal(e.params, mmName.defaultExpectation.params) { + mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) + } + } + + return mmName +} + +// Inspect accepts an inspector function that has same arguments as the genericInlineUnion.Name +func (mmName *mGenericInlineUnionMockName[T]) Inspect(f func(t1 T)) *mGenericInlineUnionMockName[T] { + if mmName.mock.inspectFuncName != nil { + mmName.mock.t.Fatalf("Inspect function is already set for GenericInlineUnionMock.Name") + } + + mmName.mock.inspectFuncName = f + + return mmName +} + +// Return sets up results that will be returned by genericInlineUnion.Name +func (mmName *mGenericInlineUnionMockName[T]) Return() *GenericInlineUnionMock[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericInlineUnionMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericInlineUnionMockNameExpectation[T]{mock: mmName.mock} + } + + return mmName.mock +} + +// Set uses given function f to mock the genericInlineUnion.Name method +func (mmName *mGenericInlineUnionMockName[T]) Set(f func(t1 T)) *GenericInlineUnionMock[T] { + if mmName.defaultExpectation != nil { + mmName.mock.t.Fatalf("Default expectation is already set for the genericInlineUnion.Name method") + } + + if len(mmName.expectations) > 0 { + mmName.mock.t.Fatalf("Some expectations are already set for the genericInlineUnion.Name method") + } + + mmName.mock.funcName = f + return mmName.mock +} + +// Name implements genericInlineUnion +func (mmName *GenericInlineUnionMock[T]) Name(t1 T) { + mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) + defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + + if mmName.inspectFuncName != nil { + mmName.inspectFuncName(t1) + } + + mm_params := &GenericInlineUnionMockNameParams[T]{t1} + + // Record call args + mmName.NameMock.mutex.Lock() + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.mutex.Unlock() + + for _, e := range mmName.NameMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmName.NameMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) + mm_want := mmName.NameMock.defaultExpectation.params + mm_got := GenericInlineUnionMockNameParams[T]{t1} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmName.t.Errorf("GenericInlineUnionMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmName.funcName != nil { + mmName.funcName(t1) + return + } + mmName.t.Fatalf("Unexpected call to GenericInlineUnionMock.Name. %v", t1) + +} + +// NameAfterCounter returns a count of finished GenericInlineUnionMock.Name invocations +func (mmName *GenericInlineUnionMock[T]) NameAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.afterNameCounter) +} + +// NameBeforeCounter returns a count of GenericInlineUnionMock.Name invocations +func (mmName *GenericInlineUnionMock[T]) NameBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.beforeNameCounter) +} + +// Calls returns a list of arguments used in each call to GenericInlineUnionMock.Name. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmName *mGenericInlineUnionMockName[T]) Calls() []*GenericInlineUnionMockNameParams[T] { + mmName.mutex.RLock() + + argCopy := make([]*GenericInlineUnionMockNameParams[T], len(mmName.callArgs)) + copy(argCopy, mmName.callArgs) + + mmName.mutex.RUnlock() + + return argCopy +} + +// MinimockNameDone returns true if the count of the Name invocations corresponds +// the number of defined expectations +func (m *GenericInlineUnionMock[T]) MinimockNameDone() bool { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + return true +} + +// MinimockNameInspect logs each unmet expectation +func (m *GenericInlineUnionMock[T]) MinimockNameInspect() { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to GenericInlineUnionMock.Name with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation.params == nil { + m.t.Error("Expected call to GenericInlineUnionMock.Name") + } else { + m.t.Errorf("Expected call to GenericInlineUnionMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + m.t.Error("Expected call to GenericInlineUnionMock.Name") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *GenericInlineUnionMock[T]) MinimockFinish() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *GenericInlineUnionMock[T]) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *GenericInlineUnionMock[T]) minimockDone() bool { + done := true + return done && + m.MinimockNameDone() +} + diff --git a/snapshots/TestSnapshot-generics_with_complex_inline_union_with_many_types.go b/snapshots/TestSnapshot-generics_with_complex_inline_union_with_many_types.go new file mode 100644 index 0000000..9a7692b --- /dev/null +++ b/snapshots/TestSnapshot-generics_with_complex_inline_union_with_many_types.go @@ -0,0 +1,255 @@ +package tests + +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInlineUnionWithManyTypes -o ./tests/generic_inline_with_many_options.go -n GenericInlineUnionWithManyTypesMock + +import ( + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" +) + +// GenericInlineUnionWithManyTypesMock implements genericInlineUnionWithManyTypes +type GenericInlineUnionWithManyTypesMock[T int | float64 | string] struct { + t minimock.Tester + + funcName func(t1 T) + inspectFuncName func(t1 T) + afterNameCounter uint64 + beforeNameCounter uint64 + NameMock mGenericInlineUnionWithManyTypesMockName[T] +} + +// NewGenericInlineUnionWithManyTypesMock returns a mock for genericInlineUnionWithManyTypes +func NewGenericInlineUnionWithManyTypesMock[T int | float64 | string](t minimock.Tester) *GenericInlineUnionWithManyTypesMock[T] { + m := &GenericInlineUnionWithManyTypesMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.NameMock = mGenericInlineUnionWithManyTypesMockName[T]{mock: m} + m.NameMock.callArgs = []*GenericInlineUnionWithManyTypesMockNameParams[T]{} + + return m +} + +type mGenericInlineUnionWithManyTypesMockName[T int | float64 | string] struct { + mock *GenericInlineUnionWithManyTypesMock[T] + defaultExpectation *GenericInlineUnionWithManyTypesMockNameExpectation[T] + expectations []*GenericInlineUnionWithManyTypesMockNameExpectation[T] + + callArgs []*GenericInlineUnionWithManyTypesMockNameParams[T] + mutex sync.RWMutex +} + +// GenericInlineUnionWithManyTypesMockNameExpectation specifies expectation struct of the genericInlineUnionWithManyTypes.Name +type GenericInlineUnionWithManyTypesMockNameExpectation[T int | float64 | string] struct { + mock *GenericInlineUnionWithManyTypesMock[T] + params *GenericInlineUnionWithManyTypesMockNameParams[T] + + Counter uint64 +} + +// GenericInlineUnionWithManyTypesMockNameParams contains parameters of the genericInlineUnionWithManyTypes.Name +type GenericInlineUnionWithManyTypesMockNameParams[T int | float64 | string] struct { + t1 T +} + +// Expect sets up expected params for genericInlineUnionWithManyTypes.Name +func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Expect(t1 T) *mGenericInlineUnionWithManyTypesMockName[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericInlineUnionWithManyTypesMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericInlineUnionWithManyTypesMockNameExpectation[T]{} + } + + mmName.defaultExpectation.params = &GenericInlineUnionWithManyTypesMockNameParams[T]{t1} + for _, e := range mmName.expectations { + if minimock.Equal(e.params, mmName.defaultExpectation.params) { + mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) + } + } + + return mmName +} + +// Inspect accepts an inspector function that has same arguments as the genericInlineUnionWithManyTypes.Name +func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Inspect(f func(t1 T)) *mGenericInlineUnionWithManyTypesMockName[T] { + if mmName.mock.inspectFuncName != nil { + mmName.mock.t.Fatalf("Inspect function is already set for GenericInlineUnionWithManyTypesMock.Name") + } + + mmName.mock.inspectFuncName = f + + return mmName +} + +// Return sets up results that will be returned by genericInlineUnionWithManyTypes.Name +func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Return() *GenericInlineUnionWithManyTypesMock[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericInlineUnionWithManyTypesMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericInlineUnionWithManyTypesMockNameExpectation[T]{mock: mmName.mock} + } + + return mmName.mock +} + +// Set uses given function f to mock the genericInlineUnionWithManyTypes.Name method +func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Set(f func(t1 T)) *GenericInlineUnionWithManyTypesMock[T] { + if mmName.defaultExpectation != nil { + mmName.mock.t.Fatalf("Default expectation is already set for the genericInlineUnionWithManyTypes.Name method") + } + + if len(mmName.expectations) > 0 { + mmName.mock.t.Fatalf("Some expectations are already set for the genericInlineUnionWithManyTypes.Name method") + } + + mmName.mock.funcName = f + return mmName.mock +} + +// Name implements genericInlineUnionWithManyTypes +func (mmName *GenericInlineUnionWithManyTypesMock[T]) Name(t1 T) { + mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) + defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + + if mmName.inspectFuncName != nil { + mmName.inspectFuncName(t1) + } + + mm_params := &GenericInlineUnionWithManyTypesMockNameParams[T]{t1} + + // Record call args + mmName.NameMock.mutex.Lock() + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.mutex.Unlock() + + for _, e := range mmName.NameMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmName.NameMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) + mm_want := mmName.NameMock.defaultExpectation.params + mm_got := GenericInlineUnionWithManyTypesMockNameParams[T]{t1} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmName.t.Errorf("GenericInlineUnionWithManyTypesMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmName.funcName != nil { + mmName.funcName(t1) + return + } + mmName.t.Fatalf("Unexpected call to GenericInlineUnionWithManyTypesMock.Name. %v", t1) + +} + +// NameAfterCounter returns a count of finished GenericInlineUnionWithManyTypesMock.Name invocations +func (mmName *GenericInlineUnionWithManyTypesMock[T]) NameAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.afterNameCounter) +} + +// NameBeforeCounter returns a count of GenericInlineUnionWithManyTypesMock.Name invocations +func (mmName *GenericInlineUnionWithManyTypesMock[T]) NameBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.beforeNameCounter) +} + +// Calls returns a list of arguments used in each call to GenericInlineUnionWithManyTypesMock.Name. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Calls() []*GenericInlineUnionWithManyTypesMockNameParams[T] { + mmName.mutex.RLock() + + argCopy := make([]*GenericInlineUnionWithManyTypesMockNameParams[T], len(mmName.callArgs)) + copy(argCopy, mmName.callArgs) + + mmName.mutex.RUnlock() + + return argCopy +} + +// MinimockNameDone returns true if the count of the Name invocations corresponds +// the number of defined expectations +func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockNameDone() bool { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + return true +} + +// MinimockNameInspect logs each unmet expectation +func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockNameInspect() { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to GenericInlineUnionWithManyTypesMock.Name with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation.params == nil { + m.t.Error("Expected call to GenericInlineUnionWithManyTypesMock.Name") + } else { + m.t.Errorf("Expected call to GenericInlineUnionWithManyTypesMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + m.t.Error("Expected call to GenericInlineUnionWithManyTypesMock.Name") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockFinish() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *GenericInlineUnionWithManyTypesMock[T]) minimockDone() bool { + done := true + return done && + m.MinimockNameDone() +} + diff --git a/snapshots/TestSnapshot-generics_with_complex_union_used_as_a_generic_constraint.go b/snapshots/TestSnapshot-generics_with_complex_union_used_as_a_generic_constraint.go new file mode 100644 index 0000000..67277e1 --- /dev/null +++ b/snapshots/TestSnapshot-generics_with_complex_union_used_as_a_generic_constraint.go @@ -0,0 +1,255 @@ +package tests + +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericComplexUnion -o ./tests/generic_complex_union.go -n GenericComplexUnionMock + +import ( + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" +) + +// GenericComplexUnionMock implements genericComplexUnion +type GenericComplexUnionMock[T complexUnion] struct { + t minimock.Tester + + funcName func(t1 T) + inspectFuncName func(t1 T) + afterNameCounter uint64 + beforeNameCounter uint64 + NameMock mGenericComplexUnionMockName[T] +} + +// NewGenericComplexUnionMock returns a mock for genericComplexUnion +func NewGenericComplexUnionMock[T complexUnion](t minimock.Tester) *GenericComplexUnionMock[T] { + m := &GenericComplexUnionMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.NameMock = mGenericComplexUnionMockName[T]{mock: m} + m.NameMock.callArgs = []*GenericComplexUnionMockNameParams[T]{} + + return m +} + +type mGenericComplexUnionMockName[T complexUnion] struct { + mock *GenericComplexUnionMock[T] + defaultExpectation *GenericComplexUnionMockNameExpectation[T] + expectations []*GenericComplexUnionMockNameExpectation[T] + + callArgs []*GenericComplexUnionMockNameParams[T] + mutex sync.RWMutex +} + +// GenericComplexUnionMockNameExpectation specifies expectation struct of the genericComplexUnion.Name +type GenericComplexUnionMockNameExpectation[T complexUnion] struct { + mock *GenericComplexUnionMock[T] + params *GenericComplexUnionMockNameParams[T] + + Counter uint64 +} + +// GenericComplexUnionMockNameParams contains parameters of the genericComplexUnion.Name +type GenericComplexUnionMockNameParams[T complexUnion] struct { + t1 T +} + +// Expect sets up expected params for genericComplexUnion.Name +func (mmName *mGenericComplexUnionMockName[T]) Expect(t1 T) *mGenericComplexUnionMockName[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericComplexUnionMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericComplexUnionMockNameExpectation[T]{} + } + + mmName.defaultExpectation.params = &GenericComplexUnionMockNameParams[T]{t1} + for _, e := range mmName.expectations { + if minimock.Equal(e.params, mmName.defaultExpectation.params) { + mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) + } + } + + return mmName +} + +// Inspect accepts an inspector function that has same arguments as the genericComplexUnion.Name +func (mmName *mGenericComplexUnionMockName[T]) Inspect(f func(t1 T)) *mGenericComplexUnionMockName[T] { + if mmName.mock.inspectFuncName != nil { + mmName.mock.t.Fatalf("Inspect function is already set for GenericComplexUnionMock.Name") + } + + mmName.mock.inspectFuncName = f + + return mmName +} + +// Return sets up results that will be returned by genericComplexUnion.Name +func (mmName *mGenericComplexUnionMockName[T]) Return() *GenericComplexUnionMock[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericComplexUnionMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericComplexUnionMockNameExpectation[T]{mock: mmName.mock} + } + + return mmName.mock +} + +// Set uses given function f to mock the genericComplexUnion.Name method +func (mmName *mGenericComplexUnionMockName[T]) Set(f func(t1 T)) *GenericComplexUnionMock[T] { + if mmName.defaultExpectation != nil { + mmName.mock.t.Fatalf("Default expectation is already set for the genericComplexUnion.Name method") + } + + if len(mmName.expectations) > 0 { + mmName.mock.t.Fatalf("Some expectations are already set for the genericComplexUnion.Name method") + } + + mmName.mock.funcName = f + return mmName.mock +} + +// Name implements genericComplexUnion +func (mmName *GenericComplexUnionMock[T]) Name(t1 T) { + mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) + defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + + if mmName.inspectFuncName != nil { + mmName.inspectFuncName(t1) + } + + mm_params := &GenericComplexUnionMockNameParams[T]{t1} + + // Record call args + mmName.NameMock.mutex.Lock() + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.mutex.Unlock() + + for _, e := range mmName.NameMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmName.NameMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) + mm_want := mmName.NameMock.defaultExpectation.params + mm_got := GenericComplexUnionMockNameParams[T]{t1} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmName.t.Errorf("GenericComplexUnionMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmName.funcName != nil { + mmName.funcName(t1) + return + } + mmName.t.Fatalf("Unexpected call to GenericComplexUnionMock.Name. %v", t1) + +} + +// NameAfterCounter returns a count of finished GenericComplexUnionMock.Name invocations +func (mmName *GenericComplexUnionMock[T]) NameAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.afterNameCounter) +} + +// NameBeforeCounter returns a count of GenericComplexUnionMock.Name invocations +func (mmName *GenericComplexUnionMock[T]) NameBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.beforeNameCounter) +} + +// Calls returns a list of arguments used in each call to GenericComplexUnionMock.Name. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmName *mGenericComplexUnionMockName[T]) Calls() []*GenericComplexUnionMockNameParams[T] { + mmName.mutex.RLock() + + argCopy := make([]*GenericComplexUnionMockNameParams[T], len(mmName.callArgs)) + copy(argCopy, mmName.callArgs) + + mmName.mutex.RUnlock() + + return argCopy +} + +// MinimockNameDone returns true if the count of the Name invocations corresponds +// the number of defined expectations +func (m *GenericComplexUnionMock[T]) MinimockNameDone() bool { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + return true +} + +// MinimockNameInspect logs each unmet expectation +func (m *GenericComplexUnionMock[T]) MinimockNameInspect() { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to GenericComplexUnionMock.Name with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation.params == nil { + m.t.Error("Expected call to GenericComplexUnionMock.Name") + } else { + m.t.Errorf("Expected call to GenericComplexUnionMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + m.t.Error("Expected call to GenericComplexUnionMock.Name") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *GenericComplexUnionMock[T]) MinimockFinish() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *GenericComplexUnionMock[T]) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *GenericComplexUnionMock[T]) minimockDone() bool { + done := true + return done && + m.MinimockNameDone() +} + diff --git a/snapshots/TestSnapshot-generics_with_simple_union_used_as_a_generic_constraint.go b/snapshots/TestSnapshot-generics_with_simple_union_used_as_a_generic_constraint.go new file mode 100644 index 0000000..9a9832f --- /dev/null +++ b/snapshots/TestSnapshot-generics_with_simple_union_used_as_a_generic_constraint.go @@ -0,0 +1,255 @@ +package tests + +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericSimpleUnion -o ./tests/generic_simple_union.go -n GenericSimpleUnionMock + +import ( + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" +) + +// GenericSimpleUnionMock implements genericSimpleUnion +type GenericSimpleUnionMock[T simpleUnion] struct { + t minimock.Tester + + funcName func(t1 T) + inspectFuncName func(t1 T) + afterNameCounter uint64 + beforeNameCounter uint64 + NameMock mGenericSimpleUnionMockName[T] +} + +// NewGenericSimpleUnionMock returns a mock for genericSimpleUnion +func NewGenericSimpleUnionMock[T simpleUnion](t minimock.Tester) *GenericSimpleUnionMock[T] { + m := &GenericSimpleUnionMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.NameMock = mGenericSimpleUnionMockName[T]{mock: m} + m.NameMock.callArgs = []*GenericSimpleUnionMockNameParams[T]{} + + return m +} + +type mGenericSimpleUnionMockName[T simpleUnion] struct { + mock *GenericSimpleUnionMock[T] + defaultExpectation *GenericSimpleUnionMockNameExpectation[T] + expectations []*GenericSimpleUnionMockNameExpectation[T] + + callArgs []*GenericSimpleUnionMockNameParams[T] + mutex sync.RWMutex +} + +// GenericSimpleUnionMockNameExpectation specifies expectation struct of the genericSimpleUnion.Name +type GenericSimpleUnionMockNameExpectation[T simpleUnion] struct { + mock *GenericSimpleUnionMock[T] + params *GenericSimpleUnionMockNameParams[T] + + Counter uint64 +} + +// GenericSimpleUnionMockNameParams contains parameters of the genericSimpleUnion.Name +type GenericSimpleUnionMockNameParams[T simpleUnion] struct { + t1 T +} + +// Expect sets up expected params for genericSimpleUnion.Name +func (mmName *mGenericSimpleUnionMockName[T]) Expect(t1 T) *mGenericSimpleUnionMockName[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericSimpleUnionMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericSimpleUnionMockNameExpectation[T]{} + } + + mmName.defaultExpectation.params = &GenericSimpleUnionMockNameParams[T]{t1} + for _, e := range mmName.expectations { + if minimock.Equal(e.params, mmName.defaultExpectation.params) { + mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) + } + } + + return mmName +} + +// Inspect accepts an inspector function that has same arguments as the genericSimpleUnion.Name +func (mmName *mGenericSimpleUnionMockName[T]) Inspect(f func(t1 T)) *mGenericSimpleUnionMockName[T] { + if mmName.mock.inspectFuncName != nil { + mmName.mock.t.Fatalf("Inspect function is already set for GenericSimpleUnionMock.Name") + } + + mmName.mock.inspectFuncName = f + + return mmName +} + +// Return sets up results that will be returned by genericSimpleUnion.Name +func (mmName *mGenericSimpleUnionMockName[T]) Return() *GenericSimpleUnionMock[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericSimpleUnionMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericSimpleUnionMockNameExpectation[T]{mock: mmName.mock} + } + + return mmName.mock +} + +// Set uses given function f to mock the genericSimpleUnion.Name method +func (mmName *mGenericSimpleUnionMockName[T]) Set(f func(t1 T)) *GenericSimpleUnionMock[T] { + if mmName.defaultExpectation != nil { + mmName.mock.t.Fatalf("Default expectation is already set for the genericSimpleUnion.Name method") + } + + if len(mmName.expectations) > 0 { + mmName.mock.t.Fatalf("Some expectations are already set for the genericSimpleUnion.Name method") + } + + mmName.mock.funcName = f + return mmName.mock +} + +// Name implements genericSimpleUnion +func (mmName *GenericSimpleUnionMock[T]) Name(t1 T) { + mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) + defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + + if mmName.inspectFuncName != nil { + mmName.inspectFuncName(t1) + } + + mm_params := &GenericSimpleUnionMockNameParams[T]{t1} + + // Record call args + mmName.NameMock.mutex.Lock() + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.mutex.Unlock() + + for _, e := range mmName.NameMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmName.NameMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) + mm_want := mmName.NameMock.defaultExpectation.params + mm_got := GenericSimpleUnionMockNameParams[T]{t1} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmName.t.Errorf("GenericSimpleUnionMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmName.funcName != nil { + mmName.funcName(t1) + return + } + mmName.t.Fatalf("Unexpected call to GenericSimpleUnionMock.Name. %v", t1) + +} + +// NameAfterCounter returns a count of finished GenericSimpleUnionMock.Name invocations +func (mmName *GenericSimpleUnionMock[T]) NameAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.afterNameCounter) +} + +// NameBeforeCounter returns a count of GenericSimpleUnionMock.Name invocations +func (mmName *GenericSimpleUnionMock[T]) NameBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.beforeNameCounter) +} + +// Calls returns a list of arguments used in each call to GenericSimpleUnionMock.Name. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmName *mGenericSimpleUnionMockName[T]) Calls() []*GenericSimpleUnionMockNameParams[T] { + mmName.mutex.RLock() + + argCopy := make([]*GenericSimpleUnionMockNameParams[T], len(mmName.callArgs)) + copy(argCopy, mmName.callArgs) + + mmName.mutex.RUnlock() + + return argCopy +} + +// MinimockNameDone returns true if the count of the Name invocations corresponds +// the number of defined expectations +func (m *GenericSimpleUnionMock[T]) MinimockNameDone() bool { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + return true +} + +// MinimockNameInspect logs each unmet expectation +func (m *GenericSimpleUnionMock[T]) MinimockNameInspect() { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to GenericSimpleUnionMock.Name with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation.params == nil { + m.t.Error("Expected call to GenericSimpleUnionMock.Name") + } else { + m.t.Errorf("Expected call to GenericSimpleUnionMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + m.t.Error("Expected call to GenericSimpleUnionMock.Name") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *GenericSimpleUnionMock[T]) MinimockFinish() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *GenericSimpleUnionMock[T]) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *GenericSimpleUnionMock[T]) minimockDone() bool { + done := true + return done && + m.MinimockNameDone() +} + diff --git a/snapshots/TestSnapshot-generics_with_specific_type_used_as_a_generic_constraint.go b/snapshots/TestSnapshot-generics_with_specific_type_used_as_a_generic_constraint.go new file mode 100644 index 0000000..f8a7ef6 --- /dev/null +++ b/snapshots/TestSnapshot-generics_with_specific_type_used_as_a_generic_constraint.go @@ -0,0 +1,256 @@ +package tests + +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericSpecific -o ./tests/generic_specific.go -n GenericSpecificMock + +import ( + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" + "google.golang.org/protobuf/proto" +) + +// GenericSpecificMock implements genericSpecific +type GenericSpecificMock[T proto.Message] struct { + t minimock.Tester + + funcName func(t1 T) + inspectFuncName func(t1 T) + afterNameCounter uint64 + beforeNameCounter uint64 + NameMock mGenericSpecificMockName[T] +} + +// NewGenericSpecificMock returns a mock for genericSpecific +func NewGenericSpecificMock[T proto.Message](t minimock.Tester) *GenericSpecificMock[T] { + m := &GenericSpecificMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.NameMock = mGenericSpecificMockName[T]{mock: m} + m.NameMock.callArgs = []*GenericSpecificMockNameParams[T]{} + + return m +} + +type mGenericSpecificMockName[T proto.Message] struct { + mock *GenericSpecificMock[T] + defaultExpectation *GenericSpecificMockNameExpectation[T] + expectations []*GenericSpecificMockNameExpectation[T] + + callArgs []*GenericSpecificMockNameParams[T] + mutex sync.RWMutex +} + +// GenericSpecificMockNameExpectation specifies expectation struct of the genericSpecific.Name +type GenericSpecificMockNameExpectation[T proto.Message] struct { + mock *GenericSpecificMock[T] + params *GenericSpecificMockNameParams[T] + + Counter uint64 +} + +// GenericSpecificMockNameParams contains parameters of the genericSpecific.Name +type GenericSpecificMockNameParams[T proto.Message] struct { + t1 T +} + +// Expect sets up expected params for genericSpecific.Name +func (mmName *mGenericSpecificMockName[T]) Expect(t1 T) *mGenericSpecificMockName[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericSpecificMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericSpecificMockNameExpectation[T]{} + } + + mmName.defaultExpectation.params = &GenericSpecificMockNameParams[T]{t1} + for _, e := range mmName.expectations { + if minimock.Equal(e.params, mmName.defaultExpectation.params) { + mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) + } + } + + return mmName +} + +// Inspect accepts an inspector function that has same arguments as the genericSpecific.Name +func (mmName *mGenericSpecificMockName[T]) Inspect(f func(t1 T)) *mGenericSpecificMockName[T] { + if mmName.mock.inspectFuncName != nil { + mmName.mock.t.Fatalf("Inspect function is already set for GenericSpecificMock.Name") + } + + mmName.mock.inspectFuncName = f + + return mmName +} + +// Return sets up results that will be returned by genericSpecific.Name +func (mmName *mGenericSpecificMockName[T]) Return() *GenericSpecificMock[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericSpecificMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericSpecificMockNameExpectation[T]{mock: mmName.mock} + } + + return mmName.mock +} + +// Set uses given function f to mock the genericSpecific.Name method +func (mmName *mGenericSpecificMockName[T]) Set(f func(t1 T)) *GenericSpecificMock[T] { + if mmName.defaultExpectation != nil { + mmName.mock.t.Fatalf("Default expectation is already set for the genericSpecific.Name method") + } + + if len(mmName.expectations) > 0 { + mmName.mock.t.Fatalf("Some expectations are already set for the genericSpecific.Name method") + } + + mmName.mock.funcName = f + return mmName.mock +} + +// Name implements genericSpecific +func (mmName *GenericSpecificMock[T]) Name(t1 T) { + mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) + defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + + if mmName.inspectFuncName != nil { + mmName.inspectFuncName(t1) + } + + mm_params := &GenericSpecificMockNameParams[T]{t1} + + // Record call args + mmName.NameMock.mutex.Lock() + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.mutex.Unlock() + + for _, e := range mmName.NameMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmName.NameMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) + mm_want := mmName.NameMock.defaultExpectation.params + mm_got := GenericSpecificMockNameParams[T]{t1} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmName.t.Errorf("GenericSpecificMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmName.funcName != nil { + mmName.funcName(t1) + return + } + mmName.t.Fatalf("Unexpected call to GenericSpecificMock.Name. %v", t1) + +} + +// NameAfterCounter returns a count of finished GenericSpecificMock.Name invocations +func (mmName *GenericSpecificMock[T]) NameAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.afterNameCounter) +} + +// NameBeforeCounter returns a count of GenericSpecificMock.Name invocations +func (mmName *GenericSpecificMock[T]) NameBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.beforeNameCounter) +} + +// Calls returns a list of arguments used in each call to GenericSpecificMock.Name. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmName *mGenericSpecificMockName[T]) Calls() []*GenericSpecificMockNameParams[T] { + mmName.mutex.RLock() + + argCopy := make([]*GenericSpecificMockNameParams[T], len(mmName.callArgs)) + copy(argCopy, mmName.callArgs) + + mmName.mutex.RUnlock() + + return argCopy +} + +// MinimockNameDone returns true if the count of the Name invocations corresponds +// the number of defined expectations +func (m *GenericSpecificMock[T]) MinimockNameDone() bool { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + return true +} + +// MinimockNameInspect logs each unmet expectation +func (m *GenericSpecificMock[T]) MinimockNameInspect() { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to GenericSpecificMock.Name with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation.params == nil { + m.t.Error("Expected call to GenericSpecificMock.Name") + } else { + m.t.Errorf("Expected call to GenericSpecificMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + m.t.Error("Expected call to GenericSpecificMock.Name") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *GenericSpecificMock[T]) MinimockFinish() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *GenericSpecificMock[T]) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *GenericSpecificMock[T]) minimockDone() bool { + done := true + return done && + m.MinimockNameDone() +} + diff --git a/snapshots/TestSnapshot-package_reference.go b/snapshots/TestSnapshot-package_reference.go new file mode 100644 index 0000000..8764f06 --- /dev/null +++ b/snapshots/TestSnapshot-package_reference.go @@ -0,0 +1,1000 @@ +package tests + +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +//go:generate minimock -i github.com/gojuno/minimock/v3.Tester -o ./tests/tester_mock_test.go -n TesterMock + +import ( + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" +) + +// TesterMock implements minimock.Tester +type TesterMock struct { + t minimock.Tester + + funcError func(p1 ...interface{}) + inspectFuncError func(p1 ...interface{}) + afterErrorCounter uint64 + beforeErrorCounter uint64 + ErrorMock mTesterMockError + + funcErrorf func(format string, args ...interface{}) + inspectFuncErrorf func(format string, args ...interface{}) + afterErrorfCounter uint64 + beforeErrorfCounter uint64 + ErrorfMock mTesterMockErrorf + + funcFailNow func() + inspectFuncFailNow func() + afterFailNowCounter uint64 + beforeFailNowCounter uint64 + FailNowMock mTesterMockFailNow + + funcFatal func(args ...interface{}) + inspectFuncFatal func(args ...interface{}) + afterFatalCounter uint64 + beforeFatalCounter uint64 + FatalMock mTesterMockFatal + + funcFatalf func(format string, args ...interface{}) + inspectFuncFatalf func(format string, args ...interface{}) + afterFatalfCounter uint64 + beforeFatalfCounter uint64 + FatalfMock mTesterMockFatalf +} + +// NewTesterMock returns a mock for minimock.Tester +func NewTesterMock(t minimock.Tester) *TesterMock { + m := &TesterMock{t: t} + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.ErrorMock = mTesterMockError{mock: m} + m.ErrorMock.callArgs = []*TesterMockErrorParams{} + + m.ErrorfMock = mTesterMockErrorf{mock: m} + m.ErrorfMock.callArgs = []*TesterMockErrorfParams{} + + m.FailNowMock = mTesterMockFailNow{mock: m} + + m.FatalMock = mTesterMockFatal{mock: m} + m.FatalMock.callArgs = []*TesterMockFatalParams{} + + m.FatalfMock = mTesterMockFatalf{mock: m} + m.FatalfMock.callArgs = []*TesterMockFatalfParams{} + + return m +} + +type mTesterMockError struct { + mock *TesterMock + defaultExpectation *TesterMockErrorExpectation + expectations []*TesterMockErrorExpectation + + callArgs []*TesterMockErrorParams + mutex sync.RWMutex +} + +// TesterMockErrorExpectation specifies expectation struct of the Tester.Error +type TesterMockErrorExpectation struct { + mock *TesterMock + params *TesterMockErrorParams + + Counter uint64 +} + +// TesterMockErrorParams contains parameters of the Tester.Error +type TesterMockErrorParams struct { + p1 []interface{} +} + +// Expect sets up expected params for Tester.Error +func (mmError *mTesterMockError) Expect(p1 ...interface{}) *mTesterMockError { + if mmError.mock.funcError != nil { + mmError.mock.t.Fatalf("TesterMock.Error mock is already set by Set") + } + + if mmError.defaultExpectation == nil { + mmError.defaultExpectation = &TesterMockErrorExpectation{} + } + + mmError.defaultExpectation.params = &TesterMockErrorParams{p1} + for _, e := range mmError.expectations { + if minimock.Equal(e.params, mmError.defaultExpectation.params) { + mmError.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmError.defaultExpectation.params) + } + } + + return mmError +} + +// Inspect accepts an inspector function that has same arguments as the Tester.Error +func (mmError *mTesterMockError) Inspect(f func(p1 ...interface{})) *mTesterMockError { + if mmError.mock.inspectFuncError != nil { + mmError.mock.t.Fatalf("Inspect function is already set for TesterMock.Error") + } + + mmError.mock.inspectFuncError = f + + return mmError +} + +// Return sets up results that will be returned by Tester.Error +func (mmError *mTesterMockError) Return() *TesterMock { + if mmError.mock.funcError != nil { + mmError.mock.t.Fatalf("TesterMock.Error mock is already set by Set") + } + + if mmError.defaultExpectation == nil { + mmError.defaultExpectation = &TesterMockErrorExpectation{mock: mmError.mock} + } + + return mmError.mock +} + +// Set uses given function f to mock the Tester.Error method +func (mmError *mTesterMockError) Set(f func(p1 ...interface{})) *TesterMock { + if mmError.defaultExpectation != nil { + mmError.mock.t.Fatalf("Default expectation is already set for the Tester.Error method") + } + + if len(mmError.expectations) > 0 { + mmError.mock.t.Fatalf("Some expectations are already set for the Tester.Error method") + } + + mmError.mock.funcError = f + return mmError.mock +} + +// Error implements minimock.Tester +func (mmError *TesterMock) Error(p1 ...interface{}) { + mm_atomic.AddUint64(&mmError.beforeErrorCounter, 1) + defer mm_atomic.AddUint64(&mmError.afterErrorCounter, 1) + + if mmError.inspectFuncError != nil { + mmError.inspectFuncError(p1...) + } + + mm_params := &TesterMockErrorParams{p1} + + // Record call args + mmError.ErrorMock.mutex.Lock() + mmError.ErrorMock.callArgs = append(mmError.ErrorMock.callArgs, mm_params) + mmError.ErrorMock.mutex.Unlock() + + for _, e := range mmError.ErrorMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmError.ErrorMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmError.ErrorMock.defaultExpectation.Counter, 1) + mm_want := mmError.ErrorMock.defaultExpectation.params + mm_got := TesterMockErrorParams{p1} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmError.t.Errorf("TesterMock.Error got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmError.funcError != nil { + mmError.funcError(p1...) + return + } + mmError.t.Fatalf("Unexpected call to TesterMock.Error. %v", p1) + +} + +// ErrorAfterCounter returns a count of finished TesterMock.Error invocations +func (mmError *TesterMock) ErrorAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmError.afterErrorCounter) +} + +// ErrorBeforeCounter returns a count of TesterMock.Error invocations +func (mmError *TesterMock) ErrorBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmError.beforeErrorCounter) +} + +// Calls returns a list of arguments used in each call to TesterMock.Error. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmError *mTesterMockError) Calls() []*TesterMockErrorParams { + mmError.mutex.RLock() + + argCopy := make([]*TesterMockErrorParams, len(mmError.callArgs)) + copy(argCopy, mmError.callArgs) + + mmError.mutex.RUnlock() + + return argCopy +} + +// MinimockErrorDone returns true if the count of the Error invocations corresponds +// the number of defined expectations +func (m *TesterMock) MinimockErrorDone() bool { + for _, e := range m.ErrorMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.ErrorMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcError != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { + return false + } + return true +} + +// MinimockErrorInspect logs each unmet expectation +func (m *TesterMock) MinimockErrorInspect() { + for _, e := range m.ErrorMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to TesterMock.Error with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.ErrorMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { + if m.ErrorMock.defaultExpectation.params == nil { + m.t.Error("Expected call to TesterMock.Error") + } else { + m.t.Errorf("Expected call to TesterMock.Error with params: %#v", *m.ErrorMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcError != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { + m.t.Error("Expected call to TesterMock.Error") + } +} + +type mTesterMockErrorf struct { + mock *TesterMock + defaultExpectation *TesterMockErrorfExpectation + expectations []*TesterMockErrorfExpectation + + callArgs []*TesterMockErrorfParams + mutex sync.RWMutex +} + +// TesterMockErrorfExpectation specifies expectation struct of the Tester.Errorf +type TesterMockErrorfExpectation struct { + mock *TesterMock + params *TesterMockErrorfParams + + Counter uint64 +} + +// TesterMockErrorfParams contains parameters of the Tester.Errorf +type TesterMockErrorfParams struct { + format string + args []interface{} +} + +// Expect sets up expected params for Tester.Errorf +func (mmErrorf *mTesterMockErrorf) Expect(format string, args ...interface{}) *mTesterMockErrorf { + if mmErrorf.mock.funcErrorf != nil { + mmErrorf.mock.t.Fatalf("TesterMock.Errorf mock is already set by Set") + } + + if mmErrorf.defaultExpectation == nil { + mmErrorf.defaultExpectation = &TesterMockErrorfExpectation{} + } + + mmErrorf.defaultExpectation.params = &TesterMockErrorfParams{format, args} + for _, e := range mmErrorf.expectations { + if minimock.Equal(e.params, mmErrorf.defaultExpectation.params) { + mmErrorf.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmErrorf.defaultExpectation.params) + } + } + + return mmErrorf +} + +// Inspect accepts an inspector function that has same arguments as the Tester.Errorf +func (mmErrorf *mTesterMockErrorf) Inspect(f func(format string, args ...interface{})) *mTesterMockErrorf { + if mmErrorf.mock.inspectFuncErrorf != nil { + mmErrorf.mock.t.Fatalf("Inspect function is already set for TesterMock.Errorf") + } + + mmErrorf.mock.inspectFuncErrorf = f + + return mmErrorf +} + +// Return sets up results that will be returned by Tester.Errorf +func (mmErrorf *mTesterMockErrorf) Return() *TesterMock { + if mmErrorf.mock.funcErrorf != nil { + mmErrorf.mock.t.Fatalf("TesterMock.Errorf mock is already set by Set") + } + + if mmErrorf.defaultExpectation == nil { + mmErrorf.defaultExpectation = &TesterMockErrorfExpectation{mock: mmErrorf.mock} + } + + return mmErrorf.mock +} + +// Set uses given function f to mock the Tester.Errorf method +func (mmErrorf *mTesterMockErrorf) Set(f func(format string, args ...interface{})) *TesterMock { + if mmErrorf.defaultExpectation != nil { + mmErrorf.mock.t.Fatalf("Default expectation is already set for the Tester.Errorf method") + } + + if len(mmErrorf.expectations) > 0 { + mmErrorf.mock.t.Fatalf("Some expectations are already set for the Tester.Errorf method") + } + + mmErrorf.mock.funcErrorf = f + return mmErrorf.mock +} + +// Errorf implements minimock.Tester +func (mmErrorf *TesterMock) Errorf(format string, args ...interface{}) { + mm_atomic.AddUint64(&mmErrorf.beforeErrorfCounter, 1) + defer mm_atomic.AddUint64(&mmErrorf.afterErrorfCounter, 1) + + if mmErrorf.inspectFuncErrorf != nil { + mmErrorf.inspectFuncErrorf(format, args...) + } + + mm_params := &TesterMockErrorfParams{format, args} + + // Record call args + mmErrorf.ErrorfMock.mutex.Lock() + mmErrorf.ErrorfMock.callArgs = append(mmErrorf.ErrorfMock.callArgs, mm_params) + mmErrorf.ErrorfMock.mutex.Unlock() + + for _, e := range mmErrorf.ErrorfMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmErrorf.ErrorfMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmErrorf.ErrorfMock.defaultExpectation.Counter, 1) + mm_want := mmErrorf.ErrorfMock.defaultExpectation.params + mm_got := TesterMockErrorfParams{format, args} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmErrorf.t.Errorf("TesterMock.Errorf got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmErrorf.funcErrorf != nil { + mmErrorf.funcErrorf(format, args...) + return + } + mmErrorf.t.Fatalf("Unexpected call to TesterMock.Errorf. %v %v", format, args) + +} + +// ErrorfAfterCounter returns a count of finished TesterMock.Errorf invocations +func (mmErrorf *TesterMock) ErrorfAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmErrorf.afterErrorfCounter) +} + +// ErrorfBeforeCounter returns a count of TesterMock.Errorf invocations +func (mmErrorf *TesterMock) ErrorfBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmErrorf.beforeErrorfCounter) +} + +// Calls returns a list of arguments used in each call to TesterMock.Errorf. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmErrorf *mTesterMockErrorf) Calls() []*TesterMockErrorfParams { + mmErrorf.mutex.RLock() + + argCopy := make([]*TesterMockErrorfParams, len(mmErrorf.callArgs)) + copy(argCopy, mmErrorf.callArgs) + + mmErrorf.mutex.RUnlock() + + return argCopy +} + +// MinimockErrorfDone returns true if the count of the Errorf invocations corresponds +// the number of defined expectations +func (m *TesterMock) MinimockErrorfDone() bool { + for _, e := range m.ErrorfMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.ErrorfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcErrorf != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { + return false + } + return true +} + +// MinimockErrorfInspect logs each unmet expectation +func (m *TesterMock) MinimockErrorfInspect() { + for _, e := range m.ErrorfMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to TesterMock.Errorf with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.ErrorfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { + if m.ErrorfMock.defaultExpectation.params == nil { + m.t.Error("Expected call to TesterMock.Errorf") + } else { + m.t.Errorf("Expected call to TesterMock.Errorf with params: %#v", *m.ErrorfMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcErrorf != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { + m.t.Error("Expected call to TesterMock.Errorf") + } +} + +type mTesterMockFailNow struct { + mock *TesterMock + defaultExpectation *TesterMockFailNowExpectation + expectations []*TesterMockFailNowExpectation +} + +// TesterMockFailNowExpectation specifies expectation struct of the Tester.FailNow +type TesterMockFailNowExpectation struct { + mock *TesterMock + + Counter uint64 +} + +// Expect sets up expected params for Tester.FailNow +func (mmFailNow *mTesterMockFailNow) Expect() *mTesterMockFailNow { + if mmFailNow.mock.funcFailNow != nil { + mmFailNow.mock.t.Fatalf("TesterMock.FailNow mock is already set by Set") + } + + if mmFailNow.defaultExpectation == nil { + mmFailNow.defaultExpectation = &TesterMockFailNowExpectation{} + } + + return mmFailNow +} + +// Inspect accepts an inspector function that has same arguments as the Tester.FailNow +func (mmFailNow *mTesterMockFailNow) Inspect(f func()) *mTesterMockFailNow { + if mmFailNow.mock.inspectFuncFailNow != nil { + mmFailNow.mock.t.Fatalf("Inspect function is already set for TesterMock.FailNow") + } + + mmFailNow.mock.inspectFuncFailNow = f + + return mmFailNow +} + +// Return sets up results that will be returned by Tester.FailNow +func (mmFailNow *mTesterMockFailNow) Return() *TesterMock { + if mmFailNow.mock.funcFailNow != nil { + mmFailNow.mock.t.Fatalf("TesterMock.FailNow mock is already set by Set") + } + + if mmFailNow.defaultExpectation == nil { + mmFailNow.defaultExpectation = &TesterMockFailNowExpectation{mock: mmFailNow.mock} + } + + return mmFailNow.mock +} + +// Set uses given function f to mock the Tester.FailNow method +func (mmFailNow *mTesterMockFailNow) Set(f func()) *TesterMock { + if mmFailNow.defaultExpectation != nil { + mmFailNow.mock.t.Fatalf("Default expectation is already set for the Tester.FailNow method") + } + + if len(mmFailNow.expectations) > 0 { + mmFailNow.mock.t.Fatalf("Some expectations are already set for the Tester.FailNow method") + } + + mmFailNow.mock.funcFailNow = f + return mmFailNow.mock +} + +// FailNow implements minimock.Tester +func (mmFailNow *TesterMock) FailNow() { + mm_atomic.AddUint64(&mmFailNow.beforeFailNowCounter, 1) + defer mm_atomic.AddUint64(&mmFailNow.afterFailNowCounter, 1) + + if mmFailNow.inspectFuncFailNow != nil { + mmFailNow.inspectFuncFailNow() + } + + if mmFailNow.FailNowMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmFailNow.FailNowMock.defaultExpectation.Counter, 1) + + return + + } + if mmFailNow.funcFailNow != nil { + mmFailNow.funcFailNow() + return + } + mmFailNow.t.Fatalf("Unexpected call to TesterMock.FailNow.") + +} + +// FailNowAfterCounter returns a count of finished TesterMock.FailNow invocations +func (mmFailNow *TesterMock) FailNowAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmFailNow.afterFailNowCounter) +} + +// FailNowBeforeCounter returns a count of TesterMock.FailNow invocations +func (mmFailNow *TesterMock) FailNowBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmFailNow.beforeFailNowCounter) +} + +// MinimockFailNowDone returns true if the count of the FailNow invocations corresponds +// the number of defined expectations +func (m *TesterMock) MinimockFailNowDone() bool { + for _, e := range m.FailNowMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.FailNowMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcFailNow != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { + return false + } + return true +} + +// MinimockFailNowInspect logs each unmet expectation +func (m *TesterMock) MinimockFailNowInspect() { + for _, e := range m.FailNowMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Error("Expected call to TesterMock.FailNow") + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.FailNowMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { + m.t.Error("Expected call to TesterMock.FailNow") + } + // if func was set then invocations count should be greater than zero + if m.funcFailNow != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { + m.t.Error("Expected call to TesterMock.FailNow") + } +} + +type mTesterMockFatal struct { + mock *TesterMock + defaultExpectation *TesterMockFatalExpectation + expectations []*TesterMockFatalExpectation + + callArgs []*TesterMockFatalParams + mutex sync.RWMutex +} + +// TesterMockFatalExpectation specifies expectation struct of the Tester.Fatal +type TesterMockFatalExpectation struct { + mock *TesterMock + params *TesterMockFatalParams + + Counter uint64 +} + +// TesterMockFatalParams contains parameters of the Tester.Fatal +type TesterMockFatalParams struct { + args []interface{} +} + +// Expect sets up expected params for Tester.Fatal +func (mmFatal *mTesterMockFatal) Expect(args ...interface{}) *mTesterMockFatal { + if mmFatal.mock.funcFatal != nil { + mmFatal.mock.t.Fatalf("TesterMock.Fatal mock is already set by Set") + } + + if mmFatal.defaultExpectation == nil { + mmFatal.defaultExpectation = &TesterMockFatalExpectation{} + } + + mmFatal.defaultExpectation.params = &TesterMockFatalParams{args} + for _, e := range mmFatal.expectations { + if minimock.Equal(e.params, mmFatal.defaultExpectation.params) { + mmFatal.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmFatal.defaultExpectation.params) + } + } + + return mmFatal +} + +// Inspect accepts an inspector function that has same arguments as the Tester.Fatal +func (mmFatal *mTesterMockFatal) Inspect(f func(args ...interface{})) *mTesterMockFatal { + if mmFatal.mock.inspectFuncFatal != nil { + mmFatal.mock.t.Fatalf("Inspect function is already set for TesterMock.Fatal") + } + + mmFatal.mock.inspectFuncFatal = f + + return mmFatal +} + +// Return sets up results that will be returned by Tester.Fatal +func (mmFatal *mTesterMockFatal) Return() *TesterMock { + if mmFatal.mock.funcFatal != nil { + mmFatal.mock.t.Fatalf("TesterMock.Fatal mock is already set by Set") + } + + if mmFatal.defaultExpectation == nil { + mmFatal.defaultExpectation = &TesterMockFatalExpectation{mock: mmFatal.mock} + } + + return mmFatal.mock +} + +// Set uses given function f to mock the Tester.Fatal method +func (mmFatal *mTesterMockFatal) Set(f func(args ...interface{})) *TesterMock { + if mmFatal.defaultExpectation != nil { + mmFatal.mock.t.Fatalf("Default expectation is already set for the Tester.Fatal method") + } + + if len(mmFatal.expectations) > 0 { + mmFatal.mock.t.Fatalf("Some expectations are already set for the Tester.Fatal method") + } + + mmFatal.mock.funcFatal = f + return mmFatal.mock +} + +// Fatal implements minimock.Tester +func (mmFatal *TesterMock) Fatal(args ...interface{}) { + mm_atomic.AddUint64(&mmFatal.beforeFatalCounter, 1) + defer mm_atomic.AddUint64(&mmFatal.afterFatalCounter, 1) + + if mmFatal.inspectFuncFatal != nil { + mmFatal.inspectFuncFatal(args...) + } + + mm_params := &TesterMockFatalParams{args} + + // Record call args + mmFatal.FatalMock.mutex.Lock() + mmFatal.FatalMock.callArgs = append(mmFatal.FatalMock.callArgs, mm_params) + mmFatal.FatalMock.mutex.Unlock() + + for _, e := range mmFatal.FatalMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmFatal.FatalMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmFatal.FatalMock.defaultExpectation.Counter, 1) + mm_want := mmFatal.FatalMock.defaultExpectation.params + mm_got := TesterMockFatalParams{args} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmFatal.t.Errorf("TesterMock.Fatal got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmFatal.funcFatal != nil { + mmFatal.funcFatal(args...) + return + } + mmFatal.t.Fatalf("Unexpected call to TesterMock.Fatal. %v", args) + +} + +// FatalAfterCounter returns a count of finished TesterMock.Fatal invocations +func (mmFatal *TesterMock) FatalAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmFatal.afterFatalCounter) +} + +// FatalBeforeCounter returns a count of TesterMock.Fatal invocations +func (mmFatal *TesterMock) FatalBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmFatal.beforeFatalCounter) +} + +// Calls returns a list of arguments used in each call to TesterMock.Fatal. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmFatal *mTesterMockFatal) Calls() []*TesterMockFatalParams { + mmFatal.mutex.RLock() + + argCopy := make([]*TesterMockFatalParams, len(mmFatal.callArgs)) + copy(argCopy, mmFatal.callArgs) + + mmFatal.mutex.RUnlock() + + return argCopy +} + +// MinimockFatalDone returns true if the count of the Fatal invocations corresponds +// the number of defined expectations +func (m *TesterMock) MinimockFatalDone() bool { + for _, e := range m.FatalMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.FatalMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcFatal != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { + return false + } + return true +} + +// MinimockFatalInspect logs each unmet expectation +func (m *TesterMock) MinimockFatalInspect() { + for _, e := range m.FatalMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to TesterMock.Fatal with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.FatalMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { + if m.FatalMock.defaultExpectation.params == nil { + m.t.Error("Expected call to TesterMock.Fatal") + } else { + m.t.Errorf("Expected call to TesterMock.Fatal with params: %#v", *m.FatalMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcFatal != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { + m.t.Error("Expected call to TesterMock.Fatal") + } +} + +type mTesterMockFatalf struct { + mock *TesterMock + defaultExpectation *TesterMockFatalfExpectation + expectations []*TesterMockFatalfExpectation + + callArgs []*TesterMockFatalfParams + mutex sync.RWMutex +} + +// TesterMockFatalfExpectation specifies expectation struct of the Tester.Fatalf +type TesterMockFatalfExpectation struct { + mock *TesterMock + params *TesterMockFatalfParams + + Counter uint64 +} + +// TesterMockFatalfParams contains parameters of the Tester.Fatalf +type TesterMockFatalfParams struct { + format string + args []interface{} +} + +// Expect sets up expected params for Tester.Fatalf +func (mmFatalf *mTesterMockFatalf) Expect(format string, args ...interface{}) *mTesterMockFatalf { + if mmFatalf.mock.funcFatalf != nil { + mmFatalf.mock.t.Fatalf("TesterMock.Fatalf mock is already set by Set") + } + + if mmFatalf.defaultExpectation == nil { + mmFatalf.defaultExpectation = &TesterMockFatalfExpectation{} + } + + mmFatalf.defaultExpectation.params = &TesterMockFatalfParams{format, args} + for _, e := range mmFatalf.expectations { + if minimock.Equal(e.params, mmFatalf.defaultExpectation.params) { + mmFatalf.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmFatalf.defaultExpectation.params) + } + } + + return mmFatalf +} + +// Inspect accepts an inspector function that has same arguments as the Tester.Fatalf +func (mmFatalf *mTesterMockFatalf) Inspect(f func(format string, args ...interface{})) *mTesterMockFatalf { + if mmFatalf.mock.inspectFuncFatalf != nil { + mmFatalf.mock.t.Fatalf("Inspect function is already set for TesterMock.Fatalf") + } + + mmFatalf.mock.inspectFuncFatalf = f + + return mmFatalf +} + +// Return sets up results that will be returned by Tester.Fatalf +func (mmFatalf *mTesterMockFatalf) Return() *TesterMock { + if mmFatalf.mock.funcFatalf != nil { + mmFatalf.mock.t.Fatalf("TesterMock.Fatalf mock is already set by Set") + } + + if mmFatalf.defaultExpectation == nil { + mmFatalf.defaultExpectation = &TesterMockFatalfExpectation{mock: mmFatalf.mock} + } + + return mmFatalf.mock +} + +// Set uses given function f to mock the Tester.Fatalf method +func (mmFatalf *mTesterMockFatalf) Set(f func(format string, args ...interface{})) *TesterMock { + if mmFatalf.defaultExpectation != nil { + mmFatalf.mock.t.Fatalf("Default expectation is already set for the Tester.Fatalf method") + } + + if len(mmFatalf.expectations) > 0 { + mmFatalf.mock.t.Fatalf("Some expectations are already set for the Tester.Fatalf method") + } + + mmFatalf.mock.funcFatalf = f + return mmFatalf.mock +} + +// Fatalf implements minimock.Tester +func (mmFatalf *TesterMock) Fatalf(format string, args ...interface{}) { + mm_atomic.AddUint64(&mmFatalf.beforeFatalfCounter, 1) + defer mm_atomic.AddUint64(&mmFatalf.afterFatalfCounter, 1) + + if mmFatalf.inspectFuncFatalf != nil { + mmFatalf.inspectFuncFatalf(format, args...) + } + + mm_params := &TesterMockFatalfParams{format, args} + + // Record call args + mmFatalf.FatalfMock.mutex.Lock() + mmFatalf.FatalfMock.callArgs = append(mmFatalf.FatalfMock.callArgs, mm_params) + mmFatalf.FatalfMock.mutex.Unlock() + + for _, e := range mmFatalf.FatalfMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmFatalf.FatalfMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmFatalf.FatalfMock.defaultExpectation.Counter, 1) + mm_want := mmFatalf.FatalfMock.defaultExpectation.params + mm_got := TesterMockFatalfParams{format, args} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmFatalf.t.Errorf("TesterMock.Fatalf got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmFatalf.funcFatalf != nil { + mmFatalf.funcFatalf(format, args...) + return + } + mmFatalf.t.Fatalf("Unexpected call to TesterMock.Fatalf. %v %v", format, args) + +} + +// FatalfAfterCounter returns a count of finished TesterMock.Fatalf invocations +func (mmFatalf *TesterMock) FatalfAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmFatalf.afterFatalfCounter) +} + +// FatalfBeforeCounter returns a count of TesterMock.Fatalf invocations +func (mmFatalf *TesterMock) FatalfBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmFatalf.beforeFatalfCounter) +} + +// Calls returns a list of arguments used in each call to TesterMock.Fatalf. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmFatalf *mTesterMockFatalf) Calls() []*TesterMockFatalfParams { + mmFatalf.mutex.RLock() + + argCopy := make([]*TesterMockFatalfParams, len(mmFatalf.callArgs)) + copy(argCopy, mmFatalf.callArgs) + + mmFatalf.mutex.RUnlock() + + return argCopy +} + +// MinimockFatalfDone returns true if the count of the Fatalf invocations corresponds +// the number of defined expectations +func (m *TesterMock) MinimockFatalfDone() bool { + for _, e := range m.FatalfMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.FatalfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcFatalf != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { + return false + } + return true +} + +// MinimockFatalfInspect logs each unmet expectation +func (m *TesterMock) MinimockFatalfInspect() { + for _, e := range m.FatalfMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to TesterMock.Fatalf with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.FatalfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { + if m.FatalfMock.defaultExpectation.params == nil { + m.t.Error("Expected call to TesterMock.Fatalf") + } else { + m.t.Errorf("Expected call to TesterMock.Fatalf with params: %#v", *m.FatalfMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcFatalf != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { + m.t.Error("Expected call to TesterMock.Fatalf") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *TesterMock) MinimockFinish() { + if !m.minimockDone() { + m.MinimockErrorInspect() + + m.MinimockErrorfInspect() + + m.MinimockFailNowInspect() + + m.MinimockFatalInspect() + + m.MinimockFatalfInspect() + m.t.FailNow() + } +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *TesterMock) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *TesterMock) minimockDone() bool { + done := true + return done && + m.MinimockErrorDone() && + m.MinimockErrorfDone() && + m.MinimockFailNowDone() && + m.MinimockFatalDone() && + m.MinimockFatalfDone() +} + diff --git a/snapshots/TestSnapshot-relative_reference.go b/snapshots/TestSnapshot-relative_reference.go new file mode 100644 index 0000000..3f15a38 --- /dev/null +++ b/snapshots/TestSnapshot-relative_reference.go @@ -0,0 +1,284 @@ +package tests + +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.Formatter -o ./tests/formatter_mock.go -n FormatterMock + +import ( + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" +) + +// FormatterMock implements Formatter +type FormatterMock struct { + t minimock.Tester + + funcFormat func(s1 string, p1 ...interface{}) (s2 string) + inspectFuncFormat func(s1 string, p1 ...interface{}) + afterFormatCounter uint64 + beforeFormatCounter uint64 + FormatMock mFormatterMockFormat +} + +// NewFormatterMock returns a mock for Formatter +func NewFormatterMock(t minimock.Tester) *FormatterMock { + m := &FormatterMock{t: t} + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.FormatMock = mFormatterMockFormat{mock: m} + m.FormatMock.callArgs = []*FormatterMockFormatParams{} + + return m +} + +type mFormatterMockFormat struct { + mock *FormatterMock + defaultExpectation *FormatterMockFormatExpectation + expectations []*FormatterMockFormatExpectation + + callArgs []*FormatterMockFormatParams + mutex sync.RWMutex +} + +// FormatterMockFormatExpectation specifies expectation struct of the Formatter.Format +type FormatterMockFormatExpectation struct { + mock *FormatterMock + params *FormatterMockFormatParams + results *FormatterMockFormatResults + Counter uint64 +} + +// FormatterMockFormatParams contains parameters of the Formatter.Format +type FormatterMockFormatParams struct { + s1 string + p1 []interface{} +} + +// FormatterMockFormatResults contains results of the Formatter.Format +type FormatterMockFormatResults struct { + s2 string +} + +// Expect sets up expected params for Formatter.Format +func (mmFormat *mFormatterMockFormat) Expect(s1 string, p1 ...interface{}) *mFormatterMockFormat { + if mmFormat.mock.funcFormat != nil { + mmFormat.mock.t.Fatalf("FormatterMock.Format mock is already set by Set") + } + + if mmFormat.defaultExpectation == nil { + mmFormat.defaultExpectation = &FormatterMockFormatExpectation{} + } + + mmFormat.defaultExpectation.params = &FormatterMockFormatParams{s1, p1} + for _, e := range mmFormat.expectations { + if minimock.Equal(e.params, mmFormat.defaultExpectation.params) { + mmFormat.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmFormat.defaultExpectation.params) + } + } + + return mmFormat +} + +// Inspect accepts an inspector function that has same arguments as the Formatter.Format +func (mmFormat *mFormatterMockFormat) Inspect(f func(s1 string, p1 ...interface{})) *mFormatterMockFormat { + if mmFormat.mock.inspectFuncFormat != nil { + mmFormat.mock.t.Fatalf("Inspect function is already set for FormatterMock.Format") + } + + mmFormat.mock.inspectFuncFormat = f + + return mmFormat +} + +// Return sets up results that will be returned by Formatter.Format +func (mmFormat *mFormatterMockFormat) Return(s2 string) *FormatterMock { + if mmFormat.mock.funcFormat != nil { + mmFormat.mock.t.Fatalf("FormatterMock.Format mock is already set by Set") + } + + if mmFormat.defaultExpectation == nil { + mmFormat.defaultExpectation = &FormatterMockFormatExpectation{mock: mmFormat.mock} + } + mmFormat.defaultExpectation.results = &FormatterMockFormatResults{s2} + return mmFormat.mock +} + +// Set uses given function f to mock the Formatter.Format method +func (mmFormat *mFormatterMockFormat) Set(f func(s1 string, p1 ...interface{}) (s2 string)) *FormatterMock { + if mmFormat.defaultExpectation != nil { + mmFormat.mock.t.Fatalf("Default expectation is already set for the Formatter.Format method") + } + + if len(mmFormat.expectations) > 0 { + mmFormat.mock.t.Fatalf("Some expectations are already set for the Formatter.Format method") + } + + mmFormat.mock.funcFormat = f + return mmFormat.mock +} + +// When sets expectation for the Formatter.Format which will trigger the result defined by the following +// Then helper +func (mmFormat *mFormatterMockFormat) When(s1 string, p1 ...interface{}) *FormatterMockFormatExpectation { + if mmFormat.mock.funcFormat != nil { + mmFormat.mock.t.Fatalf("FormatterMock.Format mock is already set by Set") + } + + expectation := &FormatterMockFormatExpectation{ + mock: mmFormat.mock, + params: &FormatterMockFormatParams{s1, p1}, + } + mmFormat.expectations = append(mmFormat.expectations, expectation) + return expectation +} + +// Then sets up Formatter.Format return parameters for the expectation previously defined by the When method +func (e *FormatterMockFormatExpectation) Then(s2 string) *FormatterMock { + e.results = &FormatterMockFormatResults{s2} + return e.mock +} + +// Format implements Formatter +func (mmFormat *FormatterMock) Format(s1 string, p1 ...interface{}) (s2 string) { + mm_atomic.AddUint64(&mmFormat.beforeFormatCounter, 1) + defer mm_atomic.AddUint64(&mmFormat.afterFormatCounter, 1) + + if mmFormat.inspectFuncFormat != nil { + mmFormat.inspectFuncFormat(s1, p1...) + } + + mm_params := &FormatterMockFormatParams{s1, p1} + + // Record call args + mmFormat.FormatMock.mutex.Lock() + mmFormat.FormatMock.callArgs = append(mmFormat.FormatMock.callArgs, mm_params) + mmFormat.FormatMock.mutex.Unlock() + + for _, e := range mmFormat.FormatMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return e.results.s2 + } + } + + if mmFormat.FormatMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmFormat.FormatMock.defaultExpectation.Counter, 1) + mm_want := mmFormat.FormatMock.defaultExpectation.params + mm_got := FormatterMockFormatParams{s1, p1} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmFormat.t.Errorf("FormatterMock.Format got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + mm_results := mmFormat.FormatMock.defaultExpectation.results + if mm_results == nil { + mmFormat.t.Fatal("No results are set for the FormatterMock.Format") + } + return (*mm_results).s2 + } + if mmFormat.funcFormat != nil { + return mmFormat.funcFormat(s1, p1...) + } + mmFormat.t.Fatalf("Unexpected call to FormatterMock.Format. %v %v", s1, p1) + return +} + +// FormatAfterCounter returns a count of finished FormatterMock.Format invocations +func (mmFormat *FormatterMock) FormatAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmFormat.afterFormatCounter) +} + +// FormatBeforeCounter returns a count of FormatterMock.Format invocations +func (mmFormat *FormatterMock) FormatBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmFormat.beforeFormatCounter) +} + +// Calls returns a list of arguments used in each call to FormatterMock.Format. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmFormat *mFormatterMockFormat) Calls() []*FormatterMockFormatParams { + mmFormat.mutex.RLock() + + argCopy := make([]*FormatterMockFormatParams, len(mmFormat.callArgs)) + copy(argCopy, mmFormat.callArgs) + + mmFormat.mutex.RUnlock() + + return argCopy +} + +// MinimockFormatDone returns true if the count of the Format invocations corresponds +// the number of defined expectations +func (m *FormatterMock) MinimockFormatDone() bool { + for _, e := range m.FormatMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.FormatMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFormatCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcFormat != nil && mm_atomic.LoadUint64(&m.afterFormatCounter) < 1 { + return false + } + return true +} + +// MinimockFormatInspect logs each unmet expectation +func (m *FormatterMock) MinimockFormatInspect() { + for _, e := range m.FormatMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to FormatterMock.Format with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.FormatMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFormatCounter) < 1 { + if m.FormatMock.defaultExpectation.params == nil { + m.t.Error("Expected call to FormatterMock.Format") + } else { + m.t.Errorf("Expected call to FormatterMock.Format with params: %#v", *m.FormatMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcFormat != nil && mm_atomic.LoadUint64(&m.afterFormatCounter) < 1 { + m.t.Error("Expected call to FormatterMock.Format") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *FormatterMock) MinimockFinish() { + if !m.minimockDone() { + m.MinimockFormatInspect() + m.t.FailNow() + } +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *FormatterMock) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *FormatterMock) minimockDone() bool { + done := true + return done && + m.MinimockFormatDone() +} + diff --git a/tests/generic_complex_union.go b/tests/generic_complex_union.go new file mode 100644 index 0000000..21df93a --- /dev/null +++ b/tests/generic_complex_union.go @@ -0,0 +1,254 @@ +package tests + +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericComplexUnion -o ./tests/generic_complex_union.go -n GenericComplexUnionMock + +import ( + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" +) + +// GenericComplexUnionMock implements genericComplexUnion +type GenericComplexUnionMock[T complexUnion] struct { + t minimock.Tester + + funcName func(t1 T) + inspectFuncName func(t1 T) + afterNameCounter uint64 + beforeNameCounter uint64 + NameMock mGenericComplexUnionMockName[T] +} + +// NewGenericComplexUnionMock returns a mock for genericComplexUnion +func NewGenericComplexUnionMock[T complexUnion](t minimock.Tester) *GenericComplexUnionMock[T] { + m := &GenericComplexUnionMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.NameMock = mGenericComplexUnionMockName[T]{mock: m} + m.NameMock.callArgs = []*GenericComplexUnionMockNameParams[T]{} + + return m +} + +type mGenericComplexUnionMockName[T complexUnion] struct { + mock *GenericComplexUnionMock[T] + defaultExpectation *GenericComplexUnionMockNameExpectation[T] + expectations []*GenericComplexUnionMockNameExpectation[T] + + callArgs []*GenericComplexUnionMockNameParams[T] + mutex sync.RWMutex +} + +// GenericComplexUnionMockNameExpectation specifies expectation struct of the genericComplexUnion.Name +type GenericComplexUnionMockNameExpectation[T complexUnion] struct { + mock *GenericComplexUnionMock[T] + params *GenericComplexUnionMockNameParams[T] + + Counter uint64 +} + +// GenericComplexUnionMockNameParams contains parameters of the genericComplexUnion.Name +type GenericComplexUnionMockNameParams[T complexUnion] struct { + t1 T +} + +// Expect sets up expected params for genericComplexUnion.Name +func (mmName *mGenericComplexUnionMockName[T]) Expect(t1 T) *mGenericComplexUnionMockName[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericComplexUnionMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericComplexUnionMockNameExpectation[T]{} + } + + mmName.defaultExpectation.params = &GenericComplexUnionMockNameParams[T]{t1} + for _, e := range mmName.expectations { + if minimock.Equal(e.params, mmName.defaultExpectation.params) { + mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) + } + } + + return mmName +} + +// Inspect accepts an inspector function that has same arguments as the genericComplexUnion.Name +func (mmName *mGenericComplexUnionMockName[T]) Inspect(f func(t1 T)) *mGenericComplexUnionMockName[T] { + if mmName.mock.inspectFuncName != nil { + mmName.mock.t.Fatalf("Inspect function is already set for GenericComplexUnionMock.Name") + } + + mmName.mock.inspectFuncName = f + + return mmName +} + +// Return sets up results that will be returned by genericComplexUnion.Name +func (mmName *mGenericComplexUnionMockName[T]) Return() *GenericComplexUnionMock[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericComplexUnionMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericComplexUnionMockNameExpectation[T]{mock: mmName.mock} + } + + return mmName.mock +} + +// Set uses given function f to mock the genericComplexUnion.Name method +func (mmName *mGenericComplexUnionMockName[T]) Set(f func(t1 T)) *GenericComplexUnionMock[T] { + if mmName.defaultExpectation != nil { + mmName.mock.t.Fatalf("Default expectation is already set for the genericComplexUnion.Name method") + } + + if len(mmName.expectations) > 0 { + mmName.mock.t.Fatalf("Some expectations are already set for the genericComplexUnion.Name method") + } + + mmName.mock.funcName = f + return mmName.mock +} + +// Name implements genericComplexUnion +func (mmName *GenericComplexUnionMock[T]) Name(t1 T) { + mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) + defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + + if mmName.inspectFuncName != nil { + mmName.inspectFuncName(t1) + } + + mm_params := &GenericComplexUnionMockNameParams[T]{t1} + + // Record call args + mmName.NameMock.mutex.Lock() + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.mutex.Unlock() + + for _, e := range mmName.NameMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmName.NameMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) + mm_want := mmName.NameMock.defaultExpectation.params + mm_got := GenericComplexUnionMockNameParams[T]{t1} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmName.t.Errorf("GenericComplexUnionMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmName.funcName != nil { + mmName.funcName(t1) + return + } + mmName.t.Fatalf("Unexpected call to GenericComplexUnionMock.Name. %v", t1) + +} + +// NameAfterCounter returns a count of finished GenericComplexUnionMock.Name invocations +func (mmName *GenericComplexUnionMock[T]) NameAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.afterNameCounter) +} + +// NameBeforeCounter returns a count of GenericComplexUnionMock.Name invocations +func (mmName *GenericComplexUnionMock[T]) NameBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.beforeNameCounter) +} + +// Calls returns a list of arguments used in each call to GenericComplexUnionMock.Name. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmName *mGenericComplexUnionMockName[T]) Calls() []*GenericComplexUnionMockNameParams[T] { + mmName.mutex.RLock() + + argCopy := make([]*GenericComplexUnionMockNameParams[T], len(mmName.callArgs)) + copy(argCopy, mmName.callArgs) + + mmName.mutex.RUnlock() + + return argCopy +} + +// MinimockNameDone returns true if the count of the Name invocations corresponds +// the number of defined expectations +func (m *GenericComplexUnionMock[T]) MinimockNameDone() bool { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + return true +} + +// MinimockNameInspect logs each unmet expectation +func (m *GenericComplexUnionMock[T]) MinimockNameInspect() { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to GenericComplexUnionMock.Name with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation.params == nil { + m.t.Error("Expected call to GenericComplexUnionMock.Name") + } else { + m.t.Errorf("Expected call to GenericComplexUnionMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + m.t.Error("Expected call to GenericComplexUnionMock.Name") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *GenericComplexUnionMock[T]) MinimockFinish() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *GenericComplexUnionMock[T]) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *GenericComplexUnionMock[T]) minimockDone() bool { + done := true + return done && + m.MinimockNameDone() +} diff --git a/tests/generic/generic_in.go b/tests/generic_in.go similarity index 97% rename from tests/generic/generic_in.go rename to tests/generic_in.go index bc269ea..7131416 100644 --- a/tests/generic/generic_in.go +++ b/tests/generic_in.go @@ -1,8 +1,8 @@ -package generic +package tests // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericIn -o ./tests/generic/generic_in.go -n GenericInMock +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericIn -o ./tests/generic_in.go -n GenericInMock import ( "sync" @@ -12,7 +12,7 @@ import ( "github.com/gojuno/minimock/v3" ) -// GenericInMock implements tests.genericIn +// GenericInMock implements genericIn type GenericInMock[T any] struct { t minimock.Tester @@ -23,7 +23,7 @@ type GenericInMock[T any] struct { NameMock mGenericInMockName[T] } -// NewGenericInMock returns a mock for tests.genericIn +// NewGenericInMock returns a mock for genericIn func NewGenericInMock[T any](t minimock.Tester) *GenericInMock[T] { m := &GenericInMock[T]{t: t} if controller, ok := t.(minimock.MockController); ok { @@ -116,7 +116,7 @@ func (mmName *mGenericInMockName[T]) Set(f func(t1 T)) *GenericInMock[T] { return mmName.mock } -// Name implements tests.genericIn +// Name implements genericIn func (mmName *GenericInMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) diff --git a/tests/generic_inline_union.go b/tests/generic_inline_union.go new file mode 100644 index 0000000..6ece3f9 --- /dev/null +++ b/tests/generic_inline_union.go @@ -0,0 +1,254 @@ +package tests + +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInlineUnion -o ./tests/generic_inline_union.go -n GenericInlineUnionMock + +import ( + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" +) + +// GenericInlineUnionMock implements genericInlineUnion +type GenericInlineUnionMock[T int | float64] struct { + t minimock.Tester + + funcName func(t1 T) + inspectFuncName func(t1 T) + afterNameCounter uint64 + beforeNameCounter uint64 + NameMock mGenericInlineUnionMockName[T] +} + +// NewGenericInlineUnionMock returns a mock for genericInlineUnion +func NewGenericInlineUnionMock[T int | float64](t minimock.Tester) *GenericInlineUnionMock[T] { + m := &GenericInlineUnionMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.NameMock = mGenericInlineUnionMockName[T]{mock: m} + m.NameMock.callArgs = []*GenericInlineUnionMockNameParams[T]{} + + return m +} + +type mGenericInlineUnionMockName[T int | float64] struct { + mock *GenericInlineUnionMock[T] + defaultExpectation *GenericInlineUnionMockNameExpectation[T] + expectations []*GenericInlineUnionMockNameExpectation[T] + + callArgs []*GenericInlineUnionMockNameParams[T] + mutex sync.RWMutex +} + +// GenericInlineUnionMockNameExpectation specifies expectation struct of the genericInlineUnion.Name +type GenericInlineUnionMockNameExpectation[T int | float64] struct { + mock *GenericInlineUnionMock[T] + params *GenericInlineUnionMockNameParams[T] + + Counter uint64 +} + +// GenericInlineUnionMockNameParams contains parameters of the genericInlineUnion.Name +type GenericInlineUnionMockNameParams[T int | float64] struct { + t1 T +} + +// Expect sets up expected params for genericInlineUnion.Name +func (mmName *mGenericInlineUnionMockName[T]) Expect(t1 T) *mGenericInlineUnionMockName[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericInlineUnionMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericInlineUnionMockNameExpectation[T]{} + } + + mmName.defaultExpectation.params = &GenericInlineUnionMockNameParams[T]{t1} + for _, e := range mmName.expectations { + if minimock.Equal(e.params, mmName.defaultExpectation.params) { + mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) + } + } + + return mmName +} + +// Inspect accepts an inspector function that has same arguments as the genericInlineUnion.Name +func (mmName *mGenericInlineUnionMockName[T]) Inspect(f func(t1 T)) *mGenericInlineUnionMockName[T] { + if mmName.mock.inspectFuncName != nil { + mmName.mock.t.Fatalf("Inspect function is already set for GenericInlineUnionMock.Name") + } + + mmName.mock.inspectFuncName = f + + return mmName +} + +// Return sets up results that will be returned by genericInlineUnion.Name +func (mmName *mGenericInlineUnionMockName[T]) Return() *GenericInlineUnionMock[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericInlineUnionMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericInlineUnionMockNameExpectation[T]{mock: mmName.mock} + } + + return mmName.mock +} + +// Set uses given function f to mock the genericInlineUnion.Name method +func (mmName *mGenericInlineUnionMockName[T]) Set(f func(t1 T)) *GenericInlineUnionMock[T] { + if mmName.defaultExpectation != nil { + mmName.mock.t.Fatalf("Default expectation is already set for the genericInlineUnion.Name method") + } + + if len(mmName.expectations) > 0 { + mmName.mock.t.Fatalf("Some expectations are already set for the genericInlineUnion.Name method") + } + + mmName.mock.funcName = f + return mmName.mock +} + +// Name implements genericInlineUnion +func (mmName *GenericInlineUnionMock[T]) Name(t1 T) { + mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) + defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + + if mmName.inspectFuncName != nil { + mmName.inspectFuncName(t1) + } + + mm_params := &GenericInlineUnionMockNameParams[T]{t1} + + // Record call args + mmName.NameMock.mutex.Lock() + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.mutex.Unlock() + + for _, e := range mmName.NameMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmName.NameMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) + mm_want := mmName.NameMock.defaultExpectation.params + mm_got := GenericInlineUnionMockNameParams[T]{t1} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmName.t.Errorf("GenericInlineUnionMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmName.funcName != nil { + mmName.funcName(t1) + return + } + mmName.t.Fatalf("Unexpected call to GenericInlineUnionMock.Name. %v", t1) + +} + +// NameAfterCounter returns a count of finished GenericInlineUnionMock.Name invocations +func (mmName *GenericInlineUnionMock[T]) NameAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.afterNameCounter) +} + +// NameBeforeCounter returns a count of GenericInlineUnionMock.Name invocations +func (mmName *GenericInlineUnionMock[T]) NameBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.beforeNameCounter) +} + +// Calls returns a list of arguments used in each call to GenericInlineUnionMock.Name. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmName *mGenericInlineUnionMockName[T]) Calls() []*GenericInlineUnionMockNameParams[T] { + mmName.mutex.RLock() + + argCopy := make([]*GenericInlineUnionMockNameParams[T], len(mmName.callArgs)) + copy(argCopy, mmName.callArgs) + + mmName.mutex.RUnlock() + + return argCopy +} + +// MinimockNameDone returns true if the count of the Name invocations corresponds +// the number of defined expectations +func (m *GenericInlineUnionMock[T]) MinimockNameDone() bool { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + return true +} + +// MinimockNameInspect logs each unmet expectation +func (m *GenericInlineUnionMock[T]) MinimockNameInspect() { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to GenericInlineUnionMock.Name with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation.params == nil { + m.t.Error("Expected call to GenericInlineUnionMock.Name") + } else { + m.t.Errorf("Expected call to GenericInlineUnionMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + m.t.Error("Expected call to GenericInlineUnionMock.Name") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *GenericInlineUnionMock[T]) MinimockFinish() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *GenericInlineUnionMock[T]) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *GenericInlineUnionMock[T]) minimockDone() bool { + done := true + return done && + m.MinimockNameDone() +} diff --git a/tests/generic_inline_with_many_options.go b/tests/generic_inline_with_many_options.go new file mode 100644 index 0000000..4517c3a --- /dev/null +++ b/tests/generic_inline_with_many_options.go @@ -0,0 +1,254 @@ +package tests + +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInlineUnionWithManyTypes -o ./tests/generic_inline_with_many_options.go -n GenericInlineUnionWithManyTypesMock + +import ( + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" +) + +// GenericInlineUnionWithManyTypesMock implements genericInlineUnionWithManyTypes +type GenericInlineUnionWithManyTypesMock[T int | float64 | string] struct { + t minimock.Tester + + funcName func(t1 T) + inspectFuncName func(t1 T) + afterNameCounter uint64 + beforeNameCounter uint64 + NameMock mGenericInlineUnionWithManyTypesMockName[T] +} + +// NewGenericInlineUnionWithManyTypesMock returns a mock for genericInlineUnionWithManyTypes +func NewGenericInlineUnionWithManyTypesMock[T int | float64 | string](t minimock.Tester) *GenericInlineUnionWithManyTypesMock[T] { + m := &GenericInlineUnionWithManyTypesMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.NameMock = mGenericInlineUnionWithManyTypesMockName[T]{mock: m} + m.NameMock.callArgs = []*GenericInlineUnionWithManyTypesMockNameParams[T]{} + + return m +} + +type mGenericInlineUnionWithManyTypesMockName[T int | float64 | string] struct { + mock *GenericInlineUnionWithManyTypesMock[T] + defaultExpectation *GenericInlineUnionWithManyTypesMockNameExpectation[T] + expectations []*GenericInlineUnionWithManyTypesMockNameExpectation[T] + + callArgs []*GenericInlineUnionWithManyTypesMockNameParams[T] + mutex sync.RWMutex +} + +// GenericInlineUnionWithManyTypesMockNameExpectation specifies expectation struct of the genericInlineUnionWithManyTypes.Name +type GenericInlineUnionWithManyTypesMockNameExpectation[T int | float64 | string] struct { + mock *GenericInlineUnionWithManyTypesMock[T] + params *GenericInlineUnionWithManyTypesMockNameParams[T] + + Counter uint64 +} + +// GenericInlineUnionWithManyTypesMockNameParams contains parameters of the genericInlineUnionWithManyTypes.Name +type GenericInlineUnionWithManyTypesMockNameParams[T int | float64 | string] struct { + t1 T +} + +// Expect sets up expected params for genericInlineUnionWithManyTypes.Name +func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Expect(t1 T) *mGenericInlineUnionWithManyTypesMockName[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericInlineUnionWithManyTypesMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericInlineUnionWithManyTypesMockNameExpectation[T]{} + } + + mmName.defaultExpectation.params = &GenericInlineUnionWithManyTypesMockNameParams[T]{t1} + for _, e := range mmName.expectations { + if minimock.Equal(e.params, mmName.defaultExpectation.params) { + mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) + } + } + + return mmName +} + +// Inspect accepts an inspector function that has same arguments as the genericInlineUnionWithManyTypes.Name +func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Inspect(f func(t1 T)) *mGenericInlineUnionWithManyTypesMockName[T] { + if mmName.mock.inspectFuncName != nil { + mmName.mock.t.Fatalf("Inspect function is already set for GenericInlineUnionWithManyTypesMock.Name") + } + + mmName.mock.inspectFuncName = f + + return mmName +} + +// Return sets up results that will be returned by genericInlineUnionWithManyTypes.Name +func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Return() *GenericInlineUnionWithManyTypesMock[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericInlineUnionWithManyTypesMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericInlineUnionWithManyTypesMockNameExpectation[T]{mock: mmName.mock} + } + + return mmName.mock +} + +// Set uses given function f to mock the genericInlineUnionWithManyTypes.Name method +func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Set(f func(t1 T)) *GenericInlineUnionWithManyTypesMock[T] { + if mmName.defaultExpectation != nil { + mmName.mock.t.Fatalf("Default expectation is already set for the genericInlineUnionWithManyTypes.Name method") + } + + if len(mmName.expectations) > 0 { + mmName.mock.t.Fatalf("Some expectations are already set for the genericInlineUnionWithManyTypes.Name method") + } + + mmName.mock.funcName = f + return mmName.mock +} + +// Name implements genericInlineUnionWithManyTypes +func (mmName *GenericInlineUnionWithManyTypesMock[T]) Name(t1 T) { + mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) + defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + + if mmName.inspectFuncName != nil { + mmName.inspectFuncName(t1) + } + + mm_params := &GenericInlineUnionWithManyTypesMockNameParams[T]{t1} + + // Record call args + mmName.NameMock.mutex.Lock() + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.mutex.Unlock() + + for _, e := range mmName.NameMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmName.NameMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) + mm_want := mmName.NameMock.defaultExpectation.params + mm_got := GenericInlineUnionWithManyTypesMockNameParams[T]{t1} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmName.t.Errorf("GenericInlineUnionWithManyTypesMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmName.funcName != nil { + mmName.funcName(t1) + return + } + mmName.t.Fatalf("Unexpected call to GenericInlineUnionWithManyTypesMock.Name. %v", t1) + +} + +// NameAfterCounter returns a count of finished GenericInlineUnionWithManyTypesMock.Name invocations +func (mmName *GenericInlineUnionWithManyTypesMock[T]) NameAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.afterNameCounter) +} + +// NameBeforeCounter returns a count of GenericInlineUnionWithManyTypesMock.Name invocations +func (mmName *GenericInlineUnionWithManyTypesMock[T]) NameBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.beforeNameCounter) +} + +// Calls returns a list of arguments used in each call to GenericInlineUnionWithManyTypesMock.Name. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Calls() []*GenericInlineUnionWithManyTypesMockNameParams[T] { + mmName.mutex.RLock() + + argCopy := make([]*GenericInlineUnionWithManyTypesMockNameParams[T], len(mmName.callArgs)) + copy(argCopy, mmName.callArgs) + + mmName.mutex.RUnlock() + + return argCopy +} + +// MinimockNameDone returns true if the count of the Name invocations corresponds +// the number of defined expectations +func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockNameDone() bool { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + return true +} + +// MinimockNameInspect logs each unmet expectation +func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockNameInspect() { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to GenericInlineUnionWithManyTypesMock.Name with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation.params == nil { + m.t.Error("Expected call to GenericInlineUnionWithManyTypesMock.Name") + } else { + m.t.Errorf("Expected call to GenericInlineUnionWithManyTypesMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + m.t.Error("Expected call to GenericInlineUnionWithManyTypesMock.Name") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockFinish() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *GenericInlineUnionWithManyTypesMock[T]) minimockDone() bool { + done := true + return done && + m.MinimockNameDone() +} diff --git a/tests/generic/generic_inout.go b/tests/generic_inout.go similarity index 97% rename from tests/generic/generic_inout.go rename to tests/generic_inout.go index 25702be..f741873 100644 --- a/tests/generic/generic_inout.go +++ b/tests/generic_inout.go @@ -1,8 +1,8 @@ -package generic +package tests // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInout -o ./tests/generic/generic_inout.go -n GenericInoutMock +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInout -o ./tests/generic_inout.go -n GenericInoutMock import ( "sync" @@ -12,7 +12,7 @@ import ( "github.com/gojuno/minimock/v3" ) -// GenericInoutMock implements tests.genericInout +// GenericInoutMock implements genericInout type GenericInoutMock[T any] struct { t minimock.Tester @@ -23,7 +23,7 @@ type GenericInoutMock[T any] struct { NameMock mGenericInoutMockName[T] } -// NewGenericInoutMock returns a mock for tests.genericInout +// NewGenericInoutMock returns a mock for genericInout func NewGenericInoutMock[T any](t minimock.Tester) *GenericInoutMock[T] { m := &GenericInoutMock[T]{t: t} if controller, ok := t.(minimock.MockController); ok { @@ -142,7 +142,7 @@ func (e *GenericInoutMockNameExpectation[T]) Then(t2 T) *GenericInoutMock[T] { return e.mock } -// Name implements tests.genericInout +// Name implements genericInout func (mmName *GenericInoutMock[T]) Name(t1 T) (t2 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) diff --git a/tests/generic/generic_out.go b/tests/generic_out.go similarity index 96% rename from tests/generic/generic_out.go rename to tests/generic_out.go index fc16cee..528e9a5 100644 --- a/tests/generic/generic_out.go +++ b/tests/generic_out.go @@ -1,8 +1,8 @@ -package generic +package tests // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericOut -o ./tests/generic/generic_out.go -n GenericOutMock +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericOut -o ./tests/generic_out.go -n GenericOutMock import ( mm_atomic "sync/atomic" @@ -11,7 +11,7 @@ import ( "github.com/gojuno/minimock/v3" ) -// GenericOutMock implements tests.genericOut +// GenericOutMock implements genericOut type GenericOutMock[T any] struct { t minimock.Tester @@ -22,7 +22,7 @@ type GenericOutMock[T any] struct { NameMock mGenericOutMockName[T] } -// NewGenericOutMock returns a mock for tests.genericOut +// NewGenericOutMock returns a mock for genericOut func NewGenericOutMock[T any](t minimock.Tester) *GenericOutMock[T] { m := &GenericOutMock[T]{t: t} if controller, ok := t.(minimock.MockController); ok { @@ -104,7 +104,7 @@ func (mmName *mGenericOutMockName[T]) Set(f func() (t1 T)) *GenericOutMock[T] { return mmName.mock } -// Name implements tests.genericOut +// Name implements genericOut func (mmName *GenericOutMock[T]) Name() (t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) diff --git a/tests/generic_simple_union.go b/tests/generic_simple_union.go new file mode 100644 index 0000000..18e25e6 --- /dev/null +++ b/tests/generic_simple_union.go @@ -0,0 +1,254 @@ +package tests + +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericSimpleUnion -o ./tests/generic_simple_union.go -n GenericSimpleUnionMock + +import ( + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" +) + +// GenericSimpleUnionMock implements genericSimpleUnion +type GenericSimpleUnionMock[T simpleUnion] struct { + t minimock.Tester + + funcName func(t1 T) + inspectFuncName func(t1 T) + afterNameCounter uint64 + beforeNameCounter uint64 + NameMock mGenericSimpleUnionMockName[T] +} + +// NewGenericSimpleUnionMock returns a mock for genericSimpleUnion +func NewGenericSimpleUnionMock[T simpleUnion](t minimock.Tester) *GenericSimpleUnionMock[T] { + m := &GenericSimpleUnionMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.NameMock = mGenericSimpleUnionMockName[T]{mock: m} + m.NameMock.callArgs = []*GenericSimpleUnionMockNameParams[T]{} + + return m +} + +type mGenericSimpleUnionMockName[T simpleUnion] struct { + mock *GenericSimpleUnionMock[T] + defaultExpectation *GenericSimpleUnionMockNameExpectation[T] + expectations []*GenericSimpleUnionMockNameExpectation[T] + + callArgs []*GenericSimpleUnionMockNameParams[T] + mutex sync.RWMutex +} + +// GenericSimpleUnionMockNameExpectation specifies expectation struct of the genericSimpleUnion.Name +type GenericSimpleUnionMockNameExpectation[T simpleUnion] struct { + mock *GenericSimpleUnionMock[T] + params *GenericSimpleUnionMockNameParams[T] + + Counter uint64 +} + +// GenericSimpleUnionMockNameParams contains parameters of the genericSimpleUnion.Name +type GenericSimpleUnionMockNameParams[T simpleUnion] struct { + t1 T +} + +// Expect sets up expected params for genericSimpleUnion.Name +func (mmName *mGenericSimpleUnionMockName[T]) Expect(t1 T) *mGenericSimpleUnionMockName[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericSimpleUnionMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericSimpleUnionMockNameExpectation[T]{} + } + + mmName.defaultExpectation.params = &GenericSimpleUnionMockNameParams[T]{t1} + for _, e := range mmName.expectations { + if minimock.Equal(e.params, mmName.defaultExpectation.params) { + mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) + } + } + + return mmName +} + +// Inspect accepts an inspector function that has same arguments as the genericSimpleUnion.Name +func (mmName *mGenericSimpleUnionMockName[T]) Inspect(f func(t1 T)) *mGenericSimpleUnionMockName[T] { + if mmName.mock.inspectFuncName != nil { + mmName.mock.t.Fatalf("Inspect function is already set for GenericSimpleUnionMock.Name") + } + + mmName.mock.inspectFuncName = f + + return mmName +} + +// Return sets up results that will be returned by genericSimpleUnion.Name +func (mmName *mGenericSimpleUnionMockName[T]) Return() *GenericSimpleUnionMock[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericSimpleUnionMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericSimpleUnionMockNameExpectation[T]{mock: mmName.mock} + } + + return mmName.mock +} + +// Set uses given function f to mock the genericSimpleUnion.Name method +func (mmName *mGenericSimpleUnionMockName[T]) Set(f func(t1 T)) *GenericSimpleUnionMock[T] { + if mmName.defaultExpectation != nil { + mmName.mock.t.Fatalf("Default expectation is already set for the genericSimpleUnion.Name method") + } + + if len(mmName.expectations) > 0 { + mmName.mock.t.Fatalf("Some expectations are already set for the genericSimpleUnion.Name method") + } + + mmName.mock.funcName = f + return mmName.mock +} + +// Name implements genericSimpleUnion +func (mmName *GenericSimpleUnionMock[T]) Name(t1 T) { + mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) + defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + + if mmName.inspectFuncName != nil { + mmName.inspectFuncName(t1) + } + + mm_params := &GenericSimpleUnionMockNameParams[T]{t1} + + // Record call args + mmName.NameMock.mutex.Lock() + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.mutex.Unlock() + + for _, e := range mmName.NameMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmName.NameMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) + mm_want := mmName.NameMock.defaultExpectation.params + mm_got := GenericSimpleUnionMockNameParams[T]{t1} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmName.t.Errorf("GenericSimpleUnionMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmName.funcName != nil { + mmName.funcName(t1) + return + } + mmName.t.Fatalf("Unexpected call to GenericSimpleUnionMock.Name. %v", t1) + +} + +// NameAfterCounter returns a count of finished GenericSimpleUnionMock.Name invocations +func (mmName *GenericSimpleUnionMock[T]) NameAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.afterNameCounter) +} + +// NameBeforeCounter returns a count of GenericSimpleUnionMock.Name invocations +func (mmName *GenericSimpleUnionMock[T]) NameBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.beforeNameCounter) +} + +// Calls returns a list of arguments used in each call to GenericSimpleUnionMock.Name. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmName *mGenericSimpleUnionMockName[T]) Calls() []*GenericSimpleUnionMockNameParams[T] { + mmName.mutex.RLock() + + argCopy := make([]*GenericSimpleUnionMockNameParams[T], len(mmName.callArgs)) + copy(argCopy, mmName.callArgs) + + mmName.mutex.RUnlock() + + return argCopy +} + +// MinimockNameDone returns true if the count of the Name invocations corresponds +// the number of defined expectations +func (m *GenericSimpleUnionMock[T]) MinimockNameDone() bool { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + return true +} + +// MinimockNameInspect logs each unmet expectation +func (m *GenericSimpleUnionMock[T]) MinimockNameInspect() { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to GenericSimpleUnionMock.Name with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation.params == nil { + m.t.Error("Expected call to GenericSimpleUnionMock.Name") + } else { + m.t.Errorf("Expected call to GenericSimpleUnionMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + m.t.Error("Expected call to GenericSimpleUnionMock.Name") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *GenericSimpleUnionMock[T]) MinimockFinish() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *GenericSimpleUnionMock[T]) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *GenericSimpleUnionMock[T]) minimockDone() bool { + done := true + return done && + m.MinimockNameDone() +} diff --git a/tests/generic_specific.go b/tests/generic_specific.go new file mode 100644 index 0000000..e833040 --- /dev/null +++ b/tests/generic_specific.go @@ -0,0 +1,255 @@ +package tests + +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericSpecific -o ./tests/generic_specific.go -n GenericSpecificMock + +import ( + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" + "google.golang.org/protobuf/proto" +) + +// GenericSpecificMock implements genericSpecific +type GenericSpecificMock[T proto.Message] struct { + t minimock.Tester + + funcName func(t1 T) + inspectFuncName func(t1 T) + afterNameCounter uint64 + beforeNameCounter uint64 + NameMock mGenericSpecificMockName[T] +} + +// NewGenericSpecificMock returns a mock for genericSpecific +func NewGenericSpecificMock[T proto.Message](t minimock.Tester) *GenericSpecificMock[T] { + m := &GenericSpecificMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.NameMock = mGenericSpecificMockName[T]{mock: m} + m.NameMock.callArgs = []*GenericSpecificMockNameParams[T]{} + + return m +} + +type mGenericSpecificMockName[T proto.Message] struct { + mock *GenericSpecificMock[T] + defaultExpectation *GenericSpecificMockNameExpectation[T] + expectations []*GenericSpecificMockNameExpectation[T] + + callArgs []*GenericSpecificMockNameParams[T] + mutex sync.RWMutex +} + +// GenericSpecificMockNameExpectation specifies expectation struct of the genericSpecific.Name +type GenericSpecificMockNameExpectation[T proto.Message] struct { + mock *GenericSpecificMock[T] + params *GenericSpecificMockNameParams[T] + + Counter uint64 +} + +// GenericSpecificMockNameParams contains parameters of the genericSpecific.Name +type GenericSpecificMockNameParams[T proto.Message] struct { + t1 T +} + +// Expect sets up expected params for genericSpecific.Name +func (mmName *mGenericSpecificMockName[T]) Expect(t1 T) *mGenericSpecificMockName[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericSpecificMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericSpecificMockNameExpectation[T]{} + } + + mmName.defaultExpectation.params = &GenericSpecificMockNameParams[T]{t1} + for _, e := range mmName.expectations { + if minimock.Equal(e.params, mmName.defaultExpectation.params) { + mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) + } + } + + return mmName +} + +// Inspect accepts an inspector function that has same arguments as the genericSpecific.Name +func (mmName *mGenericSpecificMockName[T]) Inspect(f func(t1 T)) *mGenericSpecificMockName[T] { + if mmName.mock.inspectFuncName != nil { + mmName.mock.t.Fatalf("Inspect function is already set for GenericSpecificMock.Name") + } + + mmName.mock.inspectFuncName = f + + return mmName +} + +// Return sets up results that will be returned by genericSpecific.Name +func (mmName *mGenericSpecificMockName[T]) Return() *GenericSpecificMock[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericSpecificMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericSpecificMockNameExpectation[T]{mock: mmName.mock} + } + + return mmName.mock +} + +// Set uses given function f to mock the genericSpecific.Name method +func (mmName *mGenericSpecificMockName[T]) Set(f func(t1 T)) *GenericSpecificMock[T] { + if mmName.defaultExpectation != nil { + mmName.mock.t.Fatalf("Default expectation is already set for the genericSpecific.Name method") + } + + if len(mmName.expectations) > 0 { + mmName.mock.t.Fatalf("Some expectations are already set for the genericSpecific.Name method") + } + + mmName.mock.funcName = f + return mmName.mock +} + +// Name implements genericSpecific +func (mmName *GenericSpecificMock[T]) Name(t1 T) { + mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) + defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + + if mmName.inspectFuncName != nil { + mmName.inspectFuncName(t1) + } + + mm_params := &GenericSpecificMockNameParams[T]{t1} + + // Record call args + mmName.NameMock.mutex.Lock() + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.mutex.Unlock() + + for _, e := range mmName.NameMock.expectations { + if minimock.Equal(e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmName.NameMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) + mm_want := mmName.NameMock.defaultExpectation.params + mm_got := GenericSpecificMockNameParams[T]{t1} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmName.t.Errorf("GenericSpecificMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmName.funcName != nil { + mmName.funcName(t1) + return + } + mmName.t.Fatalf("Unexpected call to GenericSpecificMock.Name. %v", t1) + +} + +// NameAfterCounter returns a count of finished GenericSpecificMock.Name invocations +func (mmName *GenericSpecificMock[T]) NameAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.afterNameCounter) +} + +// NameBeforeCounter returns a count of GenericSpecificMock.Name invocations +func (mmName *GenericSpecificMock[T]) NameBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmName.beforeNameCounter) +} + +// Calls returns a list of arguments used in each call to GenericSpecificMock.Name. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmName *mGenericSpecificMockName[T]) Calls() []*GenericSpecificMockNameParams[T] { + mmName.mutex.RLock() + + argCopy := make([]*GenericSpecificMockNameParams[T], len(mmName.callArgs)) + copy(argCopy, mmName.callArgs) + + mmName.mutex.RUnlock() + + return argCopy +} + +// MinimockNameDone returns true if the count of the Name invocations corresponds +// the number of defined expectations +func (m *GenericSpecificMock[T]) MinimockNameDone() bool { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + return false + } + return true +} + +// MinimockNameInspect logs each unmet expectation +func (m *GenericSpecificMock[T]) MinimockNameInspect() { + for _, e := range m.NameMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to GenericSpecificMock.Name with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation.params == nil { + m.t.Error("Expected call to GenericSpecificMock.Name") + } else { + m.t.Errorf("Expected call to GenericSpecificMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + m.t.Error("Expected call to GenericSpecificMock.Name") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *GenericSpecificMock[T]) MinimockFinish() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *GenericSpecificMock[T]) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *GenericSpecificMock[T]) minimockDone() bool { + done := true + return done && + m.MinimockNameDone() +} diff --git a/tests/types.go b/tests/types.go index c2af807..493bd0d 100644 --- a/tests/types.go +++ b/tests/types.go @@ -1,6 +1,8 @@ // Package tests contains tests for minimock tool and demonstrates minimock features package tests +import "google.golang.org/protobuf/proto" + type ( //Formatter interface is used to test code generated by minimock Formatter interface { @@ -23,4 +25,38 @@ type ( genericIn[T any] interface { Name(T) } + + // following types reference some specific constraints on generic types + // These are not generated properly with minimock v3.1.1 and below + + // Reference a specific type + genericSpecific[T proto.Message] interface { + Name(T) + } + + // Reference a single type as a simple union + simpleUnion interface { + int + } + + // Reference a composite union of multiple types + complexUnion interface { + int | float64 + } + + genericSimpleUnion[T simpleUnion] interface { + Name(T) + } + + genericComplexUnion[T complexUnion] interface { + Name(T) + } + + genericInlineUnion[T int | float64] interface { + Name(T) + } + + genericInlineUnionWithManyTypes[T int | float64 | string] interface { + Name(T) + } )