diff --git a/pkg/linebreak/knuthplass.go b/pkg/linebreak/knuthplass.go index 50c860f..1838b9c 100644 --- a/pkg/linebreak/knuthplass.go +++ b/pkg/linebreak/knuthplass.go @@ -38,7 +38,7 @@ func KnuthPlass(text string, width int) []string { spaces := j - i - 1 totalLen := lineLength + spaces - if totalLen > width { + if totalLen > width && j > i+1 { break } diff --git a/test/linebreak/knuthplass_test.go b/test/linebreak/knuthplass_test.go index b8275df..a37f5fa 100644 --- a/test/linebreak/knuthplass_test.go +++ b/test/linebreak/knuthplass_test.go @@ -67,6 +67,14 @@ func TestKnuthPlass(t *testing.T) { expected: []string{"The lazy", "yellow dog", "was caught by", "the slow red", "fox as he lay", "sleeping in", "the sun"}, }, }, + { + name: "Word longer than width", + args: args{ + text: "a supercalifragilisticexpialidocious word", + width: 10, + expected: []string{"a", "supercalifragilisticexpialidocious", "word"}, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {