github.com/gogf/gf/v2@v2.7.4/encoding/gproperties/gproperties_z_unit_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). 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/gogf/gf. 6 7 package gproperties_test 8 9 import ( 10 "fmt" 11 "strings" 12 "testing" 13 14 "github.com/gogf/gf/v2/encoding/gjson" 15 "github.com/gogf/gf/v2/encoding/gproperties" 16 "github.com/gogf/gf/v2/frame/g" 17 "github.com/gogf/gf/v2/test/gtest" 18 ) 19 20 var pStr string = ` 21 # 模板引擎目录 22 viewpath = "/home/www/templates/" 23 # redis数据库配置 24 redis.disk = "127.0.0.1:6379,0" 25 redis.cache = "127.0.0.1:6379,1" 26 #SQL配置 27 sql.mysql.0.type = mysql 28 sql.mysql.0.ip = 127.0.0.1 29 sql.mysql.0.user = root 30 ` 31 32 var errorTests = []struct { 33 input, msg string 34 }{ 35 // unicode literals 36 {"key\\u1 = value", "invalid unicode literal"}, 37 {"key\\u12 = value", "invalid unicode literal"}, 38 {"key\\u123 = value", "invalid unicode literal"}, 39 {"key\\u123g = value", "invalid unicode literal"}, 40 {"key\\u123", "invalid unicode literal"}, 41 42 // circular references 43 {"key=${key}", `circular reference in:\nkey=\$\{key\}`}, 44 {"key1=${key2}\nkey2=${key1}", `circular reference in:\n(key1=\$\{key2\}\nkey2=\$\{key1\}|key2=\$\{key1\}\nkey1=\$\{key2\})`}, 45 46 // malformed expressions 47 {"key=${ke", "malformed expression"}, 48 {"key=valu${ke", "malformed expression"}, 49 } 50 51 func TestDecode(t *testing.T) { 52 gtest.C(t, func(t *gtest.T) { 53 m := make(map[string]interface{}) 54 m["properties"] = pStr 55 res, err := gproperties.Encode(m) 56 if err != nil { 57 t.Errorf("encode failed. %v", err) 58 return 59 } 60 decodeMap, err := gproperties.Decode(res) 61 if err != nil { 62 t.Errorf("decode failed. %v", err) 63 return 64 } 65 t.Assert(decodeMap["properties"], pStr) 66 }) 67 68 gtest.C(t, func(t *gtest.T) { 69 for _, v := range errorTests { 70 _, err := gproperties.Decode(([]byte)(v.input)) 71 if err == nil { 72 t.Errorf("encode should be failed. %v", err) 73 return 74 } 75 t.AssertIN(`Lib magiconair load Properties data failed.`, strings.Split(err.Error(), ":")) 76 } 77 }) 78 } 79 80 func TestEncode(t *testing.T) { 81 gtest.C(t, func(t *gtest.T) { 82 m := make(map[string]interface{}) 83 m["properties"] = pStr 84 res, err := gproperties.Encode(m) 85 if err != nil { 86 t.Errorf("encode failed. %v", err) 87 return 88 } 89 decodeMap, err := gproperties.Decode(res) 90 if err != nil { 91 t.Errorf("decode failed. %v", err) 92 return 93 } 94 t.Assert(decodeMap["properties"], pStr) 95 }) 96 } 97 98 func TestToJson(t *testing.T) { 99 gtest.C(t, func(t *gtest.T) { 100 res, err := gproperties.Encode(map[string]interface{}{ 101 "sql": g.Map{ 102 "userName": "admin", 103 "password": "123456", 104 }, 105 "user": "admin", 106 "no": 123, 107 }) 108 fmt.Print(string(res)) 109 jsonPr, err := gproperties.ToJson(res) 110 if err != nil { 111 t.Errorf("ToJson failed. %v", err) 112 return 113 } 114 fmt.Print(string(jsonPr)) 115 116 p := gjson.New(res) 117 expectJson, err := p.ToJson() 118 if err != nil { 119 t.Errorf("parser ToJson failed. %v", err) 120 return 121 } 122 t.Assert(jsonPr, expectJson) 123 }) 124 gtest.C(t, func(t *gtest.T) { 125 for _, v := range errorTests { 126 _, err := gproperties.ToJson(([]byte)(v.input)) 127 if err == nil { 128 t.Errorf("encode should be failed. %v", err) 129 return 130 } 131 t.AssertIN(`Lib magiconair load Properties data failed.`, strings.Split(err.Error(), ":")) 132 } 133 }) 134 }