File: dom_test.go

package info (click to toggle)
golang-github-masterzen-simplexml 0.0~git20160608.0.4572e39-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 80 kB
  • ctags: 31
  • sloc: makefile: 2
file content (105 lines) | stat: -rw-r--r-- 2,493 bytes parent folder | download | duplicates (2)
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
package dom

import (
	. "gopkg.in/check.v1"
	"testing"
)

// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }

type DomSuite struct{}

var _ = Suite(&DomSuite{})

func (s *DomSuite) TestEmptyDocument(c *C) {
	doc := CreateDocument()
	doc.PrettyPrint = true
	c.Assert(doc.String(), Equals, "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n")
}

func (s *DomSuite) TestOneEmptyNode(c *C) {
	doc := CreateDocument()
	doc.PrettyPrint = true
	root := CreateElement("root")
	doc.SetRoot(root)
	c.Assert(doc.String(), Equals, "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<root/>\n")
}

func (s *DomSuite) TestMoreNodes(c *C) {
	doc := CreateDocument()
	doc.PrettyPrint = true
	root := CreateElement("root")
	node1 := CreateElement("node1")
	root.AddChild(node1)
	subnode := CreateElement("sub")
	node1.AddChild(subnode)
	node2 := CreateElement("node2")
	root.AddChild(node2)
	doc.SetRoot(root)
	
	expected := `<?xml version="1.0" encoding="utf-8" ?>
<root>
  <node1>
    <sub/>
  </node1>
  <node2/>
</root>
`
	
	c.Assert(doc.String(), Equals, expected)
}

func (s *DomSuite) TestAttributes(c *C) {
	doc := CreateDocument()
	doc.PrettyPrint = true
	root := CreateElement("root")
	node1 := CreateElement("node1")
	node1.SetAttr("attr1", "pouet")
	root.AddChild(node1)
	doc.SetRoot(root)
	
	expected := `<?xml version="1.0" encoding="utf-8" ?>
<root>
  <node1 attr1="pouet"/>
</root>
`
	c.Assert(doc.String(), Equals, expected)
}

func (s *DomSuite) TestContent(c *C) {
	doc := CreateDocument()
	doc.PrettyPrint = true
	root := CreateElement("root")
	node1 := CreateElement("node1")
	node1.SetContent("this is a text content")
	root.AddChild(node1)
	doc.SetRoot(root)
	
	expected := `<?xml version="1.0" encoding="utf-8" ?>
<root>
  <node1>this is a text content</node1>
</root>
`
	c.Assert(doc.String(), Equals, expected)
}

func (s *DomSuite) TestNamespace(c *C) {
	doc := CreateDocument()
	doc.PrettyPrint = true
	root := CreateElement("root")
	root.DeclareNamespace(Namespace { Prefix: "a", Uri: "http://schemas.xmlsoap.org/ws/2004/08/addressing"})
	node1 := CreateElement("node1")
	root.AddChild(node1)
	node1.SetNamespace("a", "http://schemas.xmlsoap.org/ws/2004/08/addressing")
	node1.SetContent("this is a text content")
	doc.SetRoot(root)
	
	expected := `<?xml version="1.0" encoding="utf-8" ?>
<root xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing">
  <a:node1>this is a text content</a:node1>
</root>
`
	c.Assert(doc.String(), Equals, expected)
}