~samwhited/xmpp

f941e9085d5c56f198fa32afee38c4d75e2bb4c9 — Sam Whited 5 years ago ff45826
Don't fail on marshaling nil JID pointer
2 files changed, 15 insertions(+), 0 deletions(-)

M jid/jid.go
M jid/jid_test.go
M jid/jid.go => jid/jid.go +3 -0
@@ 184,6 184,9 @@ func (j *JID) Equal(j2 JID) bool {
// MarshalXMLAttr satisfies the MarshalerAttr interface and marshals the JID as
// an XML attribute.
func (j *JID) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
	if j == nil {
		return xml.Attr{}, nil
	}
	return xml.Attr{Name: name, Value: j.String()}, nil
}


M jid/jid_test.go => jid/jid_test.go +12 -0
@@ 67,3 67,15 @@ func TestInvalidJIDs(t *testing.T) {
		}
	}
}

func TestMarshalEmpty(t *testing.T) {
	attr, err := ((*JID)(nil)).MarshalXMLAttr(xml.Name{})
	switch {
	case err != nil:
		t.Logf("Marshaling an empty JID to an attr should not error but got %v\n", err)
		t.Fail()
	case attr != xml.Attr{}:
		t.Logf("Error marshaling empty JID expected Attr{} but got: %+v\n", err)
		t.Fail()
	}
}