Skip to content

Commit

Permalink
map encoder tests, more decoder tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tigh-latte committed May 9, 2024
1 parent 54d6dbf commit bc69006
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
56 changes: 44 additions & 12 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1930,28 +1930,60 @@ func (u *unmarshaler) UnmarshalForm(ss []string) error {

func TestDecoder_UnmarshalForm(t *testing.T) {
type T1 struct {
Ptr *unmarshaler
NilPtr *unmarshaler
Struct unmarshaler
Ptr *unmarshaler
NilPtr *unmarshaler
Struct unmarshaler
Slice []unmarshaler
SlicePtr []*unmarshaler
Map map[string]unmarshaler
MapPtr map[string]*unmarshaler
}

in := url.Values{
"Ptr": []string{"Liam", "Neeson"},
"NilPtr": []string{"John", "Smith"},
"Struct": []string{"Bob", "Dylan"},
"Ptr": []string{"ptrfname", "ptrsname"},
"NilPtr": []string{"nilptrfname", "nilptrsname"},
"Struct": []string{"structfname", "structsname"},
"Slice[0]": []string{"slice0fname", "slice0sname"},
"Slice[1]": []string{"slice1fname", "slice1sname"},
"SlicePtr[0]": []string{"sliceptr0fname", "sliceptr0sname"},
"SlicePtr[1]": []string{"sliceptr1fname", "sliceptr1sname"},
"Map[key1]": []string{"mapk1fname", "mapk1sname"},
"Map[key2]": []string{"mapk2fname", "mapk2sname"},
"MapPtr[key1]": []string{"mapptrk1fname", "mapptrk1sname"},
"MapPtr[key2]": []string{"mapptrk2fname", "mapptrk2sname"},
}

v := new(T1)
v.Ptr = &unmarshaler{}
err := NewDecoder().Decode(v, in)
Equal(t, err, nil)
NotEqual(t, v.NilPtr, nil)
Equal(t, v.Ptr.fname, "Liam")
Equal(t, v.Ptr.sname, "Neeson")
Equal(t, v.NilPtr.fname, "John")
Equal(t, v.NilPtr.sname, "Smith")
Equal(t, v.Struct.fname, "Bob")
Equal(t, v.Struct.sname, "Dylan")
Equal(t, v.Ptr.fname, "ptrfname")
Equal(t, v.Ptr.sname, "ptrsname")
Equal(t, v.NilPtr.fname, "nilptrfname")
Equal(t, v.NilPtr.sname, "nilptrsname")
Equal(t, v.Struct.fname, "structfname")
Equal(t, v.Struct.sname, "structsname")

Equal(t, len(v.Slice), 2)
Equal(t, v.Slice[0].fname, "slice0fname")
Equal(t, v.Slice[1].fname, "slice1fname")

Equal(t, len(v.SlicePtr), 2)
Equal(t, v.SlicePtr[0].fname, "sliceptr0fname")
Equal(t, v.SlicePtr[1].fname, "sliceptr1fname")

Equal(t, len(v.Map), 2)
Equal(t, v.Map["key1"].fname, "mapk1fname")
Equal(t, v.Map["key1"].sname, "mapk1sname")
Equal(t, v.Map["key2"].fname, "mapk2fname")
Equal(t, v.Map["key2"].sname, "mapk2sname")

Equal(t, len(v.MapPtr), 2)
Equal(t, v.MapPtr["key1"].fname, "mapptrk1fname")
Equal(t, v.MapPtr["key1"].sname, "mapptrk1sname")
Equal(t, v.MapPtr["key2"].fname, "mapptrk2fname")
Equal(t, v.MapPtr["key2"].sname, "mapptrk2sname")
}

func TestDecoder_UnmarshalForm_Error(t *testing.T) {
Expand Down
18 changes: 9 additions & 9 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1625,18 +1625,18 @@ func Test_MarshalForm(t *testing.T) {
Sname: "structsname",
},
Slice: []marshaler{{
Fname: "slice0fname",
Sname: "slice0sname",
}, {
Fname: "slice1fname",
Sname: "slice1sname",
}, {
Fname: "slice2fname",
Sname: "slice2sname",
}},
SlicePtr: []*marshaler{{
Fname: "sliceptr0fname",
Sname: "sliceptr0sname",
}, {
Fname: "sliceptr1fname",
Sname: "sliceptr1sname",
}, {
Fname: "sliceptr2fname",
Sname: "sliceptr2sname",
}},
Map: map[string]marshaler{
"key1": {
Expand Down Expand Up @@ -1664,9 +1664,9 @@ func Test_MarshalForm(t *testing.T) {
Equal(t, values["Ptr"], []string{"ptrfname", "ptrsname"})
Equal(t, values["NilPointer"], nil)
Equal(t, values["Struct"], []string{"structfname", "structsname"})
Equal(t, values["Slice"], []string{"slice1fname", "slice1sname", "slice2fname", "slice2sname"})
Equal(t, values["SlicePtr[0]"], []string{"sliceptr1fname", "sliceptr1sname"})
Equal(t, values["SlicePtr[1]"], []string{"sliceptr2fname", "sliceptr2sname"})
Equal(t, values["Slice"], []string{"slice0fname", "slice0sname", "slice1fname", "slice1sname"})
Equal(t, values["SlicePtr[0]"], []string{"sliceptr0fname", "sliceptr0sname"})
Equal(t, values["SlicePtr[1]"], []string{"sliceptr1fname", "sliceptr1sname"})
Equal(t, values["Map[key1]"], []string{"mapk1fname", "mapk1sname"})
Equal(t, values["Map[key2]"], []string{"mapk2fname", "mapk2sname"})
Equal(t, values["MapPtr[key1]"], []string{"mapptrk1fname", "mapptrk1sname"})
Expand Down

0 comments on commit bc69006

Please sign in to comment.