gf/encoding/gini/gini_test.go

105 lines
2.4 KiB
Go
Raw Normal View History

2019-08-12 16:53:07 +08:00
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// 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 (
"github.com/gogf/gf/encoding/gini"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/test/gtest"
"testing"
)
var iniContent = `
;注释
aa=bb
[addr]
#注释
ip = 127.0.0.1
port=9001
enable=true
[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["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))
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
2019-08-12 16:53:07 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(iniMap["addr"].(map[string]interface{})["ip"], json.GetString("addr.ip"))
t.Assert(iniMap["addr"].(map[string]interface{})["port"], json.GetString("addr.port"))
t.Assert(iniMap["DBINFO"].(map[string]interface{})["user"], json.GetString("DBINFO.user"))
t.Assert(iniMap["DBINFO"].(map[string]interface{})["type"], json.GetString("DBINFO.type"))
2019-08-12 16:53:07 +08:00
})
}