util/reflect: improve test coverage #124

Merged
vtolstov merged 1 commits from cover into v3 2022-04-22 09:27:03 +03:00
3 changed files with 168 additions and 33 deletions
Showing only changes of commit 206cd8c3c9 - Show all commits

View File

@ -4,7 +4,7 @@ import (
"testing"
)
func TestPath(t *testing.T) {
func TestLookup(t *testing.T) {
type Nested2 struct {
Name string
}

View File

@ -0,0 +1,135 @@
package reflect
import (
"testing"
)
func TestFieldName(t *testing.T) {
src := "SomeVar"
chk := "some_var"
dst := FieldName(src)
if dst != chk {
t.Fatalf("FieldName error %s != %s", src, chk)
}
}
func TestMergeBool(t *testing.T) {
type str struct {
Bool bool `json:"bool"`
}
mp := make(map[string]interface{})
mp["bool"] = "true"
s := &str{}
if err := Merge(s, mp, Tags([]string{"json"})); err != nil {
t.Fatal(err)
}
if !s.Bool {
t.Fatalf("merge bool error: %#+v\n", s)
}
mp["bool"] = "false"
if err := Merge(s, mp, Tags([]string{"json"})); err != nil {
t.Fatal(err)
}
if s.Bool {
t.Fatalf("merge bool error: %#+v\n", s)
}
mp["bool"] = 1
if err := Merge(s, mp, Tags([]string{"json"})); err != nil {
t.Fatal(err)
}
if !s.Bool {
t.Fatalf("merge bool error: %#+v\n", s)
}
}
func TestMergeString(t *testing.T) {
type str struct {
Bool string `json:"bool"`
}
mp := make(map[string]interface{})
mp["bool"] = true
s := &str{}
if err := Merge(s, mp, Tags([]string{"json"})); err != nil {
t.Fatalf("merge with true err: %v", err)
}
if s.Bool != "true" {
t.Fatalf("merge bool error: %#+v\n", s)
}
mp["bool"] = false
if err := Merge(s, mp, Tags([]string{"json"})); err != nil {
t.Fatalf("merge with falst err: %v", err)
}
if s.Bool != "false" {
t.Fatalf("merge bool error: %#+v\n", s)
}
}
func TestMergeNested(t *testing.T) {
type CallReqNested struct {
StringArgs []string `json:"string_args"`
Uint64Args []uint64 `json:"uint64_args"`
Nested *CallReqNested `json:"nested2"`
}
type CallReq struct {
Name string `json:"name"`
Req string `json:"req"`
Arg2 int `json:"arg2"`
Nested *CallReqNested `json:"nested"`
}
dst := &CallReq{
Name: "name_old",
Req: "req_old",
}
mp := make(map[string]interface{})
mp["name"] = "name_new"
mp["req"] = "req_new"
mp["arg2"] = 1
mp["nested.string_args"] = []string{"args1", "args2"}
mp["nested.uint64_args"] = []uint64{1, 2, 3}
mp["nested.nested2.uint64_args"] = []uint64{1, 2, 3}
mp = FlattenMap(mp)
if err := Merge(dst, mp, Tags([]string{"json"})); err != nil {
t.Fatal(err)
}
if dst.Name != "name_new" || dst.Req != "req_new" || dst.Arg2 != 1 {
t.Fatalf("merge error: %#+v", dst)
}
if dst.Nested == nil || len(dst.Nested.Uint64Args) != 3 ||
len(dst.Nested.StringArgs) != 2 || dst.Nested.StringArgs[0] != "args1" ||
len(dst.Nested.Uint64Args) != 3 || dst.Nested.Uint64Args[2] != 3 {
t.Fatalf("merge error: %#+v", dst.Nested)
}
nmp := make(map[string]interface{})
nmp["nested.uint64_args"] = []uint64{4}
nmp = FlattenMap(nmp)
if err := Merge(dst, nmp, SliceAppend(true), Tags([]string{"json"})); err != nil {
t.Fatal(err)
}
if dst.Nested == nil || len(dst.Nested.Uint64Args) != 4 || dst.Nested.Uint64Args[3] != 4 {
t.Fatalf("merge error: %#+v", dst.Nested)
}
}

View File

@ -9,7 +9,34 @@ import (
rutil "go.unistack.org/micro/v3/util/reflect"
)
func TestStructfields(t *testing.T) {
func TestStructFields(t *testing.T) {
type NestedStr struct {
BBB string
CCC int
}
type Str struct {
Name []string `json:"name" codec:"flatten"`
XXX string `json:"xxx"`
Nested NestedStr
}
val := &Str{Name: []string{"first", "second"}, XXX: "ttt", Nested: NestedStr{BBB: "ddd", CCC: 9}}
fields, err := rutil.StructFields(val)
if err != nil {
t.Fatal(err)
}
var ok bool
for _, field := range fields {
if field.Path == "Nested.CCC" {
ok = true
}
}
if !ok {
t.Fatalf("struct fields returns invalid path: %v", fields)
}
}
func TestStructFieldsNested(t *testing.T) {
type NestedConfig struct {
Value string
}
@ -130,34 +157,7 @@ func TestStructFieldsMap(t *testing.T) {
}
}
func TestStructFields(t *testing.T) {
type NestedStr struct {
BBB string
CCC int
}
type Str struct {
Name []string `json:"name" codec:"flatten"`
XXX string `json:"xxx"`
Nested NestedStr
}
val := &Str{Name: []string{"first", "second"}, XXX: "ttt", Nested: NestedStr{BBB: "ddd", CCC: 9}}
fields, err := rutil.StructFields(val)
if err != nil {
t.Fatal(err)
}
var ok bool
for _, field := range fields {
if field.Path == "Nested.CCC" {
ok = true
}
}
if !ok {
t.Fatalf("struct fields returns invalid path: %v", fields)
}
}
func TestStructByPath(t *testing.T) {
func TestStructFieldByPath(t *testing.T) {
type NestedStr struct {
BBB string
CCC int
@ -178,7 +178,7 @@ func TestStructByPath(t *testing.T) {
}
}
func TestStructByTag(t *testing.T) {
func TestStructFieldByTag(t *testing.T) {
type Str struct {
Name []string `json:"name" codec:"flatten"`
}
@ -197,7 +197,7 @@ func TestStructByTag(t *testing.T) {
}
}
func TestStructByName(t *testing.T) {
func TestStructFieldByName(t *testing.T) {
type Str struct {
Name []string `json:"name" codec:"flatten"`
}
@ -260,7 +260,7 @@ func TestURLSliceVars(t *testing.T) {
}
}
func TestURLVars(t *testing.T) {
func TestURLMap(t *testing.T) {
u, err := url.Parse("http://localhost/v1/test/call/my_name?req=key&arg1=arg1&arg2=12345&nested.string_args=str1&nested.string_args=str2&arg2=54321")
if err != nil {
t.Fatal(err)