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 0b4c817f..2640dc61 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 59cb14ae..4adfdef9 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 c7ed23fe..fefcab66 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) {