Skip to content

Commit

Permalink
. r separate InlineJavaReporter
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEckart committed Nov 20, 2023
1 parent 33dfe57 commit d5d1362
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.approvaltests.core.Options;
import org.approvaltests.inline.InlineComparator;
import org.approvaltests.inline.InlineJavaReporter;
import org.approvaltests.reporters.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnJre;
Expand Down Expand Up @@ -73,7 +74,7 @@ public void testyMctest(int foo) {
}
""");
Approvals.verifyAll("Substitution", inputs, i -> "******\n" + i + "\nBecomes:\n"
+ InlineComparator.createNewReceivedFileText(i, "1\n2", "testyMctest"));
+ InlineJavaReporter.createNewReceivedFileText(i, "1\n2", "testyMctest"));
}
@Test
@UseReporter(QuietReporter.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,14 @@

public class InlineComparator implements ApprovalNamer, ApprovalFailureReporter
{
private final String sourceFilePath;
private final StackTraceNamer stackTraceNamer;
private final InlineJavaReporter inlineJavaReporter;
private String expected;
private final ApprovalFailureReporter reporter;
private File approvedFile;
private File receivedFile;
public InlineComparator(String expected, ApprovalFailureReporter reporter)
{
this.expected = expected;
this.reporter = reporter;
stackTraceNamer = new StackTraceNamer();
sourceFilePath = stackTraceNamer.getSourceFilePath();
inlineJavaReporter = new InlineJavaReporter(reporter);
}
@Override
public File getApprovedFile(String extensionWithDot)
Expand Down Expand Up @@ -73,61 +69,21 @@ public String getApprovalName()
@Override
public String getSourceFilePath()
{
return sourceFilePath;
return inlineJavaReporter.getSourceFilePath();
}
@Override
public boolean report(String received, String approved)
{
String sourceFile = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java";
String newSource = createReceived(FileUtils.readFile(received));
return reporter.report(newSource, sourceFile);
return inlineJavaReporter.report(received, approved);
}
private String createReceived(String actual)
{
String file = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java";
String received = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".received.txt";
String text = FileUtils.readFile(file);
String fullText = createNewReceivedFileText(text, actual, this.stackTraceNamer.getInfo().getMethodName());
FileUtils.writeFile(new File(received), fullText);
return received;
}
public static String createNewReceivedFileText(String text, String actual, String methodName)
{
text = text.replaceAll("\r\n", "\n");
int start = text.indexOf("void " + methodName + "(");
start = text.indexOf("{", start);
int next = text.indexOf("\n", start);
int end = text.indexOf("}", next);
int endString = text.indexOf("\"\"\";", next);
String part1 = text.substring(0, next);
String part2 = null;
if (0 < endString && endString < end)
{
// find next newline
endString = text.indexOf("\n", endString);
part2 = text.substring(endString + 1);
}
else
{
part2 = text.substring(next + 1);
}
String fullText = String.format("%s\n\t\tvar expected = \"\"\"\n%s\t\t\"\"\";\n%s", part1, indent(actual),
part2);
return fullText;
}
public static String indent(String actual)
{
String[] split = actual.split("\n");
String output = "";
for (String line : split)
{
output += "\t\t" + line + "\n";
}
return output;
return inlineJavaReporter.createReceived(actual);
}

public Options setForOptions(Options options)
{
if (reporter != null)
if (inlineJavaReporter.reporter != null)
{
options = options.withReporter(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.approvaltests.inline;

import com.spun.util.io.FileUtils;
import org.approvaltests.core.ApprovalFailureReporter;
import org.approvaltests.namer.StackTraceNamer;

import java.io.File;

public class InlineJavaReporter implements ApprovalFailureReporter
{
public final String sourceFilePath;
public final StackTraceNamer stackTraceNamer;
public final ApprovalFailureReporter reporter;
public InlineJavaReporter(ApprovalFailureReporter reporter)
{
this.reporter = reporter;
this.stackTraceNamer = new StackTraceNamer();
this.sourceFilePath = stackTraceNamer.getSourceFilePath();
}
public String getSourceFilePath()
{
return sourceFilePath;
}
@Override
public boolean report(String received, String approved)
{
String sourceFile = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java";
String newSource = createReceived(FileUtils.readFile(received));
return reporter.report(newSource, sourceFile);
}
public String createReceived(String actual)
{
String file = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java";
String received = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".received.txt";
String text = FileUtils.readFile(file);
String fullText = createNewReceivedFileText(text, actual, this.stackTraceNamer.getInfo().getMethodName());
FileUtils.writeFile(new File(received), fullText);
return received;
}
public static String createNewReceivedFileText(String text, String actual, String methodName)
{
text = text.replaceAll("\r\n", "\n");
int start = text.indexOf("void " + methodName + "(");
start = text.indexOf("{", start);
int next = text.indexOf("\n", start);
int end = text.indexOf("}", next);
int endString = text.indexOf("\"\"\";", next);
String part1 = text.substring(0, next);
String part2 = null;
if (0 < endString && endString < end)
{
// find next newline
endString = text.indexOf("\n", endString);
part2 = text.substring(endString + 1);
}
else
{
part2 = text.substring(next + 1);
}
String fullText = String.format("%s\n\t\tvar expected = \"\"\"\n%s\t\t\"\"\";\n%s", part1, indent(actual),
part2);
return fullText;
}
public static String indent(String actual)
{
String[] split = actual.split("\n");
String output = "";
for (String line : split)
{
output += "\t\t" + line + "\n";
}
return output;
}
}

0 comments on commit d5d1362

Please sign in to comment.