package lib import ( "farga/db/types" "slices" "testing" ) func TestPrependString(t *testing.T) { slice := []string{"world", "this is my", "queue"} expected := []string{"Hello", "world", "this is my", "queue"} result := Prepend(slice, "Hello") if !slices.Equal(result, expected) { t.Errorf(`Prepend(slice, "Hello") FAILED. It should return {"Hello", "world", "this is my", "queue"}`) } } func TestPlural(t *testing.T) { inputs := map[int]string{ 1: "", 0: "s", 21585: "s", -41: "s", } for input, expected := range inputs { if Plural(input) != expected { t.Errorf(`Plural(%v) FAILED. It should return %s`, input, expected) } } } func TestRepoRoot(t *testing.T) { input := types.Repo{ User: &types.User{ Handle: "username", }, Name: "reponame", } expected := "/~username/reponame" if RepoRoot(input) != expected { t.Errorf(`Plural(input) FAILED. It should return %s`, expected) } } func TestSplitLines(t *testing.T) { input := `LINE1 LINE 2 2 2 2 LINE 3 ` expected := []string{"LINE1", "LINE 2 2 2 2", "LINE 3", ""} result := SplitLines(input) if !slices.Equal(result, expected) { t.Errorf(`Plural(input) FAILED. It returned %s, but it should return %s`, result, expected) } } func TestSizeLen(t *testing.T) { cases := map[uint]string{ 0: "0B", 1: "1B", 1024: "1024B", 1025: "1KiB", 2048: "2KiB", 5 << 20: "5MiB", 3 << 30: "3GiB", 5 << 40: "5TiB", } for input, expected := range cases { if got := SizeLen(input); got != expected { t.Errorf(`SizeLen(%v) = %s, want %s`, input, got, expected) } } } func TestSize(t *testing.T) { if got := Size([]byte("hello")); got != "5B" { t.Errorf(`Size([]byte("hello")) = %s, want 5B`, got) } } func TestFormatTime(t *testing.T) { cases := map[int]string{ 0: "0 secs.", 1: "1 sec.", 59: "59 secs.", 60: "1 min.", 61: "1 min.", 120: "2 mins.", 3599: "59 mins.", 3600: "1 hour.", 7200: "2 hours.", 36000: "10 hours.", } for input, expected := range cases { if got := FormatTime(input); got != expected { t.Errorf(`FormatTime(%v) = %s, want %s`, input, got, expected) } } } func TestIsRangeable(t *testing.T) { if !IsRangeable([]int{1, 2}) { t.Error("IsRangeable(slice) = false, want true") } if !IsRangeable([1]int{1}) { t.Error("IsRangeable(array) = false, want true") } if !IsRangeable(map[string]int{"a": 1}) { t.Error("IsRangeable(map) = false, want true") } if !IsRangeable(make(chan int)) { t.Error("IsRangeable(chan) = false, want true") } if IsRangeable("string") { t.Error("IsRangeable(string) = true, want false") } if IsRangeable(42) { t.Error("IsRangeable(int) = true, want false") } if IsRangeable(nil) { t.Error("IsRangeable(nil) = true, want false") } } func TestTruncateString(t *testing.T) { short, err := TruncateString("hello world", 5) if err != nil { t.Fatalf("TruncateString: %v", err) } if short != "hello" { t.Errorf(`TruncateString("hello world", 5) = %q, want "hello"`, short) } // counts runes, not bytes runes, err := TruncateString("héllo", 3) if err != nil { t.Fatalf("TruncateString(runes): %v", err) } if runes != "hél" { t.Errorf(`TruncateString("héllo", 3) = %q, want "hél"`, runes) } // n larger than the string returns it in full, no error full, err := TruncateString("hi", 10) if err != nil { t.Fatalf("TruncateString(n > len): %v", err) } if full != "hi" { t.Errorf(`TruncateString("hi", 10) = %q, want "hi"`, full) } if _, err := TruncateString("hi", -1); err == nil { t.Error("TruncateString(n < 0) = nil error, want error") } }