Skip to content

Commit bb36ce1

Browse files
authored
Refactor tests to use parameterized tests (#14054)
* Parameterize tests for Issue #676 * Address PR review feedback: reformat CsvSource, rename variables, inline helper method, and improve readability
1 parent 5a91a7f commit bb36ce1

File tree

8 files changed

+316
-285
lines changed

8 files changed

+316
-285
lines changed

jablib/src/test/java/org/jabref/logic/bibtex/comparator/BibtexStringComparatorTest.java

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,68 @@ class BibtexStringComparatorTest {
1212
private final BibtexStringComparator bsc1 = new BibtexStringComparator(false);
1313
private final BibtexStringComparator bsc2 = new BibtexStringComparator(true);
1414

15+
private final BibtexString vlsiString = new BibtexString("VLSI", "Very Large Scale Integration");
16+
private final BibtexString dspString = new BibtexString("DSP", "Digital Signal Processing");
17+
private final BibtexString duplicateDspString = new BibtexString("DSP", "Digital Signal Processing");
18+
private final BibtexString dspVlsiCombined = new BibtexString("DSPVLSI", "#VLSI# #DSP#");
19+
20+
@Test
21+
void compareSameString() {
22+
assertEquals(0, bsc1.compare(vlsiString, vlsiString), "Error when comparing the same string");
23+
}
24+
25+
@Test
26+
void compareSameContent() {
27+
assertEquals(0, bsc1.compare(dspString, duplicateDspString), "Different strings do not contain the same content");
28+
}
29+
30+
@Test
31+
void compareStringsReverseAlphabeticallyOrdered() {
32+
assertTrue(bsc1.compare(vlsiString, dspString) > 0, "vlsiString does not lexicographically succeed dspString");
33+
}
34+
35+
@Test
36+
void compareStringsAlphabeticallyOrdered() {
37+
assertTrue(bsc1.compare(dspString, vlsiString) < 0, "dspString does not lexicographically precede vlsiString");
38+
}
39+
40+
@Test
41+
void compareSameStringWithInternalCheckingEnabled() {
42+
assertEquals(0, bsc2.compare(vlsiString, vlsiString), "Error when comparing the same string [internal checking enabled]");
43+
}
44+
45+
@Test
46+
void compareSameContentWithInternalCheckingEnabled() {
47+
assertEquals(0, bsc2.compare(dspString, duplicateDspString), "Different strings do not contain the same content [internal checking enabled]");
48+
}
49+
50+
@Test
51+
void compareStringsReverseAlphabeticallyOrderedWithInternalCheckingEnabled() {
52+
assertTrue(bsc2.compare(vlsiString, dspString) > 0, "vlsiString does not succeed dspString [internal checking enabled]");
53+
}
54+
55+
@Test
56+
void compareStringsAlphabeticallyOrderedWithInternalCheckingEnabled() {
57+
assertTrue(bsc2.compare(dspString, vlsiString) < 0, "dspString does not precede vlsiString [internal checking enabled]");
58+
}
59+
60+
@Test
61+
void compareRegularStringToInternalString() {
62+
assertTrue(bsc1.compare(vlsiString, dspVlsiCombined) > 0, "vlsiString does not lexicographically succeed dspVlsiCombined");
63+
}
64+
65+
@Test
66+
void compareInternalStringToRegularString() {
67+
assertTrue(bsc1.compare(dspVlsiCombined, vlsiString) < 0, "dspVlsiCombined does not lexicographically precede vlsiString");
68+
}
69+
70+
@Test
71+
void compareRegularStringToInternalStringWithInternalCheckingEnabled() {
72+
assertTrue(bsc2.compare(vlsiString, dspVlsiCombined) < 0, "dspVlsiCombined does not contain vlsiString [internal checking enabled]");
73+
}
74+
1575
@Test
16-
void test() {
17-
BibtexString bs1 = new BibtexString("VLSI", "Very Large Scale Integration");
18-
BibtexString bs2 = new BibtexString("DSP", "Digital Signal Processing");
19-
BibtexString bs3 = new BibtexString("DSP", "Digital Signal Processing");
20-
21-
// Same string
22-
assertEquals(0, bsc1.compare(bs1, bs1), "Error when comparing the same string");
23-
// Same content
24-
assertEquals(0, bsc1.compare(bs2, bs3), "Different strings do not contain the same content");
25-
// Alphabetical order
26-
assertTrue(bsc1.compare(bs1, bs2) > 0, "bs1 does not lexicographically succeed bs2");
27-
assertTrue(bsc1.compare(bs2, bs1) < 0, "bs2 does not lexicographically precede bs1");
28-
29-
// Same, but with the comparator checking for internal strings (none)
30-
assertEquals(0, bsc2.compare(bs1, bs1), "Error when comparing the same string [internal checking enabled]");
31-
assertEquals(0, bsc2.compare(bs2, bs3), "Different strings do not contain the same content [internal checking enabled]");
32-
assertTrue(bsc2.compare(bs1, bs2) > 0, "bs1 does not succeed bs2 [internal checking enabled]");
33-
assertTrue(bsc2.compare(bs2, bs1) < 0, "bs2 does not precede bs1 [internal checking enabled]");
34-
35-
// Create string with internal string
36-
BibtexString bs4 = new BibtexString("DSPVLSI", "#VLSI# #DSP#");
37-
// bs4 before bs1 if not considering that bs4 contains bs1
38-
assertTrue(bsc1.compare(bs1, bs4) > 0, "bs1 does not lexicographically succeed bs4");
39-
assertTrue(bsc1.compare(bs4, bs1) < 0, "bs4 does not lexicographically precede bs1");
40-
41-
// bs4 after bs1 if considering that bs4 contains bs1
42-
assertTrue(bsc2.compare(bs1, bs4) < 0, "bs4 does not contain bs1 [internal checking enabled]");
43-
assertTrue(bsc2.compare(bs4, bs1) > 0, "bs4 contains bs1 [internal checking enabled]");
76+
void compareInternalStringToRegularStringWithInternalCheckingEnabled() {
77+
assertTrue(bsc2.compare(dspVlsiCombined, vlsiString) > 0, "dspVlsiCombined contains vlsiString [internal checking enabled]");
4478
}
4579
}
Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
package org.jabref.logic.bst.util;
22

3+
import java.util.stream.Stream;
4+
35
import org.jabref.model.entry.AuthorList;
46

57
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.CsvSource;
11+
import org.junit.jupiter.params.provider.MethodSource;
612

713
import static org.junit.jupiter.api.Assertions.assertEquals;
814

915
class BstNameFormatterTest {
1016

17+
private static final String EDOUARD_MASTERLY = "{\\'{E}}douard Masterly";
18+
private static final String MEYER_AND_DE_LA_VALLEE_POUSSIN = "Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin";
19+
private static final String UNDERWOOD_NET_AND_POT = "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot";
20+
private static final String VICTOR_AND_CIERVA = "Paul {\\'E}mile Victor and and de la Cierva y Codorn{\\\\i}u, Juan";
21+
1122
@Test
1223
void umlautsFullNames() {
1324
AuthorList list = AuthorList.parse("Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin");
@@ -35,38 +46,35 @@ void umlautsAbbreviationsWithQuestionMark() {
3546
@Test
3647
void formatName() {
3748
AuthorList list = AuthorList.parse("Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin");
38-
3949
assertEquals("dlVP", BstNameFormatter.formatName(list.getAuthor(0), "{v{}}{l{}}"));
40-
41-
assertNameFormatA("Meyer, J?", "Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin");
42-
assertNameFormatB("J.~Meyer", "Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin");
43-
assertNameFormatC("Jonathan Meyer", "Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin");
44-
assertNameFormatA("Masterly, {\\'{E}}?", "{\\'{E}}douard Masterly");
45-
assertNameFormatB("{\\'{E}}.~Masterly", "{\\'{E}}douard Masterly");
46-
assertNameFormatC("{\\'{E}}douard Masterly", "{\\'{E}}douard Masterly");
47-
assertNameFormatA("{\\\"{U}}nderwood, U?", "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot");
48-
assertNameFormatB("U.~{\\\"{U}}nderwood", "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot");
49-
assertNameFormatC("Ulrich {\\\"{U}}nderwood", "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot");
50-
assertNameFormatA("Victor, P.~{\\'E}?", "Paul {\\'E}mile Victor and and de la Cierva y Codorn{\\\\i}u, Juan");
51-
assertNameFormatB("P.~{\\'E}. Victor", "Paul {\\'E}mile Victor and and de la Cierva y Codorn{\\\\i}u, Juan");
52-
assertNameFormatC("Paul~{\\'E}mile Victor",
53-
"Paul {\\'E}mile Victor and and de la Cierva y Codorn{\\\\i}u, Juan");
54-
}
55-
56-
private void assertNameFormat(String string, String string2, int which, String format) {
57-
assertEquals(string, BstNameFormatter.formatName(string2, which, format));
58-
}
59-
60-
private void assertNameFormatC(String string, String string2) {
61-
assertNameFormat(string, string2, 1, "{ff }{vv }{ll}{ jj}");
6250
}
6351

64-
private void assertNameFormatB(String string, String string2) {
65-
assertNameFormat(string, string2, 1, "{f.~}{vv~}{ll}{, jj}");
52+
static Stream<Arguments> provideNameFormattingCases() {
53+
return Stream.of(
54+
// Format pattern: "{vv~}{ll}{, jj}{, f}?"
55+
Arguments.of("Meyer, J?", MEYER_AND_DE_LA_VALLEE_POUSSIN, "{vv~}{ll}{, jj}{, f}?"),
56+
Arguments.of("Masterly, {\\'{E}}?", EDOUARD_MASTERLY, "{vv~}{ll}{, jj}{, f}?"),
57+
Arguments.of("{\\\"{U}}nderwood, U?", UNDERWOOD_NET_AND_POT, "{vv~}{ll}{, jj}{, f}?"),
58+
Arguments.of("Victor, P.~{\\'E}?", VICTOR_AND_CIERVA, "{vv~}{ll}{, jj}{, f}?"),
59+
60+
// Format pattern: "{f.~}{vv~}{ll}{, jj}"
61+
Arguments.of("J.~Meyer", MEYER_AND_DE_LA_VALLEE_POUSSIN, "{f.~}{vv~}{ll}{, jj}"),
62+
Arguments.of("{\\'{E}}.~Masterly", EDOUARD_MASTERLY, "{f.~}{vv~}{ll}{, jj}"),
63+
Arguments.of("U.~{\\\"{U}}nderwood", UNDERWOOD_NET_AND_POT, "{f.~}{vv~}{ll}{, jj}"),
64+
Arguments.of("P.~{\\'E}. Victor", VICTOR_AND_CIERVA, "{f.~}{vv~}{ll}{, jj}"),
65+
66+
// Format pattern: "{ff }{vv }{ll}{ jj}"
67+
Arguments.of("Jonathan Meyer", MEYER_AND_DE_LA_VALLEE_POUSSIN, "{ff }{vv }{ll}{ jj}"),
68+
Arguments.of(EDOUARD_MASTERLY, EDOUARD_MASTERLY, "{ff }{vv }{ll}{ jj}"),
69+
Arguments.of("Ulrich {\\\"{U}}nderwood", UNDERWOOD_NET_AND_POT, "{ff }{vv }{ll}{ jj}"),
70+
Arguments.of("Paul~{\\'E}mile Victor", VICTOR_AND_CIERVA, "{ff }{vv }{ll}{ jj}")
71+
);
6672
}
6773

68-
private void assertNameFormatA(String string, String string2) {
69-
assertNameFormat(string, string2, 1, "{vv~}{ll}{, jj}{, f}?");
74+
@ParameterizedTest
75+
@MethodSource("provideNameFormattingCases")
76+
void formatNameVariations(String expected, String authorList, String formatString) {
77+
assertEquals(expected, BstNameFormatter.formatName(authorList, 1, formatString));
7078
}
7179

7280
@Test
@@ -90,23 +98,29 @@ void consumeToMatchingBrace() {
9098
assertEquals("{HE{L{}L}O}", sb.toString());
9199
}
92100

93-
@Test
94-
void getFirstCharOfString() {
95-
assertEquals("C", BstNameFormatter.getFirstCharOfString("Charles"));
96-
assertEquals("V", BstNameFormatter.getFirstCharOfString("Vall{\\'e}e"));
97-
assertEquals("{\\'e}", BstNameFormatter.getFirstCharOfString("{\\'e}"));
98-
assertEquals("{\\'e", BstNameFormatter.getFirstCharOfString("{\\'e"));
99-
assertEquals("E", BstNameFormatter.getFirstCharOfString("{E"));
101+
@ParameterizedTest
102+
@CsvSource({
103+
"C, Charles",
104+
"V, Vall{\\'e}e",
105+
"{\\'e}, {\\'e}",
106+
"{\\'e, {\\'e",
107+
"E, {E"
108+
})
109+
void getFirstCharOfString(String expected, String s) {
110+
assertEquals(expected, BstNameFormatter.getFirstCharOfString(s));
100111
}
101112

102-
@Test
103-
void numberOfChars() {
104-
assertEquals(6, BstNameFormatter.numberOfChars("Vall{\\'e}e", -1));
105-
assertEquals(2, BstNameFormatter.numberOfChars("Vall{\\'e}e", 2));
106-
assertEquals(1, BstNameFormatter.numberOfChars("Vall{\\'e}e", 1));
107-
assertEquals(6, BstNameFormatter.numberOfChars("Vall{\\'e}e", 6));
108-
assertEquals(6, BstNameFormatter.numberOfChars("Vall{\\'e}e", 7));
109-
assertEquals(8, BstNameFormatter.numberOfChars("Vall{e}e", -1));
110-
assertEquals(6, BstNameFormatter.numberOfChars("Vall{\\'e this will be skipped}e", -1));
113+
@ParameterizedTest
114+
@CsvSource({
115+
"6, Vall{\\'e}e, -1",
116+
"2, Vall{\\'e}e, 2",
117+
"1, Vall{\\'e}e, 1",
118+
"6, Vall{\\'e}e, 6",
119+
"6, Vall{\\'e}e, 7",
120+
"8, Vall{e}e, -1",
121+
"6, Vall{\\'e this will be skipped}e, -1"
122+
})
123+
void numberOfChars(int expected, String token, int inStop) {
124+
assertEquals(expected, BstNameFormatter.numberOfChars(token, inStop));
111125
}
112126
}

jablib/src/test/java/org/jabref/logic/crawler/StudyRepositoryTest.java

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import org.junit.jupiter.api.BeforeEach;
3838
import org.junit.jupiter.api.Test;
3939
import org.junit.jupiter.api.io.TempDir;
40+
import org.junit.jupiter.params.ParameterizedTest;
41+
import org.junit.jupiter.params.provider.ValueSource;
4042
import org.mockito.Answers;
4143

4244
import static org.jabref.logic.citationkeypattern.CitationKeyGenerator.DEFAULT_UNWANTED_CHARACTERS;
@@ -108,19 +110,51 @@ void providePathToNonExistentRepositoryThrowsException() {
108110
* Tests whether the file structure of the repository is created correctly from the study definitions file.
109111
*/
110112
@Test
111-
void repositoryStructureCorrectlyCreated() {
113+
void quantumRepositoryStructureCorrectlyCreated() {
112114
// When repository is instantiated the directory structure is created
113115
assertTrue(Files.exists(Path.of(tempRepositoryDirectory.toString(), hashCodeQuantum + " - Quantum")));
116+
}
117+
118+
@Test
119+
void cloudComputingRepositoryStructureCorrectlyCreated() {
114120
assertTrue(Files.exists(Path.of(tempRepositoryDirectory.toString(), hashCodeCloudComputing + " - Cloud Computing")));
121+
}
122+
123+
@Test
124+
void softwareEngineeringRepositoryStructureCorrectlyCreated() {
115125
assertTrue(Files.exists(Path.of(tempRepositoryDirectory.toString(), hashCodeSoftwareEngineering + " - Software Engineering")));
116-
assertTrue(Files.exists(Path.of(tempRepositoryDirectory.toString(), hashCodeQuantum + " - Quantum", "ArXiv.bib")));
117-
assertTrue(Files.exists(Path.of(tempRepositoryDirectory.toString(), hashCodeCloudComputing + " - Cloud Computing", "ArXiv.bib")));
118-
assertTrue(Files.exists(Path.of(tempRepositoryDirectory.toString(), hashCodeSoftwareEngineering + " - Software Engineering", "ArXiv.bib")));
119-
assertTrue(Files.exists(Path.of(tempRepositoryDirectory.toString(), hashCodeQuantum + " - Quantum", "Springer.bib")));
120-
assertTrue(Files.exists(Path.of(tempRepositoryDirectory.toString(), hashCodeCloudComputing + " - Cloud Computing", "Springer.bib")));
121-
assertTrue(Files.exists(Path.of(tempRepositoryDirectory.toString(), hashCodeSoftwareEngineering + " - Software Engineering", "Springer.bib")));
126+
}
127+
128+
@ParameterizedTest
129+
@ValueSource(strings = {"ArXiv.bib", "Springer.bib"})
130+
void quantumFilesCreated(String fileName) {
131+
assertTrue(Files.exists(Path.of(tempRepositoryDirectory.toString(), hashCodeQuantum + " - Quantum", fileName)));
132+
}
133+
134+
@ParameterizedTest
135+
@ValueSource(strings = {"ArXiv.bib", "Springer.bib"})
136+
void cloudComputingFilesCreated(String fileName) {
137+
assertTrue(Files.exists(Path.of(tempRepositoryDirectory.toString(), hashCodeCloudComputing + " - Cloud Computing", fileName)));
138+
}
139+
140+
@ParameterizedTest
141+
@ValueSource(strings = {"ArXiv.bib", "Springer.bib"})
142+
void softwareEngineeringFilesCreated(String fileName) {
143+
assertTrue(Files.exists(Path.of(tempRepositoryDirectory.toString(), hashCodeSoftwareEngineering + " - Software Engineering", fileName)));
144+
}
145+
146+
@Test
147+
void doesNotCreateUnexpectedQuantumFile() {
122148
assertFalse(Files.exists(Path.of(tempRepositoryDirectory.toString(), hashCodeQuantum + " - Quantum", "IEEEXplore.bib")));
149+
}
150+
151+
@Test
152+
void doesNotCreateUnexpectedCloudComputingFile() {
123153
assertFalse(Files.exists(Path.of(tempRepositoryDirectory.toString(), hashCodeCloudComputing + " - Cloud Computing", "IEEEXplore.bib")));
154+
}
155+
156+
@Test
157+
void doesNotCreateUnexpectedSoftwareEngineeringFile() {
124158
assertFalse(Files.exists(Path.of(tempRepositoryDirectory.toString(), hashCodeSoftwareEngineering + " - Software Engineering", "IEEEXplore.bib")));
125159
}
126160

jablib/src/test/java/org/jabref/logic/formatter/bibtexfields/HtmlToLatexFormatterTest.java

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.junit.jupiter.api.BeforeEach;
44
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
57

68
import static org.junit.jupiter.api.Assertions.assertEquals;
79

@@ -17,56 +19,37 @@ void setUp() {
1719
formatter = new HtmlToLatexFormatter();
1820
}
1921

20-
@Test
21-
void formatWithoutHtmlCharactersReturnsSameString() {
22-
assertEquals("abc", formatter.format("abc"));
23-
}
24-
25-
@Test
26-
void formatIeeeHtml() {
27-
assertEquals("Towards situation-aware adaptive workflows: SitOPT --- A general purpose situation-aware workflow management system", formatter.format("Towards situation-aware adaptive workflows: SitOPT &amp;#x2014; A general purpose situation-aware workflow management system"));
28-
}
29-
30-
@Test
31-
void formatMultipleHtmlCharacters() {
32-
assertEquals("{{\\aa}}{\\\"{a}}{\\\"{o}}", formatter.format("&aring;&auml;&ouml;"));
33-
}
34-
35-
@Test
36-
void formatCombinedAccent() {
37-
assertEquals("{\\'{\\i}}", formatter.format("i&#x301;"));
38-
}
39-
40-
@Test
41-
void basic() {
42-
assertEquals("aaa", formatter.format("aaa"));
43-
}
44-
45-
@Test
46-
void html() {
47-
assertEquals("{\\\"{a}}", formatter.format("&auml;"));
48-
assertEquals("{\\\"{a}}", formatter.format("&#228;"));
49-
assertEquals("{\\\"{a}}", formatter.format("&#xe4;"));
50-
assertEquals("{{$\\Epsilon$}}", formatter.format("&Epsilon;"));
51-
}
52-
53-
@Test
54-
void htmlRemoveTags() {
55-
assertEquals("aaa", formatter.format("<b>aaa</b>"));
56-
}
57-
58-
@Test
59-
void htmlCombiningAccents() {
60-
assertEquals("{\\\"{a}}", formatter.format("a&#776;"));
61-
assertEquals("{\\\"{a}}", formatter.format("a&#x308;"));
62-
assertEquals("{\\\"{a}}b", formatter.format("a&#776;b"));
63-
assertEquals("{\\\"{a}}b", formatter.format("a&#x308;b"));
64-
}
65-
66-
@Test
67-
void keepsSingleLessThan() {
68-
String text = "(p < 0.01)";
69-
assertEquals(text, formatter.format(text));
22+
@ParameterizedTest
23+
@CsvSource({
24+
// Return the same string
25+
"abc, abc",
26+
"aaa, aaa",
27+
"(p < 0.01), (p < 0.01)",
28+
29+
// IEEE-style HTML entity for em dash
30+
"Towards situation-aware adaptive workflows: SitOPT --- A general purpose situation-aware workflow management system, Towards situation-aware adaptive workflows: SitOPT &amp;#x2014; A general purpose situation-aware workflow management system",
31+
32+
// Format multiple HTML characters
33+
"{{\\aa}}{\\\"{a}}{\\\"{o}}, &aring;&auml;&ouml;",
34+
35+
// Format combined accents
36+
"{\\'{\\i}}, i&#x301;",
37+
"{\\\"{a}}, a&#776;",
38+
"{\\\"{a}}, a&#x308;",
39+
"{\\\"{a}}b, a&#776;b",
40+
"{\\\"{a}}b, a&#x308;b",
41+
42+
// Format HTML entities
43+
"{\\\"{a}}, &auml;",
44+
"{\\\"{a}}, &#228;",
45+
"{\\\"{a}}, &#xe4;",
46+
"{{$\\Epsilon$}}, &Epsilon;",
47+
48+
// Strip tags
49+
"aaa, <b>aaa</b>"
50+
})
51+
void html(String expected, String text) {
52+
assertEquals(expected, formatter.format(text));
7053
}
7154

7255
@Test

0 commit comments

Comments
 (0)