GameOver1337 / GitClone Public
К репозиторию
2.4 KB
internal/handlers/handlers_test.go
2.4 KB
1package handlers
2
3import "testing"
4
5func TestCleanRepoPath(t *testing.T) {
6 tests := []struct {
7 in, want string
8 }{
9 {"", ""},
10 {"/", ""},
11 {"docs", "docs"},
12 {"/docs/", "docs"},
13 {"a/b/c.txt", "a/b/c.txt"},
14 {`a\b\c.txt`, "a/b/c.txt"},
15 {"../secret", ""},
16 {"a/../../b", ""},
17 {"..", ""},
18 {"./docs", "docs"},
19 }
20 for _, tt := range tests {
21 if got := cleanRepoPath(tt.in); got != tt.want {
22 t.Errorf("cleanRepoPath(%q) = %q, want %q", tt.in, got, tt.want)
23 }
24 }
25}
26
27func TestEscapePath(t *testing.T) {
28 tests := []struct {
29 in, want string
30 }{
31 {"README.md", "README.md"},
32 {"my file.txt", "my%20file.txt"},
33 {"src/app/data.txt", "src/app/data.txt"},
34 {"src/my file.txt", "src/my%20file.txt"},
35 }
36 for _, tt := range tests {
37 if got := escapePath(tt.in); got != tt.want {
38 t.Errorf("escapePath(%q) = %q, want %q", tt.in, got, tt.want)
39 }
40 }
41}
42
43func TestTruncateString(t *testing.T) {
44 if got := truncateString("коротко", 10); got != "коротко" {
45 t.Errorf("unexpected truncate: %q", got)
46 }
47 if got := truncateString("привет мир", 6); got != "привет…" {
48 t.Errorf("unexpected truncate: %q", got)
49 }
50}
51
52func TestBuildCrumbs(t *testing.T) {
53 crumbs := buildCrumbs("src/app")
54 if len(crumbs) != 2 {
55 t.Fatalf("expected 2 crumbs, got %d", len(crumbs))
56 }
57 if crumbs[0].Name != "src" || crumbs[0].Path != "src" {
58 t.Errorf("unexpected first crumb: %+v", crumbs[0])
59 }
60 if crumbs[1].Name != "app" || crumbs[1].Path != "src/app" {
61 t.Errorf("unexpected second crumb: %+v", crumbs[1])
62 }
63 if got := buildCrumbs(""); got != nil {
64 t.Errorf("expected nil crumbs for root, got %+v", got)
65 }
66}
67
68func TestLangClass(t *testing.T) {
69 tests := map[string]string{
70 "main.go": "language-go",
71 "src/app.tsx": "language-typescript",
72 "Dockerfile": "language-dockerfile",
73 "docker/Dockerfile": "language-dockerfile",
74 "config.yml": "language-yaml",
75 "notes.txt": "",
76 "index.html": "language-xml",
77 }
78 for in, want := range tests {
79 if got := langClass(in); got != want {
80 t.Errorf("langClass(%q) = %q, want %q", in, got, want)
81 }
82 }
83}
84
85func TestFormatSize(t *testing.T) {
86 tests := map[int64]string{
87 0: "0 B",
88 512: "512 B",
89 1024: "1.0 KB",
90 1536: "1.5 KB",
91 1024 * 1024: "1.0 MB",
92 }
93 for in, want := range tests {
94 if got := formatSize(in); got != want {
95 t.Errorf("formatSize(%d) = %q, want %q", in, got, want)
96 }
97 }
98}