From 4943c3a9e09243e697f73a2e2e4285ff4e5c0094 Mon Sep 17 00:00:00 2001 From: John Guo Date: Sat, 15 Jan 2022 20:25:29 +0800 Subject: [PATCH] add UT cases for package gjson --- .../gjson/gjson_z_unit_implements_test.go | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/encoding/gjson/gjson_z_unit_implements_test.go b/encoding/gjson/gjson_z_unit_implements_test.go index a29f8244a..7f94d1411 100644 --- a/encoding/gjson/gjson_z_unit_implements_test.go +++ b/encoding/gjson/gjson_z_unit_implements_test.go @@ -17,10 +17,24 @@ import ( ) func TestJson_UnmarshalJSON(t *testing.T) { - data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) + // Json Array gtest.C(t, func(t *gtest.T) { - j := gjson.New(nil) - err := json.UnmarshalUseNumber(data, j) + var ( + data = []byte(`["a", "b", "c"]`) + j = gjson.New(nil) + err = json.UnmarshalUseNumber(data, j) + ) + t.Assert(err, nil) + t.Assert(j.Get(".").String(), `["a","b","c"]`) + t.Assert(j.Get("2").String(), `c`) + }) + // Json Map + gtest.C(t, func(t *gtest.T) { + var ( + data = []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) + j = gjson.New(nil) + err = json.UnmarshalUseNumber(data, j) + ) t.Assert(err, nil) t.Assert(j.Get("n").String(), "123456789") t.Assert(j.Get("m").Map(), g.Map{"k": "v"}) @@ -28,6 +42,7 @@ func TestJson_UnmarshalJSON(t *testing.T) { t.Assert(j.Get("a").Array(), g.Slice{1, 2, 3}) t.Assert(j.Get("a.1").Int(), 2) }) + } func TestJson_UnmarshalValue(t *testing.T) { @@ -35,7 +50,7 @@ func TestJson_UnmarshalValue(t *testing.T) { Name string Json *gjson.Json } - // JSON + // Json Map. gtest.C(t, func(t *gtest.T) { var v *V err := gconv.Struct(g.Map{ @@ -50,6 +65,18 @@ func TestJson_UnmarshalValue(t *testing.T) { t.Assert(v.Json.Get("a").Slice(), g.Slice{1, 2, 3}) t.Assert(v.Json.Get("a.1").Int(), 2) }) + // Json Array. + gtest.C(t, func(t *gtest.T) { + var v *V + err := gconv.Struct(g.Map{ + "name": "john", + "json": `["a", "b", "c"]`, + }, &v) + t.Assert(err, nil) + t.Assert(v.Name, "john") + t.Assert(v.Json.Get(".").String(), `["a","b","c"]`) + t.Assert(v.Json.Get("2").String(), `c`) + }) // Map gtest.C(t, func(t *gtest.T) { var v *V