github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/isc/test/yaml_test.go (about) 1 package test 2 3 import ( 4 "fmt" 5 "github.com/isyscore/isc-gobase/isc" 6 "github.com/isyscore/isc-gobase/test" 7 "github.com/magiconair/properties" 8 "github.com/rs/zerolog/log" 9 "os" 10 "strings" 11 "testing" 12 ) 13 14 func TestMapToProperties1(t *testing.T) { 15 dataMap := map[string]any{} 16 dataMap["a"] = 12 17 dataMap["b"] = 13 18 dataMap["c"] = 14 19 dataMap["c"] = "" 20 21 act, err := isc.MapToProperties(dataMap) 22 if err != nil { 23 log.Printf("转换错误:%v", err) 24 return 25 } 26 expect := "a=12\nb=13\nc=14\n" 27 // 顺序不固定,不断言 28 //Equal(t, act, expect) 29 t.Log(act, expect) 30 } 31 32 func TestMapToProperties2(t *testing.T) { 33 dataMap := map[string]any{} 34 dataMap["a"] = 12 35 dataMap["b"] = 13 36 dataMap["c"] = 14 37 38 innerMap1 := map[string]any{} 39 innerMap1["a"] = "inner1" 40 innerMap1["b"] = "inner2" 41 innerMap1["c"] = "inner3" 42 dataMap["d"] = innerMap1 43 44 // 顺序不固定,无法测试 45 //act, err := gole.MapToProperties(dataMap) 46 //if err != nil { 47 // log.Printf("转换:%v", err) 48 // return 49 //} 50 //expect := "a=12\nb=13\nc=14\nd.a=inner1\nd.b=inner2\nd.c=inner3" 51 //Equal(t, act, expect) 52 } 53 54 func TestMapToProperties3(t *testing.T) { 55 dataMap := map[string]any{} 56 dataMap["a"] = 12 57 dataMap["b"] = 13 58 dataMap["c"] = 14 59 60 innerMap1 := map[string]any{} 61 innerMap1["a"] = "inner1" 62 innerMap1["b"] = "inner2" 63 innerMap1["c"] = "inner3" 64 dataMap["d"] = innerMap1 65 66 var array []string 67 array = append(array, "a") 68 array = append(array, "b") 69 dataMap["e"] = array 70 71 // 顺序不固定,无法测试 72 //act, err := gole.MapToProperties(dataMap) 73 //if err != nil { 74 // log.Printf("转换:%v", err) 75 // return 76 //} 77 //expect := "a=12\nb=13\nc=14\nd.a=inner1\nd.b=inner2\nd.c=inner3\ne[0]=a\ne[1]=b" 78 //Equal(t, act, expect) 79 } 80 81 func TestMapToProperties4(t *testing.T) { 82 dataMap := map[string]any{} 83 dataMap["a"] = 12 84 dataMap["b"] = 13 85 dataMap["c"] = 14 86 87 innerMap1 := map[string]any{} 88 innerMap1["a"] = "inner1" 89 innerMap1["b"] = "inner2" 90 innerMap1["c"] = "inner3" 91 array := []string{} 92 array = append(array, "a") 93 array = append(array, "b") 94 innerMap1["d"] = array 95 dataMap["d"] = innerMap1 96 97 // 顺序不固定,无法测试 98 //act, err := gole.MapToProperties(dataMap) 99 //if err != nil { 100 // log.Printf("转换:%v", err) 101 // return 102 //} 103 //expect := "a=12\nb=13\nc=14\nd.a=inner1\nd.b=inner2\nd.c=inner3\nd.d[0]=a\nd.d[1]=b" 104 //Equal(t, act, expect) 105 } 106 107 func TestYamlToMap(t *testing.T) { 108 yamlToMapTest(t, "./resources/yml/base.yml") 109 yamlToMapTest(t, "./resources/yml/base1.yml") 110 yamlToMapTest(t, "./resources/yml/array1.yml") 111 yamlToMapTest(t, "./resources/yml/array2.yml") 112 yamlToMapTest(t, "./resources/yml/array3.yml") 113 yamlToMapTest(t, "./resources/yml/array4.yml") 114 yamlToMapTest(t, "./resources/yml/array5.yml") 115 //yamlToMapTest(t, "./resources/yml/array6.yml") 116 yamlToMapTest(t, "./resources/yml/array7.yml") 117 //yamlToMapTest(t, "./resources/yml/cron.yml") 118 yamlToMapTest(t, "./resources/yml/multi_line.yml") 119 } 120 121 func TestPropertiesToYaml1(t *testing.T) { 122 //propertiesToYamlTest(t, "./resources/properties/base.properties") 123 //propertiesToYamlTest(t, "./resources/properties/base1.properties") 124 //propertiesToYamlTest(t, "./resources/properties/base2.properties") 125 //propertiesToYamlTest(t, "./resources/properties/array1.properties") 126 //propertiesToYamlTest(t, "./resources/properties/array2.properties") 127 //propertiesToYamlTest(t, "./resources/properties/array3.properties") 128 //propertiesToYamlTest(t, "./resources/properties/array4.properties") 129 //propertiesToYamlTest(t, "./resources/properties/array5.properties") 130 //propertiesToYamlTest(t, "./resources/properties/array6.properties") 131 //propertiesToYamlTest(t, "./resources/properties/array7.properties") 132 } 133 134 func TestYamlToKvList1(t *testing.T) { 135 yamlToKvListTest(t, "./resources/yml/base.yml") 136 yamlToKvListTest(t, "./resources/yml/base1.yml") 137 yamlToKvListTest(t, "./resources/yml/base2.yml") 138 yamlToKvListTest(t, "./resources/yml/array1.yml") 139 yamlToKvListTest(t, "./resources/yml/array2.yml") 140 yamlToKvListTest(t, "./resources/yml/array3.yml") 141 yamlToKvListTest(t, "./resources/yml/array4.yml") 142 yamlToKvListTest(t, "./resources/yml/array5.yml") 143 yamlToKvListTest(t, "./resources/yml/array6.yml") 144 yamlToKvListTest(t, "./resources/yml/array7.yml") 145 } 146 147 func TestYamlToPropertiesWithKey(t *testing.T) { 148 yamlToPropertiesWithKeyTest(t, "./resources/yml/base.yml") 149 yamlToPropertiesWithKeyTest(t, "./resources/yml/base1.yml") 150 yamlToPropertiesWithKeyTest(t, "./resources/yml/base2.yml") 151 yamlToPropertiesWithKeyTest(t, "./resources/yml/array1.yml") 152 yamlToPropertiesWithKeyTest(t, "./resources/yml/array2.yml") 153 yamlToPropertiesWithKeyTest(t, "./resources/yml/array3.yml") 154 yamlToPropertiesWithKeyTest(t, "./resources/yml/array4.yml") 155 yamlToPropertiesWithKeyTest(t, "./resources/yml/array5.yml") 156 yamlToPropertiesWithKeyTest(t, "./resources/yml/array6.yml") 157 yamlToPropertiesWithKeyTest(t, "./resources/yml/array7.yml") 158 } 159 160 func TestPropertiesToMap5(t *testing.T) { 161 propertiesToMap(t, "./resources/properties/base.properties") 162 propertiesToMap(t, "./resources/properties/base1.properties") 163 propertiesToMap(t, "./resources/properties/base2.properties") 164 propertiesToMap(t, "./resources/properties/array1.properties") 165 } 166 167 func propertiesToMap(t *testing.T, filePath string) { 168 bytes, err := os.ReadFile(filePath) 169 if err != nil { 170 test.Err(t, err) 171 return 172 } 173 174 expect := strings.TrimSpace(string(bytes)) 175 _, err = isc.PropertiesToMap(expect) 176 if err != nil { 177 log.Printf("转换错误:%v", err) 178 return 179 } 180 } 181 182 func yamlToPropertiesWithKeyTest(t *testing.T, filePath string) { 183 bytes, err := os.ReadFile(filePath) 184 if err != nil { 185 test.Err(t, err) 186 return 187 } 188 189 expect := strings.TrimSpace(string(bytes)) 190 property, err := isc.YamlToPropertiesWithKey("t", expect) 191 if err != nil { 192 log.Printf("转换错误:%v", err) 193 return 194 } 195 196 fmt.Println(property) 197 } 198 199 func yamlToKvListTest(t *testing.T, filePath string) { 200 bytes, err := os.ReadFile(filePath) 201 if err != nil { 202 test.Err(t, err) 203 return 204 } 205 206 expect := strings.TrimSpace(string(bytes)) 207 kvPairs, err := isc.YamlToKvList(expect) 208 if err != nil { 209 log.Printf("转换错误:%v", err) 210 return 211 } 212 213 // 获取实际数据 214 actMap := map[string]string{} 215 for _, pair := range kvPairs { 216 actMap[pair.Left] = pair.Right 217 } 218 219 // 获取标准的数据 220 property, err := isc.YamlToProperties(expect) 221 pro := properties.NewProperties() 222 err = pro.Load([]byte(property), properties.UTF8) 223 if err != nil { 224 log.Printf("转换错误:%v", err) 225 return 226 } 227 resultMap := pro.Map() 228 229 // 数据进行对比 230 for key := range resultMap { 231 actValue, exist := actMap[key] 232 if !exist || actValue != resultMap[key] { 233 t.Errorf("有数据不一致,\n期望:key=%v, value=%v\n实际:key=%v, value=%v\n", key, resultMap[key], key, actMap[key]) 234 } 235 } 236 } 237 238 func yamlToMapTest(t *testing.T, filePath string) { 239 bytes, err := os.ReadFile(filePath) 240 if err != nil { 241 test.Err(t, err) 242 return 243 } 244 expect := strings.TrimSpace(string(bytes)) 245 dataMap, err := isc.YamlToMap(expect) 246 if err != nil { 247 log.Printf("转换错误:%v", err) 248 return 249 } 250 251 value, _ := isc.ObjectToYaml(dataMap) 252 act := strings.TrimSpace(value) 253 test.Equal(t, act, expect) 254 } 255 256 func propertiesToYamlTest(t *testing.T, filePath string) { 257 bytes, err := os.ReadFile(filePath) 258 if err != nil { 259 test.Err(t, err) 260 return 261 } 262 expect := strings.TrimSpace(string(bytes)) 263 yamlContent, err := isc.PropertiesToYaml(expect) 264 //fmt.Println(yamlContent) 265 if err != nil { 266 log.Printf("转换错误:%v", err) 267 return 268 } 269 270 act, err := isc.YamlToProperties(yamlContent) 271 act = strings.TrimSpace(act) 272 test.Equal(t, act, expect) 273 }