diff --git a/widgets/view.go b/widgets/view.go index ab45eb1..4681e68 100644 --- a/widgets/view.go +++ b/widgets/view.go @@ -93,14 +93,17 @@ func (t *TextView) readInputLoop() { } func splitLine(line string, lineSize int) []string { + if line == "" { + return []string{} + } + var lines []string for { - if len(line) < lineSize { + if len(line) <= lineSize { lines = append(lines, line) return lines } lines = append(lines, line[:lineSize]) line = line[lineSize:] } - return lines } diff --git a/widgets/view_test.go b/widgets/view_test.go new file mode 100644 index 0000000..33c8aee --- /dev/null +++ b/widgets/view_test.go @@ -0,0 +1,35 @@ +package widgets + +import "testing" + +func TestSplitEmptyLine(t *testing.T) { + + result := splitLine("", 5) + if len(result) != 0 { + t.Errorf("expected: 0 lines, got: %d", len(result)) + } +} + +func TestSplitLineShorterThanLimit(t *testing.T) { + + result := splitLine("hello", 7) + if len(result) != 1 { + t.Errorf("expected: 0 lines, got: %d", len(result)) + } +} + +func TestSplitLineLongerThanLimit(t *testing.T) { + + result := splitLine("hello", 3) + if len(result) != 2 { + t.Errorf("expected: 0 lines, got: %d", len(result)) + } +} + +func TestSplitLineSameAsLimit(t *testing.T) { + + result := splitLine("hello", 5) + if len(result) != 1 { + t.Errorf("expected: 0 lines, got: %d", len(result)) + } +}