From 8239acb4e8c06812414bd8fb9e85134d9ff22a2c Mon Sep 17 00:00:00 2001 From: Lars Eckart Date: Mon, 11 Sep 2023 20:21:12 +0300 Subject: [PATCH] *** added a way to allow multiple verify per test --- .../java/org/approvaltests/ApprovalsTest.java | 2 ++ .../java/org/approvaltests/PackageSettings.java | 3 ++- .../java/org/lambda/utils/AsErrorsTest.java | 1 + .../main/java/org/lambda/query/Queryable.java | 4 ++++ .../org/approvaltests/ApprovalSettings.java | 17 +++++++++++++++++ .../main/java/org/approvaltests/Approvals.java | 4 ++++ .../approvers/ApprovalTracker.java | 11 +++++++++-- .../approvaltests/approvers/FileApprover.java | 2 +- 8 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 approvaltests/src/main/java/org/approvaltests/ApprovalSettings.java diff --git a/approvaltests-tests/src/test/java/org/approvaltests/ApprovalsTest.java b/approvaltests-tests/src/test/java/org/approvaltests/ApprovalsTest.java index 411ceb68a..443d283e1 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/ApprovalsTest.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/ApprovalsTest.java @@ -12,6 +12,8 @@ import java.awt.Dimension; import java.awt.Rectangle; +import static org.junit.jupiter.api.Assertions.assertThrows; + @UseReporter({DiffReporter.class, ClipboardReporter.class}) public class ApprovalsTest { diff --git a/approvaltests-tests/src/test/java/org/approvaltests/PackageSettings.java b/approvaltests-tests/src/test/java/org/approvaltests/PackageSettings.java index 6fa876340..6a6f19d72 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/PackageSettings.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/PackageSettings.java @@ -8,5 +8,6 @@ public class PackageSettings public static class ApprovalTestsPackageLevelReporter extends DiffReporter { } - public static ApprovalFailureReporter UseReporter = new ApprovalTestsPackageLevelReporter(); + public static ApprovalFailureReporter UseReporter = new ApprovalTestsPackageLevelReporter(); + public static boolean AllowMultipleVerifyCallsPerTest = true; } diff --git a/approvaltests-util-tests/src/test/java/org/lambda/utils/AsErrorsTest.java b/approvaltests-util-tests/src/test/java/org/lambda/utils/AsErrorsTest.java index 66e9332c2..23e24e73b 100644 --- a/approvaltests-util-tests/src/test/java/org/lambda/utils/AsErrorsTest.java +++ b/approvaltests-util-tests/src/test/java/org/lambda/utils/AsErrorsTest.java @@ -10,6 +10,7 @@ public class AsErrorsTest @Test void testFunction1() { + Approvals.settings().allowMultipleVerifyCallsForThisMethod(); Approvals.verifyException(() -> Functions.unchecked(m -> returnException(m)).call("a")); Approvals.verifyException(() -> Functions.unchecked((m, p2) -> returnException(m)).call("a", 2)); Approvals.verifyException(() -> Functions.unchecked((m, p2, p3) -> returnException(m)).call("a", 2, 3)); diff --git a/approvaltests-util/src/main/java/org/lambda/query/Queryable.java b/approvaltests-util/src/main/java/org/lambda/query/Queryable.java index 3474e04cd..8c7d1f42e 100644 --- a/approvaltests-util/src/main/java/org/lambda/query/Queryable.java +++ b/approvaltests-util/src/main/java/org/lambda/query/Queryable.java @@ -226,4 +226,8 @@ public Queryable selectRecursivelyUntil(Function1 selector, Function { return Query.selectRecursivelyUntil(this, selector, until); } + public In last() + { + return Query.last(this); + } } diff --git a/approvaltests/src/main/java/org/approvaltests/ApprovalSettings.java b/approvaltests/src/main/java/org/approvaltests/ApprovalSettings.java new file mode 100644 index 000000000..31f284e1e --- /dev/null +++ b/approvaltests/src/main/java/org/approvaltests/ApprovalSettings.java @@ -0,0 +1,17 @@ +package org.approvaltests; + +import com.spun.util.introspection.Caller; +import org.approvaltests.approvers.FileApprover; +import org.lambda.query.Queryable; + +public class ApprovalSettings +{ + public void allowMultipleVerifyCallsForThisMethod() + { + StackTraceElement caller = Caller.get(1); + String className = Queryable.as(caller.getClassName().split("\\.")).last(); + String methodName = caller.getMethodName(); + String name = className + "." + methodName; + FileApprover.tracker.addAllowedDuplicates(f -> f.contains(name)); + } +} diff --git a/approvaltests/src/main/java/org/approvaltests/Approvals.java b/approvaltests/src/main/java/org/approvaltests/Approvals.java index 7e488e6ed..3d8cb770f 100644 --- a/approvaltests/src/main/java/org/approvaltests/Approvals.java +++ b/approvaltests/src/main/java/org/approvaltests/Approvals.java @@ -388,4 +388,8 @@ public static void verifyException(Action0 runnableBlock, Options options) { throw new FormattedException("No exception thrown when running %s", runnableBlock); } Approvals.verify(String.format("%s: %s", t.getClass().getName(), t.getMessage()), options); } + public static ApprovalSettings settings() + { + return new ApprovalSettings(); + } } diff --git a/approvaltests/src/main/java/org/approvaltests/approvers/ApprovalTracker.java b/approvaltests/src/main/java/org/approvaltests/approvers/ApprovalTracker.java index e07fcafec..84aa53758 100644 --- a/approvaltests/src/main/java/org/approvaltests/approvers/ApprovalTracker.java +++ b/approvaltests/src/main/java/org/approvaltests/approvers/ApprovalTracker.java @@ -1,17 +1,24 @@ package org.approvaltests.approvers; import org.approvaltests.ApprovalsDuplicateVerifyException; +import org.lambda.functions.Function1; +import org.lambda.query.Queryable; import java.util.LinkedHashSet; import java.util.Set; public class ApprovalTracker { - private final Set tracked = new LinkedHashSet<>(); + private final Set tracked = new LinkedHashSet<>(); + private final Queryable> allowedDuplicates = new Queryable<>(); public void assertUnique(String approved) { - if (tracked.contains(approved)) + if (tracked.contains(approved) && !allowedDuplicates.any(f -> f.call(approved))) { throw new ApprovalsDuplicateVerifyException(approved); } tracked.add(approved); } + public void addAllowedDuplicates(Function1 duplicateChecker) + { + allowedDuplicates.add(duplicateChecker); + } } diff --git a/approvaltests/src/main/java/org/approvaltests/approvers/FileApprover.java b/approvaltests/src/main/java/org/approvaltests/approvers/FileApprover.java index 61c3ff6c9..a4b4ead42 100644 --- a/approvaltests/src/main/java/org/approvaltests/approvers/FileApprover.java +++ b/approvaltests/src/main/java/org/approvaltests/approvers/FileApprover.java @@ -18,7 +18,7 @@ public class FileApprover implements ApprovalApprover private File approved; private final ApprovalWriter writer; private Function2 approver; - private static ApprovalTracker tracker = new ApprovalTracker(); + public static final ApprovalTracker tracker = new ApprovalTracker(); public FileApprover(ApprovalWriter writer, ApprovalNamer namer) { this(writer, namer, FileApprover::approveTextFile);