-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
token_test.go
161 lines (149 loc) · 3.03 KB
/
token_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
package xmltokenizer_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/muktihari/xmltokenizer"
)
func TestGetToken(t *testing.T) {
alloc := testing.AllocsPerRun(10, func() {
token := xmltokenizer.GetToken()
xmltokenizer.PutToken(token)
})
if alloc != 0 {
t.Fatalf("expected alloc: 0, got: %g", alloc)
}
}
func TestIsEndElement(t *testing.T) {
tt := []struct {
name string
token xmltokenizer.Token
expected bool
}{
{
name: "an end element",
token: xmltokenizer.Token{
Name: xmltokenizer.Name{
Full: []byte("worksheet"),
},
IsEndElement: true,
},
expected: true,
},
{
name: "a start element",
token: xmltokenizer.Token{
Name: xmltokenizer.Name{
Full: []byte("worksheet"),
},
},
expected: false,
},
{
name: "a procinst",
token: xmltokenizer.Token{
Name: xmltokenizer.Name{
Full: []byte("?xml"),
},
},
expected: false,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
if r := tc.token.IsEndElement; r != tc.expected {
t.Fatalf("expected: %t, got: %t", tc.expected, r)
}
})
}
}
func TestIsEndElementOf(t *testing.T) {
tt := []struct {
name string
t1, t2 xmltokenizer.Token
expected bool
}{
{
name: "correct end element",
t1: xmltokenizer.Token{
Name: xmltokenizer.Name{
Full: []byte("worksheet"),
},
IsEndElement: true,
},
t2: xmltokenizer.Token{
Name: xmltokenizer.Name{
Full: []byte("worksheet"),
},
},
expected: true,
},
{
name: "incorrect end element",
t1: xmltokenizer.Token{
Name: xmltokenizer.Name{
Full: []byte("/gpx"),
},
},
t2: xmltokenizer.Token{
Name: xmltokenizer.Name{
Full: []byte("worksheet"),
},
},
expected: false,
},
{
name: "not even an end element",
t2: xmltokenizer.Token{
Name: xmltokenizer.Name{
Full: []byte("worksheet"),
},
},
t1: xmltokenizer.Token{
Name: xmltokenizer.Name{
Full: []byte("worksheet"),
},
},
expected: false,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
if r := tc.t1.IsEndElementOf(&tc.t2); r != tc.expected {
t.Fatalf("expected: %t, got: %t", tc.expected, r)
}
})
}
}
func TestCopy(t *testing.T) {
t1 := xmltokenizer.Token{
Name: xmltokenizer.Name{
Prefix: []byte("gpxtpx"),
Local: []byte("hr"),
Full: []byte("gpxtpx:hr"),
},
Attrs: []xmltokenizer.Attr{{
Name: xmltokenizer.Name{
Prefix: nil,
Local: []byte("units"),
Full: []byte("units"),
},
Value: []byte("bpm"),
}},
Data: []byte("70"),
}
var t2 xmltokenizer.Token
t2.Copy(t1)
if diff := cmp.Diff(t2, t1); diff != "" {
t.Fatal(diff)
}
t2.Name.Full = append(t2.Name.Full[:0], "asd"...)
t2.Data = append(t2.Data[:0], "60"...)
if diff := cmp.Diff(t2, t1); diff == "" {
t.Fatalf("expected different, got same")
}
// Test shallow copy, it should change the original
t2.Attrs[0].Name.Full[0] = 'i'
if diff := cmp.Diff(t2.Attrs, t1.Attrs); diff != "" {
t.Fatal(diff)
}
}