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
|
package xmpp
import (
"encoding/xml"
"strings"
"testing"
)
// routerTest contains a single test case for the xmpp.router. The XML element
// is given as a string and the value int describes the expected value of a
// test variable inside the corresponding unit test. See TestRouter for
// details.
type routerTest struct {
xml string
value int
}
// TestRouter tests the xmpp/router.go file. The central functionality is the
// route() function. To test this a fake routing table is created inside this
// test with XML element handler function produced by the factory function
// defined below. each handler simply sets the value of the testpoint variable
// to the value mentioned by the test point. That way the routing can be
// validated.
func TestRouter(t *testing.T) {
var testpoint int
var s session
factory := func(tp *int, i int) func(*session, []xml.Token) {
return func(*session, []xml.Token) {
*tp = i
}
}
tests := []routerTest{
routerTest{`<a></a>`, 1},
routerTest{`<b></b>`, 2},
routerTest{`<c></c>`, 3},
routerTest{`<b xmlns='https://xengineering.eu'></b>`, 4},
routerTest{`<a xmlns='https://xengineering.eu'></a>`, 5},
}
testRouting := routingTable{
{xml.Name{``, `a`}, factory(&testpoint, 1)},
{xml.Name{``, `b`}, factory(&testpoint, 2)},
{xml.Name{``, `c`}, factory(&testpoint, 3)},
{xml.Name{`https://xengineering.eu`, `b`}, factory(&testpoint, 4)},
{xml.Name{`https://xengineering.eu`, `a`}, factory(&testpoint, 5)},
}
for _, v := range tests {
testpoint = 0
r := strings.NewReader(v.xml)
d := xml.NewDecoder(r)
tokens := make([]xml.Token, 0)
for {
token, err := d.Token()
if err != nil {
break
}
tokens = append(tokens, xml.CopyToken(token))
}
route(&s, tokens, testRouting)
if testpoint != v.value {
t.Fatalf("XML element was not routed correctly!\n")
}
}
}
|