From 59fc49d137e9b8ee18cc1c221894cb01bda338bc Mon Sep 17 00:00:00 2001 From: TheMode Date: Mon, 31 Jan 2022 12:35:41 +0100 Subject: [PATCH] Add failFollowup Signed-off-by: TheMode --- .../java/net/minestom/server/api/EnvImpl.java | 19 ++++++++++++------- .../minestom/server/api/FlexibleListener.java | 8 +++++--- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/test/java/net/minestom/server/api/EnvImpl.java b/src/test/java/net/minestom/server/api/EnvImpl.java index cc46f7c65..4b37dccf6 100644 --- a/src/test/java/net/minestom/server/api/EnvImpl.java +++ b/src/test/java/net/minestom/server/api/EnvImpl.java @@ -12,6 +12,7 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Consumer; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; final class EnvImpl implements Env { private final ServerProcess process; @@ -80,7 +81,17 @@ final class EnvImpl implements Env { } @Override - public void setHandler(@NotNull Consumer handler) { + public void followup(@NotNull Consumer handler) { + updateHandler(handler); + } + + @Override + public void failFollowup() { + updateHandler(e -> fail("Event " + e.getClass().getSimpleName() + " was not expected")); + } + + void updateHandler(@NotNull Consumer handler) { + check(); this.initialized = true; this.called = false; this.handler = e -> { @@ -89,12 +100,6 @@ final class EnvImpl implements Env { }; } - @Override - public void followup(@NotNull Consumer handler) { - check(); - setHandler(handler); - } - void check() { assertTrue(!initialized || called, "Last listener has not been called: " + eventType.getSimpleName()); } diff --git a/src/test/java/net/minestom/server/api/FlexibleListener.java b/src/test/java/net/minestom/server/api/FlexibleListener.java index 49da7b9df..0dd7c73fa 100644 --- a/src/test/java/net/minestom/server/api/FlexibleListener.java +++ b/src/test/java/net/minestom/server/api/FlexibleListener.java @@ -6,11 +6,13 @@ import org.jetbrains.annotations.NotNull; import java.util.function.Consumer; public interface FlexibleListener { - - void setHandler(@NotNull Consumer handler); - /** * Updates the handler. Fails if the previous followup has not been called. */ void followup(@NotNull Consumer handler); + + /** + * Fails if an event is received. Valid until the next followup call. + */ + void failFollowup(); }