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

Refactor simple Unit Tests #7571

Merged
merged 12 commits into from
May 6, 2021
230 changes: 122 additions & 108 deletions src/test/java/org/jabref/logic/bst/BibtexCaseChangersTest.java

Large diffs are not rendered by default.

35 changes: 21 additions & 14 deletions src/test/java/org/jabref/logic/bst/BibtexPurifyTest.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
package org.jabref.logic.bst;

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;
import static org.junit.jupiter.api.Assertions.fail;

public class BibtexPurifyTest {

@Test
public void testPurify() {
assertPurify("i", "i");
assertPurify("0I ", "0I~ ");
assertPurify("Hi Hi ", "Hi Hi ");
assertPurify("oe", "{\\oe}");
assertPurify("Hi oeHi ", "Hi {\\oe }Hi ");
assertPurify("Jonathan Meyer and Charles Louis Xavier Joseph de la Vallee Poussin", "Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin");
assertPurify("e", "{\\'e}");
assertPurify("Edouard Masterly", "{\\'{E}}douard Masterly");
assertPurify("Ulrich Underwood and Ned Net and Paul Pot", "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot");
@ParameterizedTest
@MethodSource("provideTestStrings")
public void testPurify(String expected, String toBePurified) {
assertEquals(expected, BibtexPurify.purify(toBePurified, s -> fail("Should not Warn (" + s + ")! purify should be " + expected + " for " + toBePurified)));
}

private void assertPurify(final String string, final String string2) {
assertEquals(string, BibtexPurify.purify(string2, s -> fail("Should not Warn (" + s + ")! purify should be " + string + " for " + string2)));
private static Stream<Arguments> provideTestStrings() {
return Stream.of(
Arguments.of("i", "i"),
Arguments.of("0I ", "0I~ "),
Arguments.of("Hi Hi ", "Hi Hi "),
Arguments.of("oe", "{\\oe}"),
Arguments.of("Hi oeHi ", "Hi {\\oe }Hi "),
Arguments.of("Jonathan Meyer and Charles Louis Xavier Joseph de la Vallee Poussin", "Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin"),
Arguments.of("e", "{\\'e}"),
Arguments.of("Edouard Masterly", "{\\'{E}}douard Masterly"),
Arguments.of("Ulrich Underwood and Ned Net and Paul Pot", "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot")
);
}
}
64 changes: 34 additions & 30 deletions src/test/java/org/jabref/logic/bst/BibtexWidthTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.jabref.logic.bst;

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;

Expand Down Expand Up @@ -33,39 +37,39 @@
*/
public class BibtexWidthTest {

private void assertBibtexWidth(final int i, final String string) {
assertEquals(i, BibtexWidth.width(string));
@ParameterizedTest
@MethodSource("provideTestWidth")
public void testWidth(int i, String str) {
assertEquals(i, BibtexWidth.width(str));
}

@Test
public void testWidth() {

assertBibtexWidth(278, "i");

assertBibtexWidth(1639, "0I~ ");

assertBibtexWidth(2612, "Hi Hi ");

assertBibtexWidth(778, "{\\oe}");

assertBibtexWidth(3390, "Hi {\\oe }Hi ");

assertBibtexWidth(444, "{\\'e}");

assertBibtexWidth(19762, "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot");

assertBibtexWidth(7861, "{\\'{E}}douard Masterly");

assertBibtexWidth(30514, "Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin");
private static Stream<Arguments> provideTestWidth() {
return Stream.of(
Arguments.of(278, "i"),
Arguments.of(1639, "0I~ "),
Arguments.of(2612, "Hi Hi "),
Arguments.of(778, "{\\oe}"),
Arguments.of(3390, "Hi {\\oe }Hi "),
Arguments.of(444, "{\\'e}"),
Arguments.of(19762, "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot"),
Arguments.of(7861, "{\\'{E}}douard Masterly"),
Arguments.of(30514, "Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin")
);
}

@ParameterizedTest
@MethodSource("provideTestGetCharWidth")
public void testGetCharWidth(int i, Character c) {
assertEquals(i, BibtexWidth.getCharWidth(c));
}

@Test
public void testGetCharWidth() {
assertEquals(500, BibtexWidth.getCharWidth('0'));
assertEquals(361, BibtexWidth.getCharWidth('I'));
assertEquals(500, BibtexWidth.getCharWidth('~'));
assertEquals(500, BibtexWidth.getCharWidth('}'));
assertEquals(278, BibtexWidth.getCharWidth(' '));
private static Stream<Arguments> provideTestGetCharWidth() {
return Stream.of(
Arguments.of(500, '0'),
Arguments.of(361, 'I'),
Arguments.of(500, '~'),
Arguments.of(500, '}'),
Arguments.of(278, ' ')
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -13,7 +14,12 @@
*/
public class NormalizePagesFormatterTest {

private final NormalizePagesFormatter formatter = new NormalizePagesFormatter();
private NormalizePagesFormatter formatter;

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

private static Stream<Arguments> tests() {
return Stream.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

public class RemoveHyphenatedNewlinesFormatterTest {

private RemoveHyphenatedNewlinesFormatter formatter;

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -13,7 +14,12 @@
*/
public class SentenceCaseFormatterTest {

private final SentenceCaseFormatter formatter = new SentenceCaseFormatter();
private SentenceCaseFormatter formatter;

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

private static Stream<Arguments> testData() {
return Stream.of(
Expand All @@ -22,15 +28,15 @@ private static Stream<Arguments> testData() {
Arguments.of("Upper {NOT} first", "upper {NOT} FIRST"),
Arguments.of("Upper {N}ot first", "upper {N}OT FIRST"),
Arguments.of("Whose music? A sociology of musical language",
"Whose music? a sociology of musical language"),
"Whose music? a sociology of musical language"),
Arguments.of("Bibliographic software. A comparison.",
"bibliographic software. a comparison."),
"bibliographic software. a comparison."),
Arguments.of("England’s monitor; The history of the separation",
"England’s Monitor; the History of the Separation"),
"England’s Monitor; the History of the Separation"),
Arguments.of("Dr. schultz: a dentist turned bounty hunter.",
"Dr. schultz: a dentist turned bounty hunter."),
"Dr. schultz: a dentist turned bounty hunter."),
Arguments.of("Example case. {EXCLUDED SENTENCE.}",
"Example case. {EXCLUDED SENTENCE.}"),
"Example case. {EXCLUDED SENTENCE.}"),
Arguments.of("I have {Aa} dream", new SentenceCaseFormatter().getExampleInput()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -13,7 +14,12 @@
*/
public class TitleCaseFormatterTest {

private final TitleCaseFormatter formatter = new TitleCaseFormatter();
private TitleCaseFormatter formatter;

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

private static Stream<Arguments> testData() {
return Stream.of(
Expand All @@ -22,25 +28,25 @@ private static Stream<Arguments> testData() {
Arguments.of("An Upper Each First And", "an upper each first and"),
Arguments.of("An Upper Each First And", "an upper each first AND"),
Arguments.of("An Upper Each of the and First And",
"an upper each of the and first and"),
"an upper each of the and first and"),
Arguments.of("An Upper Each of the and First And",
"an upper each of the AND first and"),
"an upper each of the AND first and"),
Arguments.of("An Upper Each of: The and First And",
"an upper each of: the and first and"),
"an upper each of: the and first and"),
Arguments.of("An Upper First with and without {CURLY} {brackets}",
"AN UPPER FIRST WITH AND WITHOUT {CURLY} {brackets}"),
"AN UPPER FIRST WITH AND WITHOUT {CURLY} {brackets}"),
Arguments.of("An Upper First with {A}nd without {C}urly {b}rackets",
"AN UPPER FIRST WITH {A}ND WITHOUT {C}URLY {b}rackets"),
"AN UPPER FIRST WITH {A}ND WITHOUT {C}URLY {b}rackets"),
Arguments.of("{b}rackets {b}rac{K}ets Brack{E}ts",
"{b}RaCKeTS {b}RaC{K}eTS bRaCK{E}ts"),
"{b}RaCKeTS {b}RaC{K}eTS bRaCK{E}ts"),
Arguments.of("Two Experiences Designing for Effective Security",
"Two experiences designing for effective security"),
"Two experiences designing for effective security"),
Arguments.of("Bibliographic Software. A Comparison.",
"bibliographic software. a comparison."),
"bibliographic software. a comparison."),
Arguments.of("Bibliographic Software. {A COMPARISON.}",
"bibliographic software. {A COMPARISON.}"),
"bibliographic software. {A COMPARISON.}"),
Arguments.of("{BPMN} Conformance in Open Source Engines",
new TitleCaseFormatter().getExampleInput()));
new TitleCaseFormatter().getExampleInput()));
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -14,7 +15,12 @@
*/
public class UnprotectTermsFormatterTest {

private UnprotectTermsFormatter formatter = new UnprotectTermsFormatter();
private UnprotectTermsFormatter formatter;

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

private static Stream<Arguments> terms() throws IOException {
return Stream.of(
Expand Down
30 changes: 16 additions & 14 deletions src/test/java/org/jabref/logic/integrity/BibStringCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.StandardField;

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 @@ -15,22 +20,19 @@ public class BibStringCheckerTest {
private final BibStringChecker checker = new BibStringChecker();
private final BibEntry entry = new BibEntry();

@Test
void fieldAcceptsNoHashMarks() {
entry.setField(StandardField.TITLE, "Not a single hash mark");
assertEquals(Collections.emptyList(), checker.check(entry));
@ParameterizedTest
@MethodSource("provideAcceptedInputs")
void acceptsAllowedInputs(List<IntegrityMessage> expected, Field field, String value) {
entry.setField(field, value);
assertEquals(expected, checker.check(entry));
}

@Test
void monthAcceptsEvenNumberOfHashMarks() {
entry.setField(StandardField.MONTH, "#jan#");
assertEquals(Collections.emptyList(), checker.check(entry));
}

@Test
void authorAcceptsEvenNumberOfHashMarks() {
entry.setField(StandardField.AUTHOR, "#einstein# and #newton#");
assertEquals(Collections.emptyList(), checker.check(entry));
private static Stream<Arguments> provideAcceptedInputs() {
return Stream.of(
Arguments.of(Collections.emptyList(), StandardField.TITLE, "Not a single hash mark"),
Arguments.of(Collections.emptyList(), StandardField.MONTH, "#jan#"),
Arguments.of(Collections.emptyList(), StandardField.AUTHOR, "#einstein# and #newton#")
);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void bibTexAcceptsStringWithCapitalFirstLetter() {
}

@Test
void bibTexDoesNotCareAboutSpecialChracters() {
void bibTexDoesNotCareAboutSpecialCharacters() {
assertEquals(Optional.empty(), checker.checkValue("Lorem ipsum? 10"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,4 @@ void biblatexOnlyField(StandardField field) {
List<IntegrityMessage> messages = checker.check(entry);
assertEquals(Collections.singletonList(message), messages);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TypeCheckerTest {
private BibEntry entry;

@Test
void inProceedingshasPagesNumbers() {
void inProceedingsHasPagesNumbers() {
entry = new BibEntry(StandardEntryType.InProceedings);
entry.setField(StandardField.PAGES, "11--15");
assertEquals(Collections.emptyList(), checker.check(entry));
Expand Down
26 changes: 19 additions & 7 deletions src/test/java/org/jabref/logic/l10n/LocalizationKeyParamsTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
package org.jabref.logic.l10n;

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

public class LocalizationKeyParamsTest {

@Test
public void testReplacePlaceholders() {
assertEquals("biblatex mode", new LocalizationKeyParams("biblatex mode").replacePlaceholders());
assertEquals("biblatex mode", new LocalizationKeyParams("%0 mode", "biblatex").replacePlaceholders());
assertEquals("C:\\bla mode", new LocalizationKeyParams("%0 mode", "C:\\bla").replacePlaceholders());
assertEquals("What \n : %e %c a b", new LocalizationKeyParams("What \n : %e %c %0 %1", "a", "b").replacePlaceholders());
assertEquals("What \n : %e %c_a b", new LocalizationKeyParams("What \n : %e %c_%0 %1", "a", "b").replacePlaceholders());
@ParameterizedTest
@MethodSource("provideTestData")
public void testReplacePlaceholders(String expected, LocalizationKeyParams input) {
assertEquals(expected, input.replacePlaceholders());
}

private static Stream<Arguments> provideTestData() {
return Stream.of(
Arguments.of("biblatex mode", new LocalizationKeyParams("biblatex mode")),
Arguments.of("biblatex mode", new LocalizationKeyParams("%0 mode", "biblatex")),
Arguments.of("C:\\bla mode", new LocalizationKeyParams("%0 mode", "C:\\bla")),
Arguments.of("What \n : %e %c a b", new LocalizationKeyParams("What \n : %e %c %0 %1", "a", "b")),
Arguments.of("What \n : %e %c_a b", new LocalizationKeyParams("What \n : %e %c_%0 %1", "a", "b"))
);
}

@Test
Expand Down
Loading