gf/encoding/gyaml/gyaml_z_unit_test.go

168 lines
3.4 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-04-19 17:04:43 +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.
2019-04-19 17:04:43 +08:00
package gyaml_test
import (
"testing"
2019-07-29 21:01:19 +08:00
2021-11-13 23:23:55 +08:00
"github.com/gogf/gf/v2/encoding/gjson"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/encoding/gyaml"
2021-11-13 23:23:55 +08:00
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/test/gtest"
2019-04-19 17:04:43 +08:00
)
var yamlStr string = `
#即表示url属性值
url: https://goframe.org
2019-04-19 17:04:43 +08:00
#数组即表示server为[a,b,c]
server:
- 120.168.117.21
- 120.168.117.22
#常量
pi: 3.14 #定义一个数值3.14
hasChild: true #定义一个boolean值
name: '你好YAML' #定义一个字符串
`
var yamlErr string = `
[redis]
dd = 11
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
`
func Test_Encode(t *testing.T) {
// Map.
gtest.C(t, func(t *gtest.T) {
b, err := gyaml.Encode(g.Map{
"k": "v",
})
t.AssertNil(err)
t.Assert(string(b), `k: v
`)
})
// Array.
gtest.C(t, func(t *gtest.T) {
b, err := gyaml.Encode([]string{"a", "b", "c"})
t.AssertNil(err)
t.Assert(string(b), `- a
- b
- c
`)
})
}
func Test_EncodeIndent(t *testing.T) {
// Array.
gtest.C(t, func(t *gtest.T) {
b, err := gyaml.EncodeIndent([]string{"a", "b", "c"}, "####")
t.AssertNil(err)
t.Assert(string(b), `####- a
####- b
####- c
`)
})
}
func Test_Decode(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
result, err := gyaml.Decode([]byte(yamlStr))
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
m, ok := result.(map[string]interface{})
2020-03-19 22:56:12 +08:00
t.Assert(ok, true)
t.Assert(m, map[string]interface{}{
"url": "https://goframe.org",
"server": g.Slice{"120.168.117.21", "120.168.117.22"},
"pi": 3.14,
"hasChild": true,
"name": "你好YAML",
})
2019-04-19 17:04:43 +08:00
})
}
func Test_DecodeTo(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
result := make(map[string]interface{})
err := gyaml.DecodeTo([]byte(yamlStr), &result)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(result, map[string]interface{}{
"url": "https://goframe.org",
"server": g.Slice{"120.168.117.21", "120.168.117.22"},
"pi": 3.14,
"hasChild": true,
"name": "你好YAML",
})
2019-04-19 17:04:43 +08:00
})
}
2019-04-19 17:04:43 +08:00
func Test_DecodeError(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-19 17:04:43 +08:00
_, err := gyaml.Decode([]byte(yamlErr))
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-04-19 17:04:43 +08:00
result := make(map[string]interface{})
err = gyaml.DecodeTo([]byte(yamlErr), &result)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-04-19 17:04:43 +08:00
})
}
func Test_DecodeMapToJson(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
data := []byte(`
m:
k: v
`)
v, err := gyaml.Decode(data)
t.Assert(err, nil)
b, err := json.Marshal(v)
t.Assert(err, nil)
t.Assert(b, `{"m":{"k":"v"}}`)
})
}
func Test_ToJson(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-19 17:04:43 +08:00
m := make(map[string]string)
m["yaml"] = yamlStr
res, err := gyaml.Encode(m)
if err != nil {
t.Errorf("encode failed. %v", err)
return
}
jsonyaml, err := gyaml.ToJson(res)
if err != nil {
t.Errorf("ToJson failed. %v", err)
return
}
2021-09-27 23:05:46 +08:00
p := gjson.New(res)
2019-04-19 17:04:43 +08:00
if err != nil {
t.Errorf("parser failed. %v", err)
return
}
expectJson, err := p.ToJson()
if err != nil {
t.Errorf("parser ToJson failed. %v", err)
return
}
2020-03-19 22:56:12 +08:00
t.Assert(jsonyaml, expectJson)
2019-04-19 17:04:43 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-19 17:04:43 +08:00
_, err := gyaml.ToJson([]byte(yamlErr))
if err == nil {
t.Errorf("ToJson failed. %v", err)
return
}
})
}