105 lines
2.3 KiB
Go
105 lines
2.3 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(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 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 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 TestParseExtraEquals(t *testing.T) {
|
|
configFileStr := `
|
|
foo=bar=true
|
|
base64_tacos = dGFjb3M=
|
|
"equals=splits" the key/value, quotes and other =equals= don't matter
|
|
yup=soup
|
|
`
|
|
|
|
configFile := strings.NewReader(configFileStr)
|
|
|
|
expectedMap := make(ConfigMap)
|
|
expectedMap["foo"] = "bar=true"
|
|
expectedMap["base64_tacos"] = "dGFjb3M="
|
|
expectedMap["equals"] = "splits\" the key/value, quotes and other =equals= don't matter"
|
|
expectedMap["yup"] = "soup"
|
|
|
|
configMap, err := parseFile(configFile)
|
|
if err != nil {
|
|
t.Errorf("failed while parsing the file")
|
|
}
|
|
|
|
validateMap(t, configMap, expectedMap)
|
|
}
|