-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Quality check escaped ampersand #9758
Changes from all commits
df849de
5789411
94a70f8
5b110c2
83d6aab
1619e61
5ad2cfe
2cd91d5
ccd1e8e
d8c9f8d
38c0350
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.jabref.logic.integrity; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.MatchResult; | ||
import java.util.regex.Pattern; | ||
|
||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.field.Field; | ||
|
||
import com.google.common.base.CharMatcher; | ||
|
||
/** | ||
* Checks if the BibEntry contains unescaped ampersands. | ||
*/ | ||
public class AmpersandChecker implements EntryChecker { | ||
// matches for an & preceded by any number of \ | ||
private static final Pattern BACKSLASH_PRECEDED_AMPERSAND = Pattern.compile("\\\\*&"); | ||
|
||
@Override | ||
public List<IntegrityMessage> check(BibEntry entry) { | ||
List<IntegrityMessage> results = new ArrayList<>(); | ||
|
||
for (Map.Entry<Field, String> field : entry.getFieldMap().entrySet()) { | ||
// counts the number of even \ occurrences preceding an & | ||
long unescapedAmpersands = BACKSLASH_PRECEDED_AMPERSAND.matcher(field.getValue()) | ||
.results() | ||
.map(MatchResult::group) | ||
.filter(m -> CharMatcher.is('\\').countIn(m) % 2 == 0) | ||
.count(); | ||
|
||
if (unescapedAmpersands > 0) { | ||
results.add(new IntegrityMessage(Localization.lang("Found %0 unescaped '&'", unescapedAmpersands), entry, field.getKey())); | ||
// note: when changing the message - also do so in tests | ||
} | ||
} | ||
return results; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,68 @@ | ||||||
package org.jabref.logic.integrity; | ||||||
|
||||||
import java.util.Collections; | ||||||
import java.util.List; | ||||||
import java.util.stream.Stream; | ||||||
|
||||||
import org.jabref.model.entry.BibEntry; | ||||||
import org.jabref.model.entry.field.Field; | ||||||
import org.jabref.model.entry.field.StandardField; | ||||||
|
||||||
import org.junit.jupiter.api.Test; | ||||||
import org.junit.jupiter.params.ParameterizedTest; | ||||||
import org.junit.jupiter.params.provider.Arguments; | ||||||
import org.junit.jupiter.params.provider.MethodSource; | ||||||
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||||||
|
||||||
public class AmpersandCheckerTest { | ||||||
|
||||||
private final AmpersandChecker checker = new AmpersandChecker(); | ||||||
private final BibEntry entry = new BibEntry(); | ||||||
|
||||||
@ParameterizedTest | ||||||
@MethodSource("provideAcceptedInputs") | ||||||
void acceptsAllowedInputs(List<IntegrityMessage> expected, Field field, String value) { | ||||||
entry.setField(field, value); | ||||||
assertEquals(expected, checker.check(entry)); | ||||||
} | ||||||
|
||||||
private static Stream<Arguments> provideAcceptedInputs() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can rename that method to |
||||||
return Stream.of( | ||||||
Arguments.of(Collections.emptyList(), StandardField.TITLE, "No ampersand at all"), | ||||||
Arguments.of(Collections.emptyList(), StandardField.FOREWORD, "Properly escaped \\&"), | ||||||
Arguments.of(Collections.emptyList(), StandardField.AUTHOR, "\\& Multiple properly escaped \\&"), | ||||||
Arguments.of(Collections.emptyList(), StandardField.BOOKTITLE, "\\\\\\& With multiple backslashes"), | ||||||
Arguments.of(Collections.emptyList(), StandardField.COMMENT, "\\\\\\& With multiple backslashes multiple times \\\\\\\\\\&"), | ||||||
Arguments.of(Collections.emptyList(), StandardField.NOTE, "In the \\& middle of \\\\\\& something") | ||||||
); | ||||||
} | ||||||
|
||||||
@ParameterizedTest | ||||||
@MethodSource("provideUnacceptedInputs") | ||||||
void rejectsDisallowedInputs(String expectedMessage, Field field, String value) { | ||||||
entry.setField(field, value); | ||||||
assertEquals(List.of(new IntegrityMessage(expectedMessage, entry, field)), checker.check(entry)); | ||||||
} | ||||||
|
||||||
private static Stream<Arguments> provideUnacceptedInputs() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to above: rename to |
||||||
return Stream.of( | ||||||
Arguments.of("Found 1 unescaped '&'", StandardField.SUBTITLE, "A single &"), | ||||||
Arguments.of("Found 2 unescaped '&'", StandardField.ABSTRACT, "Multiple \\\\& not properly & escaped"), | ||||||
Arguments.of("Found 1 unescaped '&'", StandardField.AUTHOR, "To many backslashes \\\\&"), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small typo:
Suggested change
|
||||||
Arguments.of("Found 2 unescaped '&'", StandardField.LABEL, "\\\\\\\\& Multiple times \\\\& multiple backslashes") | ||||||
); | ||||||
} | ||||||
|
||||||
@Test | ||||||
void entryWithEscapedAndUnescapedAmpersand() { | ||||||
entry.setField(StandardField.TITLE, "Jack \\& Jill & more"); | ||||||
assertEquals(List.of(new IntegrityMessage("Found 1 unescaped '&'", entry, StandardField.TITLE)), checker.check(entry)); | ||||||
} | ||||||
Comment on lines
+58
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you integrate that in |
||||||
|
||||||
@Test | ||||||
void entryWithMultipleEscapedAndUnescapedAmpersands() { | ||||||
entry.setField(StandardField.AFTERWORD, "May the force be with you & live long \\\\& prosper \\& to infinity \\\\\\& beyond & assemble \\\\\\\\& excelsior!"); | ||||||
assertEquals(List.of(new IntegrityMessage("Found 4 unescaped '&'", entry, StandardField.AFTERWORD)), checker.check(entry)); | ||||||
} | ||||||
} | ||||||
Comment on lines
+63
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you integrate that in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, we can do that. The reasoning behind having it separated was that it basically combined the test cases from before, containing both escaped and unescaped ampersands (in the parametrized tests there was no mixing of the two). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a note: Although this should work both ways, translations should normally only be done with crowdin. Unified and tested common workflows lead to less errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, Crowdin will overwrite the non english translations on the next update
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the information. We will make sure to do it the proper way next time.