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 #3

Merged
merged 7 commits into from
Feb 18, 2020
Merged
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
43 changes: 43 additions & 0 deletions handleArgument.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
branch 0 was visited.
branch 1 was not visited.
branch 2 was visited.
branch 3 was not visited.
branch 4 was not visited.
branch 5 was visited.
branch 6 was visited.
branch 7 was visited.
branch 8 was not visited.
branch 9 was visited.
branch 10 was visited.
branch 11 was not visited.
branch 12 was not visited.
branch 13 was visited.
branch 14 was not visited.
branch 15 was not visited.
branch 16 was not visited.
branch 17 was visited.
branch 18 was not visited.
branch 19 was visited.
branch 20 was visited.
branch 21 was visited.
branch 22 was visited.
branch 23 was visited.
branch 24 was visited.
branch 25 was visited.
branch 26 was not visited.
branch 27 was not visited.
branch 28 was not visited.
branch 29 was visited.
branch 30 was not visited.
branch 31 was visited.
branch 32 was visited.
branch 33 was visited.
branch 34 was not visited.
branch 35 was not visited.
branch 36 was not visited.
branch 37 was visited.
branch 38 was visited.
branch 39 was visited.
branch 40 was visited.
branch 41 was not visited.
0.5714285714285714
9 changes: 9 additions & 0 deletions handleArgumentRefactor.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
branch 0 was visited.
branch 1 was visited.
branch 2 was visited.
branch 3 was visited.
branch 4 was visited.
branch 5 was visited.
branch 6 was visited.
branch 7 was not visited.
0.875
43 changes: 43 additions & 0 deletions handleArgument_NEW.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
branch 0 was visited.
branch 1 was visited.
branch 2 was visited.
branch 3 was not visited.
branch 4 was not visited.
branch 5 was visited.
branch 6 was visited.
branch 7 was visited.
branch 8 was visited.
branch 9 was visited.
branch 10 was visited.
branch 11 was not visited.
branch 12 was not visited.
branch 13 was visited.
branch 14 was not visited.
branch 15 was not visited.
branch 16 was not visited.
branch 17 was visited.
branch 18 was not visited.
branch 19 was visited.
branch 20 was visited.
branch 21 was visited.
branch 22 was visited.
branch 23 was visited.
branch 24 was visited.
branch 25 was visited.
branch 26 was visited.
branch 27 was visited.
branch 28 was visited.
branch 29 was visited.
branch 30 was not visited.
branch 31 was visited.
branch 32 was visited.
branch 33 was visited.
branch 34 was not visited.
branch 35 was not visited.
branch 36 was not visited.
branch 37 was visited.
branch 38 was visited.
branch 39 was visited.
branch 40 was visited.
branch 41 was not visited.
0.6904761904761905
73 changes: 73 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[42];

private int flMode;

Expand Down Expand Up @@ -116,100 +120,169 @@ 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 {
visited[4] = true;
}
} else if (Authors.AUTHOR_ABRV.contains(key.trim().toLowerCase(Locale.ROOT))) {
visited[5] = true;
if (comp(key, "FullName")) {
visited[6] = true;
abbreviate = false;
} else if (comp(key, "Initials")) {
visited[7] = true;
abbreviate = true;
firstInitialOnly = false;
} else if (comp(key, "FirstInitial")) {
visited[8] = true;
abbreviate = true;
firstInitialOnly = true;
} else if (comp(key, "MiddleInitial")) {
visited[9] = true;
abbreviate = true;
middleInitial = true;
} else if (comp(key, "LastName")) {
visited[10] = true;
lastNameOnly = true;
} else if (comp(key, "InitialsNoSpace")) {
visited[11] = true;
abbreviate = true;
abbrSpaces = false;
} else {
visited[12] = true;
}
} else if (Authors.AUTHOR_PUNC.contains(key.trim().toLowerCase(Locale.ROOT))) {
visited[13] = true;
if (comp(key, "FullPunc")) {
visited[14] = true;
abbrDots = true;
lastFirstSeparator = ", ";
} else if (comp(key, "NoPunc")) {
visited[15] = true;
abbrDots = false;
lastFirstSeparator = " ";
} else if (comp(key, "NoComma")) {
visited[16] = true;
abbrDots = true;
lastFirstSeparator = " ";
} else if (comp(key, "NoPeriod")) {
visited[17] = true;
abbrDots = false;
lastFirstSeparator = ", ";
} else {
visited[18] = true;
}
}

// 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[19] = true;
if (comp(key, "Comma")) {
visited[20] = true;
if (setSep) {
visited[21] = true;
lastSeparator = Authors.COMMA;
} else {
visited[22] = true;
separator = Authors.COMMA;
setSep = true;
}
} else if (comp(key, "And")) {
visited[23] = true;
if (setSep) {
visited[24] = true;
lastSeparator = Authors.AND;
} else {
visited[25] = true;
separator = Authors.AND;
setSep = true;
}
} else if (comp(key, "Colon")) {
visited[26] = true;
if (setSep) {
visited[27] = true;
lastSeparator = Authors.COLON;
} else {
visited[28] = true;
separator = Authors.COLON;
setSep = true;
}
} else if (comp(key, "Semicolon")) {
visited[29] = true;
if (setSep) {
visited[30] = true;
lastSeparator = Authors.SEMICOLON;
} else {
visited[31] = true;
separator = Authors.SEMICOLON;
setSep = true;
}
} else if (comp(key, "Oxford")) {
visited[32] = true;
lastSeparator = Authors.OXFORD;
} else if (comp(key, "Amp")) {
visited[33] = true;
lastSeparator = Authors.AMP;
} else if (comp(key, "Sep") && !value.isEmpty()) {
visited[34] = true;
separator = value;
setSep = true;
} else if (comp(key, "LastSep") && !value.isEmpty()) {
visited[35] = true;
lastSeparator = value;
} else {
visited[36] = true;
}
} else if ("etal".equalsIgnoreCase(key.trim())) {
visited[37] = true;
etAlString = value;
} else if (Authors.NUMBER_PATTERN.matcher(key.trim()).matches()) {
visited[38] = true;
// Just a number:
int num = Integer.parseInt(key.trim());
if (setMaxAuthors) {
visited[39] = true;
authorNumberEtAl = num;
} else {
visited[40] = true;
maxAuthors = num;
setMaxAuthors = true;
}
} else {
visited[41] = 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
Loading