github.com/zhongdalu/gf@v1.0.0/g/encoding/gyaml/gyaml_test.go (about) 1 // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf. 6 package gyaml_test 7 8 import ( 9 "github.com/zhongdalu/gf/g/encoding/gparser" 10 "github.com/zhongdalu/gf/g/encoding/gyaml" 11 "github.com/zhongdalu/gf/g/test/gtest" 12 "testing" 13 ) 14 15 var yamlStr string = ` 16 #即表示url属性值; 17 url: http://www.wolfcode.cn 18 #即表示server.host属性的值; 19 server: 20 host: http://www.wolfcode.cn 21 #数组,即表示server为[a,b,c] 22 server: 23 - 120.168.117.21 24 - 120.168.117.22 25 - 120.168.117.23 26 #常量 27 pi: 3.14 #定义一个数值3.14 28 hasChild: true #定义一个boolean值 29 name: '你好YAML' #定义一个字符串 30 ` 31 32 var yamlErr string = ` 33 # 模板引擎目录 34 viewpath = "/home/www/templates/" 35 # MySQL数据库配置 36 [redis] 37 dd = 11 38 [redis] 39 disk = "127.0.0.1:6379,0" 40 cache = "127.0.0.1:6379,1" 41 ` 42 43 func TestEncode(t *testing.T) { 44 gtest.Case(t, func() { 45 m := make(map[string]string) 46 m["yaml"] = yamlStr 47 res, err := gyaml.Encode(m) 48 if err != nil { 49 t.Errorf("encode failed. %v", err) 50 return 51 } 52 53 p, err := gparser.LoadContent(res) 54 if err != nil { 55 t.Errorf("parser failed. %v", err) 56 return 57 } 58 59 gtest.Assert(p.GetString("yaml"), yamlStr) 60 }) 61 62 } 63 64 func TestDecode(t *testing.T) { 65 gtest.Case(t, func() { 66 m := make(map[string]string) 67 m["yaml"] = yamlStr 68 res, err := gyaml.Encode(m) 69 if err != nil { 70 t.Errorf("encode failed. %v", err) 71 return 72 } 73 74 decodeStr, err := gyaml.Decode(res) 75 if err != nil { 76 t.Errorf("decode failed. %v", err) 77 return 78 } 79 80 gtest.Assert(decodeStr.(map[string]interface{})["yaml"], yamlStr) 81 82 decodeStr1 := make(map[string]interface{}) 83 err = gyaml.DecodeTo(res, &decodeStr1) 84 if err != nil { 85 t.Errorf("decodeTo failed. %v", err) 86 return 87 } 88 gtest.Assert(decodeStr1["yaml"], yamlStr) 89 }) 90 91 gtest.Case(t, func() { 92 _, err := gyaml.Decode([]byte(yamlErr)) 93 if err == nil { 94 t.Errorf("decode failed. %v", err) 95 return 96 } 97 98 decodeStr1 := make(map[string]interface{}) 99 err = gyaml.DecodeTo([]byte(yamlErr), &decodeStr1) 100 if err == nil { 101 t.Errorf("decodeTo failed. %v", err) 102 return 103 } 104 }) 105 } 106 107 func TestToJson(t *testing.T) { 108 gtest.Case(t, func() { 109 m := make(map[string]string) 110 m["yaml"] = yamlStr 111 res, err := gyaml.Encode(m) 112 if err != nil { 113 t.Errorf("encode failed. %v", err) 114 return 115 } 116 117 jsonyaml, err := gyaml.ToJson(res) 118 if err != nil { 119 t.Errorf("ToJson failed. %v", err) 120 return 121 } 122 123 p, err := gparser.LoadContent(res) 124 if err != nil { 125 t.Errorf("parser failed. %v", err) 126 return 127 } 128 expectJson, err := p.ToJson() 129 if err != nil { 130 t.Errorf("parser ToJson failed. %v", err) 131 return 132 } 133 gtest.Assert(jsonyaml, expectJson) 134 }) 135 136 gtest.Case(t, func() { 137 _, err := gyaml.ToJson([]byte(yamlErr)) 138 if err == nil { 139 t.Errorf("ToJson failed. %v", err) 140 return 141 } 142 }) 143 }