From 206cd8c3c91d728c51028646ed0ce79bd432c68e Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 22 Apr 2022 09:24:34 +0300 Subject: [PATCH] util/reflect: improve test coverage Signed-off-by: Vasiliy Tolstov --- util/reflect/path_test.go | 2 +- util/reflect/reflect_test.go | 135 +++++++++++++++++++++++++++++++++++ util/reflect/struct_test.go | 64 ++++++++--------- 3 files changed, 168 insertions(+), 33 deletions(-) create mode 100644 util/reflect/reflect_test.go diff --git a/util/reflect/path_test.go b/util/reflect/path_test.go index 3e3a0ec3..908b18d0 100644 --- a/util/reflect/path_test.go +++ b/util/reflect/path_test.go @@ -4,7 +4,7 @@ import ( "testing" ) -func TestPath(t *testing.T) { +func TestLookup(t *testing.T) { type Nested2 struct { Name string } diff --git a/util/reflect/reflect_test.go b/util/reflect/reflect_test.go new file mode 100644 index 00000000..b475bc08 --- /dev/null +++ b/util/reflect/reflect_test.go @@ -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) + } +} diff --git a/util/reflect/struct_test.go b/util/reflect/struct_test.go index 2906a90e..00da531d 100644 --- a/util/reflect/struct_test.go +++ b/util/reflect/struct_test.go @@ -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)