2019-06-26 15:14:52 +00:00
|
|
|
package stanza_test
|
2019-02-10 16:53:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2020-01-09 14:33:11 +00:00
|
|
|
"errors"
|
|
|
|
"regexp"
|
2019-07-09 14:59:54 +00:00
|
|
|
"testing"
|
2019-02-10 16:53:18 +00:00
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-07-09 14:59:54 +00:00
|
|
|
"gosrc.io/xmpp/stanza"
|
2019-02-10 16:53:18 +00:00
|
|
|
)
|
|
|
|
|
2020-01-09 14:33:11 +00:00
|
|
|
var reLeadcloseWhtsp = regexp.MustCompile(`^[\s\p{Zs}]+|[\s\p{Zs}]+$`)
|
|
|
|
var reInsideWhtsp = regexp.MustCompile(`[\s\p{Zs}]`)
|
|
|
|
|
2019-07-09 14:59:54 +00:00
|
|
|
// ============================================================================
|
|
|
|
// Marshaller / unmarshaller test
|
|
|
|
|
2020-01-31 10:48:03 +00:00
|
|
|
func checkMarshalling(t *testing.T, iq *stanza.IQ) (*stanza.IQ, error) {
|
2019-07-09 14:59:54 +00:00
|
|
|
// Marshall
|
|
|
|
data, err := xml.Marshal(iq)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot marshal iq: %s\n%#v", err, iq)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshall
|
|
|
|
var parsedIQ stanza.IQ
|
|
|
|
err = xml.Unmarshal(data, &parsedIQ)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unmarshal returned error: %s\n%s", err, data)
|
|
|
|
}
|
|
|
|
return &parsedIQ, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// XML structs comparison
|
|
|
|
|
2019-02-10 16:53:18 +00:00
|
|
|
// Compare iq structure but ignore empty namespace as they are set properly on
|
|
|
|
// marshal / unmarshal. There is no need to manage them on the manually
|
|
|
|
// crafted structure.
|
|
|
|
func xmlEqual(x, y interface{}) bool {
|
2019-06-27 12:30:23 +00:00
|
|
|
return cmp.Equal(x, y, xmlOpts())
|
|
|
|
}
|
|
|
|
|
|
|
|
// xmlDiff compares xml structures ignoring namespace preferences
|
|
|
|
func xmlDiff(x, y interface{}) string {
|
|
|
|
return cmp.Diff(x, y, xmlOpts())
|
|
|
|
}
|
|
|
|
|
|
|
|
func xmlOpts() cmp.Options {
|
2019-02-10 16:53:18 +00:00
|
|
|
alwaysEqual := cmp.Comparer(func(_, _ interface{}) bool { return true })
|
|
|
|
opts := cmp.Options{
|
|
|
|
cmp.FilterValues(func(x, y interface{}) bool {
|
|
|
|
xx, xok := x.(xml.Name)
|
|
|
|
yy, yok := y.(xml.Name)
|
|
|
|
if xok && yok {
|
|
|
|
zero := xml.Name{}
|
|
|
|
if xx == zero || yy == zero {
|
|
|
|
return true
|
|
|
|
}
|
2019-06-27 12:30:23 +00:00
|
|
|
if xx.Space == "" || yy.Space == "" {
|
|
|
|
return true
|
|
|
|
}
|
2019-02-10 16:53:18 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}, alwaysEqual),
|
|
|
|
}
|
2019-06-27 12:30:23 +00:00
|
|
|
return opts
|
2019-02-10 16:53:18 +00:00
|
|
|
}
|
2020-01-09 14:33:11 +00:00
|
|
|
|
|
|
|
func delSpaces(s string) string {
|
|
|
|
return reInsideWhtsp.ReplaceAllString(reLeadcloseWhtsp.ReplaceAllString(s, ""), "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareMarshal(expected, data string) error {
|
|
|
|
if delSpaces(expected) != delSpaces(data) {
|
|
|
|
return errors.New("failed to verify unmarshal->marshal. Expected :" + expected + "\ngot: " + data)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|