From 7476722062565d546872e7dd36fb79c864f6f22a Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 21 May 2020 22:53:37 +0100 Subject: [PATCH] WordWrapper: A word that can't fit on a line by itself throws --- .../java/net/kemitix/text/fit/TextLineWrapImpl.java | 3 +++ src/main/java/net/kemitix/text/fit/WordTooLong.java | 10 ++++++++++ src/main/java/net/kemitix/text/fit/WordWrapper.java | 4 ++++ .../java/net/kemitix/text/fit/TextLineWrapTest.java | 12 ++++++++++++ 4 files changed, 29 insertions(+) create mode 100644 src/main/java/net/kemitix/text/fit/WordTooLong.java diff --git a/src/main/java/net/kemitix/text/fit/TextLineWrapImpl.java b/src/main/java/net/kemitix/text/fit/TextLineWrapImpl.java index 001b5f3..7ad68c2 100644 --- a/src/main/java/net/kemitix/text/fit/TextLineWrapImpl.java +++ b/src/main/java/net/kemitix/text/fit/TextLineWrapImpl.java @@ -56,6 +56,9 @@ class TextLineWrapImpl implements WordWrapper { lineQ.forEach(wordQ::push); return removeBlankLines(lines); } + if (end == 0 && word.width > width) { + throw new WordTooLong(word.word); + } if ((end + word.width) > width) { lines.add(wordsAsString((Deque) lineQ)); lineQ.clear(); diff --git a/src/main/java/net/kemitix/text/fit/WordTooLong.java b/src/main/java/net/kemitix/text/fit/WordTooLong.java new file mode 100644 index 0000000..2f0c826 --- /dev/null +++ b/src/main/java/net/kemitix/text/fit/WordTooLong.java @@ -0,0 +1,10 @@ +package net.kemitix.text.fit; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public class WordTooLong extends RuntimeException { + private final String longWord; +} diff --git a/src/main/java/net/kemitix/text/fit/WordWrapper.java b/src/main/java/net/kemitix/text/fit/WordWrapper.java index cd41e7f..ed71c1b 100644 --- a/src/main/java/net/kemitix/text/fit/WordWrapper.java +++ b/src/main/java/net/kemitix/text/fit/WordWrapper.java @@ -16,6 +16,8 @@ public interface WordWrapper { * @return a list of the each line of text * @throws NotEnoughSpace if there are more than {@link Integer#MAX_VALUE} * lines - so not likely. + * @throws WordTooLong if there is a word that is too long to fit on a line + * by itself. */ List wrap( String text, @@ -35,6 +37,8 @@ public interface WordWrapper { * @return a list of the each line of text * @throws NotEnoughSpace if there are more lines than can be fitted in the * boxes provided. + * @throws WordTooLong if there is a word that is too long to fit on a line + * by itself. Not likely as this would simply force smaller fonts. */ List> wrap( String text, diff --git a/src/test/java/net/kemitix/text/fit/TextLineWrapTest.java b/src/test/java/net/kemitix/text/fit/TextLineWrapTest.java index 30ac6b8..aa885d5 100644 --- a/src/test/java/net/kemitix/text/fit/TextLineWrapTest.java +++ b/src/test/java/net/kemitix/text/fit/TextLineWrapTest.java @@ -107,6 +107,18 @@ public class TextLineWrapTest oneLinesWorthOfWords, WORD); } + + @Test + @DisplayName("A word that can't fit on a line by itself throws") + public void overLongWordThrows() { + String longWord = words(wordsPerLine * 2, WORD) + .replace(" ", ""); + assertThatExceptionOfType(WordTooLong.class) + .isThrownBy(() -> invoke(longWord)) + .satisfies(error -> + assertThat(error.getLongWord()) + .isEqualTo(longWord)); + } } private String words(int number, String word) {