Skip to content

Commit

Permalink
Add branch coverage tool, unit tests and refactoring for Authors::han…
Browse files Browse the repository at this point in the history
…dleArgument (#3)

* Added branch coverage tool to Authors::handleArgument
  including results before and after added tests

* Add 4 unit tests to Authors::handleArgument in AuthorsTest
  increase branch coverage from 58% to 73%

* Refactor Authors::handleArgument into a new file, AuthorsRefactor and unit tests file, AuthorsRefactorTest
  reduce cyclomatic complexity from 36 to 7 (larger than 35%)

Co-authored-by: Simon Sirak <[email protected]>
  • Loading branch information
yiiju and simonsirak authored Feb 18, 2020
1 parent ec59705 commit 9330e7e
Show file tree
Hide file tree
Showing 8 changed files with 802 additions and 0 deletions.
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

0 comments on commit 9330e7e

Please sign in to comment.