matryer / moq

Interface mocking tool for go generate
http://bit.ly/meetmoq
MIT License
1.98k stars 126 forks source link

Moq ignores package names of arguments of generic types #177

Closed lindebergue closed 1 year ago

lindebergue commented 2 years ago

When mocking a method with a generic type, moq ignores package names of type arguments.

This works:

package example

//go:generate moq -skip-ensure -stub -rm -out mocks.go . Foo:FooMock

type Bar[T any] struct{}

type Foo interface {
    Foo(bar Bar[Type])
}

But this does not:

package example

//go:generate moq -skip-ensure -stub -rm -out mocks.go . Foo:FooMock

import "path/to/pkg"

type Bar[T any] struct{}

type Foo interface {
    Foo(bar Bar[pkg.Type])
}

The generated code instantiates Bar as Bar[Type] instead of Bar[pkg.Type], failing to compile:

// Code generated by moq; DO NOT EDIT.
// github.com/matryer/moq

package example

import (
    "sync"
)

type FooMock struct {
    // FooFunc mocks the Foo method.
    FooFunc func(bar Bar[Type])

    // calls tracks calls to the methods.
    calls struct {
        // Foo holds details about calls to the Foo method.
        Foo []struct {
            // Bar is the bar argument value.
            Bar Bar[Type]
        }
    }
    lockFoo sync.RWMutex
}

I've tried workarounds like dot imports and type aliases, but got the same results. If Bar type is a built-in like map, it works fine. moq version v0.2.7.

matryer commented 2 years ago

Hmm, yeah I guess it's not 'generics ready'. I wonder if you fancy submitting a failing test in a PR?

ben-haverly-cfa commented 1 year ago

Hey, I actually ran into the same issue. Was just curious if there was any movement on this?

xsteadfastx commented 1 year ago

i read the changelog of 0.3.0 that said it got generics support. tested it today. still running into this problem.

xsteadfastx commented 1 year ago

i created a failing test through #177