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

add unit test to generateInstitutionKey #11756

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.jabref.model.strings.LatexToUnicodeAdapter;
import org.jabref.model.strings.StringUtil;

import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -1218,14 +1219,14 @@ protected static List<String> parseFieldAndModifiers(String arg) {
* <li>null if content is null</li>
* </ul>
*/
private static String generateInstitutionKey(String content) {
@VisibleForTesting
static String generateInstitutionKey(String content) {
if (content == null) {
return null;
}
if (content.isBlank()) {
return "";
}

Matcher matcher = INLINE_ABBREVIATION.matcher(content);
if (matcher.find()) {
return LatexToUnicodeAdapter.format(matcher.group());
Expand Down Expand Up @@ -1309,8 +1310,7 @@ private static String generateInstitutionKey(String content) {
// Putting parts together.
return (university == null ? Objects.toString(rest, "") : university)
+ (school == null ? "" : school)
+ ((department == null)
|| ((school != null) && department.equals(school)) ? "" : department);
+ ((department == null) || (department.equals(school)) ? "" : department);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
Expand Down Expand Up @@ -734,4 +735,31 @@ void editorFieldMarkers(String expectedCitationKey, String pattern, String edito
BracketedPattern bracketedPattern = new BracketedPattern(pattern);
assertEquals(expectedCitationKey, bracketedPattern.expand(bibEntry));
}

@ParameterizedTest
@CsvSource({
"'', ''",
"The Attributed Graph Grammar System ({AGG}),AGG",
"'The University of Science',UniScience",
"'School of Business, Department of Management',BM",
"'Graph Systems Research Group',GSRG",
"'The Great Institute, 123 Main Street, Springfield',GreatInstitute",
"'Invalid {\\Unicode}',Invalid",
"'School of Electrical Engineering ({SEE}), Department of Computer Science',SEE",
"'{The Attributed Graph Grammar System ({AGG})}',AGG",
"'{The Attributed Graph Grammar System}',AGGS",
"'{University of Example, Department of Computer Science, Some Address}',UniExampleCS",
"'{Example School of Engineering, Department of Computer Science, Some Address}',SomeAddressEECS",
"'{Example Institute, Computer Science Department, Some Address}',ExampleInstituteCS",
"'{Short Part, Some Address}',ShortPart",
"'{Example with Several Tokens, Some Address}',EST"})

void generateInstitutionKeyTest(String input, String expected) {
assertEquals(expected, BracketedPattern.generateInstitutionKey(input));
}

@Test
void generateInstitutionKeyNullTest() {
assertNull(BracketedPattern.generateInstitutionKey(null));
}
}
Loading