File: change_test.go

package info (click to toggle)
golang-github-johanneskaufmann-dom 0.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 176 kB
  • sloc: makefile: 3
file content (203 lines) | stat: -rw-r--r-- 3,980 bytes parent folder | download
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package dom

import (
	"strings"
	"testing"

	"golang.org/x/net/html"
)

func TestRemoveNode(t *testing.T) {
	child := &html.Node{
		Data: "child",
	}
	doc := &html.Node{
		Data: "parent",
	}

	if doc.FirstChild != nil {
		t.Error("expected FirstChild to be nil")
	}
	if child.Parent != nil {
		t.Error("expected Parent to be nil")
	}
	doc.AppendChild(child)
	if doc.FirstChild == nil {
		t.Error("expected FirstChild not to be nil anymore")
	}
	if child.Parent == nil {
		t.Error("expected Parent not to be nil anymore")
	}

	RemoveNode(child)
	if doc.FirstChild != nil {
		t.Error("expected FirstChild to be nil again")
	}
	if child.Parent != nil {
		t.Error("expected Parent to be nil again")
	}

	// Should not crash if run again...
	RemoveNode(child)
	if doc.Parent != nil {
		t.Error("expected Parent to still be nil")
	}
}

func TestReplaceNode(t *testing.T) {
	node1 := &html.Node{
		Data: "original",
	}
	node2 := &html.Node{
		Data: "replacement",
	}

	doc := &html.Node{}
	doc.AppendChild(node1)

	ReplaceNode(node1, node1)
	if doc.FirstChild != node1 {
		t.Error("expected the node1 to still be in place")
	}

	ReplaceNode(node1, node2)
	if doc.FirstChild != node2 {
		t.Error("expected the node2 to take the place")
	}
	if node1.Parent != nil {
		t.Error("expected node1 to not have a parent anymore")
	}
}

func TestUnwrapNode(t *testing.T) {
	child1 := &html.Node{
		Type: html.ElementNode,
		Data: "child1",
	}
	child2 := &html.Node{
		Type: html.ElementNode,
		Data: "child2",
	}
	child2.AppendChild(&html.Node{
		Type: html.ElementNode,
		Data: "child2.1",
	})

	parent := &html.Node{
		Type: html.ElementNode,
		Data: "parent",
	}
	parent.AppendChild(child1)
	parent.AppendChild(child2)

	root := &html.Node{
		Type: html.ElementNode,
		Data: "root",
	}
	root.AppendChild(parent)

	// - - - - - //

	expectedBefore := strings.TrimSpace(`
root
├─parent
│ ├─child1
│ ├─child2
│ │ ├─child2.1
	`)
	expectedAfter := strings.TrimSpace(`
root
├─child1
├─child2
│ ├─child2.1
	`)

	if RenderRepresentation(root) != expectedBefore {
		t.Error("expected a different initial render")
	}
	if root.FirstChild != parent {
		t.Error("expected the parent to be under root")
	}
	UnwrapNode(parent)
	if root.FirstChild == parent {
		t.Error("expected the parent to not be under root anymore")
	}
	if RenderRepresentation(root) != expectedAfter {
		t.Error("expected a different final render")
	}

	UnwrapNode(root)
	if root.Data != "root" {
		t.Error("expected the root to still be the root")
	}
}

func TestWrapNode(t *testing.T) {
	child1 := &html.Node{
		Type: html.ElementNode,
		Data: "child1",
	}
	child2 := &html.Node{
		Type: html.ElementNode,
		Data: "child2",
	}
	child2.AppendChild(&html.Node{
		Type: html.ElementNode,
		Data: "child2.1",
	})

	parent := &html.Node{
		Type: html.ElementNode,
		Data: "parent",
	}
	parent.AppendChild(child1)
	parent.AppendChild(child2)

	root := &html.Node{
		Type: html.ElementNode,
		Data: "root",
	}
	root.AppendChild(parent)

	// - - - - - //
	wrapper := &html.Node{
		Type: html.ElementNode,
		Data: "wrapper",
	}

	expectedBefore := strings.TrimSpace(`
root
├─parent
│ ├─child1
│ ├─child2
│ │ ├─child2.1
	`)
	expectedAfter := strings.TrimSpace(`
root
├─wrapper
│ ├─parent
│ │ ├─child1
│ │ ├─child2
│ │ │ ├─child2.1
	`)

	if RenderRepresentation(root) != expectedBefore {
		t.Error("expected a different initial render")
	}
	if root.FirstChild != parent {
		t.Error("expected the parent to be under root")
	}
	WrapNode(parent, wrapper)
	if RenderRepresentation(root) != expectedAfter {
		t.Error("expected a different final render")
	}
	if root.FirstChild != wrapper {
		t.Error("expected the wrapper to be under root")
	}

	// With no parent, there should be no change.
	WrapNode(root, &html.Node{Type: html.ElementNode, Data: "sky"})
	if RenderRepresentation(root) != expectedAfter {
		t.Error("there should be no changes")
	}
}