Skip to content

Commit

Permalink
Add test for regular expression pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Oct 7, 2019
1 parent 1c3802e commit facda00
Showing 1 changed file with 37 additions and 36 deletions.
73 changes: 37 additions & 36 deletions src/test/java/org/jabref/logic/util/BracketedPatternTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class BracketedPatternTest {
class BracketedPatternTest {

private BibEntry bibentry;
private BibDatabase database;
private BibEntry dbentry;

@BeforeEach
public void setUp() throws Exception {
void setUp() throws Exception {
bibentry = new BibEntry();
bibentry.setField(StandardField.AUTHOR, "O. Kitsune");
bibentry.setField(StandardField.YEAR, "2017");
Expand All @@ -47,43 +47,43 @@ public void setUp() throws Exception {
}

@Test
public void bibentryExpansionTest() {
void bibentryExpansionTest() {
BracketedPattern pattern = new BracketedPattern("[year]_[auth]_[firstpage]");
assertEquals("2017_Kitsune_213", pattern.expand(bibentry));
}

@Test
public void nullDatabaseExpansionTest() {
void nullDatabaseExpansionTest() {
BibDatabase another_database = null;
BracketedPattern pattern = new BracketedPattern("[year]_[auth]_[firstpage]");
assertEquals("2017_Kitsune_213", pattern.expand(bibentry, another_database));
}

@Test
public void pureauthReturnsAuthorIfEditorIsAbsent() {
void pureauthReturnsAuthorIfEditorIsAbsent() {
BibDatabase emptyDatabase = new BibDatabase();
BracketedPattern pattern = new BracketedPattern("[pureauth]");
assertEquals("Kitsune", pattern.expand(bibentry, emptyDatabase));
}

@Test
public void pureauthReturnsAuthorIfEditorIsPresent() {
void pureauthReturnsAuthorIfEditorIsPresent() {
BibDatabase emptyDatabase = new BibDatabase();
BracketedPattern pattern = new BracketedPattern("[pureauth]");
bibentry.setField(StandardField.EDITOR, "Editorlastname, Editorfirstname");
assertEquals("Kitsune", pattern.expand(bibentry, emptyDatabase));
}

@Test
public void pureauthReturnsEmptyStringIfAuthorIsAbsent() {
void pureauthReturnsEmptyStringIfAuthorIsAbsent() {
BibDatabase emptyDatabase = new BibDatabase();
BracketedPattern pattern = new BracketedPattern("[pureauth]");
bibentry.clearField(StandardField.AUTHOR);
assertEquals("", pattern.expand(bibentry, emptyDatabase));
}

@Test
public void pureauthReturnsEmptyStringIfAuthorIsAbsentAndEditorIsPresent() {
void pureauthReturnsEmptyStringIfAuthorIsAbsentAndEditorIsPresent() {
BibDatabase emptyDatabase = new BibDatabase();
BracketedPattern pattern = new BracketedPattern("[pureauth]");
bibentry.clearField(StandardField.AUTHOR);
Expand All @@ -92,14 +92,14 @@ public void pureauthReturnsEmptyStringIfAuthorIsAbsentAndEditorIsPresent() {
}

@Test
public void emptyDatabaseExpansionTest() {
void emptyDatabaseExpansionTest() {
BibDatabase another_database = new BibDatabase();
BracketedPattern pattern = new BracketedPattern("[year]_[auth]_[firstpage]");
assertEquals("2017_Kitsune_213", pattern.expand(bibentry, another_database));
}

@Test
public void databaseWithStringsExpansionTest() {
void databaseWithStringsExpansionTest() {
BibDatabase another_database = new BibDatabase();
BibtexString string = new BibtexString("sgr", "Saulius Gražulis");
another_database.addString(string);
Expand All @@ -113,31 +113,31 @@ public void databaseWithStringsExpansionTest() {
}

@Test
public void unbalancedBracketsExpandToSomething() {
void unbalancedBracketsExpandToSomething() {
BracketedPattern pattern = new BracketedPattern("[year]_[auth_[firstpage]");
assertNotEquals("", pattern.expand(bibentry));
}

@Test
public void unbalancedLastBracketExpandsToSomething() {
void unbalancedLastBracketExpandsToSomething() {
BracketedPattern pattern = new BracketedPattern("[year]_[auth]_[firstpage");
assertNotEquals("", pattern.expand(bibentry));
}

@Test
public void entryTypeExpansionTest() {
void entryTypeExpansionTest() {
BracketedPattern pattern = new BracketedPattern("[entrytype]:[year]_[auth]_[pages]");
assertEquals("Misc:2017_Kitsune_213--216", pattern.expand(bibentry));
}

@Test
public void entryTypeExpansionLowercaseTest() {
void entryTypeExpansionLowercaseTest() {
BracketedPattern pattern = new BracketedPattern("[entrytype:lower]:[year]_[auth]_[firstpage]");
assertEquals("misc:2017_Kitsune_213", pattern.expand(bibentry));
}

@Test
public void suppliedBibentryBracketExpansionTest() {
void suppliedBibentryBracketExpansionTest() {
BibDatabase another_database = null;
BracketedPattern pattern = new BracketedPattern("[year]_[auth]_[firstpage]");
BibEntry another_bibentry = new BibEntry();
Expand All @@ -148,66 +148,61 @@ public void suppliedBibentryBracketExpansionTest() {
}

@Test
public void nullBibentryBracketExpansionTest() {
void nullBibentryBracketExpansionTest() {
BibDatabase another_database = null;
BibEntry another_bibentry = null;
BracketedPattern pattern = new BracketedPattern("[year]_[auth]_[firstpage]");
assertThrows(NullPointerException.class, () -> pattern.expand(another_bibentry, ';', another_database));
}

@Test
public void bracketedExpressionDefaultConstructorTest() {
void bracketedExpressionDefaultConstructorTest() {
BibDatabase another_database = null;
BracketedPattern pattern = new BracketedPattern();
assertThrows(NullPointerException.class, () -> pattern.expand(bibentry, ';', another_database));
}

@Test
public void unknownKeyExpandsToEmptyString() {
Character separator = ';';
assertEquals("", BracketedPattern.expandBrackets("[unknownkey]", separator, dbentry, database));
void unknownKeyExpandsToEmptyString() {
assertEquals("", BracketedPattern.expandBrackets("[unknownkey]", ';', dbentry, database));
}

@Test
public void emptyPatternAndEmptyModifierExpandsToEmptyString() {
Character separator = ';';
assertEquals("", BracketedPattern.expandBrackets("[:]", separator, dbentry, database));
void emptyPatternAndEmptyModifierExpandsToEmptyString() {
assertEquals("", BracketedPattern.expandBrackets("[:]", ';', dbentry, database));
}

@Test
public void emptyPatternAndValidModifierExpandsToEmptyString() {
void emptyPatternAndValidModifierExpandsToEmptyString() {
Character separator = ';';
assertEquals("", BracketedPattern.expandBrackets("[:lower]", separator, dbentry, database));
}

@Test
public void bibtexkeyPatternExpandsToBibTeXKey() {
void bibtexkeyPatternExpandsToBibTeXKey() {
Character separator = ';';
assertEquals("HipKro03", BracketedPattern.expandBrackets("[bibtexkey]", separator, dbentry, database));
}

@Test
public void bibtexkeyPatternWithEmptyModifierExpandsToBibTeXKey() {
Character separator = ';';
assertEquals("HipKro03", BracketedPattern.expandBrackets("[bibtexkey:]", separator, dbentry, database));
void bibtexkeyPatternWithEmptyModifierExpandsToBibTeXKey() {
assertEquals("HipKro03", BracketedPattern.expandBrackets("[bibtexkey:]", ';', dbentry, database));
}

@Test
public void authorPatternTreatsVonNamePrefixCorrectly() {
Character separator = ';';
void authorPatternTreatsVonNamePrefixCorrectly() {
assertEquals("Eric von Hippel and Georg von Krogh",
BracketedPattern.expandBrackets("[author]", separator, dbentry, database));
BracketedPattern.expandBrackets("[author]", ';', dbentry, database));
}

@Test
public void lowerFormatterWorksOnVonNamePrefixes() {
Character separator = ';';
void lowerFormatterWorksOnVonNamePrefixes() {
assertEquals("eric von hippel and georg von krogh",
BracketedPattern.expandBrackets("[author:lower]", separator, dbentry, database));
BracketedPattern.expandBrackets("[author:lower]", ';', dbentry, database));
}

@Test
public void testResolvedFieldAndFormat() {
void testResolvedFieldAndFormat() {
BibEntry child = new BibEntry();
child.setField(StandardField.CROSSREF, "HipKro03");
database.insertEntry(child);
Expand All @@ -232,12 +227,18 @@ public void testResolvedFieldAndFormat() {
}

@Test
public void testResolvedParentNotInDatabase() {
void testResolvedParentNotInDatabase() {
BibEntry child = new BibEntry();
child.setField(StandardField.CROSSREF, "HipKro03");
database.removeEntry(dbentry);
database.insertEntry(child);

assertEquals("", BracketedPattern.expandBrackets("[author]", ';', child, database));
}

@Test
void regularExpressionReplace() {
assertEquals("2003-JabRef Science",
BracketedPattern.expandBrackets("[year]-[journal:regex(\"Organization\",\"JabRef\")]", ';', dbentry, database));
}
}

0 comments on commit facda00

Please sign in to comment.