-
Notifications
You must be signed in to change notification settings - Fork 1
/
conf_test.go
180 lines (162 loc) · 4.55 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
169
170
171
172
173
174
175
176
177
178
179
180
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package conf
import (
"fmt"
"testing"
)
const confFile = `
[default]
host = example.com
port = 43
compression = on
active = false
float = 2.3
[service-1]
port = 443
[list]
list-1 =
one, one
two
three
list-2 = "one,one", two, three
list-3 = one,one
two
list-4 = 1, 2, 3
list-5 = 1.3, 2.0, 3
list-6 = yes, true, no, 0, n
`
//url = http://%(host)s/something
type stringTest struct {
section string
option string
answer string
}
type stringListTest struct {
section string
option string
answer []string
}
type intTest struct {
section string
option string
answer int
}
type intListTest struct {
section string
option string
answer []int
}
type int64Test struct {
section string
option string
answer int64
}
type int64ListTest struct {
section string
option string
answer []int64
}
type float64Test struct {
section string
option string
answer float64
}
type float64ListTest struct {
section string
option string
answer []float64
}
type boolTest struct {
section string
option string
answer bool
}
type boolListTest struct {
section string
option string
answer []bool
}
var testSet = []interface{}{
stringTest{"", "host", "example.com"},
stringListTest{"list", "list-1", []string{"one, one", "two", "three"}},
intTest{"default", "port", 43},
intListTest{"list", "list-4", []int{1, 2, 3}},
int64Test{"default", "port", 43},
int64ListTest{"list", "list-4", []int64{1, 2, 3}},
float64Test{"default", "float", 2.3},
float64ListTest{"list", "list-5", []float64{1.3, 2.0, 3}},
boolTest{"default", "compression", true},
boolTest{"default", "active", false},
boolListTest{"list", "list-6", []bool{true, true, false, false, false}},
intTest{"service-1", "port", 443},
//stringTest{"service-1", "url", "http://example.com/something"},
}
func TestBuild(t *testing.T) {
c, err := ReadBytes([]byte(confFile))
if err != nil {
t.Error(err)
}
for testnum, element := range testSet {
switch element.(type) {
case stringTest:
e := element.(stringTest)
ans, err := c.String(e.section, e.option)
verify(t, testnum, "c.String", e.section, e.option, ans, e.answer, err)
case stringListTest:
e := element.(stringListTest)
ans, err := c.StringList(e.section, e.option)
verifyList(t, testnum, "c.StringList", e.section, e.option, ans, e.answer, err)
case intTest:
e := element.(intTest)
ans, err := c.Int(e.section, e.option)
verify(t, testnum, "c.Int", e.section, e.option, ans, e.answer, err)
case intListTest:
e := element.(intListTest)
ans, err := c.IntList(e.section, e.option)
verifyList(t, testnum, "c.IntList", e.section, e.option, ans, e.answer, err)
case int64Test:
e := element.(int64Test)
ans, err := c.Int64(e.section, e.option)
verify(t, testnum, "c.Int64", e.section, e.option, ans, e.answer, err)
case int64ListTest:
e := element.(int64ListTest)
ans, err := c.Int64List(e.section, e.option)
verifyList(t, testnum, "c.Int64List", e.section, e.option, ans, e.answer, err)
case float64Test:
e := element.(float64Test)
ans, err := c.Float64(e.section, e.option)
verify(t, testnum, "c.Float64", e.section, e.option, ans, e.answer, err)
case float64ListTest:
e := element.(float64ListTest)
ans, err := c.Float64List(e.section, e.option)
verifyList(t, testnum, "c.Float64List", e.section, e.option, ans, e.answer, err)
case boolTest:
e := element.(boolTest)
ans, err := c.Bool(e.section, e.option)
verify(t, testnum, "c.Bool", e.section, e.option, ans, e.answer, err)
case boolListTest:
e := element.(boolListTest)
ans, err := c.BoolList(e.section, e.option)
verifyList(t, testnum, "c.BoolList", e.section, e.option, ans, e.answer, err)
}
}
}
func verify(t *testing.T, testnum int, testcase, section, option string, output, expected interface{}, err error) {
if err != nil {
t.Fatalf(`%d. %s("%s", "%s") returned error: %v`, testnum, testcase, section, option, err.Error())
}
if output != expected {
t.Fatalf(`%d. %s("%s", "%s"): output %v != %v`, testnum, testcase, section, option, output, expected)
}
}
func verifyList(t *testing.T, testnum int, testcase, section, option string, output, expected interface{}, err error) {
if err != nil {
t.Fatalf(`%d. %s("%s", "%s") returned error: %v`, testnum, testcase, section, option, err.Error())
}
outputF := fmt.Sprintf("%v", output)
expectedF := fmt.Sprintf("%v", expected)
if outputF != expectedF {
t.Fatalf(`%d. %s("%s", "%s"): output %v != %v`, testnum, testcase, section, option, output, expected)
}
}