author | Chris Jones <chris@cjones.org> |
Thu, 24 May 2012 23:27:45 -0600 | |
branch | go.weekly.2012-01-15 |
changeset 95 | 6eac4867d095 |
parent 93 | fbd51fa6b7ea |
permissions | -rw-r--r-- |
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 ( |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
8 |
"encoding/xml" |
38 | 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) { |
95
6eac4867d095
Attempts to work around Go1 XML bugs.
Chris Jones <chris@cjones.org>
parents:
93
diff
changeset
|
16 |
// BUG(xml:lang) |
6eac4867d095
Attempts to work around Go1 XML bugs.
Chris Jones <chris@cjones.org>
parents:
93
diff
changeset
|
17 |
// iq := &Iq{From: "from", Lang: "en", Nested: []interface{}{RosterQuery{}}} |
6eac4867d095
Attempts to work around Go1 XML bugs.
Chris Jones <chris@cjones.org>
parents:
93
diff
changeset
|
18 |
// exp := `<iq from="from" xml:lang="en"><query xmlns="` + |
6eac4867d095
Attempts to work around Go1 XML bugs.
Chris Jones <chris@cjones.org>
parents:
93
diff
changeset
|
19 |
// NsRoster + `"></query></iq>` |
6eac4867d095
Attempts to work around Go1 XML bugs.
Chris Jones <chris@cjones.org>
parents:
93
diff
changeset
|
20 |
iq := &Iq{From: "from", Nested: []interface{}{RosterQuery{}}} |
6eac4867d095
Attempts to work around Go1 XML bugs.
Chris Jones <chris@cjones.org>
parents:
93
diff
changeset
|
21 |
exp := `<iq from="from"><query xmlns="` + |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
22 |
NsRoster + `"></query></iq>` |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
23 |
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
|
24 |
} |
38 | 25 |
|
26 |
func TestRosterIqUnmarshal(t *testing.T) { |
|
95
6eac4867d095
Attempts to work around Go1 XML bugs.
Chris Jones <chris@cjones.org>
parents:
93
diff
changeset
|
27 |
// BUG(xml:lang) |
6eac4867d095
Attempts to work around Go1 XML bugs.
Chris Jones <chris@cjones.org>
parents:
93
diff
changeset
|
28 |
// str := `<iq from="from" xml:lang="en"><query xmlns="` + |
6eac4867d095
Attempts to work around Go1 XML bugs.
Chris Jones <chris@cjones.org>
parents:
93
diff
changeset
|
29 |
// NsRoster + `"><item jid="a@b.c"/></query></iq>` |
6eac4867d095
Attempts to work around Go1 XML bugs.
Chris Jones <chris@cjones.org>
parents:
93
diff
changeset
|
30 |
str := `<iq from="from"><query xmlns="` + |
38 | 31 |
NsRoster + `"><item jid="a@b.c"/></query></iq>` |
32 |
var st Stanza = &Iq{} |
|
93 | 33 |
err := xml.Unmarshal([]byte(str), st) |
75
03a923eb5c01
Updated for weekly.2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
74
diff
changeset
|
34 |
if err != nil { |
03a923eb5c01
Updated for weekly.2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
74
diff
changeset
|
35 |
t.Fatalf("Unmarshal: %v", err) |
03a923eb5c01
Updated for weekly.2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
74
diff
changeset
|
36 |
} |
03a923eb5c01
Updated for weekly.2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
74
diff
changeset
|
37 |
assertEquals(t, "from", st.GetFrom()) |
72 | 38 |
m := map[string]func(*xml.Name) interface{}{NsRoster: newRosterQuery} |
75
03a923eb5c01
Updated for weekly.2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
74
diff
changeset
|
39 |
err = parseExtended(st, m) |
38 | 40 |
if err != nil { |
41 |
t.Fatalf("parseExtended: %v", err) |
|
42 |
} |
|
42
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
39
diff
changeset
|
43 |
assertEquals(t, "iq", st.GetName()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
39
diff
changeset
|
44 |
assertEquals(t, "from", st.GetFrom()) |
95
6eac4867d095
Attempts to work around Go1 XML bugs.
Chris Jones <chris@cjones.org>
parents:
93
diff
changeset
|
45 |
// BUG(xml:lang) |
6eac4867d095
Attempts to work around Go1 XML bugs.
Chris Jones <chris@cjones.org>
parents:
93
diff
changeset
|
46 |
// assertEquals(t, "en", st.GetLang()) |
42
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
39
diff
changeset
|
47 |
nested := st.GetNested() |
38 | 48 |
if nested == nil { |
49 |
t.Fatalf("nested nil") |
|
50 |
} |
|
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
51 |
if len(nested) != 1 { |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
52 |
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
|
53 |
nested) |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
54 |
} |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
55 |
var rq *RosterQuery |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
56 |
rq, ok := nested[0].(*RosterQuery) |
38 | 57 |
if !ok { |
58 |
t.Fatalf("nested not RosterQuery: %v", |
|
59 |
reflect.TypeOf(nested)) |
|
60 |
} |
|
61 |
if len(rq.Item) != 1 { |
|
62 |
t.Fatalf("Wrong # items: %v", rq.Item) |
|
63 |
} |
|
64 |
item := rq.Item[0] |
|
65 |
assertEquals(t, "a@b.c", item.Jid) |
|
66 |
} |