Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating units tests for ControlHelper truncateString #8612

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/test/java/org/jabref/gui/util/ControlHelperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.jabref.gui.util;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.NullSource;

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)
);
}
}