combining and adding tests

This commit is contained in:
Pratham Gadkari 2025-11-30 16:35:13 +05:30
parent b6e5603530
commit 527f128f96

View File

@ -741,7 +741,8 @@ func (b *badCustom) UnmarshalParam(s string) error {
return errors.New("boom")
}
func TestTrySetCustomError(t *testing.T) {
func TestTrySetCustom_SingleValues(t *testing.T) {
t.Run("Error case", func(t *testing.T) {
var s struct {
F badCustom `form:"f"`
}
@ -749,19 +750,9 @@ func TestTrySetCustomError(t *testing.T) {
err := mappingByPtr(&s, formSource{"f": {"hello"}}, "form")
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid value")
}
})
func TestTrySetCustomNotApplicable(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 TestTrySetCustomIntegrationSuccess(t *testing.T) {
t.Run("Integration success", func(t *testing.T) {
var s struct {
F testCustom `form:"f"`
}
@ -769,9 +760,21 @@ func TestTrySetCustomIntegrationSuccess(t *testing.T) {
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"`
}
func TestTrySetCustomSlice(t *testing.T) {
err := mappingByPtr(&s, formSource{"n": {"42"}}, "form")
require.NoError(t, err)
assert.Equal(t, 42, s.N)
})
}
func TestTrySetCustom_Collections(t *testing.T) {
t.Run("Slice success", func(t *testing.T) {
var s struct {
F []testCustom `form:"f"`
}
@ -780,9 +783,9 @@ func TestTrySetCustomSlice(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "prefix_one", s.F[0].Value)
assert.Equal(t, "prefix_two", s.F[1].Value)
}
})
func TestTrySetCustomArray(t *testing.T) {
t.Run("Array success", func(t *testing.T) {
var s struct {
F [2]testCustom `form:"f"`
}
@ -791,9 +794,9 @@ func TestTrySetCustomArray(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "prefix_hello", s.F[0].Value)
assert.Equal(t, "prefix_world", s.F[1].Value)
}
})
func TestTrySetCustomSliceError(t *testing.T) {
t.Run("Slice error", func(t *testing.T) {
var s struct {
F []badCustom `form:"f"`
}
@ -801,9 +804,9 @@ func TestTrySetCustomSliceError(t *testing.T) {
err := mappingByPtr(&s, formSource{"f": {"oops1", "oops2"}}, "form")
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid value")
}
})
func TestTrySetCustomArrayError(t *testing.T) {
t.Run("Array error", func(t *testing.T) {
var s struct {
F [2]badCustom `form:"f"`
}
@ -811,4 +814,5 @@ func TestTrySetCustomArrayError(t *testing.T) {
err := mappingByPtr(&s, formSource{"f": {"fail1", "fail2"}}, "form")
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid value")
})
}