Merge pull request #4620 from iabetor/feature-convenient-mock
Propoal: Convenient way to generate mock object
This commit is contained in:
commit
fd84c63c57
@ -1,5 +1,6 @@
|
|||||||
# developing
|
# developing
|
||||||
|
|
||||||
|
- Add: Convenient way to generate mock object [4620](https://github.com/beego/beego/issues/4620)
|
||||||
- Infra: use dependabot to update dependencies. [4623](https://github.com/beego/beego/pull/4623)
|
- Infra: use dependabot to update dependencies. [4623](https://github.com/beego/beego/pull/4623)
|
||||||
- Lint: use golangci-lint. [4619](https://github.com/beego/beego/pull/4619)
|
- Lint: use golangci-lint. [4619](https://github.com/beego/beego/pull/4619)
|
||||||
- Chore: format code. [4615](https://github.com/beego/beego/pull/4615)
|
- Chore: format code. [4615](https://github.com/beego/beego/pull/4615)
|
||||||
|
|||||||
142
core/bean/mock.go
Normal file
142
core/bean/mock.go
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
package bean
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// the mock object must be pointer of struct
|
||||||
|
// the element in mock object can be slices, structures, basic data types, pointers and interface
|
||||||
|
func Mock(v interface{}) (err error) {
|
||||||
|
pv := reflect.ValueOf(v)
|
||||||
|
// the input must be pointer of struct
|
||||||
|
if pv.Kind() != reflect.Ptr || pv.IsNil() {
|
||||||
|
err = fmt.Errorf("not a pointer of struct")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = mock(pv)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func mock(pv reflect.Value) (err error) {
|
||||||
|
pt := pv.Type()
|
||||||
|
for i := 0; i < pt.Elem().NumField(); i++ {
|
||||||
|
ptt := pt.Elem().Field(i)
|
||||||
|
pvv := pv.Elem().FieldByName(ptt.Name)
|
||||||
|
if !pvv.CanSet() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
kt := ptt.Type.Kind()
|
||||||
|
tagValue := ptt.Tag.Get("mock")
|
||||||
|
switch kt {
|
||||||
|
case reflect.Map:
|
||||||
|
continue
|
||||||
|
case reflect.Interface:
|
||||||
|
if pvv.IsNil() { // when interface is nil,can not sure the type
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
pvv.Set(reflect.New(pvv.Elem().Type().Elem()))
|
||||||
|
err = mock(pvv.Elem())
|
||||||
|
case reflect.Ptr:
|
||||||
|
err = mockPtr(pvv, ptt.Type.Elem())
|
||||||
|
case reflect.Struct:
|
||||||
|
err = mock(pvv.Addr())
|
||||||
|
case reflect.Array, reflect.Slice:
|
||||||
|
err = mockSlice(tagValue, pvv)
|
||||||
|
case reflect.String:
|
||||||
|
pvv.SetString(tagValue)
|
||||||
|
case reflect.Bool:
|
||||||
|
err = mockBool(tagValue, pvv)
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
|
value, e := strconv.ParseInt(tagValue, 10, 64)
|
||||||
|
if e != nil || pvv.OverflowInt(value) {
|
||||||
|
err = fmt.Errorf("the value:%s is invalid", tagValue)
|
||||||
|
}
|
||||||
|
pvv.SetInt(value)
|
||||||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||||
|
value, e := strconv.ParseUint(tagValue, 10, 64)
|
||||||
|
if e != nil || pvv.OverflowUint(value) {
|
||||||
|
err = fmt.Errorf("the value:%s is invalid", tagValue)
|
||||||
|
}
|
||||||
|
pvv.SetUint(value)
|
||||||
|
case reflect.Float32, reflect.Float64:
|
||||||
|
value, e := strconv.ParseFloat(tagValue, pvv.Type().Bits())
|
||||||
|
if e != nil || pvv.OverflowFloat(value) {
|
||||||
|
err = fmt.Errorf("the value:%s is invalid", tagValue)
|
||||||
|
}
|
||||||
|
pvv.SetFloat(value)
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// mock slice value
|
||||||
|
func mockSlice(tagValue string, pvv reflect.Value) (err error) {
|
||||||
|
if tagValue == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sliceMetas := strings.Split(tagValue, ":")
|
||||||
|
if len(sliceMetas) != 2 || sliceMetas[0] != "length" {
|
||||||
|
err = fmt.Errorf("the value:%s is invalid", tagValue)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
length, e := strconv.Atoi(sliceMetas[1])
|
||||||
|
if e != nil {
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
|
sliceType := reflect.SliceOf(pvv.Type().Elem()) // get slice type
|
||||||
|
itemType := sliceType.Elem() // get the type of item in slice
|
||||||
|
value := reflect.MakeSlice(sliceType, 0, length)
|
||||||
|
newSliceValue := make([]reflect.Value, 0, length)
|
||||||
|
for k := 0; k < length; k++ {
|
||||||
|
itemValue := reflect.New(itemType).Elem()
|
||||||
|
// if item in slice is struct or pointer,must set zero value
|
||||||
|
switch itemType.Kind() {
|
||||||
|
case reflect.Struct:
|
||||||
|
err = mock(itemValue.Addr())
|
||||||
|
case reflect.Ptr:
|
||||||
|
if itemValue.IsNil() {
|
||||||
|
itemValue.Set(reflect.New(itemType.Elem()))
|
||||||
|
if e := mock(itemValue); e != nil {
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newSliceValue = append(newSliceValue, itemValue)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
value = reflect.Append(value, newSliceValue...)
|
||||||
|
pvv.Set(value)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// mock bool value
|
||||||
|
func mockBool(tagValue string, pvv reflect.Value) (err error) {
|
||||||
|
switch tagValue {
|
||||||
|
case "true":
|
||||||
|
pvv.SetBool(true)
|
||||||
|
case "false":
|
||||||
|
pvv.SetBool(false)
|
||||||
|
default:
|
||||||
|
err = fmt.Errorf("the value:%s is invalid", tagValue)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// mock pointer
|
||||||
|
func mockPtr(pvv reflect.Value, ptt reflect.Type) (err error) {
|
||||||
|
if pvv.IsNil() {
|
||||||
|
pvv.Set(reflect.New(ptt)) // must set nil value to zero value
|
||||||
|
}
|
||||||
|
err = mock(pvv)
|
||||||
|
return
|
||||||
|
}
|
||||||
74
core/bean/mock_test.go
Normal file
74
core/bean/mock_test.go
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
package bean
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMock(t *testing.T) {
|
||||||
|
type MockSubSubObject struct {
|
||||||
|
A int `mock:"20"`
|
||||||
|
}
|
||||||
|
type MockSubObjectAnoy struct {
|
||||||
|
Anoy int `mock:"20"`
|
||||||
|
}
|
||||||
|
type MockSubObject struct {
|
||||||
|
A bool `mock:"true"`
|
||||||
|
B MockSubSubObject
|
||||||
|
}
|
||||||
|
type MockObject struct {
|
||||||
|
A string `mock:"aaaaa"`
|
||||||
|
B int8 `mock:"10"`
|
||||||
|
C []*MockSubObject `mock:"length:2"`
|
||||||
|
D bool `mock:"true"`
|
||||||
|
E *MockSubObject
|
||||||
|
F []int `mock:"length:3"`
|
||||||
|
G InterfaceA
|
||||||
|
H InterfaceA
|
||||||
|
MockSubObjectAnoy
|
||||||
|
}
|
||||||
|
m := &MockObject{G: &ImplA{}}
|
||||||
|
err := Mock(m)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("mock failed: %v", err)
|
||||||
|
}
|
||||||
|
if m.A != "aaaaa" || m.B != 10 || m.C[1].B.A != 20 ||
|
||||||
|
!m.E.A || m.E.B.A != 20 || !m.D || len(m.F) != 3 {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
_, ok := m.G.(*ImplA)
|
||||||
|
if !ok {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
_, ok = m.G.(*ImplB)
|
||||||
|
if ok {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
_, ok = m.H.(*ImplA)
|
||||||
|
if ok {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if m.Anoy != 20 {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type InterfaceA interface {
|
||||||
|
Item()
|
||||||
|
}
|
||||||
|
|
||||||
|
type ImplA struct {
|
||||||
|
A string `mock:"aaa"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *ImplA) Item() {
|
||||||
|
fmt.Println("implA")
|
||||||
|
}
|
||||||
|
|
||||||
|
type ImplB struct {
|
||||||
|
B string `mock:"bbb"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *ImplB) Item() {
|
||||||
|
fmt.Println("implB")
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user