gf/encoding/gini/gini_z_unit_test.go

108 lines
2.5 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-08-12 16:53:07 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gini_test
import (
2021-11-13 23:23:55 +08:00
"testing"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/encoding/gini"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/test/gtest"
2019-08-12 16:53:07 +08:00
)
var iniContent = `
;注释
aa=bb
[addr]
#注释
ip = 127.0.0.1
port=9001
enable=true
command=/bin/echo "gf=GoFrame"
2019-08-12 16:53:07 +08:00
[DBINFO]
type=mysql
user=root
password=password
[]
呵呵=
`
func TestDecode(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-08-12 16:53:07 +08:00
res, err := gini.Decode([]byte(iniContent))
if err != nil {
gtest.Fatal(err)
}
2020-03-19 22:56:12 +08:00
t.Assert(res["addr"].(map[string]interface{})["ip"], "127.0.0.1")
t.Assert(res["addr"].(map[string]interface{})["port"], "9001")
t.Assert(res["addr"].(map[string]interface{})["command"], `/bin/echo "gf=GoFrame"`)
2020-03-19 22:56:12 +08:00
t.Assert(res["DBINFO"].(map[string]interface{})["user"], "root")
t.Assert(res["DBINFO"].(map[string]interface{})["type"], "mysql")
t.Assert(res["键"].(map[string]interface{})["呵呵"], "值")
2019-08-12 16:53:07 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
errContent := `
a = b
`
_, err := gini.Decode([]byte(errContent))
if err == nil {
gtest.Fatal(err)
}
})
2019-08-12 16:53:07 +08:00
}
func TestEncode(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-08-12 16:53:07 +08:00
iniMap, err := gini.Decode([]byte(iniContent))
if err != nil {
gtest.Fatal(err)
}
iniStr, err := gini.Encode(iniMap)
if err != nil {
gtest.Fatal(err)
}
res, err := gini.Decode(iniStr)
if err != nil {
gtest.Fatal(err)
}
2020-03-19 22:56:12 +08:00
t.Assert(res["addr"].(map[string]interface{})["ip"], "127.0.0.1")
t.Assert(res["addr"].(map[string]interface{})["port"], "9001")
t.Assert(res["DBINFO"].(map[string]interface{})["user"], "root")
t.Assert(res["DBINFO"].(map[string]interface{})["type"], "mysql")
2019-08-12 16:53:07 +08:00
})
}
func TestToJson(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-08-12 16:53:07 +08:00
jsonStr, err := gini.ToJson([]byte(iniContent))
if err != nil {
gtest.Fatal(err)
}
json, err := gjson.LoadContent(jsonStr)
if err != nil {
gtest.Fatal(err)
}
iniMap, err := gini.Decode([]byte(iniContent))
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-08-12 16:53:07 +08:00
2021-09-28 19:04:36 +08:00
t.Assert(iniMap["addr"].(map[string]interface{})["ip"], json.Get("addr.ip").String())
t.Assert(iniMap["addr"].(map[string]interface{})["port"], json.Get("addr.port").String())
t.Assert(iniMap["DBINFO"].(map[string]interface{})["user"], json.Get("DBINFO.user").String())
t.Assert(iniMap["DBINFO"].(map[string]interface{})["type"], json.Get("DBINFO.type").String())
2019-08-12 16:53:07 +08:00
})
}