diff --git a/binder/mapping_test.go b/binder/mapping_test.go index aec91ff2be..af29b16c00 100644 --- a/binder/mapping_test.go +++ b/binder/mapping_test.go @@ -1,6 +1,7 @@ package binder import ( + "errors" "reflect" "testing" @@ -29,3 +30,50 @@ func Test_EqualFieldType(t *testing.T) { require.True(t, equalFieldType(&user, reflect.Int, "AGE")) require.True(t, equalFieldType(&user, reflect.Int, "age")) } + +func Test_ParseParamSquareBrackets(t *testing.T) { + tests := []struct { + input string + expected string + err error + }{ + { + input: "foo[bar]", + expected: "foo.bar", + err: nil, + }, + { + input: "foo[bar][baz]", + expected: "foo.bar.baz", + err: nil, + }, + { + input: "foo[bar", + expected: "", + err: errors.New("unmatched brackets"), + }, + { + input: "foo[bar][baz", + expected: "", + err: errors.New("unmatched brackets"), + }, + { + input: "foo]bar[", + expected: "", + err: errors.New("unmatched brackets"), + }, + } + + for _, tt := range tests { + t.Run(tt.input, func(t *testing.T) { + result, err := parseParamSquareBrackets(tt.input) + if tt.err != nil { + require.Error(t, err) + require.EqualError(t, err, tt.err.Error()) + } else { + require.NoError(t, err) + require.Equal(t, tt.expected, result) + } + }) + } +}