From 17e48ba9f296402677fe1f1bed46894e91a6a78b Mon Sep 17 00:00:00 2001 From: unknown <12gzk@163.com> Date: Wed, 11 May 2022 01:02:36 +0800 Subject: [PATCH] Add test case for func tojson --- .../gproperties/gproperties_z_unit_test.go | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/encoding/gproperties/gproperties_z_unit_test.go b/encoding/gproperties/gproperties_z_unit_test.go index 160e1f948..405f6dd95 100644 --- a/encoding/gproperties/gproperties_z_unit_test.go +++ b/encoding/gproperties/gproperties_z_unit_test.go @@ -12,6 +12,7 @@ import ( "fmt" "testing" + "github.com/gogf/gf/v2/encoding/gjson" "github.com/gogf/gf/v2/encoding/gproperties" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/test/gtest" @@ -43,7 +44,7 @@ func TestDecode(t *testing.T) { func TestEncode(t *testing.T) { gtest.C(t, func(t *gtest.T) { - decodeStr, err := gproperties.Encode(map[string]interface{}{ + encStr, err := gproperties.Encode(map[string]interface{}{ "sql": g.Map{ "userName": "admin", "password": "123456", @@ -55,6 +56,34 @@ func TestEncode(t *testing.T) { t.Errorf("decode failed. %v", err) return } - fmt.Printf("%v\n", string(decodeStr)) + fmt.Printf("%v\n", string(encStr)) + }) +} + +func TestToJson(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + res, err := gproperties.Encode(map[string]interface{}{ + "sql": g.Map{ + "userName": "admin", + "password": "123456", + }, + "user": "admin", + "no": 123, + }) + fmt.Print(string(res)) + jsonPr, err := gproperties.ToJson(res) + if err != nil { + t.Errorf("ToJson failed. %v", err) + return + } + fmt.Print(string(jsonPr)) + + p := gjson.New(res) + expectJson, err := p.ToJson() + if err != nil { + t.Errorf("parser ToJson failed. %v", err) + return + } + t.Assert(jsonPr, expectJson) }) }