mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Implement stringutils.Ellipsis()
This patch implements an Ellipsis utility to append an ellipsis (...) when truncating strings in output. It also fixes the existing Truncate() utility to be compatible with unicode/multibyte characters. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
ed1a313d8f
commit
51dc35cf23
7 changed files with 52 additions and 22 deletions
|
|
@ -57,24 +57,40 @@ func TestGenerateRandomAsciiStringIsAscii(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestEllipsis(t *testing.T) {
|
||||
str := "t🐳ststring"
|
||||
newstr := Ellipsis(str, 3)
|
||||
if newstr != "t🐳s" {
|
||||
t.Fatalf("Expected t🐳s, got %s", newstr)
|
||||
}
|
||||
newstr = Ellipsis(str, 8)
|
||||
if newstr != "t🐳sts..." {
|
||||
t.Fatalf("Expected tests..., got %s", newstr)
|
||||
}
|
||||
newstr = Ellipsis(str, 20)
|
||||
if newstr != "t🐳ststring" {
|
||||
t.Fatalf("Expected t🐳ststring, got %s", newstr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTruncate(t *testing.T) {
|
||||
str := "teststring"
|
||||
str := "t🐳ststring"
|
||||
newstr := Truncate(str, 4)
|
||||
if newstr != "test" {
|
||||
t.Fatalf("Expected test, got %s", newstr)
|
||||
if newstr != "t🐳st" {
|
||||
t.Fatalf("Expected t🐳st, got %s", newstr)
|
||||
}
|
||||
newstr = Truncate(str, 20)
|
||||
if newstr != "teststring" {
|
||||
t.Fatalf("Expected teststring, got %s", newstr)
|
||||
if newstr != "t🐳ststring" {
|
||||
t.Fatalf("Expected t🐳ststring, got %s", newstr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInSlice(t *testing.T) {
|
||||
slice := []string{"test", "in", "slice"}
|
||||
slice := []string{"t🐳st", "in", "slice"}
|
||||
|
||||
test := InSlice(slice, "test")
|
||||
test := InSlice(slice, "t🐳st")
|
||||
if !test {
|
||||
t.Fatalf("Expected string test to be in slice")
|
||||
t.Fatalf("Expected string t🐳st to be in slice")
|
||||
}
|
||||
test = InSlice(slice, "SLICE")
|
||||
if !test {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue