Skip to content

Add simple unit tests#7543

Merged
Siedlerchr merged 9 commits into
JabRef:mainfrom
ningxie1991:a1-ds
May 11, 2021
Merged

Add simple unit tests#7543
Siedlerchr merged 9 commits into
JabRef:mainfrom
ningxie1991:a1-ds

Conversation

@nasdas-dev

@nasdas-dev nasdas-dev commented Mar 15, 2021

Copy link
Copy Markdown
Contributor

I have added some simple unit tests that will increase code coverage / branch coverage.
They contribute to issue #6207

  • Change in CHANGELOG.md described in a way that is understandable for the average user (if applicable)
  • Tests created for changes (if applicable)
  • Manually tested changed features in running JabRef (always required)
  • Screenshots added in PR description (for UI changes)
  • Checked documentation: Is the information available and up to date? If not created an issue at https://github.com/JabRef/user-documentation/issues or, even better, submitted a pull request to the documentation repository.

FieldChange.java
18% -> 94%
Abbreviation.java
63% -> 88%
SuggestionProviders.java
0% -> 100%
FileHelper.java
-> Boundary testing of an empty file

CitationKeyGenerator.java
-> Boundary testing of testlagepage parser for 0-00 & 1-1

HTMLCharacterChecker.java
-> Null Value Boundary test
ParsedEntryLink.java
-> Partition testing of ParsingEntryLink

UpperCaseFormatter.java
-> Partition testing for special characters

CitationStyleCacheTest.java
-> Partition testing of cache storage
@nasdas-dev

Copy link
Copy Markdown
Contributor Author

will adjust and fix checkstyle this evening/tomorrow.

Checkstyle passed
@Test
void testEquals() {
Abbreviation abbreviation = new Abbreviation("Long Name", "L N", "LN");
assertTrue(abbreviation.equals(abbreviation));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I detect that this code is problematic. According to the Correctness (CORRECTNESS), SA: Self comparison of value with itself (SA_LOCAL_SELF_COMPARISON).
This method compares a local variable with itself, and may indicate a typo or a logic error. Make sure that you are comparing the right things.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! This test originally was meant to cover the branch in the implementation of the equals method, where it returns true for being compared to itself. But seeing your linked references, I agree that it makes more sense to leave it out - I have adjusted the test now.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to help :)

@koppor koppor left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments in there. The main comment is that toString doesn't need to be tested if the software does not rely on that contract.

entry.setField(journalEntryField, "Journal");
entry.setField(publisherEntryField, "Publisher");
entry.setField(specialEntryField, "2000");
database.insertEntry(entry);

Copy link
Copy Markdown
Member

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?

entry.setField(specialEntryField, "2000");
database.insertEntry(entry);

assertSame("org.jabref.gui.autocompleter.EmptySuggestionProvider", empty.getForField(personEntryField).getClass().getName());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls use assertEquals, because of consistency reasons to other tests (assertSame is seldmoly used).

Suggested change
assertSame("org.jabref.gui.autocompleter.EmptySuggestionProvider", empty.getForField(personEntryField).getClass().getName());
assertEquals(org.jabref.gui.autocompleter.EmptySuggestionProvider.class, empty.getForField(personEntryField).getClass());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(applies for all lines here)

assertEquals("43", CitationKeyGenerator.lastPage("43+"));
assertEquals("0", CitationKeyGenerator.lastPage("00--0"));
assertEquals("1", CitationKeyGenerator.lastPage("1--1"));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please no empty line at the end of the method

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please convert the whole test to a @ParameterizedTest. See #7544 for an inspiration

Comment on lines +24 to +28
BibEntry bibEntry = new BibEntry();
bibEntry.setCitationKey("test");
List<BibEntry> entries = new ArrayList<>();
entries.add(0, bibEntry);
BibDatabase database = new BibDatabase(entries);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't one do the following?

Suggested change
BibEntry bibEntry = new BibEntry();
bibEntry.setCitationKey("test");
List<BibEntry> entries = new ArrayList<>();
entries.add(0, bibEntry);
BibDatabase database = new BibDatabase(entries);
BibDatabase database = new BibDatabase(List.of(new BibEntry("test"));

BibDatabaseContext databaseContext = new BibDatabaseContext(database);
CitationStyleCache csCache = new CitationStyleCache(databaseContext);

assertNotNull(csCache.getCitationFor(bibEntry));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test for a concrete result.

Comment on lines +17 to +21
@Test
void testHashCode() {
FieldChange fcNull = new FieldChange(null, null, null, null);
assertEquals(923521, fcNull.hashCode());
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please no test for hashCode.

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();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

}

@Test
void testToString() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though "Effective Java" recomments that toString is a contract; JabRef does not rely on that in this case. Please remove that test.

}

@Test
void testToString() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JabRef does not rely on the toString contract in this case. Please remove the test.

If you want, you can check for nonNull of toString.

link = links.get(0);
source = create("source");
target = create("target");
entry = create("entry");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 givenBibEntryWhenParsingThenExpectLink.

@koppor koppor added the status: changes-required Pull requests that are not yet complete label Apr 12, 2021
@koppor

koppor commented Apr 17, 2021

Copy link
Copy Markdown
Member

I think your course is over @ellieMayVelasquez, thus no more activity here?

@nasdas-dev

Copy link
Copy Markdown
Contributor Author

I think your course is over @ellieMayVelasquez, thus no more activity here?

Thanks a lot for all the feedback @koppor ! - I will resolve all of these issues within this month hopefully, just haven't gotten around to it yet due to lots of parallel coursework atm :)

@koppor

koppor commented May 6, 2021

Copy link
Copy Markdown
Member

@nasdas-dev May I ask which month you meant? 😇

nasdas-dev added 2 commits May 9, 2021 20:26
addressed all comments & suggestions to test files
@Siedlerchr Siedlerchr merged commit 807f85e into JabRef:main May 11, 2021
Siedlerchr added a commit that referenced this pull request May 15, 2021
* upstream/main: (71 commits)
  [Bot] Update CSL styles (#7735)
  Fix for issue 6966: open all files of multiple entries (#7709)
  Add simple unit tests (#7696)
  Add simple unit tests (#7543)
  Update check-outdated-dependencies.yml
  Added preset for new entry keybindings and reintroduced defaults (#7705)
  Select the entry which has smaller dictonary order when merge (#7708)
  Update CHANGELOG.md
  fix: make more fields, fomatters, ids and languages sorted by alphabetical order (#7717)
  Bump libreoffice from 7.1.2 to 7.1.3 (#7721)
  Bump unoloader from 7.1.2 to 7.1.3 (#7724)
  Bump org.beryx.jlink from 2.23.7 to 2.23.8 (#7723)
  Bump org.openjfx.javafxplugin from 0.0.9 to 0.0.10 (#7725)
  fix: make fields sorted by lexicographical order (#7711)
  Fix tests
  Refactoring existing unit tests (#7687)
  Refactoring and addition of unit tests (#7581)
  Refactor simple Unit Tests (#7571)
  Add simple unit tests (#7544)
  add and extend unit tests (#7685)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: changes-required Pull requests that are not yet complete

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants