github.com/sdqri/sequined@v0.0.0-20240421190656-fc6bf956f4d8/internal/hyperrenderer/hyper_renderer_test.go (about) 1 package hyperrenderer_test 2 3 import ( 4 "io" 5 "os" 6 "testing" 7 8 "github.com/sdqri/sequined/internal/hyperrenderer" 9 hr "github.com/sdqri/sequined/internal/hyperrenderer" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func init() { 14 if err := os.Chdir("../.."); err != nil { 15 panic(err) 16 } 17 } 18 19 var _ hr.HyperRenderer = &MockHyperRenderer{} 20 21 type MockHyperRenderer struct { 22 ID string 23 Path string 24 Links []*MockHyperRenderer 25 } 26 27 func (m *MockHyperRenderer) GetID() string { 28 return m.ID 29 } 30 31 func (m *MockHyperRenderer) GetPath() string { 32 return m.Path 33 } 34 35 func (m *MockHyperRenderer) Render(wr io.Writer) error { 36 return nil 37 } 38 39 func (m *MockHyperRenderer) GetLinks() []hr.HyperRenderer { 40 links := make([]hr.HyperRenderer, len(m.Links)) 41 for i, link := range m.Links { 42 links[i] = link 43 } 44 return links 45 } 46 47 func TestTraverse(t *testing.T) { 48 testCases := []struct { 49 Name string 50 Root hr.HyperRenderer 51 ExpectedVisits int 52 }{ 53 { 54 Name: "Single Node", 55 Root: &MockHyperRenderer{ 56 ID: "root", 57 Path: "/root", 58 Links: []*MockHyperRenderer{ 59 { 60 ID: "child", 61 Path: "/root/child", 62 Links: nil, 63 }, 64 }, 65 }, 66 ExpectedVisits: 2, 67 }, 68 } 69 70 for _, tc := range testCases { 71 t.Run(tc.Name, func(t *testing.T) { 72 visitedMap := hr.Traverse(tc.Root, hr.NoOpVisit) 73 assert.Len(t, visitedMap, tc.ExpectedVisits, "unexpected number of visited nodes") 74 }) 75 } 76 } 77 78 func TestCreatePathMap(t *testing.T) { 79 root := &MockHyperRenderer{ 80 ID: "root", 81 Path: "/root", 82 Links: []*MockHyperRenderer{ 83 { 84 ID: "child1", 85 Path: "/root/child1", 86 Links: nil, 87 }, 88 { 89 ID: "child2", 90 Path: "/root/child2", 91 Links: []*MockHyperRenderer{ 92 { 93 ID: "grandchild1", 94 Path: "/root/child2/grandchild1", 95 Links: nil, 96 }, 97 }, 98 }, 99 }, 100 } 101 102 pathMap := hyperrenderer.CreatePathMap(root) 103 104 expectedMap := map[string]hyperrenderer.HyperRenderer{ 105 "/root": root, 106 "/root/child1": root.Links[0], 107 "/root/child2": root.Links[1], 108 "/root/child2/grandchild1": root.Links[1].GetLinks()[0], 109 } 110 111 assert.Equal(t, len(expectedMap), len(pathMap), "length of pathMap does not match expected length") 112 113 for path, expectedNode := range expectedMap { 114 actualNode, ok := pathMap[path] 115 assert.True(t, ok, "expected path %q missing in pathMap", path) 116 assert.Equal(t, expectedNode, actualNode, "unexpected node for path %q", path) 117 } 118 } 119 120 func TestFindHyperRendererByPath(t *testing.T) { 121 root := &MockHyperRenderer{ 122 ID: "root", 123 Path: "/root", 124 Links: []*MockHyperRenderer{ 125 { 126 ID: "child1", 127 Path: "/root/child1", 128 Links: nil, 129 }, 130 { 131 ID: "child2", 132 Path: "/root/child2", 133 Links: nil, 134 }, 135 }, 136 } 137 138 found := hr.FindHyperRendererByPath(root, "/root/child1") 139 assert.NotNil(t, found) 140 assert.Equal(t, "child1", found.GetID()) 141 } 142 143 func TestFindHyperRendererByID(t *testing.T) { 144 root := &MockHyperRenderer{ 145 ID: "root", 146 Path: "/root", 147 Links: []*MockHyperRenderer{ 148 { 149 ID: "child1", 150 Path: "/root/child1", 151 Links: nil, 152 }, 153 { 154 ID: "child2", 155 Path: "/root/child2", 156 Links: nil, 157 }, 158 }, 159 } 160 161 found := hr.FindHyperRendererByID(root, "child2") 162 assert.NotNil(t, found) 163 assert.Equal(t, "child2", found.GetID()) 164 }