-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
reader_test.go
133 lines (129 loc) · 2.75 KB
/
reader_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
// 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
import (
"encoding/xml"
"fmt"
"io"
"strings"
"testing"
)
var innerReaderTests = [...]struct {
R io.Reader
Read string
Err error
}{
0: {
R: strings.NewReader(``),
Read: ``,
Err: nil,
},
1: {
R: strings.NewReader(`<test></test>`),
Read: ``,
Err: nil,
},
2: {
R: strings.NewReader(`<test><inner/></test>`),
Read: `<inner/>`,
Err: nil,
},
3: {
R: strings.NewReader(`<test>Inner</test>`),
Read: `Inner`,
Err: nil,
},
4: {
R: strings.NewReader(`<test>Inner</oops>`),
Read: `Inner`,
Err: unexpectedEndError{xml.Name{Local: "oops"}},
},
5: {
R: strings.NewReader(`<stream xmlns="stream">Test</stream>`),
Read: `Test`,
Err: nil,
},
6: {
R: strings.NewReader(`<stream:stream><stream:features></stream:stream>`),
Read: `<stream:features>`,
Err: nil,
},
7: {
R: strings.NewReader(`<stream:stream><stream:features> <stream:stream>`),
Read: `<stream:features>`,
Err: errNotEnd,
},
8: {
R: strings.NewReader(`<stream:stream><stream:features><stream:stream>`),
Read: `<stream:features`,
Err: errNotEnd,
},
9: {
R: strings.NewReader(`</stream:stream>`),
Read: ``,
Err: errNotStart,
},
10: {
R: strings.NewReader(`<!-- Test -->`),
Read: ``,
Err: errNotStart,
},
11: {
R: strings.NewReader(`What is dis junk?`),
Read: ``,
Err: errNotStart,
},
12: {
R: strings.NewReader(`<test/>`),
Read: ``,
Err: nil,
},
13: {
R: strings.NewReader(`<test:test/>`),
Read: ``,
Err: nil,
},
14: {
R: strings.NewReader(`<stream:stream></stream:oops>`),
Read: ``,
Err: unexpectedEndError{xml.Name{Local: "oops", Space: "stream"}},
},
15: {
R: strings.NewReader(`<stream:stream></oops:stream>`),
Read: ``,
Err: unexpectedEndError{xml.Name{Local: "stream", Space: "oops"}},
},
16: {
R: strings.NewReader(`<stream:stream>Inner</stream:oops>`),
Read: `Inn`,
Err: errNotEnd,
},
17: {
R: strings.NewReader(`<stream:stream></stream:oooooooooooo>`),
Read: `</stre`,
Err: errNotEnd,
},
18: {
R: strings.NewReader(`<stream:stream></oooooooooooo:stream>`),
Read: `</oooo`,
Err: errNotEnd,
},
}
func TestInnerReader(t *testing.T) {
for i, tc := range innerReaderTests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
ir := InnerReader(tc.R)
if ir == nil {
t.Fatal("InnerReader returned nil reader")
}
b, err := io.ReadAll(ir)
if err != tc.Err {
t.Errorf("Unxpected error: want=`%v`, got=`%v`", tc.Err, err)
}
if string(b) != tc.Read {
t.Errorf("Unexpected value read: want=`%s`, got=`%s`", tc.Read, b)
}
})
}
}