Improve presence / message test

This commit is contained in:
Mickael Remond 2018-01-25 11:00:20 +01:00
parent 28fb5bf61b
commit 7e50d313ea
No known key found for this signature in database
GPG Key ID: E6F6045D79965AA3
4 changed files with 41 additions and 4 deletions

View File

@ -50,5 +50,3 @@ func (msg *Message) XMPPFormat() string {
msg.To, msg.To,
xmlEscape(msg.Body)) xmlEscape(msg.Body))
} }
// TODO: Func new message to create an empty message structure without the XML tag matching elements

View File

@ -22,7 +22,7 @@ func TestGenerateMessage(t *testing.T) {
t.Errorf("Unmarshal(%s) returned error", data) t.Errorf("Unmarshal(%s) returned error", data)
} }
if !xmlEqual(parsedMessage.Body, message.Body) { if !xmlEqual(parsedMessage, message) {
t.Errorf("non matching items\n%s", cmp.Diff(parsedMessage.Body, message.Body)) t.Errorf("non matching items\n%s", cmp.Diff(parsedMessage, message))
} }
} }

View File

@ -18,6 +18,18 @@ func (Presence) Name() string {
return "presence" 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{} type presenceDecoder struct{}
var presence presenceDecoder var presence presenceDecoder

27
presence_test.go Normal file
View File

@ -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))
}
}