Files
conf/conf_test.go

136 lines
2.1 KiB
Go
Raw Normal View History

2021-03-20 21:10:53 +03:00
package conf
import (
"bytes"
2025-04-09 04:57:40 +03:00
"errors"
"reflect"
"slices"
2021-03-20 21:10:53 +03:00
"testing"
)
2025-04-09 04:57:40 +03:00
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,
},
}
for _, c := range tc {
t.Run(c.name, func(t *testing.T) {
r := bytes.NewReader(c.in)
2021-03-20 21:10:53 +03:00
2025-04-09 04:57:40 +03:00
conf, err := Read(r)
if !errors.Is(err, c.err) {
t.Fatalf("want error: %v have: %v", c.err, err)
}
2021-03-20 21:10:53 +03:00
2025-04-09 04:57:40 +03:00
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")
2025-04-09 02:13:47 +03:00
if err != nil {
t.Fatal(err)
}
2025-04-09 04:57:40 +03:00
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)
}
2021-03-20 21:10:53 +03:00
}
2025-04-09 04:57:40 +03:00
}
func TestKeys(t *testing.T) {
c := Conf{
values: []*kv{
{
k: "1",
v: Value(""),
},
{
k: "2",
v: Value(""),
},
{
k: "3",
v: Value(""),
},
},
2021-03-20 21:10:53 +03:00
}
2025-04-09 04:57:40 +03:00
if !slices.Equal([]string{"1", "2", "3"}, c.Keys()) {
t.Fatal("Keys method returns incorrect values")
2021-03-20 21:10:53 +03:00
}
}