Skip to content

Commit 746001a

Browse files
committed
- B inline Java reporter detects expected and is prettier
1 parent f1ae222 commit 746001a

File tree

3 files changed

+84
-75
lines changed

3 files changed

+84
-75
lines changed

approvaltests-tests/src/test/java/org/approvaltests/InlineApprovalsTest.testCreateReceivedFileText.approved.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Becomes:
9898
Becomes:
9999
@Test
100100
void testyMctest() {
101-
Customer customer = Customer.create()
101+
Customer customer = Customer.create()
102102
.existingCustomer()
103103
.speakingEnglish()
104104
.build();

approvaltests/src/main/java/org/approvaltests/inline/CodeParts.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public static CodeParts splitCode(String text, String methodName) {
3737
return codeParts;
3838
}
3939

40+
public String getFullCode() {
41+
return before + "\n" +
42+
method + "\n" +
43+
after;
44+
}
45+
4046
private static String extractLeadingWhitespace(String text)
4147
{
4248
Pattern pattern = Pattern.compile("^\\s+");

approvaltests/src/main/java/org/approvaltests/inline/InlineJavaReporter.java

Lines changed: 77 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -8,82 +8,85 @@
88
import java.util.regex.Matcher;
99
import java.util.regex.Pattern;
1010

11-
public class InlineJavaReporter implements ApprovalFailureReporter
12-
{
13-
private final String sourceFilePath;
14-
private final StackTraceNamer stackTraceNamer;
15-
private final ApprovalFailureReporter reporter;
16-
public InlineJavaReporter(ApprovalFailureReporter reporter)
17-
{
18-
this.reporter = reporter;
19-
this.stackTraceNamer = new StackTraceNamer();
20-
this.sourceFilePath = stackTraceNamer.getSourceFilePath();
21-
}
22-
public String getSourceFilePath()
23-
{
24-
return sourceFilePath;
25-
}
26-
@Override
27-
public boolean report(String received, String approved)
28-
{
29-
String sourceFile = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java";
30-
String newSource = createReceived(FileUtils.readFile(received));
31-
return reporter.report(newSource, sourceFile);
32-
}
33-
public String createReceived(String actual)
34-
{
35-
String file = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java";
36-
String received = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".received.txt";
37-
String text = FileUtils.readFile(file);
38-
String fullText = createNewReceivedFileText(text, actual, this.stackTraceNamer.getInfo().getMethodName());
39-
FileUtils.writeFile(new File(received), fullText);
40-
return received;
41-
}
42-
public static String createNewReceivedFileText(String text, String actual, String methodName)
43-
{
44-
text = text.replaceAll("\r\n", "\n");
45-
CodeParts codeParts = CodeParts.splitCode(text, methodName);
46-
int start = text.indexOf("void " + methodName + "(");
47-
int startOfLine = text.substring(0, start).lastIndexOf("\n") + 1;
48-
String line = text.substring(startOfLine, start);
49-
String tab = extractLeadingWhitespace(line);
50-
start = text.indexOf("{", start);
51-
int codeBeforeExpected = text.indexOf("\n", start);
52-
int end = text.indexOf("}", codeBeforeExpected);
53-
// if there is an expected = then set the code before to the beginning of this line
54-
int endString = text.indexOf("\"\"\";", codeBeforeExpected);
55-
String part1 = text.substring(0, codeBeforeExpected);
56-
String part2 = null;
57-
if (0 < endString && endString < end)
58-
{
59-
// find next newline
60-
endString = text.indexOf("\n", endString);
61-
part2 = text.substring(endString + 1);
11+
public class InlineJavaReporter implements ApprovalFailureReporter {
12+
private final String sourceFilePath;
13+
private final StackTraceNamer stackTraceNamer;
14+
private final ApprovalFailureReporter reporter;
15+
16+
public InlineJavaReporter(ApprovalFailureReporter reporter) {
17+
this.reporter = reporter;
18+
this.stackTraceNamer = new StackTraceNamer();
19+
this.sourceFilePath = stackTraceNamer.getSourceFilePath();
20+
}
21+
22+
public String getSourceFilePath() {
23+
return sourceFilePath;
24+
}
25+
26+
@Override
27+
public boolean report(String received, String approved) {
28+
String sourceFile = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java";
29+
String newSource = createReceived(FileUtils.readFile(received));
30+
return reporter.report(newSource, sourceFile);
31+
}
32+
33+
public String createReceived(String actual) {
34+
String file = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java";
35+
String received = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".received.txt";
36+
String text = FileUtils.readFile(file);
37+
String fullText = createNewReceivedFileText(text, actual, this.stackTraceNamer.getInfo().getMethodName());
38+
FileUtils.writeFile(new File(received), fullText);
39+
return received;
40+
}
41+
42+
public static String createNewReceivedFileText(String text, String actual, String methodName) {
43+
text = text.replaceAll("\r\n", "\n");
44+
CodeParts codeParts = CodeParts.splitCode(text, methodName);
45+
if (codeParts.method.contains("expected = \"\"\"")) {
46+
replaceExpected(codeParts, actual);
47+
} else {
48+
addExpected(codeParts, actual);
49+
}
50+
return codeParts.getFullCode();
51+
}
52+
53+
private static void addExpected(CodeParts codeParts, String actual) {
54+
int start = codeParts.method.indexOf("{") + 2;
55+
String before = codeParts.method.substring(0, start);
56+
String after = codeParts.method.substring(start);
57+
codeParts.method = before + getExpected(actual, codeParts.tab) + after;
58+
}
59+
60+
private static String getExpected(String actual, String tab) {
61+
return String.format("%s%svar expected = \"\"\"\n%s%s%s%s\"\"\";\n",
62+
tab, tab, indent(actual, tab), tab, tab, tab);
63+
}
64+
65+
private static void replaceExpected(CodeParts codeParts, String actual) {
66+
int start = codeParts.method.indexOf("expected = \"\"\"");
67+
start = codeParts.method.substring(0, start).lastIndexOf("\n") + 1;
68+
int end = codeParts.method.indexOf("\"\"\";");
69+
end = codeParts.method.indexOf("\n", end) + 1;
70+
String before = codeParts.method.substring(0, start);
71+
String after = codeParts.method.substring(end);
72+
codeParts.method = before + getExpected(actual, codeParts.tab) + after;
6273
}
63-
else
64-
{
65-
part2 = text.substring(codeBeforeExpected + 1);
74+
75+
public static String indent(String actual, String tab) {
76+
String[] split = actual.split("\n");
77+
String output = "";
78+
for (String line : split) {
79+
output += tab + tab + tab + line + "\n";
80+
}
81+
return output;
6682
}
67-
return String.format("%s\n%s%svar expected = \"\"\"\n%s%s%s%s\"\"\";\n%s", part1, tab, tab,
68-
indent(actual, tab), tab, tab, tab, part2);
69-
}
7083

71-
public static String indent(String actual, String tab)
72-
{
73-
String[] split = actual.split("\n");
74-
String output = "";
75-
for (String line : split)
76-
{
77-
output += tab + tab + tab + line + "\n";
84+
private static String extractLeadingWhitespace(String text) {
85+
Pattern pattern = Pattern.compile("^\\s+");
86+
Matcher matcher = pattern.matcher(text);
87+
if (matcher.find()) {
88+
return matcher.group();
89+
}
90+
return "\t";
7891
}
79-
return output;
80-
}
81-
private static String extractLeadingWhitespace(String text)
82-
{
83-
Pattern pattern = Pattern.compile("^\\s+");
84-
Matcher matcher = pattern.matcher(text);
85-
if (matcher.find())
86-
{ return matcher.group(); }
87-
return "\t";
88-
}
8992
}

0 commit comments

Comments
 (0)