WordWrapper: A word that can't fit on a line by itself throws
This commit is contained in:
parent
c82151c3e3
commit
7476722062
4 changed files with 29 additions and 0 deletions
|
@ -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<Word>) lineQ));
|
||||
lineQ.clear();
|
||||
|
|
10
src/main/java/net/kemitix/text/fit/WordTooLong.java
Normal file
10
src/main/java/net/kemitix/text/fit/WordTooLong.java
Normal 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;
|
||||
}
|
|
@ -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<String> 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<List<String>> wrap(
|
||||
String text,
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue