Skip to content
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 #7544

Merged
merged 20 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,96 +1,101 @@
package org.jabref.logic.bibtex.comparator;

import java.util.stream.Stream;

import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.InternalField;
import org.jabref.model.entry.field.OrFields;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.StandardEntryType;

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 FieldComparatorTest {
@Test
public void compareMonthFieldIdentity() throws Exception {
FieldComparator comparator = new FieldComparator(StandardField.MONTH);
BibEntry equal = new BibEntry();
equal.setField(StandardField.MONTH, "1");
BibEntry equal = new BibEntry()
.withField(StandardField.MONTH, "1");

assertEquals(0, comparator.compare(equal, equal));
}

@Test
public void compareMonthFieldEquality() throws Exception {
FieldComparator comparator = new FieldComparator(StandardField.MONTH);
BibEntry equal = new BibEntry();
equal.setField(StandardField.MONTH, "1");
BibEntry equal2 = new BibEntry();
equal2.setField(StandardField.MONTH, "1");
BibEntry equal = new BibEntry()
.withField(StandardField.MONTH, "1");
BibEntry equal2 = new BibEntry()
.withField(StandardField.MONTH, "1");

assertEquals(0, comparator.compare(equal, equal2));
}

@Test
public void compareMonthFieldBiggerAscending() throws Exception {
FieldComparator comparator = new FieldComparator(StandardField.MONTH);
BibEntry smaller = new BibEntry();
smaller.setField(StandardField.MONTH, "jan");
BibEntry bigger = new BibEntry();
bigger.setField(StandardField.MONTH, "feb");
BibEntry smaller = new BibEntry()
.withField(StandardField.MONTH, "jan");
BibEntry bigger = new BibEntry()
.withField(StandardField.MONTH, "feb");

assertEquals(1, comparator.compare(bigger, smaller));
}

@Test
public void compareMonthFieldBiggerDescending() throws Exception {
FieldComparator comparator = new FieldComparator(new OrFields(StandardField.MONTH), true);
BibEntry smaller = new BibEntry();
smaller.setField(StandardField.MONTH, "feb");
BibEntry bigger = new BibEntry();
bigger.setField(StandardField.MONTH, "jan");
BibEntry smaller = new BibEntry()
.withField(StandardField.MONTH, "feb");
BibEntry bigger = new BibEntry()
.withField(StandardField.MONTH, "jan");

assertEquals(1, comparator.compare(bigger, smaller));
}

@Test
public void compareYearFieldIdentity() throws Exception {
FieldComparator comparator = new FieldComparator(StandardField.YEAR);
BibEntry equal = new BibEntry();
equal.setField(StandardField.YEAR, "2016");
BibEntry equal = new BibEntry()
.withField(StandardField.YEAR, "2016");

assertEquals(0, comparator.compare(equal, equal));
}

@Test
public void compareYearFieldEquality() throws Exception {
FieldComparator comparator = new FieldComparator(StandardField.YEAR);
BibEntry equal = new BibEntry();
equal.setField(StandardField.YEAR, "2016");
BibEntry equal2 = new BibEntry();
equal2.setField(StandardField.YEAR, "2016");
BibEntry equal = new BibEntry()
.withField(StandardField.YEAR, "2016");
BibEntry equal2 = new BibEntry()
.withField(StandardField.YEAR, "2016");

assertEquals(0, comparator.compare(equal, equal2));
}

@Test
public void compareYearFieldBiggerAscending() throws Exception {
FieldComparator comparator = new FieldComparator(StandardField.YEAR);
BibEntry smaller = new BibEntry();
smaller.setField(StandardField.YEAR, "2000");
BibEntry bigger = new BibEntry();
bigger.setField(StandardField.YEAR, "2016");
BibEntry smaller = new BibEntry()
.withField(StandardField.YEAR, "2000");
BibEntry bigger = new BibEntry()
.withField(StandardField.YEAR, "2016");

assertEquals(1, comparator.compare(bigger, smaller));
}

@Test
public void compareYearFieldBiggerDescending() throws Exception {
FieldComparator comparator = new FieldComparator(new OrFields(StandardField.YEAR), true);
BibEntry smaller = new BibEntry();
smaller.setField(StandardField.YEAR, "2016");
BibEntry bigger = new BibEntry();
bigger.setField(StandardField.YEAR, "2000");
BibEntry smaller = new BibEntry()
.withField(StandardField.YEAR, "2016");
BibEntry bigger = new BibEntry()
.withField(StandardField.YEAR, "2000");

assertEquals(1, comparator.compare(bigger, smaller));
}
Expand Down Expand Up @@ -135,42 +140,87 @@ public void compareTypeFieldBiggerDescending() throws Exception {
@Test
public void compareStringFieldsIdentity() throws Exception {
FieldComparator comparator = new FieldComparator(StandardField.TITLE);
BibEntry equal = new BibEntry();
equal.setField(StandardField.TITLE, "title");
BibEntry equal = new BibEntry()
.withField(StandardField.TITLE, "title");

assertEquals(0, comparator.compare(equal, equal));
}

@Test
public void compareStringFieldsEquality() throws Exception {
FieldComparator comparator = new FieldComparator(StandardField.TITLE);
BibEntry equal = new BibEntry();
equal.setField(StandardField.TITLE, "title");
BibEntry equal2 = new BibEntry();
equal2.setField(StandardField.TITLE, "title");
BibEntry equal = new BibEntry()
.withField(StandardField.TITLE, "title");
BibEntry equal2 = new BibEntry()
.withField(StandardField.TITLE, "title");

assertEquals(0, comparator.compare(equal, equal2));
}

@Test
public void compareStringFieldsBiggerAscending() throws Exception {
FieldComparator comparator = new FieldComparator(StandardField.TITLE);
BibEntry bigger = new BibEntry();
bigger.setField(StandardField.TITLE, "b");
BibEntry smaller = new BibEntry();
smaller.setField(StandardField.TITLE, "a");
BibEntry bigger = new BibEntry()
.withField(StandardField.TITLE, "b");
BibEntry smaller = new BibEntry()
.withField(StandardField.TITLE, "a");

assertEquals(1, comparator.compare(bigger, smaller));
}

@Test
public void compareStringFieldsBiggerDescending() throws Exception {
FieldComparator comparator = new FieldComparator(new OrFields(StandardField.TITLE), true);
BibEntry bigger = new BibEntry();
bigger.setField(StandardField.TITLE, "a");
BibEntry smaller = new BibEntry();
smaller.setField(StandardField.TITLE, "b");
BibEntry bigger = new BibEntry()
.withField(StandardField.TITLE, "a");
BibEntry smaller = new BibEntry()
.withField(StandardField.TITLE, "b");

assertEquals(1, comparator.compare(bigger, smaller));
}

@Test
public void compareNumericFieldsBiggerDescending() throws Exception {
FieldComparator comparator = new FieldComparator(new OrFields(StandardField.PMID), true);
BibEntry smaller = new BibEntry()
.withField(StandardField.PMID, "234567");
BibEntry bigger = new BibEntry()
.withField(StandardField.PMID, "123456");

assertEquals(1, comparator.compare(bigger, smaller));
}

@Test
public void compareParsableWithNonParsableNumericFieldDescending() throws Exception {
FieldComparator comparator = new FieldComparator(new OrFields(StandardField.PMID), true);
BibEntry parsable = new BibEntry()
.withField(StandardField.PMID, "123456");
BibEntry unparsable = new BibEntry()
.withField(StandardField.PMID, "abc##z");

assertEquals(1, comparator.compare(parsable, unparsable));
}

@ParameterizedTest
@MethodSource("provideArgumentsForNumericalComparison")
public void compareNumericalValues(int comparisonResult, String id1, String id2, String errorMessage) {
FieldComparator comparator = new FieldComparator(StandardField.PMID);
BibEntry entry1 = new BibEntry()
.withField(StandardField.PMID, id1);
BibEntry entry2 = new BibEntry()
.withField(StandardField.PMID, id2);

assertEquals(comparisonResult, comparator.compare(entry1, entry2), errorMessage);
}

private static Stream<Arguments> provideArgumentsForNumericalComparison() {
return Stream.of(
Arguments.of(0, "123456", "123456", "IDs are lexicographically not equal [1]"),
Arguments.of(1, "234567", "123456", "234567 is lexicographically smaller than 123456"),
Arguments.of(1, "abc##z", "123456", "abc##z is lexicographically smaller than 123456 "),
Arguments.of(0, "", "", "IDs are lexicographically not equal [2]"),
Arguments.of(1, "", "123456", "No ID is lexicographically smaller than 123456"),
Arguments.of(-1, "123456", "", "123456 is lexicographically greater than no ID")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,14 @@ void formatEscapesAmpersandsIfPresent() throws Exception {
void formatExample() {
assertEquals("Text \\& with \\&ampersands", formatter.format(formatter.getExampleInput()));
}

@Test
void formatReturnsSameTextInNewUserDefinedLatexCommandIfNoAmpersandsPresent() throws Exception {
assertEquals("\\newcommand[1]{Lorem ipsum}", formatter.format("\\newcommand[1]{Lorem ipsum}"));
}

@Test
void formatReturnsSameTextInLatexCommandIfOneAmpersandPresent() throws Exception {
assertEquals("\\textbf{Lorem\\&ipsum}", formatter.format("\\textbf{Lorem\\&ipsum}"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ void booktitleDoesNotAcceptsIfItEndsWithConferenceOn() {
assertNotEquals(Optional.empty(), checker.checkValue("Digital Information and Communication Technology and it's Applications (DICTAP), 2014 Fourth International Conference on"));
}

@Test
void booktitleIsBlank() {
assertEquals(Optional.empty(), checker.checkValue(" "));
}

}
23 changes: 23 additions & 0 deletions src/test/java/org/jabref/logic/integrity/EditionCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Optional;

import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.BibDatabaseMode;

Expand Down Expand Up @@ -80,6 +81,28 @@ void bibTexAcceptsOrdinalNumberInNumbers() {
assertEquals(Optional.empty(), checker.checkValue("2nd"));
}

@Test
void bibTexEmptyValueAsInput() {
assertEquals(Optional.empty(), checker.checkValue(""));
}

@Test
void bibTexNullValueAsInput() {
assertEquals(Optional.empty(), checker.checkValue(null));
}

@Test
void bibTexDoesNotAcceptIntegerOnly() {
var editionChecker = new EditionChecker(bibtex, false);
assertEquals(Optional.of(Localization.lang("no integer as values for edition allowed")), editionChecker.checkValue("3"));
}

@Test
void bibTexAcceptsFirstEditionAlsoIfIntegerEditionDisallowed() {
var editionChecker = new EditionChecker(bibtex, false);
assertEquals(Optional.of(Localization.lang("edition of book reported as just 1")), editionChecker.checkValue("1"));
}

@Test
void bibLaTexAcceptsEditionWithCapitalFirstLetter() {
assertEquals(Optional.empty(), checkerb.checkValue("Edition 2000"));
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/jabref/logic/integrity/ISBNCheckerTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package org.jabref.logic.integrity;

import java.util.Optional;
import java.util.stream.Stream;

import org.jabref.logic.l10n.Localization;

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;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand Down Expand Up @@ -31,4 +37,17 @@ void isbnDoesNotAcceptInvalidInput() {
assertNotEquals(Optional.empty(), checker.checkValue("0-201-53082-2"));
}

@ParameterizedTest
@MethodSource("provideBoundaryArgumentsForISBN13")
public void checkISBNValue(Optional optValue, String id) {
assertEquals(optValue, checker.checkValue(id));
}

private static Stream<Arguments> provideBoundaryArgumentsForISBN13() {
return Stream.of(
Arguments.of(Optional.empty(), "978-0-306-40615-7"),
Arguments.of(Optional.of(Localization.lang("incorrect control digit")), "978-0-306-40615-2"),
Arguments.of(Optional.of(Localization.lang("incorrect format")), "978_0_306_40615_7")
);
}
}
25 changes: 25 additions & 0 deletions src/test/java/org/jabref/logic/integrity/ISSNCheckerTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package org.jabref.logic.integrity;

import java.util.Optional;
import java.util.stream.Stream;

import org.jabref.logic.l10n.Localization;

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;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand Down Expand Up @@ -31,4 +37,23 @@ void issnDoesNotAcceptInvalidInput() {
assertNotEquals(Optional.empty(), checker.checkValue("0020-7218"));
}

@Test
void emptyIssnValue() {
assertEquals(Optional.empty(), checker.checkValue(""));
}

@ParameterizedTest
@MethodSource("provideIncorrectFormatArguments")
public void issnWithWrongFormat(String wrongISSN) {
assertEquals(Optional.of(Localization.lang("incorrect format")), checker.checkValue(wrongISSN));
}

private static Stream<Arguments> provideIncorrectFormatArguments() {
return Stream.of(
Arguments.of("020-721"),
Arguments.of("0020-72109"),
Arguments.of("0020~72109")
);
}

}
Loading