-
Notifications
You must be signed in to change notification settings - Fork 0
/
attr_test.go
156 lines (141 loc) · 4.09 KB
/
attr_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
package dom
import (
"testing"
)
// Tests basic getters etc.
func TestAttrGetters(t *testing.T) {
doc := NewDocument()
elem, _ := doc.CreateElement("tag")
doc.AppendChild(elem)
a, _ := doc.CreateAttributeNS("http://example.org/lol", "pfx:cruft")
elem.SetAttributeNode(a)
a.SetValue("valval")
if a.GetName() != "pfx:cruft" {
t.Error("incorrect node name")
}
if a.GetNodeName() != "pfx:cruft" {
t.Error("incorrect node name")
}
if a.GetLocalName() != "cruft" {
t.Error("incorrect node name")
}
if a.GetNamespacePrefix() != "pfx" {
t.Error("incorrect prefix")
}
if a.GetNamespaceURI() != "http://example.org/lol" {
t.Error("incorrect namespace URI")
}
if a.GetParentNode() != nil {
t.Error("attr cannot have a parent (must be nil)")
}
bogusElem, _ := doc.CreateElement("bogus")
if err := a.AppendChild(bogusElem); err == nil {
t.Error("expected an error at this point")
}
a.setParentNode(bogusElem)
if a.GetParentNode() != nil {
t.Error("parent node should be nil at all times")
}
if len(a.GetChildNodes()) != 0 {
t.Error("len of child nodes must be zero at all times")
}
if a.GetFirstChild() != nil {
t.Error("first child must always be nil")
}
if a.GetAttributes() != nil {
t.Error("attributes must always be nil")
}
if a.GetOwnerDocument() != doc {
t.Error("incorrect owner document")
}
if a.HasChildNodes() != false {
t.Error("must always return false, but was true")
}
if a.GetOwnerElement() != elem {
t.Error("incorrect owner element")
}
if a.GetNodeType() != AttributeNode {
t.Errorf("incorrect node type for attribute")
}
if a.GetNodeValue() != "valval" {
t.Errorf("incorrect node value: '%v'", a.GetNodeValue())
}
if a.GetValue() != "valval" {
t.Errorf("incorrect node value: '%v'", a.GetValue())
}
if a.GetPreviousSibling() != nil {
t.Error("expected nil previous sibling")
}
if a.GetNextSibling() != nil {
t.Error("expected nil next sibling")
}
if a.GetLastChild() != nil {
t.Error("expecting nil last child")
}
if a.HasAttributes() {
t.Error("attributes having attributes it just silly")
}
}
func TestAttrLookupNamespaceURI(t *testing.T) {
doc := NewDocument()
root, _ := doc.CreateElement("root")
root.SetAttribute("xmlns:pfx", "http://example.org/pfx")
root.SetAttribute("xmlns:xfb", "urn:xfbcft")
child, _ := doc.CreateElement("child")
root.AppendChild(child) // must append child first or else SetAttribute fails.
child.SetAttribute("pfx:name", "Mimi")
attr, ok := child.GetAttributes().GetNamedItem("pfx:name").(Attr)
if !ok {
t.Error("expected type assertion ok for Attr")
t.FailNow()
}
ns, found := attr.LookupNamespaceURI("pfx")
exp := "http://example.org/pfx"
if ns != exp || !found {
t.Errorf("expected '%v', got '%v'", exp, ns)
}
// Attribute node owned by nothing:
attr, _ = doc.CreateAttribute("no-owner")
if _, found := attr.LookupNamespaceURI("pfxWhatever"); found {
t.Error("expecting false")
}
}
func TestAttrLookupPrefix(t *testing.T) {
doc := NewDocument()
root, _ := doc.CreateElementNS("urn:ns:attr1", "ns1:root")
sub1, _ := doc.CreateElement("ns1:sub1")
sub2, _ := doc.CreateElement("ns1:sub2")
sub3, _ := doc.CreateElement("ns1:sub3")
sub4, _ := doc.CreateElement("ns1:sub4")
attr1, _ := doc.CreateAttribute("ns1:name")
attr1.SetValue("melissandre")
doc.AppendChild(root)
root.AppendChild(sub1)
root.AppendChild(sub2)
root.AppendChild(sub3)
root.AppendChild(sub4)
sub4.SetAttributeNode(attr1)
pfx, _ := attr1.LookupPrefix("urn:ns:attr1")
if pfx != "ns1" {
t.Errorf("expected 'ns1', got '%v'", pfx)
}
// Attribute node owned by nothing:
attr1, _ = doc.CreateAttribute("no-owner")
pfx, found := attr1.LookupPrefix("n")
if found {
t.Errorf("expected the prefix to not be found, but it was. In fact, it was '%s'", pfx)
}
}
func TestAttrReplaceInsertRemoveChild(t *testing.T) {
doc := NewDocument()
attr, _ := doc.CreateAttribute("attr")
if _, err := attr.ReplaceChild(nil, nil); err == nil {
t.Error("expected error")
}
if _, err := attr.InsertBefore(nil, nil); err == nil {
t.Error("expected error")
}
if _, err := attr.RemoveChild(nil); err == nil {
t.Error("expected error")
}
}