Skip to content

Commit 5e9b2ad

Browse files
author
Dean Karn
committed
added slice tests
1 parent 2cd3a22 commit 5e9b2ad

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

decoder_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,3 +1615,44 @@ func TestDecodeArrayBug(t *testing.T) {
16151615
Equal(t, data.G[1], "")
16161616
Equal(t, data.G[2], "20")
16171617
}
1618+
1619+
func TestDecoder_RegisterCustomTypeFuncOnSlice(t *testing.T) {
1620+
type customString string
1621+
1622+
type TestStruct struct {
1623+
Slice []customString `form:"slice"`
1624+
}
1625+
1626+
d := NewDecoder()
1627+
d.RegisterCustomTypeFunc(func(vals []string) (i interface{}, e error) {
1628+
custom := make([]customString, 0, len(vals))
1629+
for i := 0; i < len(vals); i++ {
1630+
custom = append(custom, customString("custom"+vals[i]))
1631+
}
1632+
return custom, nil
1633+
}, []customString{})
1634+
1635+
var v TestStruct
1636+
err := d.Decode(&v, url.Values{"slice": []string{"v1", "v2"}})
1637+
Equal(t, err, nil)
1638+
Equal(t, v.Slice, []customString{"customv1", "customv2"})
1639+
}
1640+
1641+
func TestDecoder_RegisterCustomTypeFunc(t *testing.T) {
1642+
type customString string
1643+
1644+
type TestStruct struct {
1645+
Slice []customString `form:"slice"`
1646+
}
1647+
1648+
d := NewDecoder()
1649+
d.RegisterCustomTypeFunc(func(vals []string) (i interface{}, e error) {
1650+
return customString("custom" + vals[0]), nil
1651+
}, customString(""))
1652+
1653+
var v TestStruct
1654+
err := d.Decode(&v, url.Values{"slice": []string{"v1", "v2"}})
1655+
Equal(t, err, nil)
1656+
1657+
Equal(t, v.Slice, []customString{"customv1", "customv2"})
1658+
}

0 commit comments

Comments
 (0)