-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmltest_test.go
191 lines (182 loc) · 5.2 KB
/
xmltest_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xmltest
import (
"bytes"
"errors"
"strings"
"testing"
)
func TestNormalize(t *testing.T) {
testCases := []struct {
desc string
n Normalizer
in string
wantXML string
wantErr error
}{{
desc: "single root element in default namespace",
in: "<root/>",
wantXML: "<root></root>",
}, {
desc: "single root element in namespace",
in: `<root xmlns="space"/>`,
wantXML: `<space:root xmlns:space="space"></space:root>`,
}, {
desc: "single root element in prefixed namespace",
in: `<s:root xmlns:s="space"/>`,
wantXML: `<space:root xmlns:space="space"></space:root>`,
}, {
desc: "element with inherited prefixed namespace",
in: `<s:root xmlns:s="space" xmlns:f="foons"><f:foo/></s:root>`,
wantXML: `` +
`<space:root xmlns:space="space">` +
`<foons:foo xmlns:foons="foons"></foons:foo>` +
`</space:root>`,
}, {
desc: "preserve attributes except xmlns",
in: `<root xmlns:i="ignored" a="foo"/>`,
wantXML: `<root a="foo"></root>`,
}, {
desc: "preserve attribute namespaces",
in: `<root xmlns:i="ignored" xmlns:b="bar" b:a="foo"/>`,
wantXML: `<root xmlns:bar="bar" bar:a="foo"></root>`,
}, {
desc: "sort attributes in lexical order",
in: `<root` +
` xmlns:a="a" xmlns:b="b" xmlns:c="c"` +
` c:bar="bar" a:baz="baz" b:bam="bam" a:bam="bam">` +
`</root>`,
wantXML: `` +
`<root` +
` xmlns:c="c" xmlns:b="b" xmlns:a="a"` +
` a:bam="bam" a:baz="baz" b:bam="bam" c:bar="bar">` +
`</root>`,
}, {
desc: "omit directives",
in: `<!DOCTYPE foo><root/>`,
wantXML: `<root></root>`,
}, {
desc: "omit preamble",
in: `<?xml version="1.0"?><root/>`,
wantXML: `<root></root>`,
}, {
desc: "omit processing instruction",
in: `<root><?foo?></root>`,
wantXML: `<root></root>`,
}, {
desc: "keep comments by default",
in: `<root><!-- a comment --></root>`,
wantXML: `<root><!-- a comment --></root>`,
}, {
desc: "omit comments if requested",
n: Normalizer{OmitComments: true},
in: `<root><!-- a comment --></root>`,
wantXML: `<root></root>`,
}, {
desc: "keep whitespace by default",
in: `<root> <foo> </foo> </root>`,
wantXML: `<root> <foo> </foo> </root>`,
}, {
desc: "omit whitespace if requested",
n: Normalizer{OmitWhitespace: true},
in: `<root> <foo> </foo> </root>`,
wantXML: `<root><foo></foo></root>`,
}, {
desc: "omit whitespace cdata but don't trim",
n: Normalizer{OmitWhitespace: true},
in: `<root> <foo> </foo> a </root>`,
wantXML: `<root><foo></foo> a </root>`,
}, {
desc: "bad: make decoder fail with a syntax error",
in: "<root></foo>",
wantErr: errors.New("some error"),
}}
for _, tc := range testCases {
var b bytes.Buffer
err := tc.n.Normalize(&b, strings.NewReader(tc.in))
if tc.wantErr != nil {
if err == nil {
t.Errorf("%s: got nil error, want %v", tc.desc, tc.wantErr)
}
continue
}
if err != nil {
t.Errorf("%s: got err %v, want nil", tc.desc, err)
continue
}
if got, want := b.String(), tc.wantXML; got != want {
t.Errorf("%s:\ngot %s\nwant %s", tc.desc, got, tc.wantXML)
}
}
}
type errorWriter struct{}
func (w *errorWriter) Write(buf []byte) (n int, err error) {
return 0, errors.New("Why have I always been a failure?")
}
func TestNormalizeFailWriter(t *testing.T) {
// This test makes the Normalizer fail to write to the Writer.
// TBH it mainly exists to pimp test coverage.
var n Normalizer
err := n.Normalize(&errorWriter{}, strings.NewReader("<root/>"))
if err == nil {
t.Errorf("failwriter: got nil error, want non-nil")
}
}
func TestEqualXML(t *testing.T) {
testCases := []struct {
desc string
a, b string
n Normalizer
wantEqual bool
wantErr error
}{{
desc: "identity",
a: "<root/>",
b: "<root/>",
wantEqual: true,
// TODO define more test cases for EqualXML
}, {
desc: "prefixed vs default namespace",
a: `<s:root xmlns:s="space"/>`,
b: `<root xmlns="space"/>`,
wantEqual: true,
}, {
desc: "prefixed namespaces",
a: `<s:root xmlns:s="space"/>`,
b: `<t:root xmlns:t="space"/>`,
wantEqual: true,
}, {
desc: "ignored prefixes",
a: `<s:root xmlns:s="space" xmlns:f="foo"/>`,
b: `<root xmlns="space"/>`,
wantEqual: true,
}, {
desc: "whitespace not omitted by default",
a: `<root> </root>`,
b: `<root/>`,
}, {
desc: "omit whitespace with custom normalizer",
n: Normalizer{OmitWhitespace: true},
a: `<root> </root>`,
b: `<root/>`,
wantEqual: true,
}}
for _, tc := range testCases {
got, err := tc.n.EqualXML(strings.NewReader(tc.a), strings.NewReader(tc.b))
if tc.wantErr != nil {
if err == nil {
t.Errorf("%s: got nil error, want %v", tc.desc, tc.wantErr)
}
continue
}
if err != nil {
t.Errorf("%s: got err %v, want nil", tc.desc, err)
continue
}
if got != tc.wantEqual {
t.Errorf("%s:\ngot %v\nwant %v", tc.desc, got, tc.wantEqual)
}
}
}