This commit is contained in:
Dmitry Fedotov
2025-04-09 04:57:40 +03:00
parent b88b2fd5e6
commit 6d0e13f05e
11 changed files with 700 additions and 604 deletions

View File

@@ -2,108 +2,134 @@ package conf
import (
"bytes"
"errors"
"reflect"
"slices"
"testing"
)
var testConf = []byte(`port=10000
# removed
two = two words
commas = abc, def, ghi
token=test
bool1=1
bool2=true
func TestRead(t *testing.T) {
tc := []struct {
name string
in []byte
res *Conf
err error
}{
{
name: "empty",
in: []byte{},
res: &Conf{
values: nil, // no reads, so the slice has not been initialized
},
err: nil, // if the reader just returned io.EOF, which isn't an error for us
},
{
name: "single key with spaces",
in: []byte("key = value"),
res: &Conf{
values: []*kv{
{
k: "key",
v: Value("value"),
},
},
},
err: nil,
},
{
name: "single key no spaces",
in: []byte("key=value"),
res: &Conf{
values: []*kv{
{
k: "key",
v: Value("value"),
},
},
},
err: nil,
},
{
name: "single key with newline",
in: []byte("key=value\n"),
res: &Conf{
values: []*kv{
{
k: "key",
v: Value("value"),
},
},
},
err: nil,
},
{
name: "single key with comment and newline",
in: []byte("key =value # this is a comment\n"),
res: &Conf{
values: []*kv{
{
k: "key",
v: Value("value"),
},
},
},
err: nil,
},
}
editor = vim
distance=13.42
floats=0.5,2.37,6
floatswithstring = 0.5, hello, 0.9
false
color`)
for _, c := range tc {
t.Run(c.name, func(t *testing.T) {
r := bytes.NewReader(c.in)
func TestPackage(t *testing.T) {
r := bytes.NewReader(testConf)
c, err := parseReader(r)
conf, err := Read(r)
if !errors.Is(err, c.err) {
t.Fatalf("want error: %v have: %v", c.err, err)
}
if !reflect.DeepEqual(c.res, conf) {
t.Fatalf("want: %+v, have: %+v", c.res, conf)
}
})
}
}
func TestOpen(t *testing.T) {
conf, err := Open("./testdata/test.conf")
if err != nil {
t.Fatal(err)
}
if _, err := c.Get("floatswithstring").Float64Slice(); err == nil {
t.Log("Float64Slice accepting incorrect values")
t.Fail()
}
if _, err := c.Get("floats").Float64Slice(); err != nil {
t.Log("Float64Slice failing on correct values")
t.Fail()
}
if v := c.Get("token").String(); v != "test" {
t.Log("failed finding key value")
t.Fail()
}
if v := c.Get("editor").String(); v != "vim" {
t.Log("failed finding key value")
t.Fail()
}
if v, _ := c.Get("port").Int(); v != 10000 {
t.Log("failed finding int")
t.Fail()
}
if v, _ := c.Get("distance").Float64(); v != 13.42 {
t.Log("failed finding key value")
t.Fail()
}
if c.HasOption("color") != true {
t.Log("failed finding option")
t.Fail()
}
if v, _ := c.Get("bool1").Bool(); v != true {
t.Log("failed finding bool1 value")
t.Fail()
}
if v, _ := c.Get("bool2").Bool(); v != true {
t.Log("failed finding bool2 value")
t.Fail()
}
if v := c.Get("two").String(); v != "two words" {
t.Log("failed finding key value")
t.Fail()
}
if v := c.Get("commas").String(); v != "abc, def, ghi" {
t.Log("failed finding key value")
t.Fail()
}
if v := c.Get("nonexistent").String(); v != "" {
t.Log("returned non-empty string for nonexistent key")
t.Fail()
}
if c.HasOption("removed") {
t.Log("commented out line shows up in config")
t.Fail()
}
splitted := c.Get("commas").StringSlice()
if len(splitted) != 3 {
t.Log("could not split string")
t.Fail()
}
abc := splitted[0]
ghi := splitted[2]
if abc != "abc" || ghi != "ghi" {
t.Log("Split() returned incorrect values")
t.Fail()
}
if c.HasOption("no") != true {
t.Log("should capture one option per line even if line holds two")
t.Fail()
}
if c.HasOption("missedme") == true {
t.Log("should only capture one option per line")
t.Fail()
}
st := Setting{}
if v, err := st.Float64(); v != 0.0 || err == nil {
t.Log("empty string erroneously converts to float")
t.Fail()
}
if def := c.GetDefault("non-existant-key", "myvalue"); def.String() != "myvalue" {
t.Log("GetDefault fails to apply default value")
t.Fail()
const (
key = "key"
value = "value"
)
for _, n := range []string{"1", "2", "3", "4"} {
v := conf.Get(key + n)
if v.String() != value+n {
t.Errorf("want: %s got: %s", value+n, v)
}
}
}
func TestKeys(t *testing.T) {
c := Conf{
values: []*kv{
{
k: "1",
v: Value(""),
},
{
k: "2",
v: Value(""),
},
{
k: "3",
v: Value(""),
},
},
}
if !slices.Equal([]string{"1", "2", "3"}, c.Keys()) {
t.Fatal("Keys method returns incorrect values")
}
}