-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
transformer_test.go
313 lines (294 loc) · 9.44 KB
/
transformer_test.go
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2016 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 xmlstream_test
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"strings"
"testing"
"mellium.im/xmlstream"
)
var _ xmlstream.Encoder = (*xml.Encoder)(nil)
var _ xmlstream.Decoder = (*xml.Decoder)(nil)
func TestDiscard(t *testing.T) {
d := xmlstream.Discard()
if err := d.EncodeToken(xml.StartElement{}); err != nil {
t.Errorf("Unexpected error while discarding token: %v", err)
}
}
type tokenizerTest struct {
Transform xmlstream.Transformer
Input string
Output string
Err bool
}
func runTests(t *testing.T, tcs []tokenizerTest) {
for i, tc := range tcs {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
d := tc.Transform(xml.NewDecoder(strings.NewReader(tc.Input)))
buf := new(bytes.Buffer)
e := xml.NewEncoder(buf)
var tok xml.Token
var err error
decodeloop:
for err == nil {
tok, err = d.Token()
switch {
case err != nil && err != io.EOF && !tc.Err:
t.Fatalf("Unexpected error decoding token: %s", err)
case err == io.EOF:
break decodeloop
}
err = e.EncodeToken(tok)
}
e.Flush()
switch {
case err != nil && err != io.EOF && tc.Err:
case (err == io.EOF || err == nil) && tc.Err:
t.Fatal("Expected error, but did not get one")
case buf.String() != tc.Output:
t.Fatalf("got=`%s`, want=`%s`", buf.String(), tc.Output)
}
})
}
}
func TestInspect(t *testing.T) {
tokens := 0
inspector := xmlstream.Inspect(func(t xml.Token) {
tokens++
})
d := inspector(xml.NewDecoder(strings.NewReader(`<quote>Now Jove,<br/>in his next commodity of hair, send thee a beard!</quote>`)))
for tok, err := d.Token(); err != io.EOF; tok, err = d.Token() {
if start, ok := tok.(xml.StartElement); ok && start.Name.Local == "br" {
if err = xmlstream.Skip(d); err == io.EOF {
break
}
}
}
if tokens != 6 {
t.Fatalf("Got %d tokens but expected 6", tokens)
}
}
func TestRemove(t *testing.T) {
runTests(t, []tokenizerTest{
{
Transform: xmlstream.Remove(func(t xml.Token) bool { return true }),
Input: `<quote>Foolery, sir, does walk about the orb like the sun,<br/>it shines every where.</quote>`,
Output: ``,
Err: false,
},
{
Transform: xmlstream.Remove(func(t xml.Token) bool {
if _, ok := t.(xml.CharData); ok {
return false
}
return true
}),
Input: `<quote>Now Jove, in his next commodity of hair, send thee a beard!</quote>`,
Output: `Now Jove, in his next commodity of hair, send thee a beard!`,
Err: false,
},
{
Transform: xmlstream.Remove(func(t xml.Token) bool {
if _, ok := t.(xml.StartElement); ok {
return true
}
return false
}),
Input: `<quote>Now Jove, in his next commodity of hair, send thee a beard!</quote>`,
Output: ``,
Err: true,
},
})
}
func TestMap(t *testing.T) {
runTests(t, []tokenizerTest{
{
Transform: xmlstream.Map(func(t xml.Token) xml.Token {
switch tok := t.(type) {
case xml.StartElement:
if tok.Name.Local == "quote" {
tok.Name.Local = "blocking"
return tok
}
case xml.EndElement:
if tok.Name.Local == "quote" {
tok.Name.Local = "blocking"
return tok
}
}
return t
}),
Input: `<quote>[Re-enter Clown with a letter, and FABIAN]</quote>`,
Output: `<blocking>[Re-enter Clown with a letter, and FABIAN]</blocking>`,
Err: false,
},
})
}
func TestRemoveAttr(t *testing.T) {
runTests(t, []tokenizerTest{
{
Transform: xmlstream.RemoveAttr(func(_ xml.StartElement, _ xml.Attr) bool {
return false
}),
Input: `<quote act="1" scene="5" character="Feste">Let her hang me: he that is well hanged in this world needs to fear no colours.</quote>`,
Output: `<quote act="1" scene="5" character="Feste">Let her hang me: he that is well hanged in this world needs to fear no colours.</quote>`,
Err: false,
},
{
Transform: xmlstream.RemoveAttr(func(_ xml.StartElement, _ xml.Attr) bool {
return true
}),
Input: `<!-- Quote --><quote act="1" scene="5" cite="Feste">Let her hang me: he that is well hanged in this world needs to fear no colours.</quote>`,
Output: `<!-- Quote --><quote>Let her hang me: he that is well hanged in this world needs to fear no colours.</quote>`,
Err: false,
},
{
Transform: xmlstream.RemoveAttr(func(_ xml.StartElement, a xml.Attr) bool {
return a.Name.Local == "cite"
}),
Input: `<quote act="1" cite="Feste" scene="5">Let her hang me: he that is well hanged in this world needs to fear no colours.</quote>`,
Output: `<quote act="1" scene="5">Let her hang me: he that is well hanged in this world needs to fear no colours.</quote>`,
Err: false,
},
{
Transform: xmlstream.RemoveAttr(func(start xml.StartElement, a xml.Attr) bool {
return start.Name.Local == "quote" && a.Name.Local == "cite"
}),
Input: `<text cite="Feste"></text><quote act="1" cite="Feste" scene="5">Let her hang me: he that is well hanged in this world needs to fear no colours.</quote>`,
Output: `<text cite="Feste"></text><quote act="1" scene="5">Let her hang me: he that is well hanged in this world needs to fear no colours.</quote>`,
Err: false,
},
})
}
var insertTestCases = [...]struct {
name xml.Name
in string
out string
}{
0: {},
1: {
name: xml.Name{Space: "jabber:client", Local: "message"},
in: `<message xmlns="jabber:client"/>`,
out: `<message xmlns="jabber:client"><test></test></message>`,
},
2: {
name: xml.Name{Space: "jabber:server", Local: "message"},
in: `<message xmlns="jabber:server"/><message xmlns="jabber:server"><body>test</body></message><message></message>`,
out: `<message xmlns="jabber:server"><test></test></message><message xmlns="jabber:server"><body xmlns="jabber:server">test</body><test></test></message><message></message>`,
},
3: {
name: xml.Name{Space: "jabber:server", Local: "message"},
in: `<message xmlns="jabber:badns"/>`,
out: `<message xmlns="jabber:badns"></message>`,
},
4: {
name: xml.Name{Space: "", Local: "message"},
in: `<message xmlns="urn:example"/><message/>`,
out: `<message xmlns="urn:example"><test></test></message><message><test></test></message>`,
},
5: {
name: xml.Name{Space: "urn:example", Local: ""},
in: `<message xmlns="urn:example"/><presence/><iq xmlns="urn:example"/>`,
out: `<message xmlns="urn:example"><test></test></message><presence></presence><iq xmlns="urn:example"><test></test></iq>`,
},
6: {
name: xml.Name{Space: "", Local: ""},
in: `<message xmlns="urn:example"/><presence/><iq xmlns="urn:example"/>`,
out: `<message xmlns="urn:example"><test></test></message><presence><test></test></presence><iq xmlns="urn:example"><test></test></iq>`,
},
}
type testPayload struct{}
func (testPayload) TokenReader() xml.TokenReader {
return xmlstream.Wrap(
nil,
xml.StartElement{Name: xml.Name{Local: "test"}},
)
}
func TestInsert(t *testing.T) {
for i, tc := range insertTestCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
inserter := xmlstream.Insert(tc.name, testPayload{})
r := inserter(xml.NewDecoder(strings.NewReader(tc.in)))
// Prevent duplicate xmlns attributes. See https://mellium.im/issue/75
r = xmlstream.RemoveAttr(func(start xml.StartElement, attr xml.Attr) bool {
return (start.Name.Local == "message" || start.Name.Local == "iq") && attr.Name.Local == "xmlns"
})(r)
var buf strings.Builder
e := xml.NewEncoder(&buf)
_, err := xmlstream.Copy(e, r)
if err != nil {
t.Fatalf("error encoding: %v", err)
}
if err = e.Flush(); err != nil {
t.Fatalf("error flushing: %v", err)
}
if out := buf.String(); tc.out != out {
t.Errorf("wrong output:\nwant=%s,\n got=%s", tc.out, out)
}
})
}
}
var insertFTestCases = [...]struct {
f func(xml.StartElement, uint64, xmlstream.TokenWriter) error
in string
out string
}{
0: {},
1: {
f: func(start xml.StartElement, depth uint64, w xmlstream.TokenWriter) error {
return nil
},
in: `<message foo="bar"><foo/></message>`,
out: `<message foo="bar"><foo></foo></message>`,
},
2: {
in: `<message foo="bar"><foo/></message>`,
out: `<message foo="bar"><foo></foo></message>`,
},
3: {
f: func(start xml.StartElement, depth uint64, w xmlstream.TokenWriter) error {
start = xml.StartElement{Name: xml.Name{Local: "test"}}
w.EncodeToken(start)
w.EncodeToken(start.End())
return nil
},
in: `<message foo="bar"><foo/></message>`,
out: `<message foo="bar"><test></test><foo><test></test></foo></message>`,
},
4: {
f: func(start xml.StartElement, depth uint64, w xmlstream.TokenWriter) error {
if depth == 2 {
start = xml.StartElement{Name: xml.Name{Local: "test"}}
w.EncodeToken(start)
w.EncodeToken(start.End())
}
return nil
},
in: `<message foo="bar"><foo/></message>`,
out: `<message foo="bar"><foo><test></test></foo></message>`,
},
}
func TestInsertFunc(t *testing.T) {
for i, tc := range insertFTestCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
inserter := xmlstream.InsertFunc(tc.f)
r := inserter(xml.NewDecoder(strings.NewReader(tc.in)))
var buf strings.Builder
e := xml.NewEncoder(&buf)
_, err := xmlstream.Copy(e, r)
if err != nil {
t.Fatalf("error encoding: %v", err)
}
if err = e.Flush(); err != nil {
t.Fatalf("error flushing: %v", err)
}
if out := buf.String(); tc.out != out {
t.Errorf("wrong output:\nwant=%s,\n got=%s", tc.out, out)
}
})
}
}