File: node_test.go

package info (click to toggle)
golang-github-gomarkdown-markdown 0.0~git20231115.a660076-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,168 kB
  • sloc: sh: 26; makefile: 5
file content (85 lines) | stat: -rw-r--r-- 1,878 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
package ast

import (
	"reflect"
	"testing"
)

func TestContainerCanHaveChildren(t *testing.T) {
	parent := &Container{}
	child := &Leaf{}

	if len(parent.GetChildren()) != 0 {
		t.Error("Parent did not start out without children")
	}

	newChildren := []Node{child}
	parent.SetChildren(newChildren)
	child.Parent = parent

	if !reflect.DeepEqual(parent.GetChildren(), newChildren) {
		t.Error("Failed to set children")
	}
}

func TestLeafCannotHaveChildren(t *testing.T) {
	parent := &Leaf{}
	child := &Leaf{}

	newChildren := []Node{child}

	// Expect that SetChildren panics
	defer func() {
		if r := recover(); r == nil {
			t.Error("Leaf.SetChildren did not panic but was expected to")
		}
	}()

	parent.SetChildren(newChildren)
	child.Parent = parent
}

func TestLeafCanSetEmptyChildren(t *testing.T) {
	parent := &Leaf{}

	parent.SetChildren(nil)
	parent.SetChildren([]Node{})
}

func TestRemoveLeaveFromTree(t *testing.T) {
	// Create a tree to remove nodes from:
	/*
	      grandparent
	           |
	         parent
	        /      \
	 toBeRemoved  sibling
	*/

	grandparent := &Container{}
	parent := &Container{}
	toBeRemoved := &Leaf{}
	sibling := &Leaf{}

	grandparent.SetChildren([]Node{parent})
	parent.Parent = grandparent

	parent.SetChildren(([]Node{toBeRemoved, sibling}))
	toBeRemoved.Parent = parent
	sibling.Parent = parent

	RemoveFromTree(toBeRemoved)

	if !reflect.DeepEqual(grandparent.GetChildren(), []Node{parent}) {
		t.Error("Unexpectedly modified children of grandparent when removing grandchild")
	}

	if !reflect.DeepEqual(parent.GetChildren(), []Node{sibling}) {
		t.Errorf("Unexpected modification of removed node's siblings: %v", parent.GetChildren())
	}

	// The parent reference of the removed node is left intact
	if toBeRemoved.Parent != parent {
		t.Errorf("Unexpectedly modified parent of removed node to: %v", toBeRemoved.Parent)
	}
}