mirror of
https://github.com/regclient/regclient.git
synced 2025-04-18 22:44:00 +03:00
I feel like I need to explain, this is all to move the descriptor package. The platform package could not use the predefined errors in types because of a circular dependency from descriptor. The most appropriate way to reorg this is to move descriptor out of the type package since it was more complex than a self contained type. When doing that, type aliases were needed to avoid breaking changes to existing users. Those aliases themselves caused circular dependency loops because of the media types and errors, so those were also pulled out to separate packages. All of the old values were aliased and deprecated, and to fix the linter, those deprecations were fixed by updating the imports... everywhere. Signed-off-by: Brandon Mitchell <git@bmitch.net>
107 lines
1.9 KiB
Go
107 lines
1.9 KiB
Go
package strparse
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/regclient/regclient/types/errs"
|
|
)
|
|
|
|
func TestSplitCSKV(t *testing.T) {
|
|
tt := []struct {
|
|
name string
|
|
str string
|
|
result map[string]string
|
|
err error
|
|
}{
|
|
{
|
|
name: "empty",
|
|
result: map[string]string{},
|
|
},
|
|
{
|
|
name: "single",
|
|
str: "key=value",
|
|
result: map[string]string{
|
|
"key": "value",
|
|
},
|
|
},
|
|
{
|
|
name: "multiple",
|
|
str: "a=123,bcd=456",
|
|
result: map[string]string{
|
|
"a": "123",
|
|
"bcd": "456",
|
|
},
|
|
},
|
|
{
|
|
name: "quote",
|
|
str: `a="123,456",b=789`,
|
|
result: map[string]string{
|
|
"a": "123,456",
|
|
"b": "789",
|
|
},
|
|
},
|
|
{
|
|
name: "escape",
|
|
str: `a\\d=123\,\"\\456,"b\\,=c"="7\\,89"`,
|
|
result: map[string]string{
|
|
"a\\d": `123,"\456`,
|
|
"b\\,=c": "7\\,89",
|
|
},
|
|
},
|
|
{
|
|
name: "noValue",
|
|
str: "a,b",
|
|
result: map[string]string{
|
|
"a": "",
|
|
"b": "",
|
|
},
|
|
},
|
|
{
|
|
name: "errEscapeKey",
|
|
str: "a\\",
|
|
err: errs.ErrParsingFailed,
|
|
},
|
|
{
|
|
name: "errEscapeVal",
|
|
str: "a=x\\",
|
|
err: errs.ErrParsingFailed,
|
|
},
|
|
{
|
|
name: "errQuoteKey",
|
|
str: "a\"",
|
|
err: errs.ErrParsingFailed,
|
|
},
|
|
{
|
|
name: "errQuoteVal",
|
|
str: "a=b\"",
|
|
err: errs.ErrParsingFailed,
|
|
},
|
|
}
|
|
for _, tc := range tt {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
result, err := SplitCSKV(tc.str)
|
|
if tc.err != nil {
|
|
if err == nil {
|
|
t.Errorf("did not fail")
|
|
} else if err.Error() != tc.err.Error() && !errors.Is(err, tc.err) {
|
|
t.Errorf("unexpected error, expected %v, received %v", tc.err, err)
|
|
}
|
|
return
|
|
} else if err != nil {
|
|
t.Fatalf("unexpected error, received %v", err)
|
|
}
|
|
for k, v := range tc.result {
|
|
if result[k] != v {
|
|
t.Errorf("unexpected result for key %s, expected %s, received %s", k, v, result[k])
|
|
}
|
|
}
|
|
for k, v := range result {
|
|
if _, ok := tc.result[k]; !ok {
|
|
t.Errorf("unexpected key, %s = %s", k, v)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|