Skip to content

Commit

Permalink
demo: add more demo
Browse files Browse the repository at this point in the history
  • Loading branch information
alpha-baby committed Jun 15, 2024
1 parent 44923c7 commit 1a16a85
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
70 changes: 70 additions & 0 deletions doc/demo/demo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
8 changes: 6 additions & 2 deletions doc/demo/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
4 changes: 2 additions & 2 deletions doc/demo/go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit 1a16a85

Please sign in to comment.