Files
ReSendit/internal/util/util_test.go

41 lines
871 B
Go

package util
import "testing"
func TestHumanSize(t *testing.T) {
tests := []struct {
in int64
want string
}{
{0, "0 B"},
{1, "1 B"},
{1023, "1023 B"},
{1024, "1.0 KB"},
{1024 * 1024, "1.0 MB"},
}
for _, tt := range tests {
if got := HumanSize(tt.in); got != tt.want {
t.Fatalf("HumanSize(%d) = %q, want %q", tt.in, got, tt.want)
}
}
}
func TestSafeFilename(t *testing.T) {
if got := SafeFilename(" hello.txt "); got != "hello.txt" {
t.Fatalf("expected trimmed filename, got %q", got)
}
// Strips control characters, quotes, and backslashes.
in := "a\n\rb\t\"c\\d"
got := SafeFilename(in)
if got != "abcd" {
t.Fatalf("SafeFilename(%q) = %q, want %q", in, got, "abcd")
}
// Empty after sanitization becomes default.
if got := SafeFilename("\n\r\t"); got != "file" {
t.Fatalf("expected default filename, got %q", got)
}
}