Repository navigation
Expand file tree
/
Copy pathgini_test.go
More file actions
104 lines (87 loc) · 2.42 KB
/
Copy pathgini_test.go
File metadata and controls
104 lines (87 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// 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 gini_test
import (
"github.com/gogf/gf/encoding/gini"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/test/gtest"
"testing"
)
var iniContent = `
;注释
aa=bb
[addr]
#注释
ip = 127.0.0.1
port=9001
enable=true
[DBINFO]
type=mysql
user=root
password=password
[键]
呵呵=值
`
func TestDecode(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
res, err := gini.Decode([]byte(iniContent))
if err != nil {
gtest.Fatal(err)
}
t.Assert(res["addr"].(map[string]interface{})["ip"], "127.0.0.1")
t.Assert(res["addr"].(map[string]interface{})["port"], "9001")
t.Assert(res["DBINFO"].(map[string]interface{})["user"], "root")
t.Assert(res["DBINFO"].(map[string]interface{})["type"], "mysql")
t.Assert(res["键"].(map[string]interface{})["呵呵"], "值")
})
gtest.C(t, func(t *gtest.T) {
errContent := `
a = b
`
_, err := gini.Decode([]byte(errContent))
if err == nil {
gtest.Fatal(err)
}
})
}
func TestEncode(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
iniMap, err := gini.Decode([]byte(iniContent))
if err != nil {
gtest.Fatal(err)
}
iniStr, err := gini.Encode(iniMap)
if err != nil {
gtest.Fatal(err)
}
res, err := gini.Decode(iniStr)
if err != nil {
gtest.Fatal(err)
}
t.Assert(res["addr"].(map[string]interface{})["ip"], "127.0.0.1")
t.Assert(res["addr"].(map[string]interface{})["port"], "9001")
t.Assert(res["DBINFO"].(map[string]interface{})["user"], "root")
t.Assert(res["DBINFO"].(map[string]interface{})["type"], "mysql")
})
}
func TestToJson(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
jsonStr, err := gini.ToJson([]byte(iniContent))
if err != nil {
gtest.Fatal(err)
}
json, err := gjson.LoadContent(jsonStr)
if err != nil {
gtest.Fatal(err)
}
iniMap, err := gini.Decode([]byte(iniContent))
t.Assert(err, nil)
t.Assert(iniMap["addr"].(map[string]interface{})["ip"], json.GetString("addr.ip"))
t.Assert(iniMap["addr"].(map[string]interface{})["port"], json.GetString("addr.port"))
t.Assert(iniMap["DBINFO"].(map[string]interface{})["user"], json.GetString("DBINFO.user"))
t.Assert(iniMap["DBINFO"].(map[string]interface{})["type"], json.GetString("DBINFO.type"))
})
}