diff options
author | xengineering <me@xengineering.eu> | 2023-06-29 21:39:54 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-06-29 21:39:54 +0200 |
commit | c8544b19df055235b9106ff296f0a5fe7cb1fe91 (patch) | |
tree | b3da91a3d41746620ca67891797b55df8edfdc09 /xmpp/element_buffer_test.go | |
parent | 9afa580f3f3207ac449be86c1a305cb716b77f76 (diff) | |
parent | 3df1a88c726c08704e8b71c467bd8e11c9a52db6 (diff) | |
download | limox-c8544b19df055235b9106ff296f0a5fe7cb1fe91.tar limox-c8544b19df055235b9106ff296f0a5fe7cb1fe91.tar.zst limox-c8544b19df055235b9106ff296f0a5fe7cb1fe91.zip |
Merge branch 'element-buffer'
This adds xengineering.eu/limox/xmpp/elementBuffer which is a buffer
for a collection of XML tokens which are further processed after a full
XML element is collected.
Diffstat (limited to 'xmpp/element_buffer_test.go')
-rw-r--r-- | xmpp/element_buffer_test.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/xmpp/element_buffer_test.go b/xmpp/element_buffer_test.go new file mode 100644 index 0000000..113a2c4 --- /dev/null +++ b/xmpp/element_buffer_test.go @@ -0,0 +1,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 + } + } +} |