Add builder & test on software version helpers

This commit is contained in:
Mickael Remond 2019-07-09 17:53:49 +02:00 committed by Mickaël Rémond
parent 014957e029
commit 61adf7e414
4 changed files with 69 additions and 3 deletions

View File

@ -14,7 +14,7 @@ func TestDiscoInfo_Builder(t *testing.T) {
disco.AddIdentity("Test Component", "gateway", "service") disco.AddIdentity("Test Component", "gateway", "service")
disco.AddFeatures(stanza.NSDiscoInfo, stanza.NSDiscoItems, "jabber:iq:version", "urn:xmpp:delegation:1") disco.AddFeatures(stanza.NSDiscoInfo, stanza.NSDiscoItems, "jabber:iq:version", "urn:xmpp:delegation:1")
parsedIQ, err := marshallUnmarshall(t, iq) parsedIQ, err := checkMarshalling(t, iq)
if err != nil { if err != nil {
return return
} }
@ -57,7 +57,7 @@ func TestDiscoItems_Builder(t *testing.T) {
AddItem("catalog.shakespeare.lit", "clothing", "Wear your literary taste with pride"). AddItem("catalog.shakespeare.lit", "clothing", "Wear your literary taste with pride").
AddItem("catalog.shakespeare.lit", "music", "Music from the time of Shakespeare") AddItem("catalog.shakespeare.lit", "music", "Music from the time of Shakespeare")
parsedIQ, err := marshallUnmarshall(t, iq) parsedIQ, err := checkMarshalling(t, iq)
if err != nil { if err != nil {
return return
} }
@ -79,6 +79,12 @@ func TestDiscoItems_Builder(t *testing.T) {
if item.JID != items[i].JID { if item.JID != items[i].JID {
t.Errorf("JID Mismatch (expected: %s): %s", items[i].JID, item.JID) t.Errorf("JID Mismatch (expected: %s): %s", items[i].JID, item.JID)
} }
if item.Node != items[i].Node {
t.Errorf("Node Mismatch (expected: %s): %s", items[i].JID, item.JID)
}
if item.Name != items[i].Name {
t.Errorf("Name Mismatch (expected: %s): %s", items[i].JID, item.JID)
}
} }
} }
} }

View File

@ -17,6 +17,26 @@ func (v *Version) Namespace() string {
return v.XMLName.Space return v.XMLName.Space
} }
// ---------------
// Builder helpers
// Version builds a default software version payload
func (iq *IQ) Version() *Version {
d := Version{
XMLName: xml.Name{Space: "jabber:iq:version", Local: "query"},
}
iq.Payload = &d
return &d
}
// Set all software version info
func (v *Version) SetInfo(name, version, os string) *Version {
v.Name = name
v.Version = version
v.OS = os
return v
}
// ============================================================================ // ============================================================================
// Registry init // Registry init

40
stanza/iq_version_test.go Normal file
View File

@ -0,0 +1,40 @@
package stanza_test
import (
"testing"
"gosrc.io/xmpp/stanza"
)
// Build a Software Version reply
// https://xmpp.org/extensions/xep-0092.html#example-2
func TestVersion_Builder(t *testing.T) {
name := "Exodus"
version := "0.7.0.4"
os := "Windows-XP 5.01.2600"
iq := stanza.NewIQ(stanza.Attrs{Type: "result", From: "romeo@montague.net/orchard",
To: "juliet@capulet.com/balcony", Id: "version_1"})
iq.Version().SetInfo(name, version, os)
parsedIQ, err := checkMarshalling(t, iq)
if err != nil {
return
}
// Check result
pp, ok := parsedIQ.Payload.(*stanza.Version)
if !ok {
t.Errorf("Parsed stanza does not contain correct IQ payload")
}
// Check version info
if pp.Name != name {
t.Errorf("Name Mismatch (expected: %s): %s", name, pp.Name)
}
if pp.Version != version {
t.Errorf("Version Mismatch (expected: %s): %s", version, pp.Version)
}
if pp.OS != os {
t.Errorf("OS Mismatch (expected: %s): %s", os, pp.OS)
}
}

View File

@ -11,7 +11,7 @@ import (
// ============================================================================ // ============================================================================
// Marshaller / unmarshaller test // Marshaller / unmarshaller test
func marshallUnmarshall(t *testing.T, iq stanza.IQ) (*stanza.IQ, error) { func checkMarshalling(t *testing.T, iq stanza.IQ) (*stanza.IQ, error) {
// Marshall // Marshall
data, err := xml.Marshal(iq) data, err := xml.Marshal(iq)
if err != nil { if err != nil {