WordWrapper: A word that can't fit on a line by itself throws

This commit is contained in:
Paul Campbell 2020-05-21 22:53:37 +01:00
parent c82151c3e3
commit 7476722062
4 changed files with 29 additions and 0 deletions

View file

@ -56,6 +56,9 @@ class TextLineWrapImpl implements WordWrapper {
lineQ.forEach(wordQ::push); lineQ.forEach(wordQ::push);
return removeBlankLines(lines); return removeBlankLines(lines);
} }
if (end == 0 && word.width > width) {
throw new WordTooLong(word.word);
}
if ((end + word.width) > width) { if ((end + word.width) > width) {
lines.add(wordsAsString((Deque<Word>) lineQ)); lines.add(wordsAsString((Deque<Word>) lineQ));
lineQ.clear(); lineQ.clear();

View file

@ -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;
}

View file

@ -16,6 +16,8 @@ public interface WordWrapper {
* @return a list of the each line of text * @return a list of the each line of text
* @throws NotEnoughSpace if there are more than {@link Integer#MAX_VALUE} * @throws NotEnoughSpace if there are more than {@link Integer#MAX_VALUE}
* lines - so not likely. * lines - so not likely.
* @throws WordTooLong if there is a word that is too long to fit on a line
* by itself.
*/ */
List<String> wrap( List<String> wrap(
String text, String text,
@ -35,6 +37,8 @@ public interface WordWrapper {
* @return a list of the each line of text * @return a list of the each line of text
* @throws NotEnoughSpace if there are more lines than can be fitted in the * @throws NotEnoughSpace if there are more lines than can be fitted in the
* boxes provided. * 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<List<String>> wrap( List<List<String>> wrap(
String text, String text,

View file

@ -107,6 +107,18 @@ public class TextLineWrapTest
oneLinesWorthOfWords, oneLinesWorthOfWords,
WORD); 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) { private String words(int number, String word) {