derision-test / go-mockgen

MIT License
57 stars 7 forks source link

Add type params to surrogate interface #46

Closed efritz closed 1 year ago

efritz commented 1 year ago

Fixes #42.

efritz commented 1 year ago

Contents of _fooermock.go:

// Code generated by go-mockgen 1.3.7; DO NOT EDIT.

package testdata

import "sync"

// MockFooer is a mock implementation of the fooer interface (from the
// package
// github.com/derision-test/go-mockgen/internal/integration/testdata) used
// for unit testing.
type MockFooer[T interface{}] struct {
    // FooFunc is an instance of a mock function object controlling the
    // behavior of the method Foo.
    FooFunc *FooerFooFunc[T]
}

// NewMockFooer creates a new mock of the fooer interface. All methods
// return zero values for all results, unless overwritten.
func NewMockFooer[T interface{}]() *MockFooer[T] {
    return &MockFooer[T]{
        FooFunc: &FooerFooFunc[T]{
            defaultHook: func() (r0 T) {
                return
            },
        },
    }
}

// NewStrictMockFooer creates a new mock of the fooer interface. All methods
// panic on invocation, unless overwritten.
func NewStrictMockFooer[T interface{}]() *MockFooer[T] {
    return &MockFooer[T]{
        FooFunc: &FooerFooFunc[T]{
            defaultHook: func() T {
                panic("unexpected invocation of MockFooer.Foo")
            },
        },
    }
}

// surrogateMockFooer is a copy of the fooer interface (from the package
// github.com/derision-test/go-mockgen/internal/integration/testdata). It is
// redefined here as it is unexported in the source package.
type surrogateMockFooer[T interface{}] interface {
    Foo() T
}

// NewMockFooerFrom creates a new mock of the MockFooer interface. All
// methods delegate to the given implementation, unless overwritten.
func NewMockFooerFrom[T interface{}](i surrogateMockFooer[T]) *MockFooer[T] {
    return &MockFooer[T]{
        FooFunc: &FooerFooFunc[T]{
            defaultHook: i.Foo,
        },
    }
}

// FooerFooFunc describes the behavior when the Foo method of the parent
// MockFooer instance is invoked.
type FooerFooFunc[T interface{}] struct {
    defaultHook func() T
    hooks       []func() T
    history     []FooerFooFuncCall[T]
    mutex       sync.Mutex
}

// Foo delegates to the next hook function in the queue and stores the
// parameter and result values of this invocation.
func (m *MockFooer[T]) Foo() T {
    r0 := m.FooFunc.nextHook()()
    m.FooFunc.appendCall(FooerFooFuncCall[T]{r0})
    return r0
}

// SetDefaultHook sets function that is called when the Foo method of the
// parent MockFooer instance is invoked and the hook queue is empty.
func (f *FooerFooFunc[T]) SetDefaultHook(hook func() T) {
    f.defaultHook = hook
}

// PushHook adds a function to the end of hook queue. Each invocation of the
// Foo method of the parent MockFooer instance invokes the hook at the front
// of the queue and discards it. After the queue is empty, the default hook
// function is invoked for any future action.
func (f *FooerFooFunc[T]) PushHook(hook func() T) {
    f.mutex.Lock()
    f.hooks = append(f.hooks, hook)
    f.mutex.Unlock()
}

// SetDefaultReturn calls SetDefaultHook with a function that returns the
// given values.
func (f *FooerFooFunc[T]) SetDefaultReturn(r0 T) {
    f.SetDefaultHook(func() T {
        return r0
    })
}

// PushReturn calls PushHook with a function that returns the given values.
func (f *FooerFooFunc[T]) PushReturn(r0 T) {
    f.PushHook(func() T {
        return r0
    })
}

func (f *FooerFooFunc[T]) nextHook() func() T {
    f.mutex.Lock()
    defer f.mutex.Unlock()

    if len(f.hooks) == 0 {
        return f.defaultHook
    }

    hook := f.hooks[0]
    f.hooks = f.hooks[1:]
    return hook
}

func (f *FooerFooFunc[T]) appendCall(r0 FooerFooFuncCall[T]) {
    f.mutex.Lock()
    f.history = append(f.history, r0)
    f.mutex.Unlock()
}

// History returns a sequence of FooerFooFuncCall objects describing the
// invocations of this function.
func (f *FooerFooFunc[T]) History() []FooerFooFuncCall[T] {
    f.mutex.Lock()
    history := make([]FooerFooFuncCall[T], len(f.history))
    copy(history, f.history)
    f.mutex.Unlock()

    return history
}

// FooerFooFuncCall is an object that describes an invocation of method Foo
// on an instance of MockFooer.
type FooerFooFuncCall[T interface{}] struct {
    // Result0 is the value of the 1st result returned from this method
    // invocation.
    Result0 T
}

// Args returns an interface slice containing the arguments of this
// invocation.
func (c FooerFooFuncCall[T]) Args() []interface{} {
    return []interface{}{}
}

// Results returns an interface slice containing the results of this
// invocation.
func (c FooerFooFuncCall[T]) Results() []interface{} {
    return []interface{}{c.Result0}
}
sonarcloud[bot] commented 1 year ago

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication