151 lines
3.2 KiB
Go
151 lines
3.2 KiB
Go
package gosimpleconf
|
|
|
|
import (
|
|
"flag"
|
|
"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(ConfigMap)
|
|
expectedMap["foo"] = "bar"
|
|
expectedMap["asdf"] = "1234"
|
|
expectedMap["wat.wat"] = "thing"
|
|
expectedMap["foo.wat"] = "stuff"
|
|
expectedMap["immaempty"] = ""
|
|
expectedMap["technically valid"] = "haha hmm..."
|
|
|
|
parsedMap, err := parseFile(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 = parseFile(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 = parseFile(configFile)
|
|
if err == nil {
|
|
t.Errorf("parse did not thrown an error when it was supposed to")
|
|
}
|
|
}
|
|
|
|
func TestFlagOverrides(t *testing.T) {
|
|
var err error
|
|
configFileStr := `
|
|
foo=bar
|
|
asdf = 1234
|
|
foo.wat =stuff
|
|
immaempty=
|
|
`
|
|
configFile := strings.NewReader(configFileStr)
|
|
|
|
expectedMap := make(ConfigMap)
|
|
expectedMap["foo"] = "bar"
|
|
expectedMap["asdf"] = "1234"
|
|
// expect to override the 'foo.wat' value with a cli flag
|
|
expectedMap["foo.wat"] = "iwasoverridden"
|
|
expectedMap["immaempty"] = ""
|
|
|
|
configMap, err := parseFile(configFile)
|
|
if err != nil {
|
|
t.Errorf("failed while parsing the file")
|
|
}
|
|
|
|
flagMap := SetupFlagOverrides(configMap)
|
|
|
|
err = flag.Set("foo.wat", "iwasoverridden")
|
|
if err != nil {
|
|
t.Errorf("failed to set cli flag override")
|
|
}
|
|
|
|
configMap = ParseFlags(configMap, flagMap)
|
|
|
|
validateMap(t, configMap, expectedMap)
|
|
}
|
|
|
|
func TestParseWithExtraQuotes(t *testing.T) {
|
|
var err error
|
|
configFileStr := `
|
|
some.name = "This is in quotes"
|
|
extra.quotes = ""This will keep the quotes""
|
|
internal.quotes = "Some "thing" is here"
|
|
"This should work too" = shrug I guess
|
|
`
|
|
|
|
configFile := strings.NewReader(configFileStr)
|
|
|
|
expectedMap := make(ConfigMap)
|
|
expectedMap["some.name"] = "This is in quotes"
|
|
expectedMap["extra.quotes"] = "\"This will keep the quotes\""
|
|
expectedMap["internal.quotes"] = "Some \"thing\" is here"
|
|
expectedMap["This should work too"] = "shrug I guess"
|
|
|
|
configMap, err := parseFile(configFile)
|
|
if err != nil {
|
|
t.Errorf("failed while parsing the file")
|
|
}
|
|
|
|
validateMap(t, configMap, expectedMap)
|
|
}
|
|
|
|
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])
|
|
}
|
|
}
|
|
}
|