From 547c0eb8b9675893d44d99c1ea624b8e124126cf Mon Sep 17 00:00:00 2001 From: Lars Eckart Date: Mon, 9 Dec 2024 20:49:27 +0200 Subject: [PATCH] - F Seam for what error is thrown from Approvals fixes #596 Co-authored-by: Jay Bazuzi Co-authored-by: Llewellyn Falco --- .../approvers/FileApproverTest.java | 14 +++++++++ .../org/approvaltests/ApprovalSettings.java | 5 ++++ .../approvaltests/approvers/FileApprover.java | 29 ++++++++++++++----- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/approvaltests-tests/src/test/java/org/approvaltests/approvers/FileApproverTest.java b/approvaltests-tests/src/test/java/org/approvaltests/approvers/FileApproverTest.java index 0b4c817f3..2640dc611 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/approvers/FileApproverTest.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/approvers/FileApproverTest.java @@ -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; @@ -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)); + } + } } diff --git a/approvaltests/src/main/java/org/approvaltests/ApprovalSettings.java b/approvaltests/src/main/java/org/approvaltests/ApprovalSettings.java index 59cb14ae0..4adfdef96 100644 --- a/approvaltests/src/main/java/org/approvaltests/ApprovalSettings.java +++ b/approvaltests/src/main/java/org/approvaltests/ApprovalSettings.java @@ -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; @@ -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 errorGenerator) + { + return FileApprover.registerErrorGenerator(errorGenerator); + } } diff --git a/approvaltests/src/main/java/org/approvaltests/approvers/FileApprover.java b/approvaltests/src/main/java/org/approvaltests/approvers/FileApprover.java index c7ed23fe8..fefcab66a 100644 --- a/approvaltests/src/main/java/org/approvaltests/approvers/FileApprover.java +++ b/approvaltests/src/main/java/org/approvaltests/approvers/FileApprover.java @@ -15,22 +15,35 @@ public class FileApprover implements ApprovalApprover { - private File received; - private File approved; - private final ApprovalWriter writer; - private Function2 approver; - public static final ApprovalTracker tracker = new ApprovalTracker(); + public static final ApprovalTracker tracker = new ApprovalTracker(); + private static Function2 errorGenerator = FileApprover::createError; + private File received; + private File approved; + private final ApprovalWriter writer; + private Function2 approver; public FileApprover(ApprovalWriter writer, ApprovalNamer namer) { this(writer, namer, FileApprover::approveTextFile); } public FileApprover(ApprovalWriter writer, ApprovalNamer namer, Function2 approver) { + this(namer.getReceivedFile(writer.getFileExtensionWithDot()), + namer.getApprovedFile(writer.getFileExtensionWithDot()), writer, approver); + } + public FileApprover(File received, File approved, ApprovalWriter writer, + Function2 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 errorGenerator) + { + Function2 old = FileApprover.errorGenerator; + FileApprover.errorGenerator = errorGenerator; + return () -> FileApprover.errorGenerator = old; + } public VerifyResult approve() { tracker.assertUnique(approved.getAbsolutePath()); @@ -58,7 +71,7 @@ public VerifyResult reportFailure(ApprovalFailureReporter reporter) } public void fail() { - throw createError(received.getAbsolutePath(), approved.getAbsolutePath()); + throw errorGenerator.call(received.getAbsolutePath(), approved.getAbsolutePath()); } private static Error createError(String received, String approved) {