Files
conf/util_test.go
2025-04-20 21:05:14 +03:00

210 lines
3.0 KiB
Go

package conf
import "testing"
func Test_trim(t *testing.T) {
tc := []struct {
name string
in string
want string
}{
{
name: "empty",
in: "",
want: "",
},
{
name: "spaces",
in: " text ",
want: "text",
},
{
name: "tabs",
in: "\ttext\t",
want: "text",
},
{
name: "newline",
in: "text\n",
want: "text",
},
{
name: "newline",
in: "text\r",
want: "text",
},
{
name: "newline",
in: "text\r\n",
want: "text",
},
{
name: "mixed",
in: " \t text\n\r",
want: "text",
},
}
for _, c := range tc {
t.Run(c.name, func(t *testing.T) {
have := trim(c.in)
if have != c.want {
t.Errorf("want: %s, have: %s", have, c.want)
}
})
}
}
func Test_stripComment(t *testing.T) {
tc := []struct {
name string
in string
want string
}{
{
name: "empty",
in: "",
want: "",
},
{
name: "comment",
in: "# some comment",
want: "",
},
{
name: "comment",
in: "#some comment",
want: "",
},
{
name: "comment",
in: "text#some",
want: "text#some",
},
{
name: "comment",
in: "text #some",
want: "text #some",
},
{
name: "comment",
in: " text #some ",
want: " text #some ",
},
}
for _, c := range tc {
t.Run(c.name, func(t *testing.T) {
have := stripComment(c.in)
if have != c.want {
t.Errorf("want: %s, have: %s", have, c.want)
}
})
}
}
func Test_toKV(t *testing.T) {
tc := []struct {
name string
in string
k string
v string
ok bool
}{
{
name: "empty",
in: "",
k: "",
v: "",
ok: false,
},
{
name: "empty value",
in: "key=",
k: "",
v: "",
ok: false,
},
{
name: "empty key",
in: "=value",
k: "",
v: "",
ok: false,
},
{
name: "incorrect separator",
in: "key/value",
k: "",
v: "",
ok: false,
},
{
name: "incorrect separator",
in: "key / value",
k: "",
v: "",
ok: false,
},
{
name: "incorrect separator",
in: "key value",
k: "",
v: "",
ok: false,
},
{
name: "no spaces",
in: "key=value",
k: "key",
v: "value",
ok: true,
},
{
name: "with spaces",
in: "key = value",
k: "key",
v: "value",
ok: true,
},
{
name: "with spaces",
in: " key = value ",
k: "key",
v: "value",
ok: true,
},
{
name: "with tabs",
in: "key\t=\tvalue",
k: "key",
v: "value",
ok: true,
},
{
name: "with spaces and tabs",
in: " key\t=\tvalue ",
k: "key",
v: "value",
ok: true,
},
{
name: "more tabs",
in: "\tkey\t=\tvalue\t",
k: "key",
v: "value",
ok: true,
},
}
for _, c := range tc {
t.Run(c.name, func(t *testing.T) {
k, v, ok := toKV(c.in, separatorKV)
if k != c.k || v != c.v || ok != c.ok {
t.Errorf("want: %s %s %t, have: %s %s %t", c.k, c.v, c.ok, k, v, ok)
}
})
}
}