diff --git a/doc/demo/demo_test.go b/doc/demo/demo_test.go index c1701c77..db6b7f4b 100644 --- a/doc/demo/demo_test.go +++ b/doc/demo/demo_test.go @@ -18,3 +18,73 @@ func TestFuncMock(t *testing.T) { t.Fatalf("expect MyFunc() to be 'mock func', actual: %s", text) } } + +type Student struct { + Name string + Age int +} + +func NewStudent() *Student { + return &Student{ + Name: "xiaoming", + Age: 18, + } +} + +func (s *Student) GetName() string { + return s.Name +} + +func (s *Student) SetName(name string) { + s.Name = name +} + +func (s *Student) getAge() int { + return s.Age +} + +func TestStructPatch(t *testing.T) { + student := NewStudent() + mock.Patch(student.GetName, func() string { + return "zhangsan" + }) + + mock.Patch(student.getAge, func() int { + return 20 + }) + + name := student.GetName() + if name != "zhangsan" { + t.Fatalf("expect GetName() to be 'zhangsan', actual: %s", name) + } + + age := student.getAge() + if age != 20 { + t.Fatalf("expect getAge() to be '20', actual: %v", age) + } + + mock.Patch(student.SetName, func(name string) { + return + }) + student.SetName("lisi") + + if student.Name != "xiaoming" { + t.Fatalf("expect Name to be 'xiaoming', actual: %s", student.Name) + } +} + +type IStudent interface { + GetName() string +} + +func TestInterfaceStructPatch(t *testing.T) { + var iStudent IStudent + iStudent = NewStudent() + mock.Patch(iStudent.GetName, func() string { + return "zhangsan" + }) + name := iStudent.GetName() + if name != "zhangsan" { + t.Fatalf("expect GetName() to be 'zhangsan', actual: %s", name) + } +} diff --git a/doc/demo/go.mod b/doc/demo/go.mod index 432a71dc..014c9a22 100644 --- a/doc/demo/go.mod +++ b/doc/demo/go.mod @@ -1,5 +1,9 @@ module demo -go 1.21.7 +go 1.18 -require github.com/xhd2015/xgo/runtime v1.0.7 +require github.com/xhd2015/xgo/runtime v1.0.40 + +replace ( + github.com/xhd2015/xgo/runtime => ../../runtime +) \ No newline at end of file diff --git a/doc/demo/go.sum b/doc/demo/go.sum index 2cc95019..2bf6e1d8 100644 --- a/doc/demo/go.sum +++ b/doc/demo/go.sum @@ -1,2 +1,2 @@ -github.com/xhd2015/xgo/runtime v1.0.7 h1:wQxpD7MWoCRaV2TkaIIV4l4mS8DrXUjcSoKOXJyOsZQ= -github.com/xhd2015/xgo/runtime v1.0.7/go.mod h1:Z9uVy/NhTPeR3pvQoOdRvlYnSosJvrM7BySVGPsht6o= +github.com/xhd2015/xgo/runtime v1.0.40 h1:LaVrX3KcbWwJmV7+IOnM71QsNSCCOdw+ipiRYbs0LZs= +github.com/xhd2015/xgo/runtime v1.0.40/go.mod h1:9GBQ2SzJCzpD3T+HRN+2C0TUOGv7qIz4s0mad1xJ8Jo=