Skip to content

Commit aa2223c

Browse files
committed
fix: PR comments: Add unit tests for icon_utils
1 parent e654b25 commit aa2223c

File tree

3 files changed

+88
-5
lines changed

3 files changed

+88
-5
lines changed

src/internal/common/icon_utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"github.com/yorukot/superfile/src/config/icon"
88
)
99

10-
func GetElementIcon(file string, isDir bool) icon.Style {
10+
func GetElementIcon(file string, isDir bool, nerdFont bool) icon.Style {
1111
ext := strings.TrimPrefix(filepath.Ext(file), ".")
1212
name := file
1313

14-
if !Config.Nerdfont {
14+
if !nerdFont {
1515
return icon.Style{
1616
Icon: "",
1717
Color: Theme.FilePanelFG,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package common
2+
3+
import (
4+
"testing"
5+
6+
"github.com/yorukot/superfile/src/config/icon"
7+
)
8+
9+
func TestGetElementIcon(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
file string
13+
isDir bool
14+
nerdFont bool
15+
expected icon.Style
16+
}{
17+
{
18+
name: "Non-nerdfont returns empty icon",
19+
file: "test.txt",
20+
isDir: false,
21+
nerdFont: false,
22+
expected: icon.Style{
23+
Icon: "",
24+
Color: Theme.FilePanelFG,
25+
},
26+
},
27+
{
28+
name: "Directory with nerd font",
29+
file: "folder",
30+
isDir: true,
31+
nerdFont: true,
32+
expected: icon.Folders["folder"],
33+
},
34+
{
35+
name: "File with known extension",
36+
file: "test.go",
37+
isDir: false,
38+
nerdFont: true,
39+
expected: icon.Icons["go"],
40+
},
41+
{
42+
name: ".git directory",
43+
file: ".git",
44+
isDir: true,
45+
nerdFont: true,
46+
expected: icon.Folders[".git"],
47+
},
48+
{
49+
name: "superfile directory",
50+
file: "superfile",
51+
isDir: true,
52+
nerdFont: true,
53+
expected: icon.Folders["superfile"],
54+
},
55+
{
56+
name: "package.json file",
57+
file: "package.json",
58+
isDir: false,
59+
nerdFont: true,
60+
expected: icon.Icons["package"],
61+
},
62+
{
63+
name: "File with unknown extension",
64+
file: "test.xyz",
65+
isDir: false,
66+
nerdFont: true,
67+
expected: icon.Style{
68+
Icon: icon.Icons["file"].Icon,
69+
// Theme is not defined here, so this will be blank
70+
Color: Theme.FilePanelFG,
71+
},
72+
},
73+
}
74+
75+
for _, tt := range tests {
76+
t.Run(tt.name, func(t *testing.T) {
77+
result := GetElementIcon(tt.file, tt.isDir, tt.nerdFont)
78+
if result.Icon != tt.expected.Icon || result.Color != tt.expected.Color {
79+
t.Errorf("GetElementIcon() = %v, want %v", result, tt.expected)
80+
}
81+
})
82+
}
83+
}

src/internal/common/string_function.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TruncateMiddleText(text string, maxChars int, talis string) string {
5858
}
5959

6060
func PrettierName(name string, width int, isDir bool, isSelected bool, bgColor lipgloss.Color) string {
61-
style := GetElementIcon(name, isDir)
61+
style := GetElementIcon(name, isDir, Config.Nerdfont)
6262
if isSelected {
6363
return StringColorRender(lipgloss.Color(style.Color), bgColor).
6464
Background(bgColor).
@@ -73,15 +73,15 @@ func PrettierName(name string, width int, isDir bool, isSelected bool, bgColor l
7373
}
7474

7575
func PrettierDirectoryPreviewName(name string, isDir bool, bgColor lipgloss.Color) string {
76-
style := GetElementIcon(name, isDir)
76+
style := GetElementIcon(name, isDir, Config.Nerdfont)
7777
return StringColorRender(lipgloss.Color(style.Color), bgColor).
7878
Background(bgColor).
7979
Render(style.Icon+" ") +
8080
FilePanelStyle.Render(name)
8181
}
8282

8383
func ClipboardPrettierName(name string, width int, isDir bool, isSelected bool) string {
84-
style := GetElementIcon(name, isDir)
84+
style := GetElementIcon(name, isDir, Config.Nerdfont)
8585
if isSelected {
8686
return StringColorRender(lipgloss.Color(style.Color), FooterBGColor).
8787
Background(FooterBGColor).

0 commit comments

Comments
 (0)