From 7e50d313ea7ee7887e63a27b8e60464073c2410d Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Thu, 25 Jan 2018 11:00:20 +0100 Subject: [PATCH] Improve presence / message test --- message.go | 2 -- message_test.go | 4 ++-- presence.go | 12 ++++++++++++ presence_test.go | 27 +++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 presence_test.go diff --git a/message.go b/message.go index 24c2d10..36aec8e 100644 --- a/message.go +++ b/message.go @@ -50,5 +50,3 @@ func (msg *Message) XMPPFormat() string { msg.To, xmlEscape(msg.Body)) } - -// TODO: Func new message to create an empty message structure without the XML tag matching elements diff --git a/message_test.go b/message_test.go index f32a9f7..fb1a557 100644 --- a/message_test.go +++ b/message_test.go @@ -22,7 +22,7 @@ func TestGenerateMessage(t *testing.T) { t.Errorf("Unmarshal(%s) returned error", data) } - if !xmlEqual(parsedMessage.Body, message.Body) { - t.Errorf("non matching items\n%s", cmp.Diff(parsedMessage.Body, message.Body)) + if !xmlEqual(parsedMessage, message) { + t.Errorf("non matching items\n%s", cmp.Diff(parsedMessage, message)) } } diff --git a/presence.go b/presence.go index c5dec66..77600a5 100644 --- a/presence.go +++ b/presence.go @@ -18,6 +18,18 @@ func (Presence) Name() string { return "presence" } +func NewPresence(from, to, id, lang string) Presence { + return Presence{ + XMLName: xml.Name{Local: "presence"}, + PacketAttrs: PacketAttrs{ + Id: id, + From: from, + To: to, + Lang: lang, + }, + } +} + type presenceDecoder struct{} var presence presenceDecoder diff --git a/presence_test.go b/presence_test.go new file mode 100644 index 0000000..06f381f --- /dev/null +++ b/presence_test.go @@ -0,0 +1,27 @@ +package xmpp + +import ( + "encoding/xml" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestGeneratePresence(t *testing.T) { + presence := NewPresence("admin@localhost", "test@localhost", "1", "en") + presence.Show = "chat" + + data, err := xml.Marshal(presence) + if err != nil { + t.Errorf("cannot marshal xml structure") + } + + parsedPresence := Presence{} + if err = xml.Unmarshal(data, &parsedPresence); err != nil { + t.Errorf("Unmarshal(%s) returned error", data) + } + + if !xmlEqual(parsedPresence, presence) { + t.Errorf("non matching items\n%s", cmp.Diff(parsedPresence, presence)) + } +}