add UT cases for package gjson

This commit is contained in:
John Guo
2022-01-15 20:25:29 +08:00
parent 58290ec9ea
commit 4943c3a9e0

View File

@ -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