-
Notifications
You must be signed in to change notification settings - Fork 7
/
indenter_test.go
174 lines (160 loc) · 4.07 KB
/
indenter_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
package xmlwriter
import (
"strings"
"testing"
tt "github.com/shabbyrobe/xmlwriter/testtool"
)
func TestIndentElemAttr(t *testing.T) {
result := strings.Join([]string{
"<a>",
" <b foo=\"bar\">",
" <c/>",
" <c/>",
" </b>",
"</a>",
}, "\n")
b, w := open(WithIndent())
must(w.Start(Elem{Name: "a"}))
must(w.Start(Elem{Name: "b", Attrs: []Attr{{Name: "foo", Value: "bar"}}}))
must(w.Write(Elem{Name: "c"}, Elem{Name: "c"}))
must(w.EndAll())
tt.Equals(t, result, str(b, w))
}
func TestIndentElemTextComplex(t *testing.T) {
result := strings.Join([]string{
"<a>",
" <b>Hi my name is <judge/>. Judge <my>",
" <name>",
" <is/>",
" </name> foo bar baz</my></b>",
"</a>",
}, "\n")
b, w := open(WithIndent())
(&ErrCollector{}).Must(
w.Start(Elem{Name: "a"}, Elem{Name: "b"}),
w.Write(Text("Hi my name is "), Elem{Name: "judge"}, Text(". Judge ")),
w.Start(Elem{Name: "my"}, Elem{Name: "name"}),
w.Write(Elem{Name: "is"}),
w.End(ElemNode),
w.Write(Text(" foo bar baz")),
w.EndAll(),
)
tt.Equals(t, result, str(b, w))
}
func TestIndentEmptyInlineBetweenText(t *testing.T) {
result := strings.Join([]string{
"<a>",
" <b>Hi my name is <judge/>.</b>",
"</a>",
}, "\n")
b, w := open(WithIndent())
must(w.Start(Elem{Name: "a"}, Elem{Name: "b"}))
must(w.Write(Text("Hi my name is "), Elem{Name: "judge"}, Text(".")))
must(w.EndAll())
tt.Equals(t, result, str(b, w))
}
func TestIndentEmptyInlineAfterText(t *testing.T) {
result := strings.Join([]string{
"<a>",
" <b>Hi my name is <judge/></b>",
"</a>",
}, "\n")
b, w := open(WithIndent())
must(w.Start(Elem{Name: "a"}, Elem{Name: "b"}))
must(w.Write(Text("Hi my name is "), Elem{Name: "judge"}))
must(w.EndAll())
tt.Equals(t, result, str(b, w))
}
func TestIndentDoc(t *testing.T) {
result := strings.Join([]string{
`<?xml version="1.0" encoding="UTF-8"?>`,
"<a>",
" <b/>",
"</a>",
}, "\n") + "\n"
b, w := open(WithIndent())
must(w.Block(Doc{},
Elem{Name: "a", Content: []Writable{Elem{Name: "b"}}},
))
tt.Equals(t, result, str(b, w))
}
func TestIndentDTD(t *testing.T) {
result := strings.Join([]string{
`<?xml version="1.0" encoding="UTF-8"?>`,
`<!DOCTYPE pants [`,
` <!NOTATION yep SYSTEM "sys">`,
` <!ELEMENT foo EMPTY>`,
` <!ENTITY hi "yep">`,
` <!ATTLIST att`,
` foo CDATA #IMPLIED`,
` bar CDATA #IMPLIED`,
` >`,
`]>`,
`<foo/>`,
}, "\n") + "\n"
b, w := open(WithIndent())
must(w.Start(Doc{}, DTD{Name: "pants"}))
must(w.Write(
Notation{Name: "yep", SystemID: "sys"},
DTDElem{Name: "foo", Decl: DTDElemEmpty},
DTDEntity{Name: "hi", Content: "yep"},
DTDAttList{Name: "att", Attrs: []DTDAttr{
{Name: "foo", Type: DTDAttrString},
{Name: "bar", Type: DTDAttrString},
}},
))
must(w.End(DTDNode))
must(w.Write(Elem{Name: "foo"}))
must(w.EndAll())
tt.Equals(t, result, str(b, w))
}
func TestIndentComment(t *testing.T) {
result := strings.Join([]string{
`<?xml version="1.0" encoding="UTF-8"?>`,
`<a>`,
` <b>`,
` <!--hi how are you-->`,
` </b>`,
`</a>`,
}, "\n") + "\n"
b, w := open(WithIndent())
must(w.Start(Doc{}))
must(w.Start(Elem{Name: "a"}))
must(w.Start(Elem{Name: "b"}))
must(w.Start(Comment{Content: "hi how are you"}))
must(w.EndAll())
tt.Equals(t, result, str(b, w))
}
func TestIndentCData(t *testing.T) {
result := strings.Join([]string{
`<?xml version="1.0" encoding="UTF-8"?>`,
`<a>`,
` <b><![CDATA[hi how are you]]></b>`,
`</a>`,
}, "\n") + "\n"
b, w := open(WithIndent())
must(w.Start(Doc{}))
must(w.Start(Elem{Name: "a"}))
must(w.Start(Elem{Name: "b"}))
must(w.Start(CData{Content: "hi how are you"}))
must(w.EndAll())
tt.Equals(t, result, str(b, w))
}
func TestIndentRaw(t *testing.T) {
result := strings.Join([]string{
`<?xml version="1.0" encoding="UTF-8"?>`,
`wat<awat a="b"wat/>`,
}, "\n") + "\n"
b, w := open(WithIndent())
// TODO: more permutations of elements
(&ErrCollector{}).Must(
w.Start(Doc{}),
w.Write(Raw("wat")),
w.Start(Elem{Name: "a"}),
w.Write(Raw("wat")),
w.Write(Attr{Name: "a", Value: "b"}),
w.Write(Raw("wat")),
w.EndAll(),
)
tt.Equals(t, result, str(b, w))
}