-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add truncate as a BibTex key modifier (#6427)
* Add modifier to truncate a BibTex key * Add tests for the truncate BibTex key modifier * Revert "Add tests for the truncate BibTex key modifier" This reverts commit 19fbbb3 The tests were placed in the wrong file. * Add tests for the truncate bracketed pattern modifier * Remove unnecessary imports in the test for the truncate bracketed pattern modifier * Fix code style of local variable * Refactor BracketedPatternTest to maintain a parallel directory structure * Refactor test for the truncate BibTex key modifier * Drop unused and unnecessary imports from BracketedPatternTest.java * Refactor truncate modifier to be compatible with Formatters * Fix type in truncate modifier test * Add check for Null in format * Refactor tests for truncate modifier * Add truncation for long directory names * Fix name and documentation of createDirNameFromPattern * Update src/main/java/org/jabref/logic/formatter/minifier/TruncateFormatter.java Co-authored-by: Christoph <cschwentker@gmail.com> * Fix matching the modifier String to a TruncateFormatter * Fix encapsulation and naming in TruncateFormatter * Fix JavaDoc in TruncateFormatter * Add changes to CHANGELOG.md * Fix createDirNameFromPattern to verify names of subfolders * Add tests for long directory names * Fix truncate with negative integer * Add test with negative index * Add test with invalid data * Refactor names of truncate tests * Add TruncateFormatter to general Formatter tests * Fix truncate with negative index * Drop truncation of long directory names * Fix mating truncate in Formatters.java Change to use a precompiled pattern for matching 'truncateN' * Fix matching `truncateN` in Formatters.java Change to use a precompiled pattern for matching 'truncateN' * Resolve conflicts * Refactor truncate tests in BracketedPatternTest Co-authored-by: Christoph <cschwentker@gmail.com>
- Loading branch information
1 parent
98b62ba
commit 16d4938
Showing
8 changed files
with
147 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/org/jabref/logic/formatter/minifier/TruncateFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.jabref.logic.formatter.minifier; | ||
|
||
import java.util.Objects; | ||
|
||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.model.cleanup.Formatter; | ||
|
||
public class TruncateFormatter extends Formatter { | ||
private final int TRUNCATE_AFTER; | ||
private final String KEY; | ||
|
||
/** | ||
* The TruncateFormatter truncates a string after the given index and removes trailing whitespaces. | ||
* | ||
* @param truncateIndex truncate a string after this index. | ||
*/ | ||
public TruncateFormatter(final int truncateIndex) { | ||
TRUNCATE_AFTER = (truncateIndex >= 0) ? truncateIndex : Integer.MAX_VALUE; | ||
KEY = "truncate" + TRUNCATE_AFTER; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return Localization.lang("Truncate"); | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return KEY; | ||
} | ||
|
||
/** | ||
* Truncates a string after the given index. | ||
*/ | ||
@Override | ||
public String format(final String input) { | ||
Objects.requireNonNull(input); | ||
final int index = Math.min(TRUNCATE_AFTER, input.length()); | ||
return input.substring(0, index).stripTrailing(); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return Localization.lang("Truncates a string after a given index."); | ||
} | ||
|
||
@Override | ||
public String getExampleInput() { | ||
return "Truncate this sentence."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/test/java/org/jabref/logic/formatter/minifier/TruncateFormatterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.jabref.logic.formatter.minifier; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
/** | ||
* Tests in addition to the general tests from {@link org.jabref.logic.formatter.FormatterTest} | ||
*/ | ||
public class TruncateFormatterTest { | ||
private final String TITLE = "A Title"; | ||
|
||
@Test | ||
void formatWorksWith0Index() { | ||
TruncateFormatter formatter = new TruncateFormatter(0); | ||
assertEquals("", formatter.format(TITLE)); | ||
} | ||
|
||
@Test | ||
void formatRemovesTrailingWhitespace() { | ||
TruncateFormatter formatter = new TruncateFormatter(2); | ||
assertEquals("A", formatter.format(TITLE)); | ||
} | ||
|
||
@Test | ||
void formatKeepsInternalWhitespace() { | ||
TruncateFormatter formatter = new TruncateFormatter(3); | ||
assertEquals("A T", formatter.format(TITLE)); | ||
} | ||
|
||
@Test | ||
void formatWorksWith9999Length() { | ||
TruncateFormatter formatter = new TruncateFormatter(9999); | ||
assertEquals(TITLE, formatter.format(TITLE)); | ||
} | ||
|
||
@Test | ||
void formatIgnoresNegativeIndex() { | ||
TruncateFormatter formatter = new TruncateFormatter(-1); | ||
assertEquals(TITLE, formatter.format(TITLE)); | ||
} | ||
|
||
@Test | ||
void formatWorksWithEmptyString() { | ||
TruncateFormatter formatter = new TruncateFormatter(9999); | ||
assertEquals("", formatter.format("")); | ||
} | ||
|
||
@Test | ||
void formatThrowsExceptionNullString() { | ||
TruncateFormatter formatter = new TruncateFormatter(9999); | ||
assertThrows(NullPointerException.class, () -> formatter.format(null)); | ||
} | ||
} |