Skip to content

Commit

Permalink
Refactoring existing unit tests into parametrized tests (#7700)
Browse files Browse the repository at this point in the history
Co-authored-by: Oliver Kopp <kopp.dev@gmail.com>
Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 7, 2021
1 parent ffd623d commit 2497ba4
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 148 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
package org.jabref.logic.layout.format;

import java.util.stream.Stream;

import org.jabref.logic.layout.LayoutFormatter;

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 AuthorFirstLastCommasTest {

LayoutFormatter authorFLCFormatter = new AuthorFirstLastCommas();

/**
* Test method for {@link org.jabref.logic.layout.format.AuthorFirstLastCommas#format(java.lang.String)}.
*/
@Test
public void testFormat() {
LayoutFormatter a = new AuthorFirstLastCommas();

// Empty case
assertEquals("", a.format(""));

// Single Names
assertEquals("Van Something Someone", a.format("Someone, Van Something"));

// Two names
assertEquals("John von Neumann and Peter Black Brown", a
.format("John von Neumann and Peter Black Brown"));

// Three names
assertEquals("John von Neumann, John Smith and Peter Black Brown", a
.format("von Neumann, John and Smith, John and Black Brown, Peter"));
@ParameterizedTest
@MethodSource("formatTests")
void paramLayoutFormatTest(String expectedString, String inputString) {
assertEquals(expectedString, authorFLCFormatter.format(inputString));
}

assertEquals("John von Neumann, John Smith and Peter Black Brown", a
.format("John von Neumann and John Smith and Black Brown, Peter"));
private static Stream<Arguments> formatTests() {
return Stream.of(
Arguments.of("", ""),
Arguments.of("Van Something Someone", "Someone, Van Something"),
Arguments.of("John von Neumann and Peter Black Brown", "John von Neumann and Peter Black Brown"),
Arguments.of("John von Neumann, John Smith and Peter Black Brown", "von Neumann, John and Smith, John and Black Brown, Peter"),
Arguments.of("John von Neumann, John Smith and Peter Black Brown", "John von Neumann and John Smith and Black Brown, Peter"),
Arguments.of("John von Neumann and Peter Black Brown", "John von Neumann and Peter Black Brown"),
Arguments.of("John von Neumann and Peter Black Brown", "John von Neumann and Peter Black Brown")
);
}
}
36 changes: 22 additions & 14 deletions src/test/java/org/jabref/logic/layout/format/AuthorNatBibTest.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package org.jabref.logic.layout.format;

import org.junit.jupiter.api.Test;
import java.util.stream.Stream;

import org.jabref.logic.layout.LayoutFormatter;

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 AuthorNatBibTest {

/**
* Test method for {@link org.jabref.logic.layout.format.AuthorNatBib#format(java.lang.String)}.
*/
@Test
public void testFormatThreeAuthors() {
assertEquals("von Neumann et al.",
new AuthorNatBib().format("von Neumann,,John and John Smith and Black Brown, Jr, Peter"));
LayoutFormatter authorNatBibFormatter = new AuthorNatBib();

@ParameterizedTest
@MethodSource("formatTests")
void paramLayoutFormatTest(String expectedString, String inputString) {
assertEquals(expectedString, authorNatBibFormatter.format(inputString));
}

/**
* Test method for {@link org.jabref.logic.layout.format.AuthorLF_FF#format(java.lang.String)}.
*/
@Test
public void testFormatTwoAuthors() {
assertEquals("von Neumann and Smith", new AuthorNatBib().format("von Neumann,,John and John Smith"));
private static Stream<Arguments> formatTests() {
return Stream.of(
// Test method for {@link org.jabref.logic.layout.format.AuthorNatBib#format(java.lang.String)}.
Arguments.of("von Neumann et al.", "von Neumann,,John and John Smith and Black Brown, Jr, Peter"),

// Test method for {@link org.jabref.logic.layout.format.AuthorLF_FF#format(java.lang.String)}.
Arguments.of("von Neumann and Smith", "von Neumann,,John and John Smith"),
Arguments.of("von Neumann", "von Neumann, John")
);
}
}
44 changes: 31 additions & 13 deletions src/test/java/org/jabref/logic/layout/format/AuthorOrgSciTest.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
package org.jabref.logic.layout.format;

import java.util.stream.Stream;

import org.jabref.logic.layout.LayoutFormatter;

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 AuthorOrgSciTest {

@Test
public void testOrgSci() {
LayoutFormatter f = new AuthorOrgSci();
LayoutFormatter authorOrgNatFormatter = new AuthorOrgSci();
LayoutFormatter authorOrgNatFormatterComposite = new CompositeFormat(new AuthorOrgSci(), new NoSpaceBetweenAbbreviations());

@ParameterizedTest
@MethodSource("formatTests")
void paramLayoutFormatTest(String expectedString, String inputString) {
assertEquals(expectedString, authorOrgNatFormatter.format(inputString));
}

@ParameterizedTest
@MethodSource("formatTestsComposite")
void paramLayoutFormatTestComposite(String expectedString, String inputString) {
assertEquals(expectedString, authorOrgNatFormatterComposite.format(inputString));
}

assertEquals("Flynn, J., S. Gartska", f.format("John Flynn and Sabine Gartska"));
assertEquals("Garvin, D. A.", f.format("David A. Garvin"));
assertEquals("Makridakis, S., S. C. Wheelwright, V. E. McGee", f.format("Sa Makridakis and Sa Ca Wheelwright and Va Ea McGee"));
private static Stream<Arguments> formatTests() {
return Stream.of(
Arguments.of("Flynn, J., S. Gartska", "John Flynn and Sabine Gartska"),
Arguments.of("Garvin, D. A.", "David A. Garvin"),
Arguments.of("Makridakis, S., S. C. Wheelwright, V. E. McGee", "Sa Makridakis and Sa Ca Wheelwright and Va Ea McGee")
);
}

@Test
public void testOrgSciPlusAbbreviation() {
LayoutFormatter f = new CompositeFormat(new AuthorOrgSci(), new NoSpaceBetweenAbbreviations());
assertEquals("Flynn, J., S. Gartska", f.format("John Flynn and Sabine Gartska"));
assertEquals("Garvin, D.A.", f.format("David A. Garvin"));
assertEquals("Makridakis, S., S.C. Wheelwright, V.E. McGee", f.format("Sa Makridakis and Sa Ca Wheelwright and Va Ea McGee"));
private static Stream<Arguments> formatTestsComposite() {
return Stream.of(
Arguments.of("Flynn, J., S. Gartska", "John Flynn and Sabine Gartska"),
Arguments.of("Garvin, D.A.", "David A. Garvin"),
Arguments.of("Makridakis, S., S.C. Wheelwright, V.E. McGee", "Sa Makridakis and Sa Ca Wheelwright and Va Ea McGee")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public void testArrayComposite() {

@Test
public void testDoubleComposite() {

LayoutFormatter f = new CompositeFormat(new AuthorOrgSci(), new NoSpaceBetweenAbbreviations());
LayoutFormatter first = new AuthorOrgSci();
LayoutFormatter second = new NoSpaceBetweenAbbreviations();
Expand Down
40 changes: 25 additions & 15 deletions src/test/java/org/jabref/logic/layout/format/DOIStripTest.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
package org.jabref.logic.layout.format;

import java.util.stream.Stream;

import org.jabref.logic.layout.LayoutFormatter;

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 DOIStripTest {

@Test
public void testFormat() {
LayoutFormatter lf = new DOIStrip();

assertEquals("", lf.format(""));
assertEquals(null, lf.format(null));

assertEquals("10.1000/ISBN1-900512-44-0", lf.format("10.1000/ISBN1-900512-44-0"));
assertEquals("10.1000/ISBN1-900512-44-0",
lf.format("http://dx.doi.org/10.1000/ISBN1-900512-44-0"));

assertEquals("10.1000/ISBN1-900512-44-0",
lf.format("http://doi.acm.org/10.1000/ISBN1-900512-44-0"));
}
LayoutFormatter layoutFormatter = new DOIStrip();

@ParameterizedTest
@MethodSource("provideDOI")
public void testFormatDOIStrip(String formattedDOI, String originalDOI) {
assertEquals(formattedDOI, layoutFormatter.format(originalDOI));
}

private static Stream<Arguments> provideDOI() {
return Stream.of(
Arguments.of("", ""),
Arguments.of(null, null),
Arguments.of("10.1000/ISBN1-900512-44-0", "10.1000/ISBN1-900512-44-0"),
Arguments.of("10.1000/ISBN1-900512-44-0", "http://dx.doi.org/10.1000/ISBN1-900512-44-0"),
Arguments.of("10.1000/ISBN1-900512-44-0", "http://doi.acm.org/10.1000/ISBN1-900512-44-0"),
Arguments.of("10.1145/354401.354407", "http://doi.acm.org/10.1145/354401.354407"),
Arguments.of("10", "10"),
Arguments.of("1", "1")
);
}
}
57 changes: 22 additions & 35 deletions src/test/java/org/jabref/logic/layout/format/DefaultTest.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,36 @@
package org.jabref.logic.layout.format;

import java.util.stream.Stream;

import org.jabref.logic.layout.ParamLayoutFormatter;

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 DefaultTest {

@Test
public void testSimpleText() {
ParamLayoutFormatter a = new Default();
a.setArgument("DEFAULT TEXT");
assertEquals("Bob Bruce", a.format("Bob Bruce"));
}

@Test
public void testFormatNullExpectReplace() {
ParamLayoutFormatter a = new Default();
a.setArgument("DEFAULT TEXT");
assertEquals("DEFAULT TEXT", a.format(null));
}

@Test
public void testFormatEmpty() {
ParamLayoutFormatter a = new Default();
a.setArgument("DEFAULT TEXT");
assertEquals("DEFAULT TEXT", a.format(""));
}

@Test
public void testNoArgumentSet() {
ParamLayoutFormatter a = new Default();
assertEquals("Bob Bruce and Jolly Jumper", a.format("Bob Bruce and Jolly Jumper"));
}
ParamLayoutFormatter paramLayoutFormatter = new Default();

@Test
public void testNoArgumentSetNullInput() {
ParamLayoutFormatter a = new Default();
assertEquals("", a.format(null));
@ParameterizedTest
@MethodSource("formatTests")
void paramLayoutFormatTest(String expectedString, String inputString, String formatterArgument) {
if (!formatterArgument.isEmpty()) {
paramLayoutFormatter.setArgument(formatterArgument);
}
assertEquals(expectedString, paramLayoutFormatter.format(inputString));
}

@Test
public void testNoArgumentSetEmptyInput() {
ParamLayoutFormatter a = new Default();
assertEquals("", a.format(""));
private static Stream<Arguments> formatTests() {
return Stream.of(
Arguments.of("Bob Bruce", "Bob Bruce", "DEFAULT TEXT"),
Arguments.of("DEFAULT TEXT", null, "DEFAULT TEXT"),
Arguments.of("DEFAULT TEXT", "", "DEFAULT TEXT"),
Arguments.of("Bob Bruce and Jolly Jumper", "Bob Bruce and Jolly Jumper", ""),
Arguments.of("", null, ""),
Arguments.of("", "", "")
);
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
package org.jabref.logic.layout.format;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.stream.Stream;

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 EntryTypeFormatterTest {

private EntryTypeFormatter formatter;

@BeforeEach
public void setUp() {
formatter = new EntryTypeFormatter();
}

@Test
public void testCorrectFormatArticle() {
assertEquals("Article", formatter.format("article"));
}
private EntryTypeFormatter formatter = new EntryTypeFormatter();

@Test
public void testCorrectFormatInBook() {
assertEquals("InBook", formatter.format("inbook"));
@ParameterizedTest
@MethodSource("formatTests")
void testCorrectFormat(String expectedString, String inputString) {
assertEquals(expectedString, formatter.format(inputString));
}

@Test
public void testIncorrectTypeAarticle() {
assertEquals("Aarticle", formatter.format("aarticle"));
private static Stream<Arguments> formatTests() {
return Stream.of(
Arguments.of("Article", "article"),
Arguments.of("Banana", "banana"),
Arguments.of("InBook", "inbook"),
Arguments.of("Aarticle", "aarticle")
);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package org.jabref.logic.layout.format;

import java.util.stream.Stream;

import org.jabref.logic.layout.LayoutFormatter;

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 HTMLParagraphsTest {

@Test
public void testFormat() {
LayoutFormatter htmlFormatter = new HTMLParagraphs();

LayoutFormatter f = new HTMLParagraphs();
@ParameterizedTest
@MethodSource("htmlFormatTests")
void testCorrectFormat(String expectedString, String inputString) {
assertEquals(expectedString, htmlFormatter.format(inputString));
}

assertEquals("", f.format(""));
assertEquals("<p>\nHello\n</p>", f.format("Hello"));
assertEquals("<p>\nHello\nWorld\n</p>", f.format("Hello\nWorld"));
assertEquals("<p>\nHello World\n</p>\n<p>\nWhat a lovely day\n</p>", f.format("Hello World\n \nWhat a lovely day\n"));
assertEquals("<p>\nHello World\n</p>\n<p>\nCould not be any better\n</p>\n<p>\nWhat a lovely day\n</p>", f.format("Hello World\n \n\nCould not be any better\n\nWhat a lovely day\n"));
private static Stream<Arguments> htmlFormatTests() {
return Stream.of(
Arguments.of("", ""),
Arguments.of("<p>\nHello\n</p>", "Hello"),
Arguments.of("<p>\nHello\nWorld\n</p>", "Hello\nWorld"),
Arguments.of("<p>\nHello World\n</p>\n<p>\nWhat a lovely day\n</p>", "Hello World\n \nWhat a lovely day\n"),
Arguments.of("<p>\nHello World\n</p>\n<p>\nCould not be any better\n</p>\n<p>\nWhat a lovely day\n</p>", "Hello World\n \n\nCould not be any better\n\nWhat a lovely day\n")
);
}
}
Loading

0 comments on commit 2497ba4

Please sign in to comment.