Skip to content

Commit 2538565

Browse files
Refactor tests to use parameterized tests (#14156)
* Parameterize tests for Issue676 * Address PR review feedback: refactor parameterized tests to CsvSource textBlock
1 parent a6d4772 commit 2538565

File tree

6 files changed

+102
-181
lines changed

6 files changed

+102
-181
lines changed

jablib/src/test/java/org/jabref/logic/layout/format/AuthorAndsReplacerTest.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import org.jabref.logic.layout.LayoutFormatter;
44

5-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
67

78
import static org.junit.jupiter.api.Assertions.assertEquals;
89

@@ -11,25 +12,19 @@ class AuthorAndsReplacerTest {
1112
/**
1213
* Test method for {@link org.jabref.logic.layout.format.AuthorAndsReplacer#format(java.lang.String)}.
1314
*/
14-
@Test
15-
void format() {
15+
@ParameterizedTest
16+
@CsvSource(
17+
delimiterString = "->",
18+
textBlock = """
19+
'' -> ''
20+
'Someone, Van Something' -> 'Someone, Van Something'
21+
'John Smith & Black Brown, Peter' -> 'John Smith and Black Brown, Peter'
22+
'von Neumann, John; Smith, John & Black Brown, Peter' -> 'von Neumann, John and Smith, John and Black Brown, Peter'
23+
'John von Neumann; John Smith & Peter Black Brown' -> 'John von Neumann and John Smith and Peter Black Brown'
24+
"""
25+
)
26+
void format(String expected, String input) {
1627
LayoutFormatter a = new AuthorAndsReplacer();
17-
18-
// Empty case
19-
assertEquals("", a.format(""));
20-
21-
// Single Names don't change
22-
assertEquals("Someone, Van Something", a.format("Someone, Van Something"));
23-
24-
// Two names just an &
25-
assertEquals("John Smith & Black Brown, Peter", a
26-
.format("John Smith and Black Brown, Peter"));
27-
28-
// Three names put a comma:
29-
assertEquals("von Neumann, John; Smith, John & Black Brown, Peter", a
30-
.format("von Neumann, John and Smith, John and Black Brown, Peter"));
31-
32-
assertEquals("John von Neumann; John Smith & Peter Black Brown", a
33-
.format("John von Neumann and John Smith and Peter Black Brown"));
28+
assertEquals(expected, a.format(input));
3429
}
3530
}

jablib/src/test/java/org/jabref/logic/layout/format/AuthorFirstLastOxfordCommasTest.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import org.jabref.logic.layout.LayoutFormatter;
44

5-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
67

78
import static org.junit.jupiter.api.Assertions.assertEquals;
89

@@ -11,25 +12,19 @@ class AuthorFirstLastOxfordCommasTest {
1112
/**
1213
* Test method for {@link org.jabref.logic.layout.format.AuthorFirstLastOxfordCommas#format(java.lang.String)}.
1314
*/
14-
@Test
15-
void format() {
16-
LayoutFormatter a = new AuthorFirstLastOxfordCommas();
17-
18-
// Empty case
19-
assertEquals("", a.format(""));
20-
21-
// Single Names
22-
assertEquals("Van Something Someone", a.format("Someone, Van Something"));
23-
24-
// Two names
25-
assertEquals("John von Neumann and Peter Black Brown", a
26-
.format("John von Neumann and Peter Black Brown"));
27-
28-
// Three names
29-
assertEquals("John von Neumann, John Smith, and Peter Black Brown", a
30-
.format("von Neumann, John and Smith, John and Black Brown, Peter"));
31-
32-
assertEquals("John von Neumann, John Smith, and Peter Black Brown", a
33-
.format("John von Neumann and John Smith and Black Brown, Peter"));
15+
@ParameterizedTest
16+
@CsvSource(
17+
delimiterString = "->",
18+
textBlock = """
19+
'' -> ''
20+
'Van Something Someone' -> 'Someone, Van Something'
21+
'John von Neumann and Peter Black Brown' -> 'John von Neumann and Peter Black Brown'
22+
'John von Neumann, John Smith, and Peter Black Brown' -> 'von Neumann, John and Smith, John and Black Brown, Peter'
23+
'John von Neumann, John Smith, and Peter Black Brown' -> 'John von Neumann and John Smith and Black Brown, Peter'
24+
"""
25+
)
26+
void format(String expected, String input) {
27+
LayoutFormatter formatter = new AuthorFirstLastOxfordCommas();
28+
assertEquals(expected, formatter.format(input));
3429
}
3530
}

jablib/src/test/java/org/jabref/logic/layout/format/AuthorLastFirstAbbrOxfordCommasTest.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import org.jabref.logic.layout.LayoutFormatter;
44

5-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
67

78
import static org.junit.jupiter.api.Assertions.assertEquals;
89

@@ -11,25 +12,19 @@ class AuthorLastFirstAbbrOxfordCommasTest {
1112
/**
1213
* Test method for {@link org.jabref.logic.layout.format.AuthorLastFirstAbbrOxfordCommas#format(java.lang.String)}.
1314
*/
14-
@Test
15-
void format() {
16-
LayoutFormatter a = new AuthorLastFirstAbbrOxfordCommas();
17-
18-
// Empty case
19-
assertEquals("", a.format(""));
20-
21-
// Single Names
22-
assertEquals("Someone, V. S.", a.format("Van Something Someone"));
23-
24-
// Two names
25-
assertEquals("von Neumann, J. and Black Brown, P.", a
26-
.format("John von Neumann and Black Brown, Peter"));
27-
28-
// Three names
29-
assertEquals("von Neumann, J., Smith, J., and Black Brown, P.", a
30-
.format("von Neumann, John and Smith, John and Black Brown, Peter"));
31-
32-
assertEquals("von Neumann, J., Smith, J., and Black Brown, P.", a
33-
.format("John von Neumann and John Smith and Black Brown, Peter"));
15+
@ParameterizedTest
16+
@CsvSource(
17+
delimiterString = "->",
18+
textBlock = """
19+
'' -> ''
20+
'Someone, V. S.' -> 'Van Something Someone'
21+
'von Neumann, J. and Black Brown, P.' -> 'John von Neumann and Black Brown, Peter'
22+
'von Neumann, J., Smith, J., and Black Brown, P.' -> 'von Neumann, John and Smith, John and Black Brown, Peter'
23+
'von Neumann, J., Smith, J., and Black Brown, P.' -> 'John von Neumann and John Smith and Black Brown, Peter'
24+
"""
25+
)
26+
void format(String expected, String input) {
27+
LayoutFormatter formatter = new AuthorLastFirstAbbrOxfordCommas();
28+
assertEquals(expected, formatter.format(input));
3429
}
3530
}
Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,32 @@
11
package org.jabref.logic.layout.format;
22

3-
import org.junit.jupiter.api.Test;
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.CsvSource;
45

56
import static org.junit.jupiter.api.Assertions.assertEquals;
67

78
/**
8-
* Test case that verifies the functionalities of the formater AuthorLastFirstAbbreviator.
9+
* Test case that verifies the functionalities of the formatter AuthorLastFirstAbbreviator.
910
*/
1011
class AuthorLastFirstAbbreviatorTest {
1112

12-
/**
13-
* Verifies the Abbreviation of one single author with a simple name.
14-
* <p/>
15-
* Ex: Lastname, Name
16-
*/
17-
@Test
18-
void oneAuthorSimpleName() {
19-
assertEquals("Lastname, N.", abbreviate("Lastname, Name"));
20-
}
21-
22-
/**
23-
* Verifies the Abbreviation of one single author with a common name.
24-
* <p/>
25-
* Ex: Lastname, Name Middlename
26-
*/
27-
@Test
28-
void oneAuthorCommonName() {
29-
assertEquals("Lastname, N. M.", abbreviate("Lastname, Name Middlename"));
30-
}
31-
32-
/**
33-
* Verifies the Abbreviation of two single with a common name.
34-
* <p/>
35-
* Ex: Lastname, Name Middlename
36-
*/
37-
@Test
38-
void twoAuthorsCommonName() {
39-
String result = abbreviate("Lastname, Name Middlename and Sobrenome, Nome Nomedomeio");
40-
String expectedResult = "Lastname, N. M. and Sobrenome, N. N.";
41-
42-
assertEquals(expectedResult, result);
43-
}
44-
45-
@Test
46-
void jrAuthor() {
47-
assertEquals("Other, Jr., A. N.", abbreviate("Other, Jr., Anthony N."));
48-
}
49-
50-
@Test
51-
void format() {
52-
assertEquals("", abbreviate(""));
53-
assertEquals("Someone, V. S.", abbreviate("Someone, Van Something"));
54-
assertEquals("Smith, J.", abbreviate("Smith, John"));
55-
assertEquals("von Neumann, J. and Smith, J. and Black Brown, P.",
56-
abbreviate("von Neumann, John and Smith, John and Black Brown, Peter"));
57-
}
58-
59-
private String abbreviate(String name) {
60-
return new AuthorLastFirstAbbreviator().format(name);
13+
private final AuthorLastFirstAbbreviator abbreviator = new AuthorLastFirstAbbreviator();
14+
15+
@ParameterizedTest
16+
@CsvSource(
17+
delimiterString = "->",
18+
textBlock = """
19+
'Lastname, N.' -> 'Lastname, Name'
20+
'Lastname, N. M.' -> 'Lastname, Name Middlename'
21+
'Lastname, N. M. and Sobrenome, N. N.' -> 'Lastname, Name Middlename and Sobrenome, Nome Nomedomeio'
22+
'Other, Jr., A. N.' -> 'Other, Jr., Anthony N.'
23+
'' -> ''
24+
'Someone, V. S.' -> 'Someone, Van Something'
25+
'Smith, J.' -> 'Smith, John'
26+
'von Neumann, J. and Smith, J. and Black Brown, P.' -> 'von Neumann, John and Smith, John and Black Brown, Peter'
27+
"""
28+
)
29+
void abbreviate(String expected, String input) {
30+
assertEquals(expected, abbreviator.format(input));
6131
}
6232
}

jablib/src/test/java/org/jabref/logic/layout/format/RemoveBracketsTest.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import org.jabref.logic.layout.LayoutFormatter;
44

55
import org.junit.jupiter.api.BeforeEach;
6-
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.CsvSource;
78

89
import static org.junit.jupiter.api.Assertions.assertEquals;
910

@@ -15,28 +16,18 @@ void setUp() {
1516
formatter = new RemoveBrackets();
1617
}
1718

18-
@Test
19-
void bracePairCorrectlyRemoved() {
20-
assertEquals("some text", formatter.format("{some text}"));
21-
}
22-
23-
@Test
24-
void singleOpeningBraceCorrectlyRemoved() {
25-
assertEquals("some text", formatter.format("{some text"));
26-
}
27-
28-
@Test
29-
void singleClosingBraceCorrectlyRemoved() {
30-
assertEquals("some text", formatter.format("some text}"));
31-
}
32-
33-
@Test
34-
void bracePairWithEscapedBackslashCorrectlyRemoved() {
35-
assertEquals("\\some text\\", formatter.format("\\{some text\\}"));
36-
}
37-
38-
@Test
39-
void withoutBracketsUnmodified() {
40-
assertEquals("some text", formatter.format("some text"));
19+
@ParameterizedTest
20+
@CsvSource(
21+
delimiterString = "->",
22+
textBlock = """
23+
some text -> '{some text}'
24+
some text -> '{some text'
25+
some text -> 'some text}'
26+
'\\some text\\' -> '\\{some text\\}'
27+
some text -> some text
28+
"""
29+
)
30+
void format(String expected, String input) {
31+
assertEquals(expected, formatter.format(input));
4132
}
4233
}
Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package org.jabref.logic.layout.format;
22

33
import org.junit.jupiter.api.BeforeEach;
4-
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
56

67
import static org.junit.jupiter.api.Assertions.assertEquals;
78

@@ -14,48 +15,22 @@ void setUp() {
1415
formatter = new RemoveLatexCommandsFormatter();
1516
}
1617

17-
@Test
18-
void withoutLatexCommandsUnmodified() {
19-
assertEquals("some text", formatter.format("some text"));
20-
}
21-
22-
@Test
23-
void singleCommandWiped() {
24-
assertEquals("", formatter.format("\\sometext"));
25-
}
26-
27-
@Test
28-
void singleSpaceAfterCommandRemoved() {
29-
assertEquals("text", formatter.format("\\some text"));
30-
}
31-
32-
@Test
33-
void multipleSpacesAfterCommandRemoved() {
34-
assertEquals("text", formatter.format("\\some text"));
35-
}
36-
37-
@Test
38-
void escapedBackslashBecomesBackslash() {
39-
assertEquals("\\", formatter.format("\\\\"));
40-
}
41-
42-
@Test
43-
void escapedBackslashFollowedByTextBecomesBackslashFollowedByText() {
44-
assertEquals("\\some text", formatter.format("\\\\some text"));
45-
}
46-
47-
@Test
48-
void escapedBackslashKept() {
49-
assertEquals("\\some text\\", formatter.format("\\\\some text\\\\"));
50-
}
51-
52-
@Test
53-
void escapedUnderscoreReplaces() {
54-
assertEquals("some_text", formatter.format("some\\_text"));
55-
}
56-
57-
@Test
58-
void exampleUrlCorrectlyCleaned() {
59-
assertEquals("http://pi.informatik.uni-siegen.de/stt/36_2/./03_Technische_Beitraege/ZEUS2016/beitrag_2.pdf", formatter.format("http://pi.informatik.uni-siegen.de/stt/36\\_2/./03\\_Technische\\_Beitraege/ZEUS2016/beitrag\\_2.pdf"));
18+
@ParameterizedTest
19+
@CsvSource(
20+
delimiterString = "->",
21+
textBlock = """
22+
some text -> some text
23+
'' -> '\\sometext'
24+
text -> '\\some text'
25+
text -> '\\some text'
26+
'\\' -> '\\\\'
27+
'\\some text' -> '\\\\some text'
28+
'\\some text\\' -> '\\\\some text\\\\'
29+
some_text -> 'some\\_text'
30+
'http://pi.informatik.uni-siegen.de/stt/36_2/./03_Technische_Beitraege/ZEUS2016/beitrag_2.pdf' -> 'http://pi.informatik.uni-siegen.de/stt/36\\_2/./03\\_Technische\\_Beitraege/ZEUS2016/beitrag\\_2.pdf'
31+
"""
32+
)
33+
void format(String expected, String input) {
34+
assertEquals(expected, formatter.format(input));
6035
}
6136
}

0 commit comments

Comments
 (0)