Add cancellable + recursive tests

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-12-15 15:04:48 +01:00
parent 604a279061
commit 16d3d4417e

View File

@ -3,6 +3,8 @@ package event;
import net.minestom.server.event.Event;
import net.minestom.server.event.EventListener;
import net.minestom.server.event.EventNode;
import net.minestom.server.event.trait.CancellableEvent;
import net.minestom.server.event.trait.RecursiveEvent;
import org.junit.jupiter.api.Test;
import java.util.concurrent.atomic.AtomicBoolean;
@ -12,7 +14,27 @@ import static org.junit.jupiter.api.Assertions.*;
public class EventNodeTest {
class EventTest implements Event {
static class EventTest implements Event {
}
static class CancellableTest implements CancellableEvent {
private boolean cancelled = false;
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}
static class Recursive1 implements RecursiveEvent {
}
static class Recursive2 extends Recursive1 {
}
@Test
@ -32,6 +54,47 @@ public class EventNodeTest {
assertFalse(result.get(), "The event should not be called after the removal");
}
@Test
public void testCancellable() {
var node = EventNode.all("main");
AtomicBoolean result = new AtomicBoolean(false);
var listener = EventListener.builder(CancellableTest.class)
.handler(event -> {
event.setCancelled(true);
result.set(true);
assertTrue(event.isCancelled(), "The event should be cancelled");
}).build();
node.addListener(listener);
node.call(new CancellableTest());
assertTrue(result.get(), "The event should be called after the call");
// Test cancelling
node.addListener(CancellableTest.class, event -> fail("The event must have been cancelled"));
node.call(new CancellableTest());
}
@Test
public void testRecursive() {
var node = EventNode.all("main");
AtomicBoolean result1 = new AtomicBoolean(false);
AtomicBoolean result2 = new AtomicBoolean(false);
var listener1 = EventListener.of(Recursive1.class, event -> result1.set(true));
var listener2 = EventListener.of(Recursive2.class, event -> result2.set(true));
node.addListener(listener1);
node.addListener(listener2);
node.call(new Recursive2());
assertTrue(result2.get(), "Recursive2 should have been called directly");
assertTrue(result1.get(), "Recursive1 should be called due to the RecursiveEvent interface");
// Remove the direct listener
result1.set(false);
result2.set(false);
node.removeListener(listener2);
node.call(new Recursive2());
assertFalse(result2.get(), "There is no listener for Recursive2");
assertTrue(result1.get(), "Recursive1 should be called due to the RecursiveEvent interface");
}
@Test
public void testChildren() {
var node = EventNode.all("main");