Skip to content

Commit aeb7bbb

Browse files
committed
extending test coverage
1 parent e798bf8 commit aeb7bbb

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

strings/stringsutil.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,10 @@ func LongestRepeatingSequence(s string) LongestSequence {
246246
return LongestSequence{Sequence: res, Count: resCount}
247247
}
248248

249-
//Truncate a string to max length
249+
// Truncate a string to max length
250250
func Truncate(data string, maxSize int) string {
251-
if maxSize > 0 && len(data) > maxSize {
251+
if maxSize >= 0 && len(data) > maxSize {
252252
return data[:maxSize]
253253
}
254254
return data
255-
}
255+
}

strings/stringsutil_test.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -289,23 +289,25 @@ func TestLongestRepeatingSequence(t *testing.T) {
289289
}
290290
}
291291

292-
293-
type truncatetest struct{
294-
MaxSize int
295-
Result interface{}
292+
type truncaTeest struct {
293+
test string
294+
maxSize int
295+
result string
296296
}
297297

298298
func TestTruncate(t *testing.T) {
299-
tests := map[string]truncatetest{
300-
"abcd": {MaxSize: 0, Result: "abcd"},
301-
"abcde": {MaxSize: 3, Result: "abc"},
302-
"abcdef": {MaxSize: 8, Result: "abcdef"},
303-
"abcdefg": {MaxSize: 6, Result: "abcdef"},
299+
tests := []truncaTeest{
300+
{test: "abcd", maxSize: -1, result: "abcd"},
301+
{test: "abcd", maxSize: 0, result: ""},
302+
{test: "abcde", maxSize: 3, result: "abc"},
303+
{test: "abcdef", maxSize: 8, result: "abcdef"},
304+
{test: "abcdefg", maxSize: 6, result: "abcdef"},
305+
{test: "aaaa", maxSize: 20, result: "aaaa"},
306+
{test: "aaaa", maxSize: 4, result: "aaaa"},
304307
}
305308

306-
for str, test := range tests {
307-
res := Truncate(str, test.MaxSize)
308-
require.Equalf(t, test.Result, res, "test:%s maxsize: %d result: %s", str, test.MaxSize, res)
309+
for _, test := range tests {
310+
res := Truncate(test.test, test.maxSize)
311+
require.Equalf(t, test.result, res, "test:%s maxsize: %d result: %s", test.test, test.maxSize, res)
309312
}
310-
311-
}
313+
}

0 commit comments

Comments
 (0)