Test and fix edge cases in map args.

This commit is contained in:
John Beisley 2022-03-31 19:54:33 +01:00
parent f69c4d0ee2
commit cc75a26e13
2 changed files with 26 additions and 4 deletions

View File

@ -60,6 +60,8 @@ func NewSendAction(serviceType, actionName string, args any) *Action {
}
}
var stringType = reflect.TypeOf("")
var _ xml.Marshaler = &Action{}
// MarshalXML implements `xml.Marshaller`.
@ -94,8 +96,7 @@ func (a *Action) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
iter := v.MapRange()
for iter.Next() {
k := iter.Key()
// TODO: does this support string newtypes? convert?
ks := k.Interface().(string)
ks := k.Convert(stringType).Interface().(string)
v := iter.Value()
ke := xml.StartElement{Name: xml.Name{Local: ks}}
if err := e.EncodeElement(v.Interface(), ke); err != nil {

View File

@ -10,6 +10,7 @@ import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/huin/goupnp/v2alpha/soap/types"
)
type testStructArgs struct {
@ -17,6 +18,8 @@ type testStructArgs struct {
Bar string
}
type newString string
// TestWriteRead tests the round-trip of writing an envelope and reading it back.
func TestWriteRead(t *testing.T) {
tests := []struct {
@ -33,13 +36,29 @@ func TestWriteRead(t *testing.T) {
&testStructArgs{},
},
{
"map",
"mapString",
map[string]string{
"Foo": "foo-1",
"Bar": "bar-2",
},
map[string]string{},
},
{
"mapUI2",
map[string]types.UI2{
"Foo": 1,
"Bar": 2,
},
map[string]types.UI2{},
},
{
"mapNewStringKey",
map[newString]string{
"Foo": "foo-1",
"Bar": "bar-2",
},
map[newString]string{},
},
}
for _, test := range tests {
@ -52,7 +71,6 @@ func TestWriteRead(t *testing.T) {
if err != nil {
t.Fatalf("Write want success, got err=%v", err)
}
t.Logf("Encoded envelope:\n%v", buf)
actionOut := NewRecvAction(test.argsOut)
@ -67,6 +85,9 @@ func TestWriteRead(t *testing.T) {
if diff := cmp.Diff(test.argsIn, test.argsOut); diff != "" {
t.Errorf("\nwant argsOut=%+v\ngot %+v\ndiff:\n%s", test.argsIn, test.argsOut, diff)
}
if t.Failed() {
t.Logf("Encoded envelope:\n%v", buf)
}
})
}
}