github.com/gogf/gf/v2@v2.7.4/encoding/gini/gini_z_unit_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gini_test 8 9 import ( 10 "testing" 11 12 "github.com/gogf/gf/v2/encoding/gini" 13 "github.com/gogf/gf/v2/encoding/gjson" 14 "github.com/gogf/gf/v2/test/gtest" 15 ) 16 17 var iniContent = ` 18 19 ;注释 20 aa=bb 21 [addr] 22 #注释 23 ip = 127.0.0.1 24 port=9001 25 enable=true 26 command=/bin/echo "gf=GoFrame" 27 28 [DBINFO] 29 type=mysql 30 user=root 31 password=password 32 [键] 33 呵呵=值 34 35 ` 36 37 func TestDecode(t *testing.T) { 38 gtest.C(t, func(t *gtest.T) { 39 res, err := gini.Decode([]byte(iniContent)) 40 if err != nil { 41 gtest.Fatal(err) 42 } 43 t.Assert(res["addr"].(map[string]interface{})["ip"], "127.0.0.1") 44 t.Assert(res["addr"].(map[string]interface{})["port"], "9001") 45 t.Assert(res["addr"].(map[string]interface{})["command"], `/bin/echo "gf=GoFrame"`) 46 t.Assert(res["DBINFO"].(map[string]interface{})["user"], "root") 47 t.Assert(res["DBINFO"].(map[string]interface{})["type"], "mysql") 48 t.Assert(res["键"].(map[string]interface{})["呵呵"], "值") 49 }) 50 51 gtest.C(t, func(t *gtest.T) { 52 errContent := ` 53 a = b 54 ` 55 _, err := gini.Decode([]byte(errContent)) 56 if err == nil { 57 gtest.Fatal(err) 58 } 59 }) 60 } 61 62 func TestEncode(t *testing.T) { 63 gtest.C(t, func(t *gtest.T) { 64 iniMap, err := gini.Decode([]byte(iniContent)) 65 if err != nil { 66 gtest.Fatal(err) 67 } 68 69 iniStr, err := gini.Encode(iniMap) 70 if err != nil { 71 gtest.Fatal(err) 72 } 73 74 res, err := gini.Decode(iniStr) 75 if err != nil { 76 gtest.Fatal(err) 77 } 78 79 t.Assert(res["addr"].(map[string]interface{})["ip"], "127.0.0.1") 80 t.Assert(res["addr"].(map[string]interface{})["port"], "9001") 81 t.Assert(res["DBINFO"].(map[string]interface{})["user"], "root") 82 t.Assert(res["DBINFO"].(map[string]interface{})["type"], "mysql") 83 84 }) 85 } 86 87 func TestToJson(t *testing.T) { 88 gtest.C(t, func(t *gtest.T) { 89 jsonStr, err := gini.ToJson([]byte(iniContent)) 90 if err != nil { 91 gtest.Fatal(err) 92 } 93 94 json, err := gjson.LoadContent(jsonStr) 95 if err != nil { 96 gtest.Fatal(err) 97 } 98 99 iniMap, err := gini.Decode([]byte(iniContent)) 100 t.AssertNil(err) 101 102 t.Assert(iniMap["addr"].(map[string]interface{})["ip"], json.Get("addr.ip").String()) 103 t.Assert(iniMap["addr"].(map[string]interface{})["port"], json.Get("addr.port").String()) 104 t.Assert(iniMap["DBINFO"].(map[string]interface{})["user"], json.Get("DBINFO.user").String()) 105 t.Assert(iniMap["DBINFO"].(map[string]interface{})["type"], json.Get("DBINFO.type").String()) 106 }) 107 }