From 4149e014d4b13b91c459ea45d91e939e6361e29c Mon Sep 17 00:00:00 2001 From: Thiago Paiva Date: Sun, 27 Mar 2022 11:43:54 -0300 Subject: [PATCH] Add ControlHelper truncateString tests --- .../jabref/gui/util/ControlHelperTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/test/java/org/jabref/gui/util/ControlHelperTest.java diff --git a/src/test/java/org/jabref/gui/util/ControlHelperTest.java b/src/test/java/org/jabref/gui/util/ControlHelperTest.java new file mode 100644 index 00000000000..e30223056e4 --- /dev/null +++ b/src/test/java/org/jabref/gui/util/ControlHelperTest.java @@ -0,0 +1,58 @@ +package org.jabref.gui.util; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class ControlHelperTest { + + private final String TEXT = "abcdef"; + private final int MAX_CHARACTERS = 5; + private final int DEFAULT_MAX_CHARACTERS = -1; + private final String ELLIPSIS_STRING = "***"; + private final ControlHelper.EllipsisPosition ELLIPSIS_POSITION = ControlHelper.EllipsisPosition.ENDING; + + @ParameterizedTest + @NullAndEmptySource + void truncateWithTextNullAndEmptyReturnsSource(String text){ + String truncatedText = ControlHelper.truncateString(text, MAX_CHARACTERS, ELLIPSIS_STRING, ELLIPSIS_POSITION); + assertEquals(text, truncatedText); + } + + @Test + void truncateWithDefaultMaxCharactersReturnsText(){ + String truncatedText = ControlHelper.truncateString(TEXT, DEFAULT_MAX_CHARACTERS, ELLIPSIS_STRING, ELLIPSIS_POSITION); + assertEquals(TEXT, truncatedText); + } + + @Test + void truncateWithEllipsisPositionBeginningReturnsTruncatedText(){ + String truncatedText = ControlHelper.truncateString(TEXT, MAX_CHARACTERS, ELLIPSIS_STRING, ControlHelper.EllipsisPosition.BEGINNING); + assertEquals("***ef", truncatedText); + } + + @Test + void truncateWithEllipsisPositionCenterReturnsTruncatedText(){ + String truncatedText = ControlHelper.truncateString(TEXT, MAX_CHARACTERS, ELLIPSIS_STRING, ControlHelper.EllipsisPosition.CENTER); + assertEquals("a***f", truncatedText); + } + + @Test + void truncateWithDefaultMaxCharactersAndNullEllipsisAndPositionEndingReturnsTruncatedText(){ + String text = "a".repeat(75) + "b".repeat(25); + String truncatedText = ControlHelper.truncateString(text, DEFAULT_MAX_CHARACTERS, null, ControlHelper.EllipsisPosition.ENDING); + assertEquals("a".repeat(75), truncatedText); + } + + @ParameterizedTest + @NullSource + void truncateWithNullEllipsisPositionThrowsNullPointerException(ControlHelper.EllipsisPosition ellipsisPosition){ + assertThrows( + NullPointerException.class, + () -> ControlHelper.truncateString(TEXT, MAX_CHARACTERS, ELLIPSIS_STRING, ellipsisPosition) + ); + } +}