~samwhited/xmpp

xmpp/paging/rsm_test.go -rw-r--r-- 4.1 KiB
e9b0a2deSam Whited docs: do a quick editing pass over the docs a day 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2021 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package paging_test

import (
	"encoding/xml"
	"strconv"
	"strings"
	"testing"

	"mellium.im/xmlstream"
	"mellium.im/xmpp/paging"
)

var (
	_ xmlstream.Marshaler = (*paging.RequestCount)(nil)
	_ xmlstream.WriterTo  = (*paging.RequestCount)(nil)
	_ xml.Marshaler       = (*paging.RequestCount)(nil)
	_ xmlstream.Marshaler = (*paging.RequestNext)(nil)
	_ xmlstream.WriterTo  = (*paging.RequestNext)(nil)
	_ xml.Marshaler       = (*paging.RequestNext)(nil)
	_ xmlstream.Marshaler = (*paging.RequestPrev)(nil)
	_ xmlstream.WriterTo  = (*paging.RequestPrev)(nil)
	_ xml.Marshaler       = (*paging.RequestPrev)(nil)
	_ xmlstream.Marshaler = (*paging.RequestIndex)(nil)
	_ xmlstream.WriterTo  = (*paging.RequestIndex)(nil)
	_ xml.Marshaler       = (*paging.RequestIndex)(nil)
	_ xmlstream.Marshaler = (*paging.Set)(nil)
	_ xmlstream.WriterTo  = (*paging.Set)(nil)
	_ xml.Marshaler       = (*paging.Set)(nil)
)

var iterTests = [...]struct {
	in          string
	out         string
	nextQueries string
	prevQueries string
	curQueries  string
	err         error
}{
	0: {
		in: `<a></a>`,
	},
	1: {
		in:  `<nums><a>1</a><a/></nums>`,
		out: `<a>1</a><a></a>`,
	},
	2: {
		in: `<nums><a>1</a><b/><set xmlns='http://jabber.org/protocol/rsm'>
<last>2</last>
</set>
</nums>`,
		out:         "<a>1</a><b></b>\n",
		nextQueries: `<set xmlns="http://jabber.org/protocol/rsm"><max>10</max><after>2</after></set>`,
		curQueries:  `<set xmlns="http://jabber.org/protocol/rsm"><first></first><last>2</last></set>`,
	},
	3: {
		in: `<nums><set xmlns='http://jabber.org/protocol/rsm'>
<first>1</first>
</set><b/></nums>`,
		out:         "<b></b>",
		prevQueries: `<set xmlns="http://jabber.org/protocol/rsm"><before>1</before><max>10</max></set>`,
		curQueries:  `<set xmlns="http://jabber.org/protocol/rsm"><first>1</first><last></last></set>`,
	},
}

func TestIter(t *testing.T) {
	for i, tc := range iterTests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			var buf, curQueries, nextQueries, prevQueries strings.Builder
			d := xml.NewDecoder(strings.NewReader(tc.in))
			e := xml.NewEncoder(&buf)
			_, err := d.Token()
			if err != nil {
				t.Fatalf("error popping first token: %v", err)
			}
			iter := paging.NewIter(d, 10)
			nextSet := iter.NextPage()
			if nextSet != nil {
				t.Fatalf("should not start with next page set, got %+v", nextSet)
			}
			for iter.Next() {
				start, r := iter.Current()
				if start != nil {
					err := e.EncodeToken(*start)
					if err != nil {
						t.Fatalf("error encoding start element: %v", err)
					}
				}
				_, err = xmlstream.Copy(e, r)
				if err != nil {
					t.Fatalf("error encoding stream: %v", err)
				}
			}
			if err := iter.Err(); err != nil {
				t.Fatalf("error iterating: %v", err)
			}
			if err := e.Flush(); err != nil {
				t.Fatalf("error flushing output: %v", err)
			}
			// Next
			query, err := xml.Marshal(iter.NextPage())
			if err != nil {
				t.Fatalf("error marshaling next set: %v", err)
			}
			_, err = nextQueries.Write(query)
			if err != nil {
				t.Fatalf("error writing next query: %v", err)
			}
			// Prev
			query, err = xml.Marshal(iter.PreviousPage())
			if err != nil {
				t.Fatalf("error marshaling previous set: %v", err)
			}
			_, err = prevQueries.Write(query)
			if err != nil {
				t.Fatalf("error writing prev query: %v", err)
			}
			// Current
			query, err = xml.Marshal(iter.CurrentPage())
			if err != nil {
				t.Fatalf("error marshaling current set: %v", err)
			}
			_, err = curQueries.Write(query)
			if err != nil {
				t.Fatalf("error writing current query: %v", err)
			}
			if out := buf.String(); out != tc.out {
				t.Errorf("wrong output: want=%s, got=%s", tc.out, out)
			}
			if q := nextQueries.String(); q != tc.nextQueries {
				t.Errorf("wrong next queries:\nwant=%s,\n got=%s", tc.nextQueries, q)
			}
			if q := prevQueries.String(); q != tc.prevQueries {
				t.Errorf("wrong prev queries:\nwant=%s,\n got=%s", tc.prevQueries, q)
			}
			if q := curQueries.String(); q != tc.curQueries {
				t.Errorf("wrong current queries:\nwant=%s,\n got=%s", tc.curQueries, q)
			}
		})
	}
}