2022-07-06 13:58:26 +00:00
|
|
|
package dynamic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIterateFields(t *testing.T) {
|
2022-07-06 14:01:35 +00:00
|
|
|
|
|
|
|
t.Run("basic", func(t *testing.T) {
|
|
|
|
var a = struct {
|
|
|
|
A int
|
|
|
|
B float64
|
|
|
|
C *os.File
|
|
|
|
}{}
|
|
|
|
|
|
|
|
cnt := 0
|
|
|
|
err := IterateFields(&a, func(ft reflect.StructField, fv reflect.Value) error {
|
|
|
|
cnt++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 3, cnt)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("non-ptr", func(t *testing.T) {
|
|
|
|
err := IterateFields(struct{}{}, func(ft reflect.StructField, fv reflect.Value) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2022-07-06 13:58:26 +00:00
|
|
|
})
|
2022-07-06 14:01:35 +00:00
|
|
|
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
err := IterateFields(nil, func(ft reflect.StructField, fv reflect.Value) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2022-07-06 13:58:26 +00:00
|
|
|
}
|