Skip to content

Commit

Permalink
*** added a way to allow multiple verify per test
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEckart committed Sep 11, 2023
1 parent 577f636 commit 8239acb
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,8 @@ public Queryable<In> selectRecursivelyUntil(Function1<In, In> selector, Function
{
return Query.selectRecursivelyUntil(this, selector, until);
}
public In last()
{
return Query.last(this);
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
4 changes: 4 additions & 0 deletions approvaltests/src/main/java/org/approvaltests/Approvals.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<String> tracked = new LinkedHashSet<>();
private final Set<String> tracked = new LinkedHashSet<>();
private final Queryable<Function1<String, Boolean>> 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<String, Boolean> duplicateChecker)
{
allowedDuplicates.add(duplicateChecker);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class FileApprover implements ApprovalApprover
private File approved;
private final ApprovalWriter writer;
private Function2<File, File, VerifyResult> 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);
Expand Down

0 comments on commit 8239acb

Please sign in to comment.