xmtop/utils/runes_test.go

51 lines
1.3 KiB
Go
Raw Normal View History

2019-06-05 05:21:13 +08:00
package utils
import "testing"
const (
ELLIPSIS = "…"
)
func TestTruncateFront(t *testing.T) {
tests := []struct {
s string
w int
prefix string
want string
}{
{"", 0, ELLIPSIS, ""},
{"", 1, ELLIPSIS, ""},
{"", 10, ELLIPSIS, ""},
{"abcdef", 0, ELLIPSIS, ELLIPSIS},
{"abcdef", 1, ELLIPSIS, ELLIPSIS},
{"abcdef", 2, ELLIPSIS, ELLIPSIS + "f"},
{"abcdef", 5, ELLIPSIS, ELLIPSIS + "cdef"},
{"abcdef", 6, ELLIPSIS, "abcdef"},
{"abcdef", 10, ELLIPSIS, "abcdef"},
{"abcdef", 0, "...", "..."},
{"abcdef", 1, "...", "..."},
{"abcdef", 3, "...", "..."},
{"abcdef", 4, "...", "...f"},
{"abcdef", 5, "...", "...ef"},
{"abcdef", 6, "...", "abcdef"},
{"abcdef", 10, "...", "abcdef"},
{"⦅fullwidth⦆", 15, ".", "⦅fullwidth⦆"},
{"⦅fullwidth⦆", 14, ".", ".fullwidth⦆"},
{"⦅fullwidth⦆", 13, ".", ".ullwidth⦆"},
{"⦅fullwidth⦆", 10, ".", ".width⦆"},
{"⦅fullwidth⦆", 9, ".", ".width⦆"},
{"⦅fullwidth⦆", 8, ".", ".width⦆"},
{"⦅fullwidth⦆", 3, ".", ".⦆"},
{"⦅fullwidth⦆", 2, ".", "."},
}
for _, test := range tests {
if got := TruncateFront(test.s, test.w, test.prefix); got != test.want {
t.Errorf("TruncateFront(%q, %d, %q) = %q; want %q", test.s, test.w, test.prefix, got, test.want)
}
}
}