Initial backend repository commit.
Set up project files and add .gitignore to exclude local build/runtime artifacts. Made-with: Cursor
This commit is contained in:
118
internal/pkg/avatar/avatar_test.go
Normal file
118
internal/pkg/avatar/avatar_test.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package avatar
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetInitials(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
username string
|
||||
want string
|
||||
}{
|
||||
{"中文用户名", "张三", "张"},
|
||||
{"英文用户名", "John", "J"},
|
||||
{"英文全名", "John Doe", "JD"},
|
||||
{"带下划线", "john_doe", "JD"},
|
||||
{"带连字符", "john-doe", "JD"},
|
||||
{"空字符串", "", "?"},
|
||||
{"小写英文", "alice", "A"},
|
||||
{"中文复合", "李小龙", "李"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := getInitials(tt.username)
|
||||
if got != tt.want {
|
||||
t.Errorf("getInitials(%q) = %q, want %q", tt.username, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringToColor(t *testing.T) {
|
||||
// 测试同一用户名生成的颜色一致
|
||||
color1 := stringToColor("张三")
|
||||
color2 := stringToColor("张三")
|
||||
if color1 != color2 {
|
||||
t.Errorf("stringToColor should return consistent colors for the same input")
|
||||
}
|
||||
|
||||
// 测试不同用户名生成不同颜色(大概率)
|
||||
color3 := stringToColor("李四")
|
||||
if color1 == color3 {
|
||||
t.Logf("Warning: different usernames generated the same color (possible but unlikely)")
|
||||
}
|
||||
|
||||
// 测试空字符串
|
||||
color4 := stringToColor("")
|
||||
if color4 == "" {
|
||||
t.Errorf("stringToColor should return a color for empty string")
|
||||
}
|
||||
|
||||
// 验证颜色格式
|
||||
if !strings.HasPrefix(color4, "#") {
|
||||
t.Errorf("stringToColor should return hex color format starting with #")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateSVGAvatar(t *testing.T) {
|
||||
svg := GenerateSVGAvatar("张三", 100)
|
||||
|
||||
// 验证SVG结构
|
||||
if !strings.Contains(svg, "<svg") {
|
||||
t.Errorf("SVG should contain <svg tag")
|
||||
}
|
||||
if !strings.Contains(svg, "</svg>") {
|
||||
t.Errorf("SVG should contain </svg> tag")
|
||||
}
|
||||
if !strings.Contains(svg, "width=\"100\"") {
|
||||
t.Errorf("SVG should have width=100")
|
||||
}
|
||||
if !strings.Contains(svg, "height=\"100\"") {
|
||||
t.Errorf("SVG should have height=100")
|
||||
}
|
||||
if !strings.Contains(svg, "张") {
|
||||
t.Errorf("SVG should contain the initial character")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateAvatarDataURI(t *testing.T) {
|
||||
dataURI := GenerateAvatarDataURI("张三", 100)
|
||||
|
||||
// 验证Data URI格式
|
||||
if !strings.HasPrefix(dataURI, "data:image/svg+xml;base64,") {
|
||||
t.Errorf("Data URI should start with data:image/svg+xml;base64,")
|
||||
}
|
||||
|
||||
// 验证base64部分不为空
|
||||
parts := strings.Split(dataURI, ",")
|
||||
if len(parts) != 2 {
|
||||
t.Errorf("Data URI should have two parts separated by comma")
|
||||
}
|
||||
if parts[1] == "" {
|
||||
t.Errorf("Base64 part should not be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsChinese(t *testing.T) {
|
||||
tests := []struct {
|
||||
r rune
|
||||
want bool
|
||||
}{
|
||||
{'中', true},
|
||||
{'文', true},
|
||||
{'a', false},
|
||||
{'Z', false},
|
||||
{'0', false},
|
||||
{'_', false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
got := isChinese(tt.r)
|
||||
if got != tt.want {
|
||||
t.Errorf("isChinese(%q) = %v, want %v", tt.r, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user