Skip to content

Commit d5d1362

Browse files
committed
. r separate InlineJavaReporter
1 parent 33dfe57 commit d5d1362

File tree

3 files changed

+83
-52
lines changed

3 files changed

+83
-52
lines changed

approvaltests-tests/src/test/java/org/approvaltests/InlineApprovalsTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.approvaltests.core.Options;
44
import org.approvaltests.inline.InlineComparator;
5+
import org.approvaltests.inline.InlineJavaReporter;
56
import org.approvaltests.reporters.*;
67
import org.junit.jupiter.api.Test;
78
import org.junit.jupiter.api.condition.EnabledOnJre;
@@ -73,7 +74,7 @@ public void testyMctest(int foo) {
7374
}
7475
""");
7576
Approvals.verifyAll("Substitution", inputs, i -> "******\n" + i + "\nBecomes:\n"
76-
+ InlineComparator.createNewReceivedFileText(i, "1\n2", "testyMctest"));
77+
+ InlineJavaReporter.createNewReceivedFileText(i, "1\n2", "testyMctest"));
7778
}
7879
@Test
7980
@UseReporter(QuietReporter.class)

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

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,14 @@
1414

1515
public class InlineComparator implements ApprovalNamer, ApprovalFailureReporter
1616
{
17-
private final String sourceFilePath;
18-
private final StackTraceNamer stackTraceNamer;
17+
private final InlineJavaReporter inlineJavaReporter;
1918
private String expected;
20-
private final ApprovalFailureReporter reporter;
2119
private File approvedFile;
2220
private File receivedFile;
2321
public InlineComparator(String expected, ApprovalFailureReporter reporter)
2422
{
2523
this.expected = expected;
26-
this.reporter = reporter;
27-
stackTraceNamer = new StackTraceNamer();
28-
sourceFilePath = stackTraceNamer.getSourceFilePath();
24+
inlineJavaReporter = new InlineJavaReporter(reporter);
2925
}
3026
@Override
3127
public File getApprovedFile(String extensionWithDot)
@@ -73,61 +69,21 @@ public String getApprovalName()
7369
@Override
7470
public String getSourceFilePath()
7571
{
76-
return sourceFilePath;
72+
return inlineJavaReporter.getSourceFilePath();
7773
}
7874
@Override
7975
public boolean report(String received, String approved)
8076
{
81-
String sourceFile = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java";
82-
String newSource = createReceived(FileUtils.readFile(received));
83-
return reporter.report(newSource, sourceFile);
77+
return inlineJavaReporter.report(received, approved);
8478
}
8579
private String createReceived(String actual)
8680
{
87-
String file = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java";
88-
String received = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".received.txt";
89-
String text = FileUtils.readFile(file);
90-
String fullText = createNewReceivedFileText(text, actual, this.stackTraceNamer.getInfo().getMethodName());
91-
FileUtils.writeFile(new File(received), fullText);
92-
return received;
93-
}
94-
public static String createNewReceivedFileText(String text, String actual, String methodName)
95-
{
96-
text = text.replaceAll("\r\n", "\n");
97-
int start = text.indexOf("void " + methodName + "(");
98-
start = text.indexOf("{", start);
99-
int next = text.indexOf("\n", start);
100-
int end = text.indexOf("}", next);
101-
int endString = text.indexOf("\"\"\";", next);
102-
String part1 = text.substring(0, next);
103-
String part2 = null;
104-
if (0 < endString && endString < end)
105-
{
106-
// find next newline
107-
endString = text.indexOf("\n", endString);
108-
part2 = text.substring(endString + 1);
109-
}
110-
else
111-
{
112-
part2 = text.substring(next + 1);
113-
}
114-
String fullText = String.format("%s\n\t\tvar expected = \"\"\"\n%s\t\t\"\"\";\n%s", part1, indent(actual),
115-
part2);
116-
return fullText;
117-
}
118-
public static String indent(String actual)
119-
{
120-
String[] split = actual.split("\n");
121-
String output = "";
122-
for (String line : split)
123-
{
124-
output += "\t\t" + line + "\n";
125-
}
126-
return output;
81+
return inlineJavaReporter.createReceived(actual);
12782
}
83+
12884
public Options setForOptions(Options options)
12985
{
130-
if (reporter != null)
86+
if (inlineJavaReporter.reporter != null)
13187
{
13288
options = options.withReporter(this);
13389
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.approvaltests.inline;
2+
3+
import com.spun.util.io.FileUtils;
4+
import org.approvaltests.core.ApprovalFailureReporter;
5+
import org.approvaltests.namer.StackTraceNamer;
6+
7+
import java.io.File;
8+
9+
public class InlineJavaReporter implements ApprovalFailureReporter
10+
{
11+
public final String sourceFilePath;
12+
public final StackTraceNamer stackTraceNamer;
13+
public final ApprovalFailureReporter reporter;
14+
public InlineJavaReporter(ApprovalFailureReporter reporter)
15+
{
16+
this.reporter = reporter;
17+
this.stackTraceNamer = new StackTraceNamer();
18+
this.sourceFilePath = stackTraceNamer.getSourceFilePath();
19+
}
20+
public String getSourceFilePath()
21+
{
22+
return sourceFilePath;
23+
}
24+
@Override
25+
public boolean report(String received, String approved)
26+
{
27+
String sourceFile = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java";
28+
String newSource = createReceived(FileUtils.readFile(received));
29+
return reporter.report(newSource, sourceFile);
30+
}
31+
public String createReceived(String actual)
32+
{
33+
String file = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java";
34+
String received = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".received.txt";
35+
String text = FileUtils.readFile(file);
36+
String fullText = createNewReceivedFileText(text, actual, this.stackTraceNamer.getInfo().getMethodName());
37+
FileUtils.writeFile(new File(received), fullText);
38+
return received;
39+
}
40+
public static String createNewReceivedFileText(String text, String actual, String methodName)
41+
{
42+
text = text.replaceAll("\r\n", "\n");
43+
int start = text.indexOf("void " + methodName + "(");
44+
start = text.indexOf("{", start);
45+
int next = text.indexOf("\n", start);
46+
int end = text.indexOf("}", next);
47+
int endString = text.indexOf("\"\"\";", next);
48+
String part1 = text.substring(0, next);
49+
String part2 = null;
50+
if (0 < endString && endString < end)
51+
{
52+
// find next newline
53+
endString = text.indexOf("\n", endString);
54+
part2 = text.substring(endString + 1);
55+
}
56+
else
57+
{
58+
part2 = text.substring(next + 1);
59+
}
60+
String fullText = String.format("%s\n\t\tvar expected = \"\"\"\n%s\t\t\"\"\";\n%s", part1, indent(actual),
61+
part2);
62+
return fullText;
63+
}
64+
public static String indent(String actual)
65+
{
66+
String[] split = actual.split("\n");
67+
String output = "";
68+
for (String line : split)
69+
{
70+
output += "\t\t" + line + "\n";
71+
}
72+
return output;
73+
}
74+
}

0 commit comments

Comments
 (0)