roster_test.go
author Chris Jones <christian.jones@sri.com>
Fri, 28 Dec 2012 17:07:20 -0700
changeset 114 a058e33c1666
parent 111 36287f2cf06e
permissions -rw-r--r--
Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible. Also improved the output from the two assert functions to show the info of the caller rather than the assert function itself.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     1
// Copyright 2011 The Go Authors.  All rights reserved.
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     2
// Use of this source code is governed by a BSD-style
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     3
// license that can be found in the LICENSE file.
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     4
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     5
package xmpp
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     6
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     7
import (
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
     8
	"encoding/xml"
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
     9
	"reflect"
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    10
	"testing"
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    11
)
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    12
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    13
// This is mostly just tests of the roster data structures.
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    14
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    15
func TestRosterIqMarshal(t *testing.T) {
111
36287f2cf06e Step 1 of moving to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents: 110
diff changeset
    16
	iq := &Iq{Header: Header{From: "from", Lang: "en",
110
7696e6a01709 Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents: 98
diff changeset
    17
		Nested: []interface{}{RosterQuery{}}}}
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    18
	exp := `<iq from="from" xml:lang="en"><query xmlns="` +
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    19
		NsRoster + `"></query></iq>`
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    20
	assertMarshal(t, exp, iq)
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    21
}
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    22
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    23
func TestRosterIqUnmarshal(t *testing.T) {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    24
	str := `<iq from="from" xml:lang="en"><query xmlns="` +
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    25
		NsRoster + `"><item jid="a@b.c"/></query></iq>`
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    26
	iq := Iq{}
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    27
	xml.Unmarshal([]byte(str), &iq)
72
Chris Jones <christian.jones@sri.com>
parents: 61
diff changeset
    28
	m := map[string]func(*xml.Name) interface{}{NsRoster: newRosterQuery}
111
36287f2cf06e Step 1 of moving to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents: 110
diff changeset
    29
	err := parseExtended(&iq.Header, m)
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    30
	if err != nil {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    31
		t.Fatalf("parseExtended: %v", err)
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    32
	}
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    33
	assertEquals(t, "iq", iq.XMLName.Local)
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    34
	assertEquals(t, "from", iq.From)
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    35
	assertEquals(t, "en", iq.Lang)
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    36
	nested := iq.Nested
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    37
	if nested == nil {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    38
		t.Fatalf("nested nil")
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    39
	}
61
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    40
	if len(nested) != 1 {
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    41
		t.Fatalf("wrong size nested(%d): %v", len(nested),
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    42
			nested)
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    43
	}
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    44
	var rq *RosterQuery
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    45
	rq, ok := nested[0].(*RosterQuery)
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    46
	if !ok {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    47
		t.Fatalf("nested not RosterQuery: %v",
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    48
			reflect.TypeOf(nested))
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    49
	}
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    50
	if len(rq.Item) != 1 {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    51
		t.Fatalf("Wrong # items: %v", rq.Item)
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    52
	}
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    53
	item := rq.Item[0]
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    54
	assertEquals(t, "a@b.c", item.Jid)
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    55
}