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

Added branch coverage tool and unit tests for Authors::handleArgument #5950

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/main/java/org/jabref/logic/layout/format/Authors.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 = ", ";
}
Expand All @@ -160,56 +181,99 @@ private void handleArgument(String key, String value) {
// AuthorSep = [Comma | And | Colon | Semicolon | sep=<string>]
// AuthorLastSep = [And | Comma | Colon | Semicolon | Amp | Oxford | lastsep=<string>]
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 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;
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("Did not find the path");
}

// SHOULD BE: 58%
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jabref/model/entry/Author.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/org/jabref/logic/layout/format/AuthorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down