First step, write a sample of golang object
package obj
import "log"
func NewFarmer() (farmer *Farmer) {
return &Farmer{life: &User{}}
}
func (this *Farmer) habbies() {
this.life.Eat()
this.life.Work()
}
type Farmer struct {
life Life
}
type User struct {
name string
id uint64
working bool
full bool
happy bool
}
func (this *User) Work() {
log.Println("User working.........")
this.working = true
}
func (this *User) Eat() {
this.full = true
}
func (this *User) Happy() {
this.happy = true
}
type Animal struct {
name string
id uint64
working bool
full bool
happy bool
}
func (this *Animal) Work() {
log.Println("Animal working.........")
this.working = true
}
func (this *Animal) Eat() {
this.full = true
}
func (this *Animal) Happy() {
this.happy = true
}
type Life interface {
Work()
Eat()
Happy()
}
Second step: generate test case by use goland
package obj
import "testing"
func TestFarmer_habbies(t *testing.T) {
type fields struct {
life Life
}
tests := []struct {
name string
fields fields
}{
{
"test01",
fields{&User{}},
},
{
"test02",
fields{&Animal{}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
this := &Farmer{
life: tt.fields.life,
}
this.habbies()
})
}
}