From 030ba2894f3e9f9442b4bbf83fec09bda1ca88c5 Mon Sep 17 00:00:00 2001
From: Matthias Loibl <mail@matthiasloibl.com>
Date: Mon, 7 Nov 2016 21:27:14 +0100
Subject: [PATCH] Add tests for EllipsisString() and fix bug if param length <
 3

---
 modules/base/tool.go      |  5 ++++-
 modules/base/tool_test.go | 16 ++++++++++++----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/modules/base/tool.go b/modules/base/tool.go
index ff072f857f..9177cfe193 100644
--- a/modules/base/tool.go
+++ b/modules/base/tool.go
@@ -462,7 +462,10 @@ func Subtract(left interface{}, right interface{}) interface{} {
 // EllipsisString returns a truncated short string,
 // it appends '...' in the end of the length of string is too large.
 func EllipsisString(str string, length int) string {
-	if len(str) < length {
+	if length <= 3 {
+		return "..."
+	}
+	if len(str) <= length {
 		return str
 	}
 	return str[:length-3] + "..."
diff --git a/modules/base/tool_test.go b/modules/base/tool_test.go
index bc46a2a883..b29c8ea717 100644
--- a/modules/base/tool_test.go
+++ b/modules/base/tool_test.go
@@ -68,14 +68,11 @@ func TestAvatarLink(t *testing.T) {
 	)
 }
 
-// TODO: AvatarLink()
 // TODO: computeTimeDiff()
 // TODO: TimeSincePro()
 // TODO: timeSince()
 // TODO: RawTimeSince()
 // TODO: TimeSince()
-// TODO: logn()
-// TODO: humanateBytes()
 
 func TestFileSize(t *testing.T) {
 	var size int64
@@ -96,7 +93,18 @@ func TestFileSize(t *testing.T) {
 }
 
 // TODO: Subtract()
-// TODO: EllipsisString()
+
+func TestEllipsisString(t *testing.T) {
+	assert.Equal(t, "...", EllipsisString("foobar", 0))
+	assert.Equal(t, "...", EllipsisString("foobar", 1))
+	assert.Equal(t, "...", EllipsisString("foobar", 2))
+	assert.Equal(t, "...", EllipsisString("foobar", 3))
+	assert.Equal(t, "f...", EllipsisString("foobar", 4))
+	assert.Equal(t, "fo...", EllipsisString("foobar", 5))
+	assert.Equal(t, "foobar", EllipsisString("foobar", 6))
+	assert.Equal(t, "foobar", EllipsisString("foobar", 10))
+}
+
 // TODO: TruncateString()
 // TODO: StringsToInt64s()
 // TODO: Int64sToStrings()