BoxFitterTest: put existing tests inside a nest
This commit is contained in:
parent
b215021eee
commit
9f9797dcbd
1 changed files with 65 additions and 58 deletions
|
@ -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<Integer, Font> 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<String, Integer> 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<String, Integer> 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<String, Integer> 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<String, Integer> 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));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue