feat: test for gone.TagStringParse

This commit is contained in:
dapeng 2024-11-23 18:27:41 +08:00
parent b13651e811
commit 1e80a30b07
2 changed files with 58 additions and 18 deletions

36
help.go
View File

@ -284,6 +284,24 @@ func (p *Preparer) Test(fn any) {
p.testKit().AfterStart(fn).Run()
}
// TagStringParse parse tag string to map
// example: "a=1,b=2" -> map[string]string{"a":"1","b":"2"}
func TagStringParse(conf string) map[string]string {
conf = strings.TrimSpace(conf)
specs := strings.Split(conf, ",")
m := make(map[string]string)
for _, spec := range specs {
spec = strings.TrimSpace(spec)
pairs := strings.Split(spec, "=")
if len(pairs) == 1 && pairs[0] != "" {
m[pairs[0]] = ""
} else if len(pairs) > 1 && pairs[0] != "" {
m[pairs[0]] = pairs[1]
}
}
return m
}
type provider[T any] struct {
Flag
cemetery Cemetery `gone:"*"`
@ -299,24 +317,6 @@ func (p *provider[T]) Suck(conf string, v reflect.Value, field reflect.StructFie
return nil
}
// TagStringParse parse tag string to map
// example: "a=1,b=2" -> map[string]string{"a":"1","b":"2"}
func TagStringParse(conf string) map[string]string {
conf = strings.TrimSpace(conf)
specs := strings.Split(conf, ",")
m := make(map[string]string)
for _, spec := range specs {
spec = strings.TrimSpace(spec)
pairs := strings.Split(spec, "=")
if len(pairs) == 1 {
m[pairs[0]] = ""
} else if len(pairs) > 1 {
m[pairs[0]] = pairs[1]
}
}
return m
}
// NewProviderPriest create a provider priest function for goner from a function like: `func(tagConf string, injectableStructParam struct{}) (provideType T, err error)`
// example:
// ```go

View File

@ -314,3 +314,43 @@ func TestBuryMockCemetery_Bury(t *testing.T) {
tombs := cemetery.GetTomByType(reflect.TypeOf(*point))
assert.Equal(t, 2, len(tombs))
}
func TestTagStringParse(t *testing.T) {
type args struct {
conf string
}
tests := []struct {
name string
args args
want map[string]string
}{
{
name: "suc",
args: args{
conf: "a=1,b=2",
},
want: map[string]string{
"a": "1",
"b": "2",
},
},
{
name: "suc",
args: args{
conf: "x,a=1,b=2,,,default,ok=",
},
want: map[string]string{
"a": "1",
"b": "2",
"x": "",
"default": "",
"ok": "",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, TagStringParse(tt.args.conf), "TagStringParse(%v)", tt.args.conf)
})
}
}