gf/encoding/gyaml/gyaml_test.go

121 lines
2.6 KiB
Go
Raw Normal View History

2019-04-19 17:04:43 +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 gyaml_test
import (
"testing"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/encoding/gparser"
"github.com/gogf/gf/frame/g"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/encoding/gyaml"
"github.com/gogf/gf/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_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_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
}
p, err := gparser.LoadContent(res)
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
}
})
}