~samwhited/xmpp

ref: c71a17569b03f07e0fc407f1da2094c1796cec90 xmpp/iq_test.go -rw-r--r-- 1.8 KiB
c71a1756Sam Whited ibr2: Add API stability disclaimer to docs 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2016 Sam Whited.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp

import (
	"encoding/xml"
	"fmt"
	"testing"
)

var (
	_ xml.UnmarshalerAttr = (*iqType)(nil)
	_ xml.MarshalerAttr   = (*iqType)(nil)
	_ xml.MarshalerAttr   = GetIQ
	_ fmt.Stringer        = (*iqType)(nil)
	_ fmt.Stringer        = GetIQ
)

func TestMarshalIQTypeAttr(t *testing.T) {
	n := xml.Name{Space: "space", Local: "type"}
	for _, test := range []struct {
		iqtype iqType
		value  string
	}{{GetIQ, "get"}, {SetIQ, "set"}, {ResultIQ, "result"}, {ErrorIQ, "error"}} {
		attr, err := test.iqtype.MarshalXMLAttr(n)
		if err != nil {
			t.Error(err)
			continue
		}
		if attr.Name != n {
			t.Errorf("Got wrong attribute name for IQ type %s. Got %v, want %v", test.value, attr.Name, n)
		}
		if attr.Value != test.value {
			t.Errorf("Got wrong attribute value for IQ type %s: `%s`", test.value, attr.Value)
		}
	}
}

func TestUnmarshalIQTypeAttr(t *testing.T) {
	for _, test := range []struct {
		attr   xml.Attr
		iqtype iqType
		err    bool
	}{
		{xml.Attr{Name: xml.Name{}, Value: "get"}, GetIQ, false},
		{xml.Attr{Name: xml.Name{Space: "", Local: "type"}, Value: "set"}, SetIQ, false},
		{xml.Attr{Name: xml.Name{Space: "urn", Local: "loc"}, Value: "result"}, ResultIQ, false},
		{xml.Attr{Name: xml.Name{}, Value: "error"}, ErrorIQ, false},
		{xml.Attr{Name: xml.Name{}, Value: "stuff"}, ErrorIQ, true},
	} {
		iqtype := iqType(0)
		switch err := (&iqtype).UnmarshalXMLAttr(test.attr); {
		case test.err && err == nil:
			t.Error("Expected unmarshaling IQ type to error")
			continue
		case !test.err && err != nil:
			t.Error(err)
			continue
		case test.err && err != nil:
			continue
		case iqtype != test.iqtype:
			t.Errorf("Expected attr %+v to unmarshal into %s type IQ but got %s", test.attr, test.iqtype, iqtype)
		}
	}
}