Skip to content

Commit

Permalink
add unit test to generateInstitutionKey (#11756)
Browse files Browse the repository at this point in the history
* add unit test to generateInstitutionKey

prepare to refactor this method

* Update BracketedPatternTest.java
  • Loading branch information
leaf-soba authored Sep 13, 2024
1 parent f7fde15 commit 939f696
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
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));
}
}

0 comments on commit 939f696

Please sign in to comment.