Skip to content

Commit

Permalink
converted to parameterized tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BShaq committed Apr 13, 2021
1 parent 4aca5fb commit 9492146
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 170 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
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;

Expand Down Expand Up @@ -174,37 +179,6 @@ public void compareStringFieldsBiggerDescending() throws Exception {
assertEquals(1, comparator.compare(bigger, smaller));
}

@Test
public void compareNumericFieldsIdentity() throws Exception {
FieldComparator comparator = new FieldComparator(StandardField.PMID);
BibEntry equal = new BibEntry()
.withField(StandardField.PMID, "123456");

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

@Test
public void compareNumericFieldsEquality() throws Exception {
FieldComparator comparator = new FieldComparator(StandardField.PMID);
BibEntry equal = new BibEntry()
.withField(StandardField.PMID, "123456");
BibEntry equal2 = new BibEntry()
.withField(StandardField.PMID, "123456");

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

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

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

@Test
public void compareNumericFieldsBiggerDescending() throws Exception {
FieldComparator comparator = new FieldComparator(new OrFields(StandardField.PMID), true);
Expand All @@ -227,42 +201,26 @@ public void compareParsableWithNonParsableNumericFieldDescending() throws Except
assertEquals(1, comparator.compare(parsable, unparsable));
}

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

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

@Test
public void compareEmptyFieldsAscending() throws Exception {
@ParameterizedTest
@MethodSource("provideArgumentsForNumericalComparison")
public void compareNumericalValues(int comparisonResult, String id1, String id2, String errorMessage) {
FieldComparator comparator = new FieldComparator(StandardField.PMID);
BibEntry empty1 = new BibEntry();
BibEntry empty2 = new BibEntry();

assertEquals(0, comparator.compare(empty1, empty2));
}

@Test
public void compareEmptyWithAssignedFieldAscending() throws Exception {
FieldComparator comparator = new FieldComparator(StandardField.PMID);
BibEntry empty1 = new BibEntry();
BibEntry assigned = new BibEntry().withField(StandardField.PMID, "123456");

assertEquals(1, comparator.compare(empty1, assigned));
}

@Test
public void compareAssignedWithEmptyFieldAscending() throws Exception {
FieldComparator comparator = new FieldComparator(StandardField.PMID);
BibEntry assigned = new BibEntry()
.withField(StandardField.PMID, "123456");
BibEntry empty = new BibEntry();

assertEquals(-1, comparator.compare(assigned, empty));
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")
);
}
}
25 changes: 14 additions & 11 deletions src/test/java/org/jabref/logic/integrity/ISBNCheckerTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +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 @@ -33,18 +37,17 @@ void isbnDoesNotAcceptInvalidInput() {
assertNotEquals(Optional.empty(), checker.checkValue("0-201-53082-2"));
}

@Test
void isbnAcceptsCorrectControlDigitForIsbn13() {
assertEquals(Optional.empty(), checker.checkValue("978-0-306-40615-7"));
@ParameterizedTest
@MethodSource("provideBoundaryArgumentsForISBN13")
public void checkISBNValue(Optional optValue, String id) {
assertEquals(optValue, checker.checkValue(id));
}

@Test
void isbnDoesNotAcceptIncorrectControlDigitForIsbn13() {
assertEquals(Optional.of(Localization.lang("incorrect control digit")), checker.checkValue("978-0-306-40615-2"));
}

@Test
void isbnDoesNotAcceptInvalidFormatForIsbn13() {
assertEquals(Optional.of(Localization.lang("incorrect format")), checker.checkValue("978_0_306_40615_7"));
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")
);
}
}
29 changes: 16 additions & 13 deletions src/test/java/org/jabref/logic/integrity/ISSNCheckerTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +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 @@ -34,23 +38,22 @@ void issnDoesNotAcceptInvalidInput() {
}

@Test
void lessThanEightDigitCode() {
assertEquals(Optional.of(Localization.lang("incorrect format")), checker.checkValue("020-721"));
void emptyIssnValue() {
assertEquals(Optional.empty(), checker.checkValue(""));
}

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

@Test
void issnDividedByWrongCharacter() {
assertEquals(Optional.of(Localization.lang("incorrect format")), checker.checkValue("0020~72109"));
}

@Test
void emptyIssnValue() {
assertEquals(Optional.empty(), checker.checkValue(""));
private static Stream<Arguments> provideIncorrectFormatArguments() {
return Stream.of(
Arguments.of("020-721"),
Arguments.of("0020-72109"),
Arguments.of("0020~72109")
);
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package org.jabref.logic.layout.format;

import java.util.stream.Stream;

import org.jabref.logic.layout.LayoutFormatter;

import org.junit.jupiter.api.BeforeEach;
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;

Expand All @@ -16,38 +21,25 @@ public void setUp() {
formatter = new ShortMonthFormatter();
}

@Test
public void formatShortName() {
assertEquals("jan", formatter.format("jan"));
}

@Test
public void formatFullName() {
assertEquals("jan", formatter.format("January"));
}

@Test
public void formatGermanFullName() {
assertEquals("jan", formatter.format("Januar"));
}

@Test
public void formatMonthNumber() {
assertEquals("jan", formatter.format("01"));
}

@Test
public void formatRandomInput() {
assertEquals("", formatter.format("Invented Month"));
}

@Test
public void formatNullInput() {
assertEquals("", formatter.format(null));
}

@Test
public void formatEmptyInput() {
assertEquals("", formatter.format(""));
@ParameterizedTest
@MethodSource("provideArguments")
public void formatDifferentInputs(String formattedString, String originalString) {
assertEquals(formattedString, formatter.format(originalString));
}

private static Stream<Arguments> provideArguments() {
return Stream.of(
Arguments.of("jan", "jan"),
Arguments.of("jan", "January"),
Arguments.of("jan", "Januar"),
Arguments.of("jan", "01"),
Arguments.of("", "Invented Month"),
Arguments.of("", "")
);
}
}
50 changes: 19 additions & 31 deletions src/test/java/org/jabref/logic/layout/format/ToLowerCaseTest.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,37 @@
package org.jabref.logic.layout.format;

import java.util.stream.Stream;

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.assertNull;

public class ToLowerCaseTest {

@Test
public void testEmpty() {
assertEquals("", new ToLowerCase().format(""));
}

@Test
public void testNull() {
assertNull(new ToLowerCase().format(null));
}

@Test
public void testLowerCase() {
assertEquals("abcd efg ", new ToLowerCase().format("abcd efg "));
}

@Test
public void testUpperCase() {
assertEquals("abcd efg", new ToLowerCase().format("ABCD EFG"));
}

@Test
public void testMixedCase() {
assertEquals("abcd efg", new ToLowerCase().format("abCD eFg"));
}

@Test
public void includeNumbersInString() {
assertEquals("abcd123efg", new ToLowerCase().format("abCD123eFg"));
@ParameterizedTest
@MethodSource("provideArguments")
public void toLowerCaseWithDifferentInputs(String expectedString, String originalString) {
assertEquals(expectedString, new ToLowerCase().format(originalString));
}

@Test
public void includeSpecialCharactersInString() {
assertEquals("hello!*#", new ToLowerCase().format("Hello!*#"));
}

@Test
public void includeOnlyNumbersAndSpecialCharacters() {
assertEquals("123*%&456", new ToLowerCase().format("123*%&456"));
private static Stream<Arguments> provideArguments() {
return Stream.of(
Arguments.of("", ""),
Arguments.of("abcd efg", "abcd efg"),
Arguments.of("abcd efg", "ABCD EFG"),
Arguments.of("abcd efg", "abCD eFg"),
Arguments.of("abcd123efg", "abCD123eFg"),
Arguments.of("hello!*#", "Hello!*#"),
Arguments.of("123*%&456", "123*%&456")
);
}
}
Loading

0 comments on commit 9492146

Please sign in to comment.