From f54d0a339c30f29d4ea1b65b3da7244200d62520 Mon Sep 17 00:00:00 2001 From: huangqian Date: Thu, 24 Feb 2022 21:14:11 +0800 Subject: [PATCH] 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)