Skip to content

Commit

Permalink
- F InlineOptions.semiAutomaticWithPreviousApproved()
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEckart committed May 20, 2024
1 parent 2f264d8 commit 48e5460
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,40 @@ private static Mutable<String> hijackInlineReporter(Options options)
reporter.createNewReceivedFileText = (s, a, m) -> result.set(a);
return result;
}
}
@Test
void testSemiAutomaticMessage()
{
var expected = """
41
***** DELETE ME TO APPROVE *****
""";
var options = new Options().inline(expected, InlineOptions.semiAutomatic());
try
{
Approvals.verify("41", options);
}
catch (Throwable e)
{
}
Approvals.verify(expected);
}
@Test
void testSemiAutomaticWithPreviousApproved()
{
var expected = """
42
***** DELETE ME TO APPROVE *****
vvvvv PREVIOUS RESULT vvvvv
41
""";
var options = new Options().inline(expected, InlineOptions.semiAutomaticWithPreviousApproved());
try
{
Approvals.verify("42", options);
}
catch (Throwable e)
{
}
Approvals.verify(expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
41
***** DELETE ME TO APPROVE *****
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
42
***** DELETE ME TO APPROVE *****
vvvvv PREVIOUS RESULT vvvvv
41
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.spun.util.StringUtils;
import com.spun.util.io.FileUtils;
import org.apache.commons.lang3.function.TriFunction;
import org.approvaltests.core.ApprovalFailureReporter;
import org.approvaltests.core.ApprovalReporterWithCleanUp;
import org.approvaltests.namer.StackTraceNamer;
import org.lambda.functions.Function2;
import org.lambda.functions.Function3;

import java.io.File;
Expand All @@ -16,16 +16,21 @@ public class InlineJavaReporter implements ApprovalFailureReporter, ApprovalRepo
{
private final String sourceFilePath;
private final StackTraceNamer stackTraceNamer;
private Function2<String, String, String> footerCreator;
public ApprovalFailureReporter reporter;
private final String additionalLines;
private String additionalLines;
public Function3<String, String, String, String> createNewReceivedFileText;
public InlineJavaReporter(ApprovalFailureReporter reporter, boolean addApprovalLine)
public InlineJavaReporter(ApprovalFailureReporter reporter, Function2<String, String, String> footerCreator)
{
this.reporter = reporter;
this.stackTraceNamer = new StackTraceNamer();
this.sourceFilePath = stackTraceNamer.getSourceFilePath();
this.additionalLines = addApprovalLine ? "***** DELETE ME TO APPROVE *****\n" : "";
this.createNewReceivedFileText = InlineJavaReporter::createNewReceivedFileText;
if (footerCreator == null)
{
footerCreator = (source, actual) -> "";
}
this.footerCreator = footerCreator;
}
public String getSourceFilePath()
{
Expand All @@ -34,6 +39,7 @@ public String getSourceFilePath()
@Override
public boolean report(String received, String approved)
{
additionalLines = footerCreator.call(received, approved);
String sourceFile = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java";
String newSource = createReceived(FileUtils.readFile(received));
return reporter.report(newSource, sourceFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package org.approvaltests.inline;

import com.spun.util.io.FileUtils;
import org.approvaltests.core.Options;
import org.approvaltests.reporters.AutoApproveReporter;

public interface InlineOptions
{
String DELETE_ME_TO_APPROVE = "***** DELETE ME TO APPROVE *****\n";
String PREVIOUS_RESULT = "vvvvv PREVIOUS RESULT vvvvv\n";
Options apply(Options options);
public static InlineOptions showCode(boolean doShowCode)
{
if (doShowCode)
{
return options -> options.withReporter(new InlineJavaReporter(options.getReporter(), false));
return options -> options.withReporter(new InlineJavaReporter(options.getReporter(), null));
}
else
{
Expand All @@ -19,10 +22,31 @@ public static InlineOptions showCode(boolean doShowCode)
}
public static InlineOptions automatic()
{
return options -> options.withReporter(new InlineJavaReporter(new AutoApproveReporter(), false));
return options -> options.withReporter(new InlineJavaReporter(new AutoApproveReporter(), null));
}
public static InlineOptions semiAutomatic()
{
return options -> options.withReporter(new InlineJavaReporter(new AutoApproveReporter(), true));
return options -> options
.withReporter(new InlineJavaReporter(new AutoApproveReporter(), (x, y) -> DELETE_ME_TO_APPROVE));
}
public static InlineOptions semiAutomaticWithPreviousApproved()
{
return options -> options.withReporter(
new InlineJavaReporter(new AutoApproveReporter(), InlineOptions::createPreviousCaptureFooter));
}
static String createPreviousCaptureFooter(String receivedPath, String approvedPath)
{
String approvedText = FileUtils.readFile((approvedPath));
approvedText = approvedText.substring(0, approvedText.lastIndexOf("\n"));
int previousResultIndex = approvedText.lastIndexOf(PREVIOUS_RESULT);
if (previousResultIndex != -1)
{
approvedText = approvedText.substring(previousResultIndex + PREVIOUS_RESULT.length());
}
String receivedText = FileUtils.readFile((receivedPath));
receivedText = receivedText.substring(0, receivedText.lastIndexOf("\n"));
if (!receivedText.equals(approvedText))
{ return DELETE_ME_TO_APPROVE + PREVIOUS_RESULT + approvedText; }
return "";
}
}

0 comments on commit 48e5460

Please sign in to comment.