-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf_test.go
168 lines (131 loc) · 2.79 KB
/
conf_test.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package xf_conf
import (
"strings"
"testing"
)
func Test_SplitString(t *testing.T) {
str := " aaa bb ccc ddd kk "
noSpace := strings.TrimSpace(str)
arrs := splitSpace(noSpace)
if len(arrs) != 5 {
t.Error("splitSpace error")
}
}
func TestConfigSection_Int(t *testing.T) {
sec1 := loadRedisDefaultSec(t)
tcpBacklog, err := sec1.Int("tcp-backlog")
if err != nil {
t.Error("parse int error")
}
if tcpBacklog != 511 {
t.Error("int case error")
}
}
func TestConfigSection_String(t *testing.T) {
sec1 := loadRedisDefaultSec(t)
logLevel, err := sec1.String("loglevel")
if err != nil {
t.Error("parse string error")
}
if logLevel != "notice" {
t.Errorf("string case error")
}
}
func TestConfigSection_Bool(t *testing.T) {
sec1 := loadRedisDefaultSec(t)
daemonize, err := sec1.Bool("daemonize")
if err != nil {
t.Error("parse bool error")
}
if daemonize {
t.Error("bool case error")
}
}
func TestConfigSection_Strings(t *testing.T) {
sec2 := loadExampleTest1Section(t)
arr1, err := sec2.Strings("arr1")
if err != nil {
t.Error("parse array error")
}
if len(arr1) != 6 {
t.Error("strings case error")
}
var found = false
for _, a := range arr1 {
if a == "8888" {
found = true
}
}
if !found {
t.Error("string case error")
}
}
func TestConfigSection_Uint(t *testing.T) {
sec2 := loadExampleTest1Section(t)
port, err := sec2.Uint("port")
if err != nil {
t.Error("parse uint error")
}
if port != 3306 {
t.Error("uint case error")
}
}
func TestConfigSection_Ip(t *testing.T) {
sec2 := loadExampleTest1Section(t)
ip, err := sec2.Ip("ip")
if err != nil {
t.Error("parse ip error")
}
if ip != "127.0.0.1" {
t.Error("ip case error")
}
}
func loadRedisConfig() (*FileConfig, error) {
conf := New("./examples/redis.conf")
err := conf.Parse()
return conf, err
}
func loadRedisDefaultSec(t *testing.T) *ConfigSection {
conf, err := loadRedisConfig()
if err != nil {
t.Error("parse ")
}
sec1 := conf.DefaultSection()
if sec1 == nil {
t.Error("get default section error")
return nil
}
return sec1
}
func loadExampleConfig() (*FileConfig, error) {
conf2 := New("./examples/example1.conf")
err := conf2.Parse()
return conf2, err
}
func loadExampleTest1Section(t *testing.T) *ConfigSection {
conf, err := loadExampleConfig()
if err != nil {
t.Error("parse ")
}
sec2 := conf.Section("test1")
if sec2 == nil {
t.Error("get sec2 section error")
return nil
}
return sec2
}
func Test_Parse(t *testing.T) {
sec1 := loadRedisDefaultSec(t)
if sec1 == nil {
t.Error("get default section error")
return
}
sec2 := loadExampleTest1Section(t)
if sec2 == nil {
t.Error("get sec2 section error")
return
}
if len(sec2.kvs) != 3 {
t.Error("number of section test1 error")
}
}