-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder_test.go
More file actions
180 lines (164 loc) · 3.98 KB
/
builder_test.go
File metadata and controls
180 lines (164 loc) · 3.98 KB
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
package xmlbuilder
import (
"bytes"
"io/ioutil"
"os"
"testing"
)
func readString(filename string) string {
buf, _ := ioutil.ReadFile(filename)
return string(buf)
}
func TestTag(t *testing.T) {
buf := &bytes.Buffer{}
xml := New(buf)
xml.Tag("Joran")
if a, b := "<Joran />\n", buf.String(); a != b {
t.Errorf("%s and %s are not equal", a, b)
}
}
func TestAttributes(t *testing.T) {
buf := &bytes.Buffer{}
xml := New(buf)
xml.Tag("person", "name", "Joran", "age", 40)
if a, b := `<person name="Joran" age="40" />`+"\n", buf.String(); a != b {
t.Errorf("%s and %s are not equal", a, b)
}
}
func TestElementAttr(t *testing.T) {
buf := &bytes.Buffer{}
xml := New(buf)
xml.Element("person")
xml.Attr("name", "Joran")
xml.Attr("age", 40)
xml.End()
if a, b := `<person name="Joran" age="40" />`+"\n", buf.String(); a != b {
t.Errorf("%s and %s are not equal", a, b)
}
}
func TestNestedElement(t *testing.T) {
buf := &bytes.Buffer{}
xml := New(buf)
xml.Element("person", "name", "Joran")
{
xml.Tag("tel", "nr", "1276536271")
xml.Element("tel", "nr", "1232123212").End()
}
xml.End()
if a, b := readString("test/nested_element.xml"), buf.String(); a != b {
t.Errorf("%s and %s are not equal", a, b)
}
}
func TestChars(t *testing.T) {
buf := &bytes.Buffer{}
xml := New(buf)
xml.Element("person", "name", "Joran").Chars("Hello!").End()
if a, b := readString("test/chars.xml"), buf.String(); a != b {
t.Errorf("%s and %s are not equal", a, b)
}
buf = &bytes.Buffer{}
xml = New(buf)
xml.Element("person", "Hello!", "name", "Joran").End()
if a, b := readString("test/chars.xml"), buf.String(); a != b {
t.Errorf("%s and %s are not equal", a, b)
}
buf = &bytes.Buffer{}
xml = New(buf)
xml.Tag("person", "Hello!", "name", "Joran")
if a, b := readString("test/chars_inline.xml"), buf.String(); a != b {
t.Errorf("%s and %s are not equal", a, b)
}
}
func TestAttr(t *testing.T) {
buf := &bytes.Buffer{}
xml := New(buf)
xml.Element("person", "name", "Joran")
xml.Attr("age", 40)
xml.End()
if a, b := `<person name="Joran" age="40" />`+"\n", buf.String(); a != b {
t.Errorf("%s and %s are not equal", a, b)
}
buf = &bytes.Buffer{}
xml = New(buf)
xml.Attr("age", 40)
xml.Element("person", "name", "Joran")
xml.End()
if a, b := `<person age="40" name="Joran" />`+"\n", buf.String(); a != b {
t.Errorf("%s and %s are not equal", a, b)
}
}
func TestInstructXML(t *testing.T) {
buf := &bytes.Buffer{}
xml := New(buf)
xml.InstructXML()
xml.Element("address")
{
xml.Attr("id", 12)
xml.Tag("street", "Some street")
xml.Tag("city", "Eindhoven")
xml.Tag("phone", "1298376142", "type", "mobile")
}
xml.End()
if a, b := readString("test/instructxml.xml"), buf.String(); a != b {
t.Errorf("%s and %s are not equal", a, b)
}
}
func TestNotPretty(t *testing.T) {
buf := &bytes.Buffer{}
xml := New(buf).Inline()
xml.Element("address")
{
xml.Attr("id", 12)
xml.Tag("street", "Some street")
xml.Tag("city", "Eindhoven")
xml.Tag("phone", "1298376142", "type", "mobile")
}
xml.End()
if a, b := readString("test/not_pretty.xml"), buf.String(); a != b {
t.Errorf("%s and %s are not equal", a, b)
}
}
func Example() {
xml := New(os.Stdout)
xml.Element("people")
{
xml.Element("person", "id", 1)
{
xml.Tag("name", "Joran")
xml.Tag("age", 40)
}
xml.End()
}
xml.End()
// Output:
// <people>
// <person id="1">
// <name>Joran</name>
// <age>40</age>
// </person>
// </people>
}
func ExampleDoctype() {
xml := New(os.Stdout)
xml.Doctype(DoctypeHTML5)
xml.Inline().Element("p").Chars("Hello").End().EndInline()
// Output:
// <!DOCTYPE html>
// <p>Hello</p>
}
func ExampleInline() {
xml := New(os.Stdout)
xml.Doctype(DoctypeHTML5)
xml.Element("ul")
{
xml.Inline().Element("li").Chars("Hello ").Tag("b", "there").End().EndInline()
xml.Inline().Element("li").Chars("Test ").Tag("b", "this").End().EndInline()
}
xml.End()
// Output:
// <!DOCTYPE html>
// <ul>
// <li>Hello <b>there</b></li>
// <li>Test <b>this</b></li>
// </ul>
}