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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions mockgen/internal/tests/generics/generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

//go:generate mockgen --source=generics.go --destination=source/mock_generics_mock.go --package source
//go:generate mockgen --destination=package_mode/mock_test.go --package=package_mode . Bar,Universe,MilkyWay,SolarSystem,Earth,Water
//go:generate mockgen --destination=package_mode/mock_test.go --package=package_mode . Bar,BarAliasIntString,Universe,MilkyWay,SolarSystem,Earth,Water

type Bar[T any, R any] interface {
One(string) string
Expand All @@ -30,9 +30,14 @@ type Bar[T any, R any] interface {
Nineteen() AliasType
}

// BarAliasIntString is an alias of a generic type.
type BarAliasIntString = Bar[int, string]

type Foo[T any, R any] struct{}

type Baz[T any] struct{}
type Baz[T any] struct {
V T
}

type Iface[T any] any

Expand Down
18 changes: 18 additions & 0 deletions mockgen/internal/tests/generics/generics_alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build go1.24

package generics

//go:generate mockgen --build_constraint go1.24 --destination=package_mode/mock_generics_alias_test.go --package=package_mode . BarAliasTString,BarAliasIntR,BarAliasIntBazR,BarAliasIntBazString

// BarAliasTString is a generic alias of a generic with one complete and one
// generic type arg.
type BarAliasTString[T any] = Bar[T, string]

// BarAliasIntR is a generic alias of a generic with a renamed type param.
type BarAliasIntR[Q any] = Bar[int, Q]

// BarAliasIntBazR is a generic alias of a generic alias with a generic type arg.
type BarAliasIntBazR[Q any] = Bar[int, Baz[Q]]

// BarAliasIntBazString is an alias of a generic alias.
type BarAliasIntBazString = BarAliasIntBazR[string]
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//go:build go1.24

package package_mode

import (
"testing"

"go.uber.org/mock/gomock"
"go.uber.org/mock/mockgen/internal/tests/generics"
)

func TestMockGenericAliasOfGeneric(t *testing.T) {
ctrl := gomock.NewController(t)

m := NewMockBarAliasIntR[string](ctrl)
m.EXPECT().Three(10).Return("bar")

alias := generics.Bar[int, string](m)
if v := alias.Three(10); v != "bar" {
t.Errorf("Three(10) = %v, want %v", v, "bar")
}
}

func TestMockGenericAliasOfGenericWithRenamedTypeParam(t *testing.T) {
ctrl := gomock.NewController(t)

m := NewMockBarAliasTString[int](ctrl)
m.EXPECT().Three(10).Return("bar")

alias := generics.Bar[int, string](m)
if v := alias.Three(10); v != "bar" {
t.Errorf("Three(10) = %v, want %v", v, "bar")
}
}

func TestMockGenericAliasOfGenericWithGenericTypeArg(t *testing.T) {
ctrl := gomock.NewController(t)

m := NewMockBarAliasIntBazR[string](ctrl)
m.EXPECT().Three(10).Return(generics.Baz[string]{V: "bar"})

alias := generics.Bar[int, generics.Baz[string]](m)
if v := alias.Three(10); v != (generics.Baz[string]{V: "bar"}) {
t.Errorf("Three(10) = %v, want %v", v, "bar")
}
}

func TestMockAliasOfInstatiatedGenericAlias(t *testing.T) {
ctrl := gomock.NewController(t)

m := NewMockBarAliasIntBazString(ctrl)
m.EXPECT().Three(10).Return(generics.Baz[string]{V: "bar"})

alias := generics.Bar[int, generics.Baz[string]](m)
if v := alias.Three(10); v != (generics.Baz[string]{V: "bar"}) {
t.Errorf("Three(10) = %v, want %v", v, "bar")
}
}
50 changes: 50 additions & 0 deletions mockgen/internal/tests/generics/package_mode/generics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package package_mode

import (
"context"
"testing"

"go.uber.org/mock/gomock"
"go.uber.org/mock/mockgen/internal/tests/generics"
)

func TestMockEmbeddingIface_One(t *testing.T) {
ctrl := gomock.NewController(t)

m := NewMockEmbeddingIface[int, float64](ctrl)
m.EXPECT().One("foo").Return("bar")
if v := m.One("foo"); v != "bar" {
t.Errorf("One() = %v, want %v", v, "bar")
}
}

func TestMockUniverse_Water(t *testing.T) {
ctrl := gomock.NewController(t)

m := NewMockUniverse[int](ctrl)
m.EXPECT().Water(1024)
m.Water(1024)
}

func TestNewMockGroup_Join(t *testing.T) {
ctrl := gomock.NewController(t)

m := NewMockGroup[generics.Generator[any]](ctrl)
ctx := context.TODO()
m.EXPECT().Join(ctx).Return(nil)
if v := m.Join(ctx); v != nil {
t.Errorf("Join() = %v, want %v", v, nil)
}
}

func TestMockAliasOfInstantiatedGeneric(t *testing.T) {
ctrl := gomock.NewController(t)

m := NewMockBarAliasIntString(ctrl)
m.EXPECT().Three(10).Return("bar")

alias := generics.Bar[int, string](m)
if v := alias.Three(10); v != "bar" {
t.Errorf("Three(10) = %v, want %v", v, "bar")
}
}
Loading