Compare commits

...

3 Commits

Author SHA1 Message Date
Pratham Gadkari
2a973b32f8
Merge 527f128f96bf4d9e412b8e7f01323c33910d069e into f416d1e594a027063e73f66ac873a82113036fd8 2025-11-30 16:35:21 +05:30
Pratham Gadkari
527f128f96 combining and adding tests 2025-11-30 16:35:13 +05:30
Pratham Gadkari
b6e5603530 more tests because coverage is failing 2025-11-30 16:26:33 +05:30

View File

@ -741,54 +741,78 @@ func (b *badCustom) UnmarshalParam(s string) error {
return errors.New("boom")
}
func TestTrySetCustomError(t *testing.T) {
var s struct {
F badCustom `form:"f"`
}
func TestTrySetCustom_SingleValues(t *testing.T) {
t.Run("Error case", func(t *testing.T) {
var s struct {
F badCustom `form:"f"`
}
err := mappingByPtr(&s, formSource{"f": {"hello"}}, "form")
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid value")
err := mappingByPtr(&s, formSource{"f": {"hello"}}, "form")
require.Error(t, err)
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) {
var s struct {
N int `form:"n"`
}
func TestTrySetCustom_Collections(t *testing.T) {
t.Run("Slice success", func(t *testing.T) {
var s struct {
F []testCustom `form:"f"`
}
err := mappingByPtr(&s, formSource{"n": {"42"}}, "form")
require.NoError(t, err)
assert.Equal(t, 42, s.N)
}
func TestTrySetCustomIntegrationSuccess(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)
}
func TestTrySetCustomSlice(t *testing.T) {
var s struct {
F []testCustom `form:"f"`
}
err := mappingByPtr(&s, formSource{"f": {"one", "two"}}, "form")
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) {
var s struct {
F [2]testCustom `form:"f"`
}
err := mappingByPtr(&s, formSource{"f": {"hello", "world"}}, "form")
require.NoError(t, err)
assert.Equal(t, "prefix_hello", s.F[0].Value)
assert.Equal(t, "prefix_world", s.F[1].Value)
err := mappingByPtr(&s, formSource{"f": {"one", "two"}}, "form")
require.NoError(t, err)
assert.Equal(t, "prefix_one", s.F[0].Value)
assert.Equal(t, "prefix_two", s.F[1].Value)
})
t.Run("Array success", func(t *testing.T) {
var s struct {
F [2]testCustom `form:"f"`
}
err := mappingByPtr(&s, formSource{"f": {"hello", "world"}}, "form")
require.NoError(t, err)
assert.Equal(t, "prefix_hello", s.F[0].Value)
assert.Equal(t, "prefix_world", s.F[1].Value)
})
t.Run("Slice error", func(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")
})
t.Run("Array error", func(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")
})
}