-
-
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
Add simple unit tests #7543
Add simple unit tests #7543
Changes from 7 commits
3084c57
0004bc5
4e4f8fe
8f68850
f8ed4b1
4ea42ed
8e297b7
4f7c6ba
2723e8e
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,49 @@ | ||||||
package org.jabref.gui.autocompleter; | ||||||
|
||||||
import org.jabref.logic.journals.JournalAbbreviationRepository; | ||||||
import org.jabref.model.database.BibDatabase; | ||||||
import org.jabref.model.entry.BibEntry; | ||||||
import org.jabref.model.entry.field.Field; | ||||||
import org.jabref.model.entry.field.FieldFactory; | ||||||
import org.jabref.model.entry.field.SpecialField; | ||||||
import org.jabref.model.entry.field.StandardField; | ||||||
|
||||||
import org.junit.jupiter.api.Test; | ||||||
|
||||||
import static org.junit.jupiter.api.Assertions.assertSame; | ||||||
import static org.mockito.Mockito.mock; | ||||||
|
||||||
class SuggestionProvidersTest { | ||||||
|
||||||
@Test | ||||||
void getForFieldTest() { | ||||||
BibDatabase database = new BibDatabase(); | ||||||
JournalAbbreviationRepository abbreviationRepository = mock(JournalAbbreviationRepository.class); | ||||||
BibEntry entry = new BibEntry(); | ||||||
Field personEntryField = StandardField.AUTHOR; | ||||||
Field singleEntryField = StandardField.XREF; | ||||||
Field multipleEntryField = StandardField.XDATA; | ||||||
Field journalEntryField = StandardField.JOURNAL; | ||||||
Field publisherEntryField = StandardField.PUBLISHER; | ||||||
Field specialEntryField = SpecialField.PRINTED; | ||||||
AutoCompletePreferences autoCompletePreferences = new AutoCompletePreferences(true, AutoCompleteFirstNameMode.BOTH, AutoCompletePreferences.NameFormat.BOTH, FieldFactory.parseFieldList(personEntryField.getName() + ";" + singleEntryField.getName() + ";" + multipleEntryField.getName() + ";" + journalEntryField.getName() + ";" + publisherEntryField.getName() + ";" + specialEntryField.getName()), null); | ||||||
SuggestionProviders sp = new SuggestionProviders(database, abbreviationRepository, autoCompletePreferences); | ||||||
SuggestionProviders empty = new SuggestionProviders(); | ||||||
|
||||||
entry.setField(personEntryField, "Goethe"); | ||||||
entry.setField(singleEntryField, "Single"); | ||||||
entry.setField(multipleEntryField, "Multiple"); | ||||||
entry.setField(journalEntryField, "Journal"); | ||||||
entry.setField(publisherEntryField, "Publisher"); | ||||||
entry.setField(specialEntryField, "2000"); | ||||||
database.insertEntry(entry); | ||||||
|
||||||
assertSame("org.jabref.gui.autocompleter.EmptySuggestionProvider", empty.getForField(personEntryField).getClass().getName()); | ||||||
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. Pls use
Suggested change
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. (applies for all lines here) |
||||||
assertSame("org.jabref.gui.autocompleter.PersonNameSuggestionProvider", sp.getForField(personEntryField).getClass().getName()); | ||||||
assertSame("org.jabref.gui.autocompleter.BibEntrySuggestionProvider", sp.getForField(singleEntryField).getClass().getName()); | ||||||
assertSame("org.jabref.gui.autocompleter.BibEntrySuggestionProvider", sp.getForField(multipleEntryField).getClass().getName()); | ||||||
assertSame("org.jabref.gui.autocompleter.JournalsSuggestionProvider", sp.getForField(journalEntryField).getClass().getName()); | ||||||
assertSame("org.jabref.gui.autocompleter.JournalsSuggestionProvider", sp.getForField(publisherEntryField).getClass().getName()); | ||||||
assertSame("org.jabref.gui.autocompleter.WordSuggestionProvider", sp.getForField(specialEntryField).getClass().getName()); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -747,6 +747,9 @@ void testLastPage() { | |
assertEquals("97", CitationKeyGenerator.lastPage("7,41,73--97")); | ||
assertEquals("97", CitationKeyGenerator.lastPage("7,41,97--73")); | ||
assertEquals("43", CitationKeyGenerator.lastPage("43+")); | ||
assertEquals("0", CitationKeyGenerator.lastPage("00--0")); | ||
assertEquals("1", CitationKeyGenerator.lastPage("1--1")); | ||
|
||
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. Please no empty line at the end of the method 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. Please convert the whole test to a |
||
} | ||
|
||
@SuppressWarnings("ConstantConditions") | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||||||||||
package org.jabref.logic.citationstyle; | ||||||||||||||
|
||||||||||||||
import java.util.ArrayList; | ||||||||||||||
import java.util.List; | ||||||||||||||
|
||||||||||||||
import org.jabref.model.database.BibDatabase; | ||||||||||||||
import org.jabref.model.database.BibDatabaseContext; | ||||||||||||||
import org.jabref.model.entry.BibEntry; | ||||||||||||||
|
||||||||||||||
import org.junit.jupiter.api.Test; | ||||||||||||||
|
||||||||||||||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||||||||||||||
|
||||||||||||||
class CitationStyleCacheTest { | ||||||||||||||
|
||||||||||||||
private BibEntry bibEntry; | ||||||||||||||
private List<BibEntry> entries; | ||||||||||||||
private BibDatabase database; | ||||||||||||||
private BibDatabaseContext databaseContext; | ||||||||||||||
private CitationStyleCache csCache; | ||||||||||||||
|
||||||||||||||
@Test | ||||||||||||||
void getCitationForTest() { | ||||||||||||||
BibEntry bibEntry = new BibEntry(); | ||||||||||||||
bibEntry.setCitationKey("test"); | ||||||||||||||
List<BibEntry> entries = new ArrayList<>(); | ||||||||||||||
entries.add(0, bibEntry); | ||||||||||||||
BibDatabase database = new BibDatabase(entries); | ||||||||||||||
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't one do the following?
Suggested change
|
||||||||||||||
BibDatabaseContext databaseContext = new BibDatabaseContext(database); | ||||||||||||||
CitationStyleCache csCache = new CitationStyleCache(databaseContext); | ||||||||||||||
|
||||||||||||||
assertNotNull(csCache.getCitationFor(bibEntry)); | ||||||||||||||
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. Please test for a concrete result. |
||||||||||||||
} | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,9 @@ public void test() { | |
assertEquals("UPPER", formatter.format("UPPER")); | ||
assertEquals("UPPER {lower}", formatter.format("upper {lower}")); | ||
assertEquals("UPPER {l}OWER", formatter.format("upper {l}ower")); | ||
assertEquals("1", formatter.format("1")); | ||
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. Please convert to |
||
assertEquals("!", formatter.format("!")); | ||
|
||
} | ||
|
||
@Test | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,6 +3,8 @@ | |||||
import org.junit.jupiter.api.Test; | ||||||
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||||||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||||||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||||||
|
||||||
class AbbreviationTest { | ||||||
|
||||||
|
@@ -101,4 +103,18 @@ void testDefaultAndShortestUniqueAbbreviationsAreSame() { | |||||
Abbreviation abbreviation = new Abbreviation("Long Name", "L N"); | ||||||
assertEquals(abbreviation.getAbbreviation(), abbreviation.getShortestUniqueAbbreviation()); | ||||||
} | ||||||
|
||||||
@Test | ||||||
void testToString() { | ||||||
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. JabRef does not rely on the If you want, you can check for nonNull of |
||||||
Abbreviation abbreviation = new Abbreviation("Long Name", "L N", "LN"); | ||||||
assertEquals(abbreviation.toString(), "Abbreviation{name=Long Name, abbreviation=L N, medlineAbbreviation=L N, shortestUniqueAbbreviation=LN}"); | ||||||
} | ||||||
|
||||||
@Test | ||||||
void testEquals() { | ||||||
Abbreviation abbreviation = new Abbreviation("Long Name", "L N", "LN"); | ||||||
Abbreviation otherAbbreviation = new Abbreviation("Long Name", "L N", "LN"); | ||||||
assertTrue(abbreviation.equals(otherAbbreviation)); | ||||||
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.
Suggested change
|
||||||
assertFalse(abbreviation.equals("String")); | ||||||
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.
Suggested change
|
||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.jabref.model; | ||
|
||
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 static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class FieldChangeTest { | ||
|
||
private FieldChange Fieldchange; | ||
|
||
@Test | ||
void testHashCode() { | ||
FieldChange fcNull = new FieldChange(null, null, null, null); | ||
assertEquals(923521, fcNull.hashCode()); | ||
} | ||
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. Please no test for You can include https://jqno.nl/equalsverifier/ as dependency and work on that. Please do not test for that manually. |
||
|
||
@Test | ||
void testEquals() { | ||
BibEntry entry = new BibEntry(); | ||
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. This is a very hard to read test. Please use https://jqno.nl/equalsverifier/ or just delete the test. I never used https://jqno.nl/equalsverifier/ for myself. Please add a proposal based on that and we can investigate whether this is really worth it or causes much trouble. |
||
BibEntry entryOther = new BibEntry(); | ||
final Field field = StandardField.DOI; | ||
final Field fieldOther = StandardField.DOI; | ||
entry.setField(StandardField.DOI, "foo"); | ||
final String oldValue = "foo"; | ||
final String newValue = "bar"; | ||
final String oldValueOther = "fooX"; | ||
final String newValueOther = "barX"; | ||
|
||
FieldChange fc = new FieldChange(entry, field, oldValue, newValue); | ||
FieldChange fcOther = new FieldChange(entryOther, fieldOther, oldValueOther, newValueOther); | ||
FieldChange fcBlankAll = new FieldChange(null, null, null, null); | ||
FieldChange fcBlankField = new FieldChange(entry, null, oldValue, newValue); | ||
FieldChange fcBlankOldValue = new FieldChange(entry, field, null, newValue); | ||
FieldChange fcBlankNewValue = new FieldChange(entry, field, oldValue, null); | ||
|
||
assertFalse(fc.equals("foo")); | ||
assertTrue(fc.equals(fc)); | ||
assertFalse(fcBlankAll.equals(fc)); | ||
assertFalse(fc.equals(fcOther)); | ||
assertFalse(fcBlankField.equals(fc)); | ||
assertFalse(fcBlankOldValue.equals(fc)); | ||
assertFalse(fcBlankNewValue.equals(fc)); | ||
assertTrue(fcBlankAll.equals(fcBlankAll)); | ||
} | ||
|
||
@Test | ||
void testToString() { | ||
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. Even though "Effective Java" recomments that |
||
BibEntry entry = new BibEntry(); | ||
Field field = StandardField.DOI; | ||
entry.setCitationKey("CitationKey"); | ||
final String oldValue = "Old"; | ||
final String newValue = "New"; | ||
|
||
FieldChange fc = new FieldChange(entry, field, oldValue, newValue); | ||
assertEquals("FieldChange [entry=CitationKey, field=DOI, oldValue=Old, newValue=New]", fc.toString()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class EntryLinkListTest { | ||
|
@@ -21,6 +22,7 @@ public class EntryLinkListTest { | |
private ParsedEntryLink link; | ||
private BibEntry source; | ||
private BibEntry target; | ||
private BibEntry entry; | ||
|
||
@BeforeEach | ||
public void before() { | ||
|
@@ -29,6 +31,7 @@ public void before() { | |
link = links.get(0); | ||
source = create("source"); | ||
target = create("target"); | ||
entry = create("entry"); | ||
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. Please do not initalize the variable here. It is used only in one test. Just move the variable to |
||
} | ||
|
||
private BibEntry create(String citeKey) { | ||
|
@@ -59,6 +62,12 @@ public void givenFieldValueAndDatabaseWhenParsingThenExpectLink() { | |
assertEquals(expected, link); | ||
} | ||
|
||
@Test | ||
public void givenBibEntryWhenParsingThenExpectLink() { | ||
ParsedEntryLink expected = new ParsedEntryLink(entry); | ||
assertFalse(expected.getLinkedEntry().isEmpty()); | ||
} | ||
|
||
@Test | ||
public void givenNullFieldValueAndDatabaseWhenParsingThenExpectLinksIsEmpty() { | ||
links = EntryLinkList.parse(null, database); | ||
|
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.
Why is the
database
constructed here? Where is it used? Maybe, the whole part can be removed?