From da465bb030272ee58fb19577ad59e74b6b207763 Mon Sep 17 00:00:00 2001 From: huangqian Date: Wed, 23 Feb 2022 00:46:13 +0800 Subject: [PATCH] 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", //} }