mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-13 11:41:52 +01:00
Add reusable Collector interface for the testing framework
This commit is contained in:
parent
6ecf486b9d
commit
5e0340c9d8
21
src/test/java/net/minestom/server/api/Collector.java
Normal file
21
src/test/java/net/minestom/server/api/Collector.java
Normal file
@ -0,0 +1,21 @@
|
||||
package net.minestom.server.api;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
|
||||
public interface Collector<T> {
|
||||
@NotNull List<@NotNull T> collect();
|
||||
|
||||
default <P extends T> void assertSingle(@NotNull Class<P> type, @NotNull Consumer<P> consumer) {
|
||||
List<T> elements = collect();
|
||||
assertEquals(1, elements.size(), "Expected 1 element, got " + elements);
|
||||
var element = elements.get(0);
|
||||
assertInstanceOf(type, element, "Expected type " + type.getSimpleName() + ", got " + element.getClass().getSimpleName());
|
||||
consumer.accept((P) element);
|
||||
}
|
||||
}
|
@ -6,7 +6,6 @@ import net.minestom.server.instance.Instance;
|
||||
import net.minestom.server.network.packet.server.ServerPacket;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@ -18,15 +17,9 @@ public interface TestConnection {
|
||||
});
|
||||
}
|
||||
|
||||
<T extends ServerPacket> @NotNull PacketTracker<T> trackIncoming(@NotNull Class<T> type);
|
||||
<T extends ServerPacket> @NotNull Collector<T> trackIncoming(@NotNull Class<T> type);
|
||||
|
||||
default @NotNull PacketTracker<ServerPacket> trackIncoming() {
|
||||
default @NotNull Collector<ServerPacket> trackIncoming() {
|
||||
return trackIncoming(ServerPacket.class);
|
||||
}
|
||||
|
||||
interface PacketTracker<T> {
|
||||
@NotNull List<@NotNull T> collect();
|
||||
|
||||
<P extends T> void assertSingle(Class<P> packetType, Consumer<P> consumer);
|
||||
}
|
||||
}
|
||||
|
@ -20,9 +20,6 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
|
||||
final class TestConnectionImpl implements TestConnection {
|
||||
private final Env env;
|
||||
private final ServerProcess process;
|
||||
@ -59,7 +56,7 @@ final class TestConnectionImpl implements TestConnection {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull <T extends ServerPacket> PacketTracker<T> trackIncoming(@NotNull Class<T> type) {
|
||||
public @NotNull <T extends ServerPacket> Collector<T> trackIncoming(@NotNull Class<T> type) {
|
||||
var tracker = new TrackerImpl<>(type);
|
||||
this.incomingTrackers.add(TrackerImpl.class.cast(tracker));
|
||||
return tracker;
|
||||
@ -85,7 +82,7 @@ final class TestConnectionImpl implements TestConnection {
|
||||
}
|
||||
}
|
||||
|
||||
final class TrackerImpl<T extends ServerPacket> implements PacketTracker<T> {
|
||||
final class TrackerImpl<T extends ServerPacket> implements Collector<T> {
|
||||
private final Class<T> type;
|
||||
private final List<T> packets = new CopyOnWriteArrayList<>();
|
||||
|
||||
@ -98,14 +95,5 @@ final class TestConnectionImpl implements TestConnection {
|
||||
incomingTrackers.remove(this);
|
||||
return List.copyOf(packets);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <P extends T> void assertSingle(Class<P> packetType, Consumer<P> consumer) {
|
||||
var packets = collect();
|
||||
assertEquals(1, packets.size(), "Expected 1 packet, got " + packets);
|
||||
var packet = packets.get(0);
|
||||
assertInstanceOf(packetType, packet, "Expected packet of type " + packetType.getSimpleName() + ", got " + packet.getClass().getSimpleName());
|
||||
consumer.accept((P) packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user