Skip to content

Commit

Permalink
Merge branch 'applesauce'
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEckart committed Dec 9, 2024
2 parents d7f717e + 547c0eb commit 64ac582
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.spun.util.io.FileUtils;
import com.spun.util.tests.StackTraceReflectionResult;
import com.spun.util.tests.TestUtils;
import org.approvaltests.ApprovalSettings;
import org.approvaltests.Approvals;
import org.approvaltests.core.Options;
import org.approvaltests.core.VerifyResult;
Expand Down Expand Up @@ -69,4 +70,17 @@ public void testCustomApprover()
Approvals.verify(new FileApprover(writer, namer, approveEverything));
// end-snippet
}
@Test
void testCustomError() throws Exception
{
var expected = """
java.lang.AssertionError: Custom message
""";
try (AutoCloseable old = Approvals.settings()
.registerErrorGenerator((received, approved) -> new AssertionError("Custom message")))
{
FileApprover fileApprover = new FileApprover(new File("a.txt"), new File("b.txt"), null, null);
Approvals.verifyException(fileApprover::fail, new Options().inline(expected));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.spun.util.introspection.Caller;
import org.approvaltests.approvers.FileApprover;
import org.lambda.functions.Function2;
import org.lambda.query.Queryable;

import java.io.File;
Expand All @@ -22,4 +23,8 @@ public void allowMultipleVerifyCallsForThisClass()
String className = caller.getClassName().replace('.', File.separatorChar);
FileApprover.tracker.addAllowedDuplicates(f -> f.contains(className));
}
public static AutoCloseable registerErrorGenerator(Function2<String, String, Error> errorGenerator)
{
return FileApprover.registerErrorGenerator(errorGenerator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,35 @@

public class FileApprover implements ApprovalApprover
{
private File received;
private File approved;
private final ApprovalWriter writer;
private Function2<File, File, VerifyResult> approver;
public static final ApprovalTracker tracker = new ApprovalTracker();
public static final ApprovalTracker tracker = new ApprovalTracker();
private static Function2<String, String, Error> errorGenerator = FileApprover::createError;
private File received;
private File approved;
private final ApprovalWriter writer;
private Function2<File, File, VerifyResult> approver;
public FileApprover(ApprovalWriter writer, ApprovalNamer namer)
{
this(writer, namer, FileApprover::approveTextFile);
}
public FileApprover(ApprovalWriter writer, ApprovalNamer namer, Function2<File, File, VerifyResult> approver)
{
this(namer.getReceivedFile(writer.getFileExtensionWithDot()),
namer.getApprovedFile(writer.getFileExtensionWithDot()), writer, approver);
}
public FileApprover(File received, File approved, ApprovalWriter writer,
Function2<File, File, VerifyResult> approver)
{
this.received = received;
this.approved = approved;
this.writer = writer;
received = namer.getReceivedFile(writer.getFileExtensionWithDot());
approved = namer.getApprovedFile(writer.getFileExtensionWithDot());
this.approver = approver;
}
public static AutoCloseable registerErrorGenerator(Function2<String, String, Error> errorGenerator)
{
Function2<String, String, Error> old = FileApprover.errorGenerator;
FileApprover.errorGenerator = errorGenerator;
return () -> FileApprover.errorGenerator = old;
}
public VerifyResult approve()
{
tracker.assertUnique(approved.getAbsolutePath());
Expand Down Expand Up @@ -58,8 +71,11 @@ public VerifyResult reportFailure(ApprovalFailureReporter reporter)
}
public void fail()
{
throw new Error(String.format("Failed Approval\n Approved:%s\n Received:%s", approved.getAbsolutePath(),
received.getAbsolutePath()));
throw errorGenerator.call(received.getAbsolutePath(), approved.getAbsolutePath());
}
private static Error createError(String received, String approved)
{
return new Error(String.format("Failed Approval\n Approved:%s\n Received:%s", approved, received));
}
public static VerifyResult approveTextFile(File received, File approved)
{
Expand Down

0 comments on commit 64ac582

Please sign in to comment.