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
| package util_test
import ( "reflect" "testing"
"{{{ .Package }}}/app/util" )
func TestNodeGet(t *testing.T) { t.Parallel() root := &util.Node[int]{ Name: "root", Children: util.Nodes[int]{ {Name: "child1", Children: util.Nodes[int]{{Name: "grandchild"}}}, {Name: "child2"}, }, }
tests := []struct { path []string expected string }{ {[]string{}, "root"}, {[]string{"child1"}, "child1"}, {[]string{"child1", "grandchild"}, "grandchild"}, {[]string{"child2"}, "child2"}, {[]string{"nonexistent"}, ""}, }
for _, tt := range tests { result := root.Get(tt.path...) if result == nil && tt.expected != "" { t.Errorf("Expected %s, got nil for path %v", tt.expected, tt.path) } else if result != nil && result.Name != tt.expected { t.Errorf("Expected %s, got %s for path %v", tt.expected, result.Name, tt.path) } } }
func TestNodeFlatten(t *testing.T) { t.Parallel() root := &util.Node[int]{ Name: "root", Children: util.Nodes[int]{ {Name: "child1", Children: util.Nodes[int]{{Name: "grandchild"}}}, {Name: "child2"}, }, }
expected := []string{ "root/child1/grandchild", "root/child1", "root/child2", "root", }
result := root.Flatten("") if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v, got %v", expected, result) } }
func TestNodesSort(t *testing.T) { t.Parallel() nodes := util.Nodes[int]{ {Name: "c"}, {Name: "a"}, {Name: "b"}, }
sorted := nodes.Sort() expected := []string{"a", "b", "c"}
for i, node := range sorted { if node.Name != expected[i] { t.Errorf("Expected %s at position %d, got %s", expected[i], i, node.Name) } } }
func TestNodesMerge(t *testing.T) { t.Parallel() n1 := util.Nodes[int]{ {Name: "a", Tags: []string{"tag1"}}, {Name: "b", Children: util.Nodes[int]{{Name: "b1"}}}, } n2 := util.Nodes[int]{ {Name: "a", Tags: []string{"tag2"}}, {Name: "c"}, }
merged := n1.Merge(n2)
if len(merged) != 3 { t.Errorf("Expected 3 nodes, got %d", len(merged)) }
aNode := merged.Get("a") if aNode == nil || !reflect.DeepEqual(aNode.Tags, []string{"tag1", "tag2"}) { t.Errorf("Incorrect merge for node 'a'") }
if merged.Get("b").Children.Get("b1") == nil { t.Errorf("Child node 'b1' not preserved in merge") }
if merged.Get("c") == nil { t.Errorf("Node 'c' not added in merge") } }
func TestTreeFlatten(t *testing.T) { t.Parallel() tree := util.Tree[int]{ Nodes: util.Nodes[int]{ {Name: "root", Children: util.Nodes[int]{ {Name: "child"}, }}, }, }
flattened := tree.Flatten() expected := []string{"root/child", "root"}
if !reflect.DeepEqual(flattened, expected) { t.Errorf("Expected %v, got %v", expected, flattened) } }
func TestTreeMerge(t *testing.T) { t.Parallel() t1 := &util.Tree[int]{ Nodes: util.Nodes[int]{{Name: "a"}}, Config: util.ValueMap{"key1": "value1"}, } t2 := &util.Tree[int]{ Nodes: util.Nodes[int]{{Name: "b"}}, Config: util.ValueMap{"key2": "value2"}, }
merged := t1.Merge(t2)
if len(merged.Nodes) != 2 { t.Errorf("Expected 2 nodes, got %d", len(merged.Nodes)) }
if merged.Config["key1"] != "value1" || merged.Config["key2"] != "value2" { t.Errorf("Incorrect config merge") } }
|