-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathxmlreader_test.go
169 lines (151 loc) · 4.68 KB
/
xmlreader_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
package idr
import (
"io"
"strings"
"testing"
"github.com/bradleyjkemp/cupaloy"
"github.com/stretchr/testify/assert"
)
func TestXMLStreamReader_InvalidXPath(t *testing.T) {
sp, err := NewXMLStreamReader(strings.NewReader(""), "[invalid")
assert.Error(t, err)
assert.Equal(t, "invalid xpath '[invalid', err: expression must evaluate to a node-set", err.Error())
assert.Nil(t, sp)
}
func TestXMLStreamReader_SuccessWithXPathWithFilter(t *testing.T) {
s := `
<ROOT xmlns="uri://default" xmlns:t="uri://test">
<AAA>
<CCC>c1</CCC>
<t:BBB>b1</t:BBB>
<DDD>d1</DDD>
<t:BBB>b2<ZZZ z="1">z1</ZZZ></t:BBB>
<t:BBB>b3</t:BBB>
</AAA>
<ZZZ>
<t:BBB>b4</t:BBB>
<CCC>c3</CCC>
</ZZZ>
</ROOT>`
sp, err := NewXMLStreamReader(strings.NewReader(s), "/ROOT/*/t:BBB[. != 'b3']")
assert.NoError(t, err)
assert.Equal(t, 1, sp.AtLine())
// First `<t:BBB>` read
n, err := sp.Read()
assert.NoError(t, err)
assert.Equal(t, "b1", n.InnerText())
t.Run("IDR snapshot after 1st Read", func(t *testing.T) {
cupaloy.SnapshotT(t, JSONify1(rootOf(n)))
})
// intentionally not calling sp.Release() to verify subsequent sp.Read()
// call does the right thing and frees the stream node.
assert.Equal(t, 5, sp.AtLine())
// Second `<t:BBB>` read
n, err = sp.Read()
assert.NoError(t, err)
assert.Equal(t, "b2z1", n.InnerText())
t.Run("IDR snapshot after 2nd Read", func(t *testing.T) {
cupaloy.SnapshotT(t, JSONify1(rootOf(n)))
})
sp.Release(n)
assert.Equal(t, 7, sp.AtLine())
// Third `<t:BBB>` read (Note we will skip 'b3' since the streamElementFilter excludes it)
n, err = sp.Read()
assert.NoError(t, err)
assert.Equal(t, "b4", n.InnerText())
t.Run("IDR snapshot after 3rd Read", func(t *testing.T) {
cupaloy.SnapshotT(t, JSONify1(rootOf(n)))
})
sp.Release(n)
assert.Equal(t, 11, sp.AtLine())
n, err = sp.Read()
assert.Equal(t, io.EOF, err)
assert.Nil(t, n)
}
func TestXMLStreamReader_SuccessWithXPathWithoutFilter(t *testing.T) {
s := `
<ROOT xmlns="uri://default" xmlns:t="uri://test">
<AAA>
<CCC>c1</CCC>
<t:BBB>b1</t:BBB>
<DDD>d1</DDD>
<t:BBB>b2<ZZZ z="1">z1</ZZZ></t:BBB>
<t:BBB>b3</t:BBB>
</AAA>
<ZZZ>
<t:BBB>b4</t:BBB>
<CCC>c3</CCC>
</ZZZ>
</ROOT>`
sp, err := NewXMLStreamReader(strings.NewReader(s), "/ROOT/*/t:BBB")
assert.NoError(t, err)
assert.Equal(t, 1, sp.AtLine())
// First `<t:BBB>` read
n, err := sp.Read()
assert.NoError(t, err)
assert.Equal(t, "b1", n.InnerText())
t.Run("IDR snapshot after 1st Read", func(t *testing.T) {
cupaloy.SnapshotT(t, JSONify1(rootOf(n)))
})
assert.Equal(t, 5, sp.AtLine())
// Second `<t:BBB>` read
n, err = sp.Read()
assert.NoError(t, err)
assert.Equal(t, "b2z1", n.InnerText())
t.Run("IDR snapshot after 2nd Read", func(t *testing.T) {
cupaloy.SnapshotT(t, JSONify1(rootOf(n)))
})
sp.Release(n)
assert.Equal(t, 7, sp.AtLine())
// Third `<t:BBB>` read
n, err = sp.Read()
assert.NoError(t, err)
assert.Equal(t, "b3", n.InnerText())
t.Run("IDR snapshot after 3rd Read", func(t *testing.T) {
cupaloy.SnapshotT(t, JSONify1(rootOf(n)))
})
sp.Release(n)
assert.Equal(t, 8, sp.AtLine())
// Fourth `<t:BBB>` read
n, err = sp.Read()
assert.NoError(t, err)
assert.Equal(t, "b4", n.InnerText())
t.Run("IDR snapshot after 4th Read", func(t *testing.T) {
cupaloy.SnapshotT(t, JSONify1(rootOf(n)))
})
sp.Release(n)
assert.Equal(t, 11, sp.AtLine())
n, err = sp.Read()
assert.Equal(t, io.EOF, err)
assert.Nil(t, n)
}
func TestXMLStreamReader_FailureInvalidElementNodeNamespacePrefix(t *testing.T) {
s := `<ROOT><non_existing:AAA/></ROOT>`
sp, err := NewXMLStreamReader(strings.NewReader(s), "/ROOT/non_existing:AAA")
assert.NoError(t, err)
n, err := sp.Read()
assert.Error(t, err)
assert.Equal(t, "unknown namespace 'non_existing' on ElementNode 'AAA'", err.Error())
assert.Nil(t, n)
// repeat Read() again to show/verify that stream reader will stop processing
// once fatal error is detected.
n, err = sp.Read()
assert.Error(t, err)
assert.Equal(t, "unknown namespace 'non_existing' on ElementNode 'AAA'", err.Error())
assert.Nil(t, n)
}
func TestXMLStreamReader_FailureInvalidAttributeNodeNamespacePrefix(t *testing.T) {
s := `<ROOT xmlns:valid="uri://valid-namespace"><AAA non_existing:attr="test" /></ROOT>`
sp, err := NewXMLStreamReader(strings.NewReader(s), "/ROOT/AAA")
assert.NoError(t, err)
n, err := sp.Read()
assert.Error(t, err)
assert.Equal(t, "unknown namespace 'non_existing' on AttributeNode 'attr'", err.Error())
assert.Nil(t, n)
// repeat Read() again to show/verify that stream reader will stop processing
// once fatal error is detected.
n, err = sp.Read()
assert.Error(t, err)
assert.Equal(t, "unknown namespace 'non_existing' on AttributeNode 'attr'", err.Error())
assert.Nil(t, n)
}