91 lines
1.8 KiB
Go
91 lines
1.8 KiB
Go
package gosimpleconf
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseWithValidConfig(t *testing.T) {
|
|
var err error
|
|
configFileStr := `
|
|
# Some config file
|
|
|
|
foo=bar
|
|
asdf = 1234
|
|
|
|
# Things
|
|
wat.wat= thing
|
|
foo.wat =stuff
|
|
|
|
immaempty=
|
|
|
|
technically valid = haha hmm...
|
|
|
|
#Done
|
|
`
|
|
configFile := strings.NewReader(configFileStr)
|
|
|
|
expectedMap := make(map[string]string)
|
|
expectedMap["foo"] = "bar"
|
|
expectedMap["asdf"] = "1234"
|
|
expectedMap["wat.wat"] = "thing"
|
|
expectedMap["foo.wat"] = "stuff"
|
|
expectedMap["immaempty"] = ""
|
|
expectedMap["technically valid"] = "haha hmm..."
|
|
|
|
parsedMap, err := parse(configFile)
|
|
if err != nil {
|
|
t.Errorf("failed while parsing the file")
|
|
}
|
|
|
|
validateMap(t, parsedMap, expectedMap)
|
|
}
|
|
|
|
func TestParseWithExtraEquals(t *testing.T) {
|
|
var err error
|
|
configFileStr := `
|
|
foo=bar=true
|
|
`
|
|
configFile := strings.NewReader(configFileStr)
|
|
|
|
_, err = parse(configFile)
|
|
if err == nil {
|
|
t.Errorf("parse did not thrown an error when it was supposed to")
|
|
}
|
|
}
|
|
|
|
func TestParseWithNoEquals(t *testing.T) {
|
|
var err error
|
|
configFileStr := `
|
|
Something here with no equals to split on
|
|
`
|
|
configFile := strings.NewReader(configFileStr)
|
|
|
|
_, err = parse(configFile)
|
|
if err == nil {
|
|
t.Errorf("parse did not thrown an error when it was supposed to")
|
|
}
|
|
}
|
|
|
|
func validateMap(t *testing.T, given map[string]string, expected map[string]string) {
|
|
if given == nil {
|
|
t.Errorf("given map was nil")
|
|
}
|
|
|
|
if expected == nil {
|
|
t.Errorf("expected map was nil")
|
|
}
|
|
|
|
expectedLen := len(expected)
|
|
givenLen := len(given)
|
|
if expectedLen != givenLen {
|
|
t.Errorf("size mismatch on maps - expected %v, given %v", expectedLen, givenLen)
|
|
}
|
|
|
|
for k, v := range expected {
|
|
if v != given[k] {
|
|
t.Errorf("incorrect value for key %v - expected %v, given %v", k, expected[k], given[k])
|
|
}
|
|
}
|
|
}
|