Skip to content

Commit 69f4104

Browse files
committed
feat: go 1.24 generic type aliases
1 parent 349aac6 commit 69f4104

7 files changed

Lines changed: 1646 additions & 7 deletions

File tree

mockgen/internal/tests/generics/generics.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

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

1111
type Bar[T any, R any] interface {
1212
One(string) string
@@ -30,9 +30,14 @@ type Bar[T any, R any] interface {
3030
Nineteen() AliasType
3131
}
3232

33+
// BarAliasIntString is an alias of a generic type.
34+
type BarAliasIntString = Bar[int, string]
35+
3336
type Foo[T any, R any] struct{}
3437

35-
type Baz[T any] struct{}
38+
type Baz[T any] struct {
39+
V T
40+
}
3641

3742
type Iface[T any] any
3843

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//go:build go1.24
2+
3+
package generics
4+
5+
//go:generate mockgen --build_constraint go1.24 --destination=package_mode/mock_generics_alias_test.go --package=package_mode . BarAliasTString,BarAliasIntR,BarAliasIntBazR,BarAliasIntBazString
6+
7+
// BarAliasTString is a generic alias of a generic with one complete and one
8+
// generic type arg.
9+
type BarAliasTString[T any] = Bar[T, string]
10+
11+
// BarAliasIntR is a generic alias of a generic with a renamed type param.
12+
type BarAliasIntR[Q any] = Bar[int, Q]
13+
14+
// BarAliasIntBazR is a generic alias of a generic alias with a generic type arg.
15+
type BarAliasIntBazR[Q any] = Bar[int, Baz[Q]]
16+
17+
// BarAliasIntBazString is an alias of a generic alias.
18+
type BarAliasIntBazString = BarAliasIntBazR[string]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//go:build go1.24
2+
3+
package package_mode
4+
5+
import (
6+
"testing"
7+
8+
"go.uber.org/mock/gomock"
9+
"go.uber.org/mock/mockgen/internal/tests/generics"
10+
)
11+
12+
func TestMockGenericAliasOfGeneric(t *testing.T) {
13+
ctrl := gomock.NewController(t)
14+
15+
m := NewMockBarAliasIntR[string](ctrl)
16+
m.EXPECT().Three(10).Return("bar")
17+
18+
alias := generics.Bar[int, string](m)
19+
if v := alias.Three(10); v != "bar" {
20+
t.Errorf("Three(10) = %v, want %v", v, "bar")
21+
}
22+
}
23+
24+
func TestMockGenericAliasOfGenericWithRenamedTypeParam(t *testing.T) {
25+
ctrl := gomock.NewController(t)
26+
27+
m := NewMockBarAliasTString[int](ctrl)
28+
m.EXPECT().Three(10).Return("bar")
29+
30+
alias := generics.Bar[int, string](m)
31+
if v := alias.Three(10); v != "bar" {
32+
t.Errorf("Three(10) = %v, want %v", v, "bar")
33+
}
34+
}
35+
36+
func TestMockGenericAliasOfGenericWithGenericTypeArg(t *testing.T) {
37+
ctrl := gomock.NewController(t)
38+
39+
m := NewMockBarAliasIntBazR[string](ctrl)
40+
m.EXPECT().Three(10).Return(generics.Baz[string]{V: "bar"})
41+
42+
alias := generics.Bar[int, generics.Baz[string]](m)
43+
if v := alias.Three(10); v != (generics.Baz[string]{V: "bar"}) {
44+
t.Errorf("Three(10) = %v, want %v", v, "bar")
45+
}
46+
}
47+
48+
func TestMockAliasOfInstatiatedGenericAlias(t *testing.T) {
49+
ctrl := gomock.NewController(t)
50+
51+
m := NewMockBarAliasIntBazString(ctrl)
52+
m.EXPECT().Three(10).Return(generics.Baz[string]{V: "bar"})
53+
54+
alias := generics.Bar[int, generics.Baz[string]](m)
55+
if v := alias.Three(10); v != (generics.Baz[string]{V: "bar"}) {
56+
t.Errorf("Three(10) = %v, want %v", v, "bar")
57+
}
58+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package package_mode
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"go.uber.org/mock/gomock"
8+
"go.uber.org/mock/mockgen/internal/tests/generics"
9+
)
10+
11+
func TestMockEmbeddingIface_One(t *testing.T) {
12+
ctrl := gomock.NewController(t)
13+
14+
m := NewMockEmbeddingIface[int, float64](ctrl)
15+
m.EXPECT().One("foo").Return("bar")
16+
if v := m.One("foo"); v != "bar" {
17+
t.Errorf("One() = %v, want %v", v, "bar")
18+
}
19+
}
20+
21+
func TestMockUniverse_Water(t *testing.T) {
22+
ctrl := gomock.NewController(t)
23+
24+
m := NewMockUniverse[int](ctrl)
25+
m.EXPECT().Water(1024)
26+
m.Water(1024)
27+
}
28+
29+
func TestNewMockGroup_Join(t *testing.T) {
30+
ctrl := gomock.NewController(t)
31+
32+
m := NewMockGroup[generics.Generator[any]](ctrl)
33+
ctx := context.TODO()
34+
m.EXPECT().Join(ctx).Return(nil)
35+
if v := m.Join(ctx); v != nil {
36+
t.Errorf("Join() = %v, want %v", v, nil)
37+
}
38+
}
39+
40+
func TestMockAliasOfInstantiatedGeneric(t *testing.T) {
41+
ctrl := gomock.NewController(t)
42+
43+
m := NewMockBarAliasIntString(ctrl)
44+
m.EXPECT().Three(10).Return("bar")
45+
46+
alias := generics.Bar[int, string](m)
47+
if v := alias.Three(10); v != "bar" {
48+
t.Errorf("Three(10) = %v, want %v", v, "bar")
49+
}
50+
}

0 commit comments

Comments
 (0)