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.assertj.core.api.WithAssertions;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
@ -22,82 +23,88 @@ public class BoxFitterTest
|
||||||
implements WithAssertions {
|
implements WithAssertions {
|
||||||
|
|
||||||
private final BoxFitter boxFitter = TextFit.fitter();
|
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 final Font font;
|
||||||
private Function<Integer, Font> fontFactory;
|
private Function<Integer, Font> fontFactory;
|
||||||
private Rectangle2D box = new Rectangle(imageSize, imageSize);
|
|
||||||
|
|
||||||
public BoxFitterTest() throws URISyntaxException, IOException, FontFormatException {
|
public BoxFitterTest() throws URISyntaxException, IOException, FontFormatException {
|
||||||
URL resource = this.getClass().getResource("alice/Alice-Regular.ttf");
|
URL resource = this.getClass().getResource("alice/Alice-Regular.ttf");
|
||||||
|
int initialFontSize = 20;
|
||||||
font = Font.createFont(Font.TRUETYPE_FONT, new File(resource.toURI()))
|
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);
|
fontFactory = size -> font.deriveFont(Font.PLAIN, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Nested
|
||||||
@DisplayName("Fit single words")
|
@DisplayName("Single Box")
|
||||||
public void fitSingleWord() {
|
public class SingleBox {
|
||||||
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
|
private final int imageSize = 300;
|
||||||
@DisplayName("Fit various lengths")
|
private final Graphics2D graphics2D = graphics(imageSize, imageSize);
|
||||||
public void fitVariousLengths() {
|
private Rectangle2D box = new Rectangle(imageSize, imageSize);
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Text too long to fit throws and exception")
|
@DisplayName("Fit single words")
|
||||||
// too long to fit means it would need to be rendered at a font size of <2
|
public void fitSingleWord() {
|
||||||
public void tooLongThrows() {
|
Map<String, Integer> wordMap = Map.of(
|
||||||
String longText = longStringGenerator(197);
|
".", 263,
|
||||||
assertThatExceptionOfType(IllegalArgumentException.class)
|
"a", 263,
|
||||||
.isThrownBy(() -> invoke(longText));
|
"Word", 121,
|
||||||
}
|
"longer", 104,
|
||||||
|
"extralongword", 44
|
||||||
|
);
|
||||||
|
wordMap.forEach((word, expectedSize) ->
|
||||||
|
assertThat(invoke(word))
|
||||||
|
.as(word)
|
||||||
|
.isEqualTo(expectedSize));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Long text can be fitted down to a font size of 3")
|
@DisplayName("Fit various lengths")
|
||||||
public void veryLongFits() {
|
public void fitVariousLengths() {
|
||||||
String longText = longStringGenerator(196);
|
Map<String, Integer> wordMap = Map.of(
|
||||||
assertThatCode(() -> invoke(longText))
|
". .", 263,
|
||||||
.doesNotThrowAnyException();
|
"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) {
|
@Test
|
||||||
return boxFitter.fit(longText, fontFactory, graphics2D, box);
|
@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) {
|
private String longStringGenerator(int cycles) {
|
||||||
String text = "This is a long piece of text that should result in an " +
|
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.";
|
"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));
|
.mapToObj(x -> "\n").collect(Collectors.joining(text));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue