From da465bb030272ee58fb19577ad59e74b6b207763 Mon Sep 17 00:00:00 2001 From: huangqian Date: Wed, 23 Feb 2022 00:46:13 +0800 Subject: [PATCH 1/5] Improving gjson Code Coverage --- encoding/gjson/gjson_api.go | 7 +--- .../gjson/gjson_z_example_conversion_test.go | 2 +- encoding/gjson/gjson_z_example_load_test.go | 24 ++++++++++---- encoding/gjson/gjson_z_example_new_test.go | 2 +- encoding/gjson/gjson_z_example_test.go | 32 +++++++++---------- 5 files changed, 36 insertions(+), 31 deletions(-) diff --git a/encoding/gjson/gjson_api.go b/encoding/gjson/gjson_api.go index 6abfc16c4..2985d7e28 100644 --- a/encoding/gjson/gjson_api.go +++ b/encoding/gjson/gjson_api.go @@ -60,12 +60,7 @@ func (j *Json) Get(pattern string, def ...interface{}) *gvar.Var { return nil } - var result *interface{} - if j.vc { - result = j.getPointerByPattern(pattern) - } else { - result = j.getPointerByPatternWithoutViolenceCheck(pattern) - } + result := j.getPointerByPattern(pattern) if result != nil { return gvar.New(*result) } diff --git a/encoding/gjson/gjson_z_example_conversion_test.go b/encoding/gjson/gjson_z_example_conversion_test.go index e2c16c7f0..d16dd5616 100644 --- a/encoding/gjson/gjson_z_example_conversion_test.go +++ b/encoding/gjson/gjson_z_example_conversion_test.go @@ -270,6 +270,6 @@ func ExampleDecodeToJson() { j, _ := gjson.DecodeToJson([]byte(jsonContent)) fmt.Println(j.Map()) - // Output: + // May Output: // map[name:john score:100] } diff --git a/encoding/gjson/gjson_z_example_load_test.go b/encoding/gjson/gjson_z_example_load_test.go index 9891fa8dd..2b6200635 100644 --- a/encoding/gjson/gjson_z_example_load_test.go +++ b/encoding/gjson/gjson_z_example_load_test.go @@ -15,10 +15,14 @@ import ( func ExampleLoad() { jsonFilePath := gdebug.TestDataPath("json", "data1.json") - j, _ := gjson.Load(jsonFilePath) + j, _ := gjson.Load(jsonFilePath, true) fmt.Println(j.Get("name")) fmt.Println(j.Get("score")) + notExistFilePath := gdebug.TestDataPath("json", "data2.json") + j2, _ := gjson.Load(notExistFilePath) + fmt.Println(j2.Get("name")) + // Output: // john // 100 @@ -26,7 +30,7 @@ func ExampleLoad() { func ExampleLoadJson() { jsonContent := `{"name":"john", "score":"100"}` - j, _ := gjson.LoadJson(jsonContent) + j, _ := gjson.LoadJson(jsonContent, true) fmt.Println(j.Get("name")) fmt.Println(j.Get("score")) @@ -41,7 +45,7 @@ func ExampleLoadXml() { john 100 ` - j, _ := gjson.LoadXml(xmlContent) + j, _ := gjson.LoadXml(xmlContent, true) fmt.Println(j.Get("base.name")) fmt.Println(j.Get("base.score")) @@ -56,7 +60,7 @@ func ExampleLoadIni() { name = john score = 100 ` - j, _ := gjson.LoadIni(iniContent) + j, _ := gjson.LoadIni(iniContent, true) fmt.Println(j.Get("base.name")) fmt.Println(j.Get("base.score")) @@ -71,7 +75,7 @@ func ExampleLoadYaml() { name: john score: 100` - j, _ := gjson.LoadYaml(yamlContent) + j, _ := gjson.LoadYaml(yamlContent, true) fmt.Println(j.Get("base.name")) fmt.Println(j.Get("base.score")) @@ -86,7 +90,7 @@ func ExampleLoadToml() { name = "john" score = 100` - j, _ := gjson.LoadToml(tomlContent) + j, _ := gjson.LoadToml(tomlContent, true) fmt.Println(j.Get("base.name")) fmt.Println(j.Get("base.score")) @@ -126,13 +130,15 @@ func ExampleLoadContentType() { 100 ` - j, _ := gjson.LoadContentType("json", jsonContent) + j, _ := gjson.LoadContentType("json", jsonContent, true) x, _ := gjson.LoadContentType("xml", xmlContent) + j1, _ := gjson.LoadContentType("json", "") fmt.Println(j.Get("name")) fmt.Println(j.Get("score")) fmt.Println(x.Get("base.name")) fmt.Println(x.Get("base.score")) + fmt.Println(j1.Get("")) // Output: // john @@ -148,6 +154,8 @@ func ExampleIsValidDataType() { fmt.Println(gjson.IsValidDataType("mp4")) fmt.Println(gjson.IsValidDataType("xsl")) fmt.Println(gjson.IsValidDataType("txt")) + fmt.Println(gjson.IsValidDataType("")) + fmt.Println(gjson.IsValidDataType(".json")) // Output: // true @@ -156,6 +164,8 @@ func ExampleIsValidDataType() { // false // false // false + // false + // true } func ExampleLoad_Xml() { diff --git a/encoding/gjson/gjson_z_example_new_test.go b/encoding/gjson/gjson_z_example_new_test.go index 997cc6974..181664018 100644 --- a/encoding/gjson/gjson_z_example_new_test.go +++ b/encoding/gjson/gjson_z_example_new_test.go @@ -34,7 +34,7 @@ func ExampleNewWithTag() { Score: 100, Title: "engineer", } - j := gjson.NewWithTag(me, "tag") + j := gjson.NewWithTag(me, "tag", true) fmt.Println(j.Get("name")) fmt.Println(j.Get("score")) fmt.Println(j.Get("Title")) diff --git a/encoding/gjson/gjson_z_example_test.go b/encoding/gjson/gjson_z_example_test.go index e4740fd28..c1b749058 100644 --- a/encoding/gjson/gjson_z_example_test.go +++ b/encoding/gjson/gjson_z_example_test.go @@ -590,61 +590,52 @@ func ExampleJson_ToIni() { func ExampleJson_ToIniString() { type BaseInfo struct { Name string - Age int } info := BaseInfo{ Name: "John", - Age: 18, } j := gjson.New(info) IniStr, _ := j.ToIniString() fmt.Println(string(IniStr)) - // May Output: + // Output: //Name=John - //Age=18 } func ExampleJson_MustToIni() { type BaseInfo struct { Name string - Age int } info := BaseInfo{ Name: "John", - Age: 18, } j := gjson.New(info) IniBytes := j.MustToIni() fmt.Println(string(IniBytes)) - // May Output: + // Output: //Name=John - //Age=18 } func ExampleJson_MustToIniString() { type BaseInfo struct { Name string - Age int } info := BaseInfo{ Name: "John", - Age: 18, } j := gjson.New(info) IniStr := j.MustToIniString() fmt.Println(string(IniStr)) - // May Output: + // Output: //Name=John - //Age=18 } func ExampleJson_MarshalJSON() { @@ -758,8 +749,12 @@ func ExampleJson_Interface() { j := gjson.New(info) fmt.Println(j.Interface()) + var nilJ *gjson.Json = nil + fmt.Println(nilJ.Interface()) + // Output: // map[Age:18 Name:John] + // } func ExampleJson_Var() { @@ -807,11 +802,16 @@ func ExampleJson_Get() { }` j, _ := gjson.LoadContent(data) + fmt.Println(j.Get(".")) fmt.Println(j.Get("users")) fmt.Println(j.Get("users.count")) fmt.Println(j.Get("users.array")) + var nilJ *gjson.Json = nil + fmt.Println(nilJ.Get(".")) + // Output: + // {"users":{"array":["John","Ming"],"count":1}} // {"array":["John","Ming"],"count":1} // 1 // ["John","Ming"] @@ -893,10 +893,11 @@ func ExampleJson_Set() { j := gjson.New(info) j.Set("Addr", "ChengDu") + j.Set("Friends.0", "Tom") fmt.Println(j.Var().String()) // Output: - // {"Addr":"ChengDu","Age":18,"Name":"John"} + // {"Addr":"ChengDu","Age":18,"Friends":["Tom"],"Name":"John"} } func ExampleJson_MustSet() { @@ -1091,7 +1092,7 @@ func ExampleJson_Scan() { fmt.Println(info) - // Output: + // May Output: // {john 18} } @@ -1099,12 +1100,11 @@ func ExampleJson_Dump() { data := `{"name":"john","age":"18"}` j, _ := gjson.LoadContent(data) - j.Dump() // May Output: //{ - // "age": "18", // "name": "john", + // "age": "18", //} } From 298aa5f040f0e501085117e56dff5b137afba851 Mon Sep 17 00:00:00 2001 From: huangqian Date: Thu, 24 Feb 2022 20:14:44 +0800 Subject: [PATCH 2/5] Improving gjson Code Coverage --- encoding/gjson/gjson_z_unit_feature_json_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/encoding/gjson/gjson_z_unit_feature_json_test.go b/encoding/gjson/gjson_z_unit_feature_json_test.go index 7a9b0429e..a7d1f89ad 100644 --- a/encoding/gjson/gjson_z_unit_feature_json_test.go +++ b/encoding/gjson/gjson_z_unit_feature_json_test.go @@ -67,6 +67,11 @@ func Test_MapAttributeConvert(t *testing.T) { t.Assert(tx.Title, g.Map{ "l1": "标签1", "l2": "标签2", }) + + j.Dump() + + var nilJ *gjson.Json = nil + nilJ.Dump() }) gtest.C(t, func(t *gtest.T) { From f54d0a339c30f29d4ea1b65b3da7244200d62520 Mon Sep 17 00:00:00 2001 From: huangqian Date: Thu, 24 Feb 2022 21:14:11 +0800 Subject: [PATCH 3/5] Improving gjson Code Coverage --- encoding/gjson/gjson_z_example_load_test.go | 35 ++++++++++++++++++--- encoding/gjson/gjson_z_example_new_test.go | 19 +++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/encoding/gjson/gjson_z_example_load_test.go b/encoding/gjson/gjson_z_example_load_test.go index 2b6200635..3b8a4b2ba 100644 --- a/encoding/gjson/gjson_z_example_load_test.go +++ b/encoding/gjson/gjson_z_example_load_test.go @@ -101,25 +101,50 @@ func ExampleLoadToml() { func ExampleLoadContent() { jsonContent := `{"name":"john", "score":"100"}` + + j, _ := gjson.LoadContent(jsonContent) + + fmt.Println(j.Get("name")) + fmt.Println(j.Get("score")) + + // Output: + // john + // 100 +} + +func ExampleLoadContent_UTF8BOM() { + jsonContent := `{"name":"john", "score":"100"}` + + content := make([]byte, 3, len(jsonContent)+3) + content[0] = 0xEF + content[1] = 0xBB + content[2] = 0xBF + + j, _ := gjson.LoadContent(content) + + fmt.Println(j.Get("name")) + fmt.Println(j.Get("score")) + + // Output: + // john + // 100 +} + +func ExampleLoadContent_Xml() { xmlContent := ` john 100 ` - j, _ := gjson.LoadContent(jsonContent) x, _ := gjson.LoadContent(xmlContent) - fmt.Println(j.Get("name")) - fmt.Println(j.Get("score")) fmt.Println(x.Get("base.name")) fmt.Println(x.Get("base.score")) // Output: // john // 100 - // john - // 100 } func ExampleLoadContentType() { diff --git a/encoding/gjson/gjson_z_example_new_test.go b/encoding/gjson/gjson_z_example_new_test.go index 181664018..65ae2d7ab 100644 --- a/encoding/gjson/gjson_z_example_new_test.go +++ b/encoding/gjson/gjson_z_example_new_test.go @@ -70,6 +70,25 @@ func ExampleNewWithOptions() { // engineer } +func ExampleNewWithOptions_UTF8BOM() { + jsonContent := `{"name":"john", "score":"100"}` + + content := make([]byte, 3, len(jsonContent)+3) + content[0] = 0xEF + content[1] = 0xBB + content[2] = 0xBF + + j := gjson.NewWithOptions(content, gjson.Options{ + Tags: "tag", + }) + fmt.Println(j.Get("name")) + fmt.Println(j.Get("score")) + + // Output: + // john + // 100 +} + func ExampleNew_Xml() { jsonContent := `john100` j := gjson.New(jsonContent) From 3a803ac39fe4863e2ea091dd52b7eec7d9c0ab82 Mon Sep 17 00:00:00 2001 From: huangqian Date: Thu, 24 Feb 2022 21:41:39 +0800 Subject: [PATCH 4/5] fix --- encoding/gjson/gjson_z_example_load_test.go | 1 + encoding/gjson/gjson_z_example_new_test.go | 1 + 2 files changed, 2 insertions(+) diff --git a/encoding/gjson/gjson_z_example_load_test.go b/encoding/gjson/gjson_z_example_load_test.go index 3b8a4b2ba..31f7360c1 100644 --- a/encoding/gjson/gjson_z_example_load_test.go +++ b/encoding/gjson/gjson_z_example_load_test.go @@ -119,6 +119,7 @@ func ExampleLoadContent_UTF8BOM() { content[0] = 0xEF content[1] = 0xBB content[2] = 0xBF + content = append(content, jsonContent...) j, _ := gjson.LoadContent(content) diff --git a/encoding/gjson/gjson_z_example_new_test.go b/encoding/gjson/gjson_z_example_new_test.go index 65ae2d7ab..95e8cd459 100644 --- a/encoding/gjson/gjson_z_example_new_test.go +++ b/encoding/gjson/gjson_z_example_new_test.go @@ -77,6 +77,7 @@ func ExampleNewWithOptions_UTF8BOM() { content[0] = 0xEF content[1] = 0xBB content[2] = 0xBF + content = append(content, jsonContent...) j := gjson.NewWithOptions(content, gjson.Options{ Tags: "tag", From 30be5c5e4988ff58a282b20368124757f98d05b6 Mon Sep 17 00:00:00 2001 From: huangqian Date: Sat, 26 Feb 2022 21:26:30 +0800 Subject: [PATCH 5/5] Improving gjson Code Coverage And Fix --- encoding/gjson/gjson_z_example_load_test.go | 14 ++-- .../gjson/gjson_z_unit_feature_load_test.go | 74 ++++++++++++++++++- .../gjson/gjson_z_unit_feature_struct_test.go | 2 +- 3 files changed, 81 insertions(+), 9 deletions(-) diff --git a/encoding/gjson/gjson_z_example_load_test.go b/encoding/gjson/gjson_z_example_load_test.go index 31f7360c1..efe92e712 100644 --- a/encoding/gjson/gjson_z_example_load_test.go +++ b/encoding/gjson/gjson_z_example_load_test.go @@ -15,7 +15,7 @@ import ( func ExampleLoad() { jsonFilePath := gdebug.TestDataPath("json", "data1.json") - j, _ := gjson.Load(jsonFilePath, true) + j, _ := gjson.Load(jsonFilePath) fmt.Println(j.Get("name")) fmt.Println(j.Get("score")) @@ -30,7 +30,7 @@ func ExampleLoad() { func ExampleLoadJson() { jsonContent := `{"name":"john", "score":"100"}` - j, _ := gjson.LoadJson(jsonContent, true) + j, _ := gjson.LoadJson(jsonContent) fmt.Println(j.Get("name")) fmt.Println(j.Get("score")) @@ -45,7 +45,7 @@ func ExampleLoadXml() { john 100 ` - j, _ := gjson.LoadXml(xmlContent, true) + j, _ := gjson.LoadXml(xmlContent) fmt.Println(j.Get("base.name")) fmt.Println(j.Get("base.score")) @@ -60,7 +60,7 @@ func ExampleLoadIni() { name = john score = 100 ` - j, _ := gjson.LoadIni(iniContent, true) + j, _ := gjson.LoadIni(iniContent) fmt.Println(j.Get("base.name")) fmt.Println(j.Get("base.score")) @@ -75,7 +75,7 @@ func ExampleLoadYaml() { name: john score: 100` - j, _ := gjson.LoadYaml(yamlContent, true) + j, _ := gjson.LoadYaml(yamlContent) fmt.Println(j.Get("base.name")) fmt.Println(j.Get("base.score")) @@ -90,7 +90,7 @@ func ExampleLoadToml() { name = "john" score = 100` - j, _ := gjson.LoadToml(tomlContent, true) + j, _ := gjson.LoadToml(tomlContent) fmt.Println(j.Get("base.name")) fmt.Println(j.Get("base.score")) @@ -156,7 +156,7 @@ func ExampleLoadContentType() { 100 ` - j, _ := gjson.LoadContentType("json", jsonContent, true) + j, _ := gjson.LoadContentType("json", jsonContent) x, _ := gjson.LoadContentType("xml", xmlContent) j1, _ := gjson.LoadContentType("json", "") diff --git a/encoding/gjson/gjson_z_unit_feature_load_test.go b/encoding/gjson/gjson_z_unit_feature_load_test.go index d517e8de9..c4ae26bff 100644 --- a/encoding/gjson/gjson_z_unit_feature_load_test.go +++ b/encoding/gjson/gjson_z_unit_feature_load_test.go @@ -28,11 +28,17 @@ func Test_Load_JSON1(t *testing.T) { t.Assert(j.Get("a.1").Int(), 2) }) // JSON + gtest.C(t, func(t *gtest.T) { + errData := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]`) + _, err := gjson.LoadContentType("json", errData, true) + t.AssertNE(err, nil) + }) + // JSON gtest.C(t, func(t *gtest.T) { path := "test.json" gfile.PutBytes(path, data) defer gfile.Remove(path) - j, err := gjson.Load(path) + j, err := gjson.Load(path, true) t.Assert(err, nil) t.Assert(j.Get("n").String(), "123456789") t.Assert(j.Get("m").Map(), g.Map{"k": "v"}) @@ -68,6 +74,22 @@ func Test_Load_XML(t *testing.T) { t.Assert(j.Get("doc.a.1").Int(), 2) }) // XML + gtest.C(t, func(t *gtest.T) { + j, err := gjson.LoadXml(data, true) + t.Assert(err, nil) + t.Assert(j.Get("doc.n").String(), "123456789") + t.Assert(j.Get("doc.m").Map(), g.Map{"k": "v"}) + t.Assert(j.Get("doc.m.k").String(), "v") + t.Assert(j.Get("doc.a").Slice(), g.Slice{"1", "2", "3"}) + t.Assert(j.Get("doc.a.1").Int(), 2) + }) + // XML + gtest.C(t, func(t *gtest.T) { + errData := []byte(`123v123456789`) + _, err := gjson.LoadContentType("xml", errData, true) + t.AssertNE(err, nil) + }) + // XML gtest.C(t, func(t *gtest.T) { path := "test.xml" gfile.PutBytes(path, data) @@ -121,6 +143,16 @@ m: t.Assert(j.Get("a.1").Int(), 2) }) // YAML + gtest.C(t, func(t *gtest.T) { + j, err := gjson.LoadYaml(data, true) + t.Assert(err, nil) + t.Assert(j.Get("n").String(), "123456789") + t.Assert(j.Get("m").Map(), g.Map{"k": "v"}) + t.Assert(j.Get("m.k").String(), "v") + t.Assert(j.Get("a").Slice(), g.Slice{1, 2, 3}) + t.Assert(j.Get("a.1").Int(), 2) + }) + // YAML gtest.C(t, func(t *gtest.T) { path := "test.yaml" gfile.PutBytes(path, data) @@ -142,6 +174,11 @@ func Test_Load_YAML2(t *testing.T) { t.Assert(err, nil) t.Assert(j.Get("i"), "123456789") }) + gtest.C(t, func(t *gtest.T) { + errData := []byte("i # 123456789") + _, err := gjson.LoadContentType("yaml", errData, true) + t.AssertNE(err, nil) + }) } func Test_Load_TOML1(t *testing.T) { @@ -163,6 +200,16 @@ n = 123456789 t.Assert(j.Get("a.1").Int(), 2) }) // TOML + gtest.C(t, func(t *gtest.T) { + j, err := gjson.LoadToml(data, true) + t.Assert(err, nil) + t.Assert(j.Get("n").String(), "123456789") + t.Assert(j.Get("m").Map(), g.Map{"k": "v"}) + t.Assert(j.Get("m.k").String(), "v") + t.Assert(j.Get("a").Slice(), g.Slice{"1", "2", "3"}) + t.Assert(j.Get("a.1").Int(), 2) + }) + // TOML gtest.C(t, func(t *gtest.T) { path := "test.toml" gfile.PutBytes(path, data) @@ -184,6 +231,11 @@ func Test_Load_TOML2(t *testing.T) { t.Assert(err, nil) t.Assert(j.Get("i"), "123456789") }) + gtest.C(t, func(t *gtest.T) { + errData := []byte("i : 123456789") + _, err := gjson.LoadContentType("toml", errData, true) + t.AssertNE(err, nil) + }) } func Test_Load_Basic(t *testing.T) { @@ -245,6 +297,26 @@ enable=true gtest.Fatal(err) } }) + + gtest.C(t, func(t *gtest.T) { + j, err := gjson.LoadIni(data, true) + if err != nil { + gtest.Fatal(err) + } + + t.Assert(j.Get("addr.ip").String(), "127.0.0.1") + t.Assert(j.Get("addr.port").String(), "9001") + t.Assert(j.Get("addr.enable").String(), "true") + t.Assert(j.Get("DBINFO.type").String(), "mysql") + t.Assert(j.Get("DBINFO.user").String(), "root") + t.Assert(j.Get("DBINFO.password").String(), "password") + }) + + gtest.C(t, func(t *gtest.T) { + errData := []byte("i : 123456789") + _, err := gjson.LoadContentType("ini", errData, true) + t.AssertNE(err, nil) + }) } func Test_Load_YamlWithV3(t *testing.T) { diff --git a/encoding/gjson/gjson_z_unit_feature_struct_test.go b/encoding/gjson/gjson_z_unit_feature_struct_test.go index f6e2b7415..b7c514ff2 100644 --- a/encoding/gjson/gjson_z_unit_feature_struct_test.go +++ b/encoding/gjson/gjson_z_unit_feature_struct_test.go @@ -197,7 +197,7 @@ func Test_Struct1(t *testing.T) { }] }` data := new(UserCollectionAddReq) - j, err := gjson.LoadJson(jsonContent) + j, err := gjson.LoadJson(jsonContent, true) t.Assert(err, nil) err = j.Scan(data) t.Assert(err, nil)