-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf_test.go
110 lines (93 loc) · 2.49 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
package conf
import (
"strconv"
"testing"
)
const confFile = `
[default]
host = example.com
port = 43
compression = on
active = false
[service-1]
port = 443
`
//url = http://%(host)s/something
type stringtest struct {
section string
option string
answer string
}
type inttest struct {
section string
option string
answer int
}
type booltest struct {
section string
option string
answer bool
}
var testSet = []interface{}{
stringtest{"", "host", "example.com"},
inttest{"default", "port", 43},
booltest{"default", "compression", true},
booltest{"default", "active", false},
inttest{"service-1", "port", 443},
//stringtest{"service-1", "url", "http://example.com/something"},
}
var options struct {
Host string
Port uint
Compression bool
}
func TestBuild(t *testing.T) {
c, err := ReadConfigBytes([]byte(confFile))
if err != nil {
t.Error(err)
}
for _, element := range testSet {
switch element.(type) {
case stringtest:
e := element.(stringtest)
ans, err := c.GetString(e.section, e.option)
if err != nil {
t.Error("c.GetString(\"" + e.section + "\",\"" + e.option + "\") returned error: " + err.Error())
} else if ans != e.answer {
t.Error("c.GetString(\"" + e.section + "\",\"" + e.option + "\") returned incorrect answer: " + ans)
}
case inttest:
e := element.(inttest)
ans, err := c.GetInt(e.section, e.option)
if err != nil {
t.Error("c.GetInt(\"" + e.section + "\",\"" + e.option + "\") returned error: " + err.Error())
} else if ans != e.answer {
t.Error("c.GetInt(\"" + e.section + "\",\"" + e.option + "\") returned incorrect answer: " + strconv.Itoa(ans))
}
case booltest:
e := element.(booltest)
ans, err := c.GetBool(e.section, e.option)
if err != nil {
t.Error("c.GetBool(\"" + e.section + "\",\"" + e.option + "\") returned error: " + err.Error())
} else if ans != e.answer {
t.Error("c.GetBool(\"" + e.section + "\",\"" + e.option + "\") returned incorrect answer")
}
}
}
if 43 != c.Int("port", 123) {
t.Error("c.Int(\"port\") did not return 43")
}
if 123 != c.Int("ports", 123) {
t.Error("c.Int(\"ports\") did not return 123")
}
c.Struct(&options)
if "example.com" != options.Host {
t.Error("options.Host did not equal to 'example.com' string")
}
if 43 != options.Port {
t.Error("options.Port did not equal to int 43")
}
if true != options.Compression {
t.Error("options.Compression did not equal to bool true")
}
}