package conf import ( "bytes" "errors" "reflect" "testing" ) func Test_trim(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: " #some comment", want: "", }, { name: "key value", in: "key = value", want: "key = value", }, { name: "key value with whitespace", in: " key = value ", want: "key = value", }, { name: "key value with whitespace", in: "\tkey = value \t", want: "key = value", }, { name: "key value with whitespace", in: "\tkey = value \t\n", want: "key = value", }, { name: "key value with comment", in: "key = value #comment", want: "key = value", }, { name: "option", in: "option \n", want: "option", }, { name: "option with comment", in: "option # comment \n", want: "option", }, } 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 TestRead(t *testing.T) { tc := []struct { name string in []byte res *Conf err error }{ { name: "empty", in: []byte{}, res: &Conf{ settings: make(map[string]string), options: map[string]struct{}{}, }, err: nil, // if the reader is empty its not out fault }, { name: "single key with spaces", in: []byte("key = value"), res: &Conf{ settings: map[string]string{ "key": "value", }, options: make(map[string]struct{}), }, err: nil, }, { name: "single key no spaces", in: []byte("key=value"), res: &Conf{ settings: map[string]string{ "key": "value", }, options: make(map[string]struct{}), }, err: nil, }, { name: "single key with newline", in: []byte("key=value\n"), res: &Conf{ settings: map[string]string{ "key": "value", }, options: make(map[string]struct{}), }, err: nil, }, { name: "single key with comment and newline", in: []byte("key =value # this is a comment\n"), res: &Conf{ settings: map[string]string{ "key": "value", }, options: make(map[string]struct{}), }, err: nil, }, } for _, c := range tc { t.Run(c.name, func(t *testing.T) { r := bytes.NewReader(c.in) 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) } }) } }