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
|
package xmpp
import (
"encoding/xml"
"strings"
"testing"
)
// bufTest is a struct containing a test point for the
// xengineering.eu/limox/xmpp.elementBuffer. It contains a test XML string
// which has to be exactly one XML element and an array of indentation levels
// which have to be checked after each token which is parsed.
type bufTest struct {
xml string
levels []int
}
func TestElementBuffer(t *testing.T) {
tests := []bufTest{
bufTest{`<stream></stream>`, []int{1, 0}},
bufTest{`<stream/>`, []int{1, 0}},
bufTest{`<a><b>testing</b></a>`, []int{1, 2, 2, 1, 0}},
bufTest{`<a><!-- comment --><b>testing</b></a>`, []int{1, 1, 2, 2, 1, 0}},
bufTest{`<!-- comment --><a><b>testing</b></a>`, []int{0, 1, 2, 2, 1, 0}},
}
for _, v := range tests {
r := strings.NewReader(v.xml)
d := xml.NewDecoder(r)
b := newElementBuffer()
i := 0
for {
token, err := d.Token()
if err != nil {
if i != len(v.levels) {
t.Fatalf("Stopped parsing at unexpected index due to error `%v`!\n", err)
}
break
}
err = b.add(token)
if err != nil {
t.Fatalf("add(token) failed with error `%v`!\n", err)
}
if b.level != v.levels[i] {
t.Fatalf("Indent level of xmpp.elementBuffer %d does not match value given by test data %d!\n", b.level, v.levels[i])
}
i += 1
}
}
}
|