From 209e73b30ff6806db2abfd20b705c9a39eecc0a0 Mon Sep 17 00:00:00 2001 From: Simon Sirak Date: Wed, 12 Feb 2020 11:57:02 +0100 Subject: [PATCH 1/2] Added branch coverage to handleArgument --- .../jabref/logic/layout/format/Authors.java | 60 +++++++++++++++++++ .../java/org/jabref/model/entry/Author.java | 1 + 2 files changed, 61 insertions(+) diff --git a/src/main/java/org/jabref/logic/layout/format/Authors.java b/src/main/java/org/jabref/logic/layout/format/Authors.java index b1514623c87..7fec249670b 100644 --- a/src/main/java/org/jabref/logic/layout/format/Authors.java +++ b/src/main/java/org/jabref/logic/layout/format/Authors.java @@ -9,6 +9,9 @@ import org.jabref.model.entry.Author; import org.jabref.model.entry.AuthorList; +import java.io.FileWriter; +import java.io.BufferedWriter; +import java.io.File; /** * Versatile author name formatter that takes arguments to control the formatting style. */ @@ -77,6 +80,7 @@ public class Authors extends AbstractParamLayoutFormatter { private static final String SEMICOLON = "; "; private static final String AND = " and "; private static final String OXFORD = ", and "; + private static boolean[] visited = new boolean[38]; private int flMode; @@ -116,42 +120,59 @@ public void setArgument(String arg) { private void handleArgument(String key, String value) { if (Authors.AUTHOR_ORDER.contains(key.trim().toLowerCase(Locale.ROOT))) { + visited[0] = true; if (comp(key, "FirstFirst")) { + visited[1] = true; flMode = Authors.FIRST_FIRST; } else if (comp(key, "LastFirst")) { + visited[2] = true; flMode = Authors.LAST_FIRST; } else if (comp(key, "LastFirstFirstFirst")) { + visited[3] = true; flMode = Authors.LF_FF; } } else if (Authors.AUTHOR_ABRV.contains(key.trim().toLowerCase(Locale.ROOT))) { + visited[4] = true; if (comp(key, "FullName")) { + visited[5] = true; abbreviate = false; } else if (comp(key, "Initials")) { + visited[6] = true; abbreviate = true; firstInitialOnly = false; } else if (comp(key, "FirstInitial")) { + visited[7] = true; abbreviate = true; firstInitialOnly = true; } else if (comp(key, "MiddleInitial")) { + visited[8] = true; abbreviate = true; middleInitial = true; } else if (comp(key, "LastName")) { + visited[9] = true; lastNameOnly = true; } else if (comp(key, "InitialsNoSpace")) { + visited[10] = true; abbreviate = true; abbrSpaces = false; } + visited[11] = true; } else if (Authors.AUTHOR_PUNC.contains(key.trim().toLowerCase(Locale.ROOT))) { + visited[12] = true; if (comp(key, "FullPunc")) { + visited[13] = true; abbrDots = true; lastFirstSeparator = ", "; } else if (comp(key, "NoPunc")) { + visited[14] = true; abbrDots = false; lastFirstSeparator = " "; } else if (comp(key, "NoComma")) { + visited[15] = true; abbrDots = true; lastFirstSeparator = " "; } else if (comp(key, "NoPeriod")) { + visited[16] = true; abbrDots = false; lastFirstSeparator = ", "; } @@ -160,56 +181,95 @@ private void handleArgument(String key, String value) { // AuthorSep = [Comma | And | Colon | Semicolon | sep=] // AuthorLastSep = [And | Comma | Colon | Semicolon | Amp | Oxford | lastsep=] else if (Authors.SEPARATORS.contains(key.trim().toLowerCase(Locale.ROOT)) || Authors.LAST_SEPARATORS.contains(key.trim().toLowerCase(Locale.ROOT))) { + visited[17] = true; if (comp(key, "Comma")) { + visited[18] = true; if (setSep) { + visited[19] = true; lastSeparator = Authors.COMMA; } else { + visited[20] = true; separator = Authors.COMMA; setSep = true; } } else if (comp(key, "And")) { + visited[21] = true; if (setSep) { + visited[22] = true; lastSeparator = Authors.AND; } else { + visited[23] = true; separator = Authors.AND; setSep = true; } } else if (comp(key, "Colon")) { + visited[24] = true; if (setSep) { + visited[25] = true; lastSeparator = Authors.COLON; } else { + visited[26] = true; separator = Authors.COLON; setSep = true; } } else if (comp(key, "Semicolon")) { + visited[27] = true; if (setSep) { + visited[28] = true; lastSeparator = Authors.SEMICOLON; } else { + visited[29] = true; separator = Authors.SEMICOLON; setSep = true; } } else if (comp(key, "Oxford")) { + visited[30] = true; lastSeparator = Authors.OXFORD; } else if (comp(key, "Amp")) { + visited[31] = true; lastSeparator = Authors.AMP; } else if (comp(key, "Sep") && !value.isEmpty()) { + visited[32] = true; separator = value; setSep = true; } else if (comp(key, "LastSep") && !value.isEmpty()) { + visited[33] = true; lastSeparator = value; } } else if ("etal".equalsIgnoreCase(key.trim())) { + visited[34] = true; etAlString = value; } else if (Authors.NUMBER_PATTERN.matcher(key.trim()).matches()) { + visited[35] = true; // Just a number: int num = Integer.parseInt(key.trim()); if (setMaxAuthors) { + visited[36] = true; authorNumberEtAl = num; } else { + visited[37] = true; maxAuthors = num; setMaxAuthors = true; } } + + try { + File f = new File("/tmp/handleArgument.txt"); + + BufferedWriter bw = new BufferedWriter(new FileWriter(f)); + double frac = 0; + for(int i = 0; i < visited.length; ++i) { + frac += (visited[i] ? 1 : 0); + bw.write("branch " + i + " was " + (visited[i] ? " visited." : " not visited.") + "\n"); + } + + bw.write("" + frac/visited.length); + bw.close(); + } catch (Exception e) { + System.err.println("ye"); + } + + // SHOULD BE: 58% } /** diff --git a/src/main/java/org/jabref/model/entry/Author.java b/src/main/java/org/jabref/model/entry/Author.java index ed48a62f09a..8d9ce771f50 100644 --- a/src/main/java/org/jabref/model/entry/Author.java +++ b/src/main/java/org/jabref/model/entry/Author.java @@ -56,6 +56,7 @@ public Author(String first, String firstabbr, String von, String last, String jr } public static String addDotIfAbbreviation(String name) { + //static boolean[] branches = new boolean[15]; if ((name == null) || name.isEmpty()) { return name; } From 45322d055765c6b1236b32d00495e3be52432fc4 Mon Sep 17 00:00:00 2001 From: yiiju Date: Sat, 15 Feb 2020 15:51:00 +0100 Subject: [PATCH 2/2] Add 4 unit tests in AuthorsTest --- .../jabref/logic/layout/format/Authors.java | 8 ++- .../logic/layout/format/AuthorsTest.java | 57 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jabref/logic/layout/format/Authors.java b/src/main/java/org/jabref/logic/layout/format/Authors.java index 7fec249670b..a31be9fdfe0 100644 --- a/src/main/java/org/jabref/logic/layout/format/Authors.java +++ b/src/main/java/org/jabref/logic/layout/format/Authors.java @@ -254,7 +254,11 @@ else if (Authors.SEPARATORS.contains(key.trim().toLowerCase(Locale.ROOT)) || Aut } try { - File f = new File("/tmp/handleArgument.txt"); + File directory = new File("/Temp"); + if (!directory.exists()){ + directory.mkdir(); + } + File f = new File(directory + "/handleArgument.txt"); BufferedWriter bw = new BufferedWriter(new FileWriter(f)); double frac = 0; @@ -266,7 +270,7 @@ else if (Authors.SEPARATORS.contains(key.trim().toLowerCase(Locale.ROOT)) || Aut bw.write("" + frac/visited.length); bw.close(); } catch (Exception e) { - System.err.println("ye"); + System.err.println("Did not find the path"); } // SHOULD BE: 58% 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..883318989a7 100644 --- a/src/test/java/org/jabref/logic/layout/format/AuthorsTest.java +++ b/src/test/java/org/jabref/logic/layout/format/AuthorsTest.java @@ -61,6 +61,50 @@ public void testStandardUsageSix() { a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper and Chuck Chuckles")); } + /** + * Test the FirstFirst method in authors order. + * setArgument() will pass the String into handleArgument() to set flMode. + * Increase branch coverage from 58% to 61%. + */ + @Test + public void testStandardUsageFirstFirst() { + ParamLayoutFormatter a = new Authors(); + a.setArgument("FirstFirst, Comma, Comma"); + assertEquals("B. C. Bruce, C. Manson, J. Jumper", + a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper")); + } + + /** + * Test the And method in separators of authors. + * setArgument() will pass the String into handleArgument() to set separator and lastSeparator. + * Increase branch coverage from 58% to 66%. + */ + @Test + public void testStandardUsageAnd() { + ParamLayoutFormatter a = new Authors(); + a.setArgument("fullname, LastFirst, Comma, And"); + assertEquals("Bruce, Bob Croydon, Jumper, Jolly and Manson, Charles", + a.format("Bob Croydon Bruce and Jolly Jumper and Charles Manson")); + + a = new Authors(); + a.setArgument("fullname, LastFirst, Add, And"); + assertEquals("Bruce, Bob Croydon and Jumper, Jolly and Manson, Charles", + a.format("Bob Croydon Bruce and Jolly Jumper and Charles Manson")); + } + + /** + * Test the Colon method in separators of authors. + * setArgument() will pass the String into handleArgument() to set separator and lastSeparator. + * Increase branch coverage from 58% to 66% + */ + @Test + public void testStandardUsageColon() { + ParamLayoutFormatter a = new Authors(); + a.setArgument("fullname, LastFirst, Colon, Colon"); + assertEquals("Bruce, Bob Croydon: Jumper, Jolly: Manson, Charles", + a.format("Bob Croydon Bruce and Jolly Jumper and Charles Manson")); + } + @Test public void testSpecialEtAl() { ParamLayoutFormatter a = new Authors(); @@ -123,6 +167,19 @@ public void testMiddleInitial() { a.format("Bruce, Bob Croydon and Charles Kermit von Manson and Jumper, Jolly")); } + /** + * Test the FirstInitial method in abbreviation of authors. + * setArgument() will pass the String into handleArgument() to set abbreviate. + * Increase branch coverage from 58% to 61%. + */ + @Test + public void testFirstInitial() { + ParamLayoutFormatter a = new Authors(); + a.setArgument("FirstInitial"); + assertEquals("B. Bruce, C. von Manson and J. Jumper", + a.format("Bruce, Bob Croydon and Charles Kermit von Manson and Jumper, Jolly")); + } + @Test public void testNoPeriod() { ParamLayoutFormatter a = new Authors();