-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_test.go
129 lines (120 loc) · 3.39 KB
/
text_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
package dom
import (
"fmt"
"testing"
)
func TestTextGetters(t *testing.T) {
doc := NewDocument()
parent, _ := doc.CreateElement("element")
txt := doc.CreateText("sample")
doc.AppendChild(parent)
parent.AppendChild(txt)
if txt.GetNodeName() != "#text" {
t.Error("expected '#text'")
}
if txt.GetNodeType() != TextNode {
t.Error("expected TextNode")
}
if txt.GetNodeValue() != "sample" {
t.Errorf("expected 'sample', got '%v'", txt.GetNodeValue())
}
if txt.GetLocalName() != "" {
t.Error("local name should always be an empty string")
}
if txt.GetParentNode() != parent {
t.Error("incorrect parent node")
}
if err := txt.AppendChild(doc.CreateText("meh")); err == nil {
t.Error("expected error, but got none")
}
if txt.GetFirstChild() != nil {
t.Error("text nodes cannot have children")
}
if txt.GetLastChild() != nil {
t.Error("text nodes cannot have children")
}
if txt.HasChildNodes() {
t.Error("text nodes cannot have children")
}
if txt.GetAttributes() != nil {
t.Error("text nodes cannot have attributes")
}
if txt.GetOwnerDocument() != doc {
t.Error("incorrect owner document")
}
if txt.GetNamespaceURI() != "" {
t.Error("namespace URI should be an empty string")
}
if txt.GetNamespacePrefix() != "" {
t.Error("namespace prefix should be an empty string")
}
if _, err := txt.ReplaceChild(nil, nil); err == nil {
t.Error("replacing child should always return an error")
}
if _, err := txt.RemoveChild(nil); err == nil {
t.Error("removing child should always return an error")
}
if _, err := txt.InsertBefore(nil, nil); err == nil {
t.Error("inserting a child should always return an error")
}
_, found := txt.LookupPrefix("anything")
if found {
t.Errorf("LookupPrefix should always return a false for 'found'")
}
if _, found := txt.LookupNamespaceURI("asd"); found {
t.Error("LookupNamespaceURI should always return an empty string and false")
}
thetext := "some text content"
txt.SetTextContent(thetext)
if txt.GetTextContent() != thetext {
t.Errorf("expected '%v', got '%v'", thetext, txt.GetTextContent())
}
txt.SetText("this is a sample string, longer than 30 characters.")
s := fmt.Sprintf("%v", txt)
expected := "TEXT_NODE: 'this is a sample string, longe [...]'"
if s != expected {
t.Errorf("expected '%v', got '%v'", expected, s)
}
if txt.HasAttributes() {
t.Error("text nodes cannot have attributes")
}
}
func TestTextPrevNextSibling(t *testing.T) {
doc := NewDocument()
root, _ := doc.CreateElement("root")
child1, _ := doc.CreateElement("child1")
text := doc.CreateText("some text")
child3, _ := doc.CreateElement("child3")
doc.AppendChild(root)
root.AppendChild(child1)
root.AppendChild(text)
root.AppendChild(child3)
node := text.GetPreviousSibling()
if node != child1 {
t.Errorf("expected 'child1' as previous sibling, got '%v'", node)
}
node = text.GetNextSibling()
if node != child3 {
t.Errorf("expected 'child3' as next sibling, got '%v'", node)
}
}
func TestTextIsIgnorableWhitespace(t *testing.T) {
var tests = []struct {
t string
expected bool
}{
{"\n\r\n\r\t\t", true},
{"\n\ra\n\r\t\t", false},
{"\r\n\r\n \r ", true},
{" ", true},
{" \r", true},
{" x ", false},
}
doc := NewDocument()
for _, test := range tests {
text := doc.CreateText(test.t)
if text.IsElementContentWhitespace() != test.expected {
t.Errorf("expected '%v'", test.expected)
}
}
}