2010-03-30 17:58:34 +00:00
|
|
|
package conf_test
|
|
|
|
|
|
|
|
import(
|
|
|
|
. "conf"
|
|
|
|
"testing"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const confFile = `
|
|
|
|
[default]
|
|
|
|
host = example.com
|
|
|
|
port = 43
|
|
|
|
compression = on
|
|
|
|
active = false
|
|
|
|
|
|
|
|
[service-1]
|
|
|
|
port = 443
|
2010-03-31 19:39:10 +00:00
|
|
|
url = http://%(host)s/something`
|
2010-03-30 17:58:34 +00:00
|
|
|
|
|
|
|
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{"default", "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"},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuild(t *testing.T) {
|
|
|
|
c, err := ReadConfigBytes([]byte(confFile))
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, element := range testSet {
|
|
|
|
switch i := 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.String())
|
|
|
|
}
|
|
|
|
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.String())
|
|
|
|
}
|
|
|
|
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.String())
|
|
|
|
}
|
|
|
|
if ans != e.answer {
|
|
|
|
t.Error("c.GetBool(\"" + e.section + "\",\"" + e.option + "\") returned incorrect answer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|