From 9f9797dcbddf3bddc8ff4734cc90beb97ae232ec Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 21 May 2020 07:53:59 +0100 Subject: [PATCH] BoxFitterTest: put existing tests inside a nest --- .../net/kemitix/text/fit/BoxFitterTest.java | 123 +++++++++--------- 1 file changed, 65 insertions(+), 58 deletions(-) diff --git a/src/test/java/net/kemitix/text/fit/BoxFitterTest.java b/src/test/java/net/kemitix/text/fit/BoxFitterTest.java index ed9116a..1379629 100644 --- a/src/test/java/net/kemitix/text/fit/BoxFitterTest.java +++ b/src/test/java/net/kemitix/text/fit/BoxFitterTest.java @@ -2,6 +2,7 @@ package net.kemitix.text.fit; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import java.awt.*; @@ -22,82 +23,88 @@ public class BoxFitterTest implements WithAssertions { private final BoxFitter boxFitter = TextFit.fitter(); - private final int imageSize = 300; - private final int fontSize = 20; - private final Graphics2D graphics2D = graphics(imageSize, imageSize); private final Font font; private Function fontFactory; - private Rectangle2D box = new Rectangle(imageSize, imageSize); public BoxFitterTest() throws URISyntaxException, IOException, FontFormatException { URL resource = this.getClass().getResource("alice/Alice-Regular.ttf"); + int initialFontSize = 20; font = Font.createFont(Font.TRUETYPE_FONT, new File(resource.toURI())) - .deriveFont(Font.PLAIN, fontSize); + .deriveFont(Font.PLAIN, initialFontSize); fontFactory = size -> font.deriveFont(Font.PLAIN, size); } - @Test - @DisplayName("Fit single words") - public void fitSingleWord() { - Map wordMap = Map.of( - ".", 263, - "a", 263, - "Word", 121, - "longer", 104, - "extralongword", 44 - ); - wordMap.forEach((word, expectedSize) -> - assertThat(invoke(word)) - .as(word) - .isEqualTo(expectedSize)); - } + @Nested + @DisplayName("Single Box") + public class SingleBox { - @Test - @DisplayName("Fit various lengths") - public void fitVariousLengths() { - Map wordMap = Map.of( - ". .", 263, - "a a", 208, - "Another Word", 81, - longStringGenerator(1), 100, - longStringGenerator(2), 36, - longStringGenerator(3), 27, - longStringGenerator(4), 22, - longStringGenerator(5), 20, - longStringGenerator(100), 4, - longStringGenerator(196), 3 - ); - wordMap.forEach((word, expectedSize) -> - assertThat(invoke(word)) - .as(word) - .isEqualTo(expectedSize)); - } + private final int imageSize = 300; + private final Graphics2D graphics2D = graphics(imageSize, imageSize); + private Rectangle2D box = new Rectangle(imageSize, imageSize); - @Test - @DisplayName("Text too long to fit throws and exception") - // too long to fit means it would need to be rendered at a font size of <2 - public void tooLongThrows() { - String longText = longStringGenerator(197); - assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> invoke(longText)); - } + @Test + @DisplayName("Fit single words") + public void fitSingleWord() { + Map wordMap = Map.of( + ".", 263, + "a", 263, + "Word", 121, + "longer", 104, + "extralongword", 44 + ); + wordMap.forEach((word, expectedSize) -> + assertThat(invoke(word)) + .as(word) + .isEqualTo(expectedSize)); + } - @Test - @DisplayName("Long text can be fitted down to a font size of 3") - public void veryLongFits() { - String longText = longStringGenerator(196); - assertThatCode(() -> invoke(longText)) - .doesNotThrowAnyException(); - } + @Test + @DisplayName("Fit various lengths") + public void fitVariousLengths() { + Map wordMap = Map.of( + ". .", 263, + "a a", 208, + "Another Word", 81, + longStringGenerator(1), 100, + longStringGenerator(2), 36, + longStringGenerator(3), 27, + longStringGenerator(4), 22, + longStringGenerator(5), 20, + longStringGenerator(100), 4, + longStringGenerator(196), 3 + ); + wordMap.forEach((word, expectedSize) -> + assertThat(invoke(word)) + .as(word) + .isEqualTo(expectedSize)); + } - private int invoke(String longText) { - return boxFitter.fit(longText, fontFactory, graphics2D, box); + @Test + @DisplayName("Text too long to fit throws and exception") + // too long to fit means it would need to be rendered at a font size of <2 + public void tooLongThrows() { + String longText = longStringGenerator(197); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> invoke(longText)); + } + + @Test + @DisplayName("Long text can be fitted down to a font size of 3") + public void veryLongFits() { + String longText = longStringGenerator(196); + assertThatCode(() -> invoke(longText)) + .doesNotThrowAnyException(); + } + + private int invoke(String longText) { + return boxFitter.fit(longText, fontFactory, graphics2D, box); + } } private String longStringGenerator(int cycles) { String text = "This is a long piece of text that should result in an " + "attempt to render it at a font size on less than 2."; - return "cycles: " + cycles + IntStream.range(0, cycles) + return "cycles: " + cycles + IntStream.range(0, cycles) .mapToObj(x -> "\n").collect(Collectors.joining(text)); }