-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmocker_generics_test.go
92 lines (75 loc) · 2.01 KB
/
mocker_generics_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//go:build go1.18
// +build go1.18
// Package mocker_test mock单元测试包
// 泛型相关的mock实现,特性支持参照来源于: https://taoshu.in/go/monkey/generic.html
package mocker_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/suite"
"github.com/tencent/goom"
)
type GT[T any] struct {
}
//go:noinline
func (gt *GT[T]) Hello() T {
var t T
fmt.Println("")
return t
}
//go:noinline
func Hello[T int | string]() T {
var t T
fmt.Println("")
return t
}
// TestUnitGenericsSuite 测试入口
func TestUnitGenericsSuite(t *testing.T) {
// 开启 debug
// 1.可以查看 apply 和 reset 的状态日志
// 2.查看 mock 调用日志
mocker.OpenDebug()
suite.Run(t, new(mockerTestGenericsSuite))
}
type mockerTestGenericsSuite struct {
suite.Suite
}
// TestGenericsMethod 测试泛型方法调用
func (s *mockerTestGenericsSuite) TestGenericsMethod() {
s.Run("success", func() {
myMocker := mocker.Create()
defer myMocker.Reset()
myMocker.Struct(>[string]{}).Method("Hello").
Return("hello")
myMocker.Struct(>[int]{}).Method("Hello").
Return(1)
var gt *GT[string]
s.Equal("hello", gt.Hello(), "foo mock check")
var gt1 *GT[int]
s.Equal(1, gt1.Hello(), "foo mock check")
})
}
// TestGenericsMethodFunc 测试泛型方法函数调用
func (s *mockerTestGenericsSuite) TestGenericsMethodFunc() {
s.Run("success", func() {
myMocker := mocker.Create()
defer myMocker.Reset()
hello1 := (>[string]{}).Hello
myMocker.Func(hello1).Return("hello")
hello2 := (>[int]{}).Hello
myMocker.Func(hello2).Return(1)
s.Equal("hello", hello1(), "foo mock check")
s.Equal(1, hello2(), "foo mock check")
})
}
// TestGenericsFunc 测试泛型函数调用
func (s *mockerTestGenericsSuite) TestGenericsFunc() {
s.Run("success", func() {
myMocker := mocker.Create()
defer myMocker.Reset()
myMocker.Func(Hello[string]).Return("hello")
myMocker.Func(Hello[int]).Return(1)
s.Equal("hello", Hello[string](), "foo mock check")
s.Equal(1, Hello[int](), "foo mock check")
})
}