From 753d1eebf92ff3155398fc5b57a5de70ef9d3f7e Mon Sep 17 00:00:00 2001 From: ningxie Date: Wed, 17 Mar 2021 23:33:45 +0100 Subject: [PATCH] Add simple unit tests (#7545) --- .../casechanger/CapitalizeFormatterTest.java | 25 +++++++ .../integrity/ASCIICharacterCheckerTest.java | 17 +++++ .../logic/integrity/YearCheckerTest.java | 5 ++ .../logic/layout/format/AuthorsTest.java | 68 +++++++++++++++++++ .../layout/format/DateFormatterTest.java | 16 +++++ .../logic/layout/format/RTFCharsTest.java | 56 +++++++++++++++ .../logic/layout/format/RisMonthTest.java | 23 ++++++- .../util/strings/StringSimilarityTest.java | 30 ++++++++ 8 files changed, 237 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/jabref/logic/util/strings/StringSimilarityTest.java diff --git a/src/test/java/org/jabref/logic/formatter/casechanger/CapitalizeFormatterTest.java b/src/test/java/org/jabref/logic/formatter/casechanger/CapitalizeFormatterTest.java index 61d7fdd9837..5cba9c90172 100644 --- a/src/test/java/org/jabref/logic/formatter/casechanger/CapitalizeFormatterTest.java +++ b/src/test/java/org/jabref/logic/formatter/casechanger/CapitalizeFormatterTest.java @@ -2,6 +2,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -28,4 +30,27 @@ public void test() { public void formatExample() { assertEquals("I Have {a} Dream", formatter.format(formatter.getExampleInput())); } + + @ParameterizedTest(name = "input={0}, formattedStr={1}") + @CsvSource({ + "{}, {}", // {} + "{upper, {upper", // unmatched braces + "upper, Upper", // single word lower case + "Upper, Upper", // single word correct + "UPPER, Upper", // single word upper case + "upper each first, Upper Each First", // multiple words lower case + "Upper Each First, Upper Each First", // multiple words correct + "UPPER EACH FIRST, Upper Each First", // multiple words upper case + "{u}pp{e}r, {u}pp{e}r", // single word lower case with {} + "{U}pp{e}r, {U}pp{e}r", // single word correct with {} + "{U}PP{E}R, {U}pp{E}r", // single word upper case with {} + "upper each {NOT} first, Upper Each {NOT} First", // multiple words lower case with {} + "Upper {E}ach {NOT} First, Upper {E}ach {NOT} First", // multiple words correct with {} + "UPPER {E}ACH {NOT} FIRST, Upper {E}ach {NOT} First", // multiple words upper case with {} + + }) + public void testInputs(String input, String expectedResult) { + String formattedStr = formatter.format(input); + assertEquals(expectedResult, formattedStr); + } } diff --git a/src/test/java/org/jabref/logic/integrity/ASCIICharacterCheckerTest.java b/src/test/java/org/jabref/logic/integrity/ASCIICharacterCheckerTest.java index 0b5ca6a9d9e..5a6af99aad3 100644 --- a/src/test/java/org/jabref/logic/integrity/ASCIICharacterCheckerTest.java +++ b/src/test/java/org/jabref/logic/integrity/ASCIICharacterCheckerTest.java @@ -33,4 +33,21 @@ void fieldDoesNotAcceptUnicode() { assertEquals(List.of(new IntegrityMessage("Non-ASCII encoded character found", entry, StandardField.AUTHOR)), checker.check(entry)); } + @Test + void fieldAcceptsOnlyAsciiCharacters() { + String field = ""; + for (int i = 32; i <= 127; i++) { + field += Character.toString(i); + } + entry.setField(StandardField.TITLE, field); + assertEquals(Collections.emptyList(), checker.check(entry)); + } + + @Test + void fieldDoesNotAcceptNonAsciiCharacters() { + String field = Character.toString(31) + Character.toString(128); + entry.setField(StandardField.TITLE, field); + assertEquals(List.of(new IntegrityMessage("Non-ASCII encoded character found", entry, StandardField.TITLE)), checker.check(entry)); + } + } diff --git a/src/test/java/org/jabref/logic/integrity/YearCheckerTest.java b/src/test/java/org/jabref/logic/integrity/YearCheckerTest.java index 89dfc884e5a..dcd68676def 100644 --- a/src/test/java/org/jabref/logic/integrity/YearCheckerTest.java +++ b/src/test/java/org/jabref/logic/integrity/YearCheckerTest.java @@ -85,4 +85,9 @@ void yearFieldDoesNotRemoveStringInsideBraceAndPercentage() { void yearFieldDoesNotRemoveStringBeforeSpecialCharacters() { assertNotEquals(Optional.empty(), checker.checkValue("1986a(){},.;!?<>%&$")); } + + @Test + void testEmptyValue() { + assertEquals(Optional.empty(), checker.checkValue("")); + } } diff --git a/src/test/java/org/jabref/logic/layout/format/AuthorsTest.java b/src/test/java/org/jabref/logic/layout/format/AuthorsTest.java index fe0cb06ca4d..09c5f7fd2d3 100644 --- a/src/test/java/org/jabref/logic/layout/format/AuthorsTest.java +++ b/src/test/java/org/jabref/logic/layout/format/AuthorsTest.java @@ -3,6 +3,8 @@ import org.jabref.logic.layout.ParamLayoutFormatter; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -154,4 +156,70 @@ public void testEmptyEtAl() { assertEquals("Bruce, Bob Croydon", a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper and Chuck Chuckles")); } + + @ParameterizedTest(name = "arg={0}, formattedStr={1}") + @CsvSource({ + "FirstFirst, 'B. C. Bruce, C. Manson, J. Jumper and C. Chuckles'", // FirstFirst + "LastFirst, 'Bruce, B. C., Manson, C., Jumper, J. and Chuckles, C.'", // LastFirst + "LastFirstFirstFirst, 'Bruce, B. C., C. Manson, J. Jumper and C. Chuckles'" // LastFirstFirstFirst + }) + public void testAuthorOrder(String arg, String expectedResult) { + ParamLayoutFormatter a = new Authors(); + a.setArgument(arg); + String formattedStr = a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper and Chuck Chuckles"); + assertEquals(expectedResult, formattedStr); + } + + @ParameterizedTest(name = "arg={0}, formattedStr={1}") + @CsvSource({ + "FullName, 'Bob Croydon Bruce, Charles Manson, Jolly Jumper and Chuck Chuckles'", // FullName + "Initials, 'B. C. Bruce, C. Manson, J. Jumper and C. Chuckles'", // Initials + "FirstInitial, 'B. Bruce, C. Manson, J. Jumper and C. Chuckles'", // FirstInitial + "MiddleInitial, 'Bob C. Bruce, Charles Manson, Jolly Jumper and Chuck Chuckles'", // MiddleInitial + "LastName, 'Bruce, Manson, Jumper and Chuckles'", // LastName + "InitialsNoSpace, 'B.C. Bruce, C. Manson, J. Jumper and C. Chuckles'" // InitialsNoSpace + }) + public void testAuthorABRV(String arg, String expectedResult) { + ParamLayoutFormatter a = new Authors(); + a.setArgument(arg); + String formattedStr = a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper and Chuck Chuckles"); + assertEquals(expectedResult, formattedStr); + } + + @ParameterizedTest(name = "arg={0}, formattedStr={1}") + @CsvSource({ + "FullPunc, 'B. C. Bruce, C. Manson, J. Jumper and C. Chuckles'", // FullPunc + "NoPunc, 'B C Bruce, C Manson, J Jumper and C Chuckles'", // NoPunc + "NoComma, 'B. C. Bruce, C. Manson, J. Jumper and C. Chuckles'", // NoComma + "NoPeriod, 'B C Bruce, C Manson, J Jumper and C Chuckles'" // NoPeriod + }) + public void testAuthorPUNC(String arg, String expectedResult) { + ParamLayoutFormatter a = new Authors(); + a.setArgument(arg); + String formattedStr = a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper and Chuck Chuckles"); + assertEquals(expectedResult, formattedStr); + } + + @ParameterizedTest(name = "arg={0}, formattedStr={1}") + @CsvSource({ + "Comma, 'B. C. Bruce, C. Manson, J. Jumper and C. Chuckles'", // Comma + "And, 'B. C. Bruce and C. Manson and J. Jumper and C. Chuckles'", // And + "Colon, 'B. C. Bruce: C. Manson: J. Jumper and C. Chuckles'", // Colon + "Semicolon, 'B. C. Bruce; C. Manson; J. Jumper and C. Chuckles'", // Semicolon + "Oxford, 'B. C. Bruce, C. Manson, J. Jumper, and C. Chuckles'", // Oxford + "Amp, 'B. C. Bruce, C. Manson, J. Jumper & C. Chuckles'", // Amp + "Sep, 'B. C. Bruce, C. Manson, J. Jumper and C. Chuckles'", // Sep + "LastSep, 'B. C. Bruce, C. Manson, J. Jumper and C. Chuckles'", // LastSep + "Sep=|, 'B. C. Bruce|C. Manson|J. Jumper and C. Chuckles'", // Custom Sep + "LastSep=|, 'B. C. Bruce, C. Manson, J. Jumper|C. Chuckles'", // Custom LastSep + "'Comma, And', 'B. C. Bruce, C. Manson, J. Jumper and C. Chuckles'", // Comma And + "'Comma, Colon', 'B. C. Bruce, C. Manson, J. Jumper: C. Chuckles'", // Comma Colon + "'Comma, Semicolon', 'B. C. Bruce, C. Manson, J. Jumper; C. Chuckles'", // Comma Semicolon + }) + public void testAuthorSEPARATORS(String arg, String expectedResult) { + ParamLayoutFormatter a = new Authors(); + a.setArgument(arg); + String formattedStr = a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper and Chuck Chuckles"); + assertEquals(expectedResult, formattedStr); + } } diff --git a/src/test/java/org/jabref/logic/layout/format/DateFormatterTest.java b/src/test/java/org/jabref/logic/layout/format/DateFormatterTest.java index b9f878d25ae..d5eda7aa69a 100644 --- a/src/test/java/org/jabref/logic/layout/format/DateFormatterTest.java +++ b/src/test/java/org/jabref/logic/layout/format/DateFormatterTest.java @@ -4,6 +4,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -26,4 +28,18 @@ public void testRequestedFormat() { formatter.setArgument("MM/yyyy"); assertEquals("07/2016", formatter.format("2016-07-15")); } + + @ParameterizedTest(name = "formatArg={0}, input={1}, formattedStr={2}") + @CsvSource({ + "MM/dd/yyyy, 2016-07-15, 07/15/2016", // MM/dd/yyyy + "dd MMMM yyyy, 2016-07-15, 15 July 2016", // dd MMMM yyyy + "MM-dd-yyyy, 2016-07-15, 07-15-2016", // MM-dd-yyyy + "yyyy.MM.dd, 2016-07-15, 2016.07.15", // yyyy.MM.dd + "yyyy/MM, 2016-07-15, 2016/07", // yyyy/MM + }) + public void testOtherFormats(String formatArg, String input, String expectedResult) { + formatter.setArgument(formatArg); + String formattedStr = formatter.format(input); + assertEquals(expectedResult, formattedStr); + } } diff --git a/src/test/java/org/jabref/logic/layout/format/RTFCharsTest.java b/src/test/java/org/jabref/logic/layout/format/RTFCharsTest.java index 558fbfa3adf..b39d06e6be3 100644 --- a/src/test/java/org/jabref/logic/layout/format/RTFCharsTest.java +++ b/src/test/java/org/jabref/logic/layout/format/RTFCharsTest.java @@ -5,6 +5,8 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -128,6 +130,60 @@ void testSpecialCharacters() { assertEquals("\\u182P", formatter.format("{\\P}")); // ¶ assertEquals("\\u169?", formatter.format("{\\copyright}")); // © assertEquals("\\u163?", formatter.format("{\\pounds}")); // £ + + } + + @ParameterizedTest(name = "specialChar={0}, formattedStr={1}") + @CsvSource({ + "ÀÁÂÃÄĀĂĄ, \\u192A\\u193A\\u194A\\u195A\\u196A\\u256A\\u258A\\u260A", // A + "àáâãäåāăą, \\u224a\\u225a\\u226a\\u227a\\u228a\\u229a\\u257a\\u259a\\u261a", // a + "ÇĆĈĊČ, \\u199C\\u262C\\u264C\\u266C\\u268C", // C + "çćĉċč, \\u231c\\u263c\\u265c\\u267c\\u269c", // c + "ÐĐ, \\u208D\\u272D", // D + "ðđ, \\u240d\\u273d", // d + "ÈÉÊËĒĔĖĘĚ, \\u200E\\u201E\\u202E\\u203E\\u274E\\u276E\\u278E\\u280E\\u282E", // E + "èéêëēĕėęě, \\u232e\\u233e\\u234e\\u235e\\u275e\\u277e\\u279e\\u281e\\u283e", // e + "ĜĞĠĢŊ, \\u284G\\u286G\\u288G\\u290G\\u330G", // G + "ĝğġģŋ, \\u285g\\u287g\\u289g\\u291g\\u331g", // g + "ĤĦ, \\u292H\\u294H", // H + "ĥħ, \\u293h\\u295h", // h + "ÌÍÎÏĨĪĬĮİ, \\u204I\\u205I\\u206I\\u207I\\u296I\\u298I\\u300I\\u302I\\u304I", // I + "ìíîïĩīĭį, \\u236i\\u237i\\u238i\\u239i\\u297i\\u299i\\u301i\\u303i", // i + "Ĵ, \\u308J", // J + "ĵ, \\u309j", // j + "Ķ, \\u310K", // K + "ķ, \\u311k", // k + "ĹĻĿ, \\u313L\\u315L\\u319L", // L + "ĺļŀł, \\u314l\\u316l\\u320l\\u322l", // l + "ÑŃŅŇ, \\u209N\\u323N\\u325N\\u327N", // N + "ñńņň, \\u241n\\u324n\\u326n\\u328n", // n + "ÒÓÔÕÖØŌŎ, \\u210O\\u211O\\u212O\\u213O\\u214O\\u216O\\u332O\\u334O", // O + "òóôõöøōŏ, \\u242o\\u243o\\u244o\\u245o\\u246o\\u248o\\u333o\\u335o", // o + "ŔŖŘ, \\u340R\\u342R\\u344R", // R + "ŕŗř, \\u341r\\u343r\\u345r", // r + "ŚŜŞŠ, \\u346S\\u348S\\u350S\\u352S", // S + "śŝşš, \\u347s\\u349s\\u351s\\u353s", // s + "ŢŤŦ, \\u354T\\u356T\\u358T", // T + "ţŧ, \\u355t\\u359t", // t + "ÙÚÛÜŨŪŬŮŲ, \\u217U\\u218U\\u219U\\u220U\\u360U\\u362U\\u364U\\u366U\\u370U", // U + "ùúûũūŭůų, \\u249u\\u250u\\u251u\\u361u\\u363u\\u365u\\u367u\\u371u", // u + "Ŵ, \\u372W", // W + "ŵ, \\u373w", // w + "ŶŸÝ, \\u374Y\\u376Y\\u221Y", // Y + "ŷÿ, \\u375y\\u255y", // y + "ŹŻŽ, \\u377Z\\u379Z\\u381Z", // Z + "źżž, \\u378z\\u380z\\u382z", // z + "Æ, \\u198AE", // AE + "æ, \\u230ae", // ae + "Œ, \\u338OE", // OE + "œ, \\u339oe", // oe + "Þ, \\u222TH", // TH + "ß, \\u223ss", // ss + "¡, \\u161!" // ! + }) + public void testMoreSpecialCharacters(String specialChar, String expectedResult) { + String formattedStr = formatter.format(specialChar); + assertEquals(expectedResult, formattedStr); } @Test diff --git a/src/test/java/org/jabref/logic/layout/format/RisMonthTest.java b/src/test/java/org/jabref/logic/layout/format/RisMonthTest.java index cd8bfbec58a..a323dcd1d2e 100644 --- a/src/test/java/org/jabref/logic/layout/format/RisMonthTest.java +++ b/src/test/java/org/jabref/logic/layout/format/RisMonthTest.java @@ -1,6 +1,8 @@ package org.jabref.logic.layout.format; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -16,9 +18,24 @@ public void testNull() { assertEquals("", new RisMonth().format(null)); } - @Test - public void testMonth() { - assertEquals("12", new RisMonth().format("dec")); + @ParameterizedTest(name = "input={0}, formattedStr={1}") + @CsvSource({ + "jan, 01", // jan + "feb, 02", // feb + "mar, 03", // mar + "apr, 04", // apr + "may, 05", // may + "jun, 06", // jun + "jul, 07", // jul + "aug, 08", // aug + "sep, 09", // sep + "oct, 10", // oct + "nov, 11", // nov + "dec, 12", // dec + }) + public void testValidMonth(String input, String expectedResult) { + String formattedStr = new RisMonth().format(input); + assertEquals(expectedResult, formattedStr); } @Test diff --git a/src/test/java/org/jabref/logic/util/strings/StringSimilarityTest.java b/src/test/java/org/jabref/logic/util/strings/StringSimilarityTest.java new file mode 100644 index 00000000000..908727784a5 --- /dev/null +++ b/src/test/java/org/jabref/logic/util/strings/StringSimilarityTest.java @@ -0,0 +1,30 @@ +package org.jabref.logic.util.strings; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class StringSimilarityTest { + + private StringSimilarity similarityChecker = new StringSimilarity(); + + @ParameterizedTest(name = "a={0}, b={1}, result={2}") + @CsvSource({ + "'', '', true", // same empty strings + "a, a, true", // same one-letter strings + "a, '', true", // one string is empty and similarity < threshold (4) + "'', a, true", // one string is empty and similarity < threshold (4) + "abcd, '', true", // one string is empty and similarity == threshold (4) + "'', abcd, true", // one string is empty and similarity == threshold (4) + "abcde, '', false", // one string is empty and similarity > threshold (4) + "'', abcde, false", // one string is empty and similarity > threshold (4) + "abcdef, abcdef, true", // same multi-letter strings + "abcdef, abc, true", // no empty strings and similarity < threshold (4) + "abcdef, ab, true", // no empty strings and similarity == threshold (4) + "abcdef, a, false" // no empty string sand similarity > threshold (4) + }) + public void testStringSimilarity(String a, String b, String expectedResult) { + assertEquals(Boolean.valueOf(expectedResult), similarityChecker.isSimilar(a, b)); + } +}