mirror of
https://github.com/gin-gonic/gin.git
synced 2025-12-11 19:47:00 +08:00
combining and adding tests
This commit is contained in:
parent
b6e5603530
commit
527f128f96
@ -741,74 +741,78 @@ func (b *badCustom) UnmarshalParam(s string) error {
|
|||||||
return errors.New("boom")
|
return errors.New("boom")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTrySetCustomError(t *testing.T) {
|
func TestTrySetCustom_SingleValues(t *testing.T) {
|
||||||
var s struct {
|
t.Run("Error case", func(t *testing.T) {
|
||||||
F badCustom `form:"f"`
|
var s struct {
|
||||||
}
|
F badCustom `form:"f"`
|
||||||
|
}
|
||||||
|
|
||||||
err := mappingByPtr(&s, formSource{"f": {"hello"}}, "form")
|
err := mappingByPtr(&s, formSource{"f": {"hello"}}, "form")
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
assert.Contains(t, err.Error(), "invalid value")
|
assert.Contains(t, err.Error(), "invalid value")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Integration success", func(t *testing.T) {
|
||||||
|
var s struct {
|
||||||
|
F testCustom `form:"f"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err := mappingByPtr(&s, formSource{"f": {"hello"}}, "form")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "prefix_hello", s.F.Value)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Not applicable type", func(t *testing.T) {
|
||||||
|
var s struct {
|
||||||
|
N int `form:"n"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err := mappingByPtr(&s, formSource{"n": {"42"}}, "form")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 42, s.N)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTrySetCustomNotApplicable(t *testing.T) {
|
func TestTrySetCustom_Collections(t *testing.T) {
|
||||||
var s struct {
|
t.Run("Slice success", func(t *testing.T) {
|
||||||
N int `form:"n"`
|
var s struct {
|
||||||
}
|
F []testCustom `form:"f"`
|
||||||
|
}
|
||||||
|
|
||||||
err := mappingByPtr(&s, formSource{"n": {"42"}}, "form")
|
err := mappingByPtr(&s, formSource{"f": {"one", "two"}}, "form")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, 42, s.N)
|
assert.Equal(t, "prefix_one", s.F[0].Value)
|
||||||
}
|
assert.Equal(t, "prefix_two", s.F[1].Value)
|
||||||
|
})
|
||||||
func TestTrySetCustomIntegrationSuccess(t *testing.T) {
|
|
||||||
var s struct {
|
t.Run("Array success", func(t *testing.T) {
|
||||||
F testCustom `form:"f"`
|
var s struct {
|
||||||
}
|
F [2]testCustom `form:"f"`
|
||||||
|
}
|
||||||
err := mappingByPtr(&s, formSource{"f": {"hello"}}, "form")
|
|
||||||
require.NoError(t, err)
|
err := mappingByPtr(&s, formSource{"f": {"hello", "world"}}, "form")
|
||||||
assert.Equal(t, "prefix_hello", s.F.Value)
|
require.NoError(t, err)
|
||||||
}
|
assert.Equal(t, "prefix_hello", s.F[0].Value)
|
||||||
|
assert.Equal(t, "prefix_world", s.F[1].Value)
|
||||||
func TestTrySetCustomSlice(t *testing.T) {
|
})
|
||||||
var s struct {
|
|
||||||
F []testCustom `form:"f"`
|
t.Run("Slice error", func(t *testing.T) {
|
||||||
}
|
var s struct {
|
||||||
|
F []badCustom `form:"f"`
|
||||||
err := mappingByPtr(&s, formSource{"f": {"one", "two"}}, "form")
|
}
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, "prefix_one", s.F[0].Value)
|
err := mappingByPtr(&s, formSource{"f": {"oops1", "oops2"}}, "form")
|
||||||
assert.Equal(t, "prefix_two", s.F[1].Value)
|
require.Error(t, err)
|
||||||
}
|
assert.Contains(t, err.Error(), "invalid value")
|
||||||
|
})
|
||||||
func TestTrySetCustomArray(t *testing.T) {
|
|
||||||
var s struct {
|
t.Run("Array error", func(t *testing.T) {
|
||||||
F [2]testCustom `form:"f"`
|
var s struct {
|
||||||
}
|
F [2]badCustom `form:"f"`
|
||||||
|
}
|
||||||
err := mappingByPtr(&s, formSource{"f": {"hello", "world"}}, "form")
|
|
||||||
require.NoError(t, err)
|
err := mappingByPtr(&s, formSource{"f": {"fail1", "fail2"}}, "form")
|
||||||
assert.Equal(t, "prefix_hello", s.F[0].Value)
|
require.Error(t, err)
|
||||||
assert.Equal(t, "prefix_world", s.F[1].Value)
|
assert.Contains(t, err.Error(), "invalid value")
|
||||||
}
|
})
|
||||||
|
|
||||||
func TestTrySetCustomSliceError(t *testing.T) {
|
|
||||||
var s struct {
|
|
||||||
F []badCustom `form:"f"`
|
|
||||||
}
|
|
||||||
|
|
||||||
err := mappingByPtr(&s, formSource{"f": {"oops1", "oops2"}}, "form")
|
|
||||||
require.Error(t, err)
|
|
||||||
assert.Contains(t, err.Error(), "invalid value")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTrySetCustomArrayError(t *testing.T) {
|
|
||||||
var s struct {
|
|
||||||
F [2]badCustom `form:"f"`
|
|
||||||
}
|
|
||||||
|
|
||||||
err := mappingByPtr(&s, formSource{"f": {"fail1", "fail2"}}, "form")
|
|
||||||
require.Error(t, err)
|
|
||||||
assert.Contains(t, err.Error(), "invalid value")
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user