-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathoutput_test.go
More file actions
204 lines (169 loc) Β· 5.17 KB
/
output_test.go
File metadata and controls
204 lines (169 loc) Β· 5.17 KB
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
204
package main
import (
"strings"
"testing"
)
func createTestTree() *TreeNode {
// Create a simple test tree structure
root := &TreeNode{
Name: "root",
IsDir: true,
Depth: 0,
}
dir1 := &TreeNode{
Name: "dir1",
IsDir: true,
Depth: 1,
}
file1 := &TreeNode{
Name: "file1.txt",
IsDir: false,
Depth: 1,
}
file2 := &TreeNode{
Name: "file2.go",
IsDir: false,
Depth: 2,
}
// Build tree structure
root.Children = append(root.Children, dir1, file1)
dir1.Children = append(dir1.Children, file2)
return root
}
func TestToIndentString(t *testing.T) {
// Create a simple test tree structure
tree := createTestTree()
// Test without icons
result := tree.ToIndentString(2, false, false, false, false) // No icons
// Check if output contains expected content
expectedLines := []string{
"root/",
" dir1/",
" file2.go",
" file1.txt",
}
for _, line := range expectedLines {
if !strings.Contains(result, line) {
t.Errorf("Indented output missing expected line: %s", line)
}
}
// Check indentation
lines := strings.Split(strings.TrimSpace(result), "\n")
if !strings.HasPrefix(lines[1], " ") {
t.Error("dir1 line should have 2 spaces indentation")
}
if !strings.HasPrefix(lines[2], " ") {
t.Error("file2.go line should have 4 spaces indentation")
}
// Check if directories have trailing slash
if !strings.HasSuffix(lines[0], "/") {
t.Error("Root directory should have a trailing slash")
}
if !strings.HasSuffix(lines[1], "/") {
t.Error("Directory 'dir1' should have a trailing slash")
}
if strings.HasSuffix(lines[2], "/") {
t.Error("File 'file2.go' should not have a trailing slash")
}
if strings.HasSuffix(lines[3], "/") {
t.Error("File 'file1.txt' should not have a trailing slash")
}
//
iconResult := tree.ToIndentString(2, true)
if !strings.Contains(iconResult, "π") {
t.Error("Icon mode should display folder icon for directories")
}
}
func TestToTreeString(t *testing.T) {
tree := createTestTree()
result := tree.ToTreeString(true, "", false) // No icons
// Check if output contains expected content and tree symbols
expectedPatterns := []string{
"root/",
"βββ dir1/",
"β βββ file2.go",
"βββ file1.txt",
}
for _, pattern := range expectedPatterns {
if !strings.Contains(result, pattern) {
t.Errorf("Tree output missing expected pattern: %s", pattern)
}
}
// Ensure empty depth (root node) doesn't show prefix symbols
lines := strings.Split(strings.TrimSpace(result), "\n")
if strings.HasPrefix(lines[0], "βββ ") {
t.Error("Root node should not display prefix symbol")
}
// Check if directories have trailing slash
for i, line := range lines {
if i == 0 && !strings.HasSuffix(line, "/") {
t.Error("Root directory should have a trailing slash")
}
if strings.Contains(line, "dir1") && !strings.HasSuffix(line, "/") {
t.Error("Directory 'dir1' should have a trailing slash")
}
if strings.Contains(line, "file1.txt") && strings.HasSuffix(line, "/") {
t.Error("File 'file1.txt' should not have a trailing slash")
}
if strings.Contains(line, "file2.go") && strings.HasSuffix(line, "/") {
t.Error("File 'file2.go' should not have a trailing slash")
}
}
// Test icon mode
iconResult := tree.ToTreeString(true, "", true)
if !strings.Contains(iconResult, "π") {
t.Error("Icon mode should display folder icon for directories")
}
if !strings.Contains(iconResult, "πΉ") {
t.Error("Icon mode should display Go file icon for .go files")
}
}
func TestToMarkdownString(t *testing.T) {
tree := createTestTree()
result := tree.ToMarkdownString(0, false)
// Check if output contains expected markdown list format
expectedPatterns := []string{
"- root/",
" - dir1/",
" - file2.go",
" - file1.txt",
}
for _, pattern := range expectedPatterns {
if !strings.Contains(result, pattern) {
t.Errorf("Markdown output missing expected pattern: %s", pattern)
}
}
// Ensure directories have trailing slash
if !strings.Contains(result, "root/") || !strings.Contains(result, "dir1/") {
t.Error("Directory names should have trailing slashes")
}
// Ensure files don't have trailing slash
if strings.Contains(result, "file1.txt/") || strings.Contains(result, "file2.go/") {
t.Error("File names should not have trailing slashes")
}
}
func TestToMermaidString(t *testing.T) {
tree := createTestTree()
result := tree.ToMermaidString()
// Check if mermaid output format is correct
expectedPatterns := []string{
"graph TD",
"N1[root/]",
"N2[dir1/]",
"N1 --> N2",
"N3[file2.go]",
"N2 --> N3",
"N4[file1.txt]",
"N1 --> N4",
}
for _, pattern := range expectedPatterns {
if !strings.Contains(result, pattern) {
t.Errorf("Mermaid output missing expected pattern: %s\n%s", pattern, result)
}
}
// Ensure node IDs are incremental
if !strings.Contains(result, "N1") || !strings.Contains(result, "N2") ||
!strings.Contains(result, "N3") || !strings.Contains(result, "N4") {
t.Error("Mermaid output should contain incremental node IDs")
}
}