-
-
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 more unit tests #7638
Merged
Merged
Add more unit tests #7638
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fdc508f
Add more unit tests
Davfon 6ff6781
Merge remote-tracking branch 'upstream/main' into a3-df
Davfon 1bf0a1d
Delete unnecessary test
Davfon f8b0ea0
Override equals method for easier testing
Davfon ef59b66
Fix checkstyle
Davfon 46f6fb4
Suggested changes & no Id comparison in BibtexString
Davfon dcc4926
Compare optionals and compare lists
Davfon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
34 changes: 34 additions & 0 deletions
34
src/test/java/org/jabref/logic/bibtex/comparator/BibStringDiffTest.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,34 @@ | ||
package org.jabref.logic.bibtex.comparator; | ||
|
||
import java.util.List; | ||
|
||
import org.jabref.model.database.BibDatabase; | ||
import org.jabref.model.entry.BibtexString; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class BibStringDiffTest { | ||
|
||
private final BibDatabase originalDataBase = mock(BibDatabase.class); | ||
private final BibDatabase newDataBase = mock(BibDatabase.class); | ||
private final BibStringDiff diff = new BibStringDiff(new BibtexString("name2", "content2"), new BibtexString("name2", "content3")); | ||
|
||
@BeforeEach | ||
void setUp() { | ||
when(originalDataBase.hasNoStrings()).thenReturn(false); | ||
when(newDataBase.hasNoStrings()).thenReturn(false); | ||
when(originalDataBase.getStringValues()).thenReturn(List.of(new BibtexString("name", "content"), new BibtexString("name2", "content2"))); | ||
when(newDataBase.getStringValues()).thenReturn(List.of(new BibtexString("name", "content"), new BibtexString("name2", "content3"))); | ||
} | ||
|
||
@Test | ||
void compareTest() { | ||
BibStringDiff result = BibStringDiff.compare(originalDataBase, newDataBase).get(0); | ||
assertEquals(diff, result); | ||
koppor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/test/java/org/jabref/logic/bibtex/comparator/PreambleDiffTest.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,45 @@ | ||
package org.jabref.logic.bibtex.comparator; | ||
|
||
import java.util.Optional; | ||
|
||
import org.jabref.model.database.BibDatabase; | ||
import org.jabref.model.database.BibDatabaseContext; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class PreambleDiffTest { | ||
|
||
private final BibDatabaseContext originalDataBaseContext = mock(BibDatabaseContext.class); | ||
private final BibDatabaseContext newDataBaseContext = mock(BibDatabaseContext.class); | ||
private final BibDatabase originalDataBase = mock(BibDatabase.class); | ||
private final BibDatabase newDataBase = mock(BibDatabase.class); | ||
|
||
@BeforeEach | ||
void setUp() { | ||
when(originalDataBaseContext.getDatabase()).thenReturn(originalDataBase); | ||
when(newDataBaseContext.getDatabase()).thenReturn(newDataBase); | ||
} | ||
|
||
@Test | ||
void compareSamePreambleTest() { | ||
when(originalDataBase.getPreamble()).thenReturn(Optional.of("preamble")); | ||
when(newDataBase.getPreamble()).thenReturn(Optional.of("preamble")); | ||
|
||
assertEquals(Optional.empty(), PreambleDiff.compare(originalDataBaseContext, newDataBaseContext)); | ||
} | ||
|
||
@Test | ||
void compareDifferentPreambleTest() { | ||
when(originalDataBase.getPreamble()).thenReturn(Optional.of("preamble")); | ||
when(newDataBase.getPreamble()).thenReturn(Optional.of("otherPreamble")); | ||
|
||
PreambleDiff expected = new PreambleDiff("preamble", "otherPreamble"); | ||
PreambleDiff result = PreambleDiff.compare(originalDataBaseContext, newDataBaseContext).orElse(null); | ||
assertEquals(expected, result); | ||
koppor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do not use toString() beause the BIbTexString class has it's own equals methods which will then be called autoamtically internally.
And tip for the next time: Your IDE has an option to generate equals/hasCode methos
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.
That's a good point, but since the BibtexString equals() method is strict and also compares the IDs of two BibtexStrings, it makes things a bit more complicated.
Is it intended, that two BibtexStrings that only differ in their ID are not considered equal? Because right now these would not be equal:
Since they get different IDs when they are created.
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.
@koppor you have more knowledge of this BibtexStrings, can I have multiple ones with the same name?
@Davfon you might check the BibtexString dialog for some clues if there are additional restrictions
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.
Strings are IMHO key/value pairs: https://docs.jabref.org/fields/strings.
I think, the Id fiels should not be treated when "equals". However, please, please have a look at the way how a BibEntry is compared and how Ids are treated. There should be a comment on that in the JavaDoc.
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.
I think so too (that they are key/value pairs)
A BibEntry is compared by type, fields & commentsBeforeEntry. The Ids are not used for comparing. There was no JavaDoc for the equals method. I would suggest removing the Id comparison in the BibtexString.equals() method. (see last commit)