mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-08 11:50:36 +01:00
Make EventNode#toString return a graph of the current node state
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
070a45cb97
commit
a2b1148343
@ -218,6 +218,46 @@ non-sealed class EventNodeImpl<T extends Event> implements EventNode<T> {
|
||||
return parent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return createStringGraph(createGraph());
|
||||
}
|
||||
|
||||
Graph createGraph() {
|
||||
synchronized (GLOBAL_CHILD_LOCK) {
|
||||
List<Graph> children = this.children.stream().map(EventNodeImpl::createGraph).toList();
|
||||
return new Graph(getName(), getEventType().getSimpleName(), getPriority(), children);
|
||||
}
|
||||
}
|
||||
|
||||
static String createStringGraph(Graph graph) {
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
genToStringTree(buffer, "", "", graph);
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
private static void genToStringTree(StringBuilder buffer, String prefix, String childrenPrefix, Graph graph) {
|
||||
buffer.append(prefix);
|
||||
buffer.append(String.format("%s - EventType: %s - Priority: %d", graph.name(), graph.eventType(), graph.priority()));
|
||||
buffer.append('\n');
|
||||
var nextNodes = graph.children();
|
||||
for (Iterator<? extends @NotNull Graph> iterator = nextNodes.iterator(); iterator.hasNext(); ) {
|
||||
Graph next = iterator.next();
|
||||
if (iterator.hasNext()) {
|
||||
genToStringTree(buffer, childrenPrefix + '\u251C' + '\u2500' + " ", childrenPrefix + '\u2502' + " ", next);
|
||||
} else {
|
||||
genToStringTree(buffer, childrenPrefix + '\u2514' + '\u2500' + " ", childrenPrefix + " ", next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
record Graph(String name, String eventType, int priority,
|
||||
List<Graph> children) {
|
||||
public Graph {
|
||||
children = children.stream().sorted(Comparator.comparingInt(Graph::priority)).toList();
|
||||
}
|
||||
}
|
||||
|
||||
private void invalidateEventsFor(EventNodeImpl<? super T> node) {
|
||||
for (Class<? extends T> eventType : listenerMap.keySet()) {
|
||||
node.invalidateEvent(eventType);
|
||||
|
@ -0,0 +1,55 @@
|
||||
package net.minestom.server.event;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class EventNodeGraphTest {
|
||||
|
||||
@Test
|
||||
public void single() {
|
||||
EventNode<Event> node = EventNode.all("main");
|
||||
verifyGraph(node, new EventNodeImpl.Graph("main", "Event", 0, List.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleChild() {
|
||||
EventNode<Event> node = EventNode.all("main");
|
||||
node.addChild(EventNode.all("child"));
|
||||
verifyGraph(node, new EventNodeImpl.Graph("main", "Event", 0,
|
||||
List.of(new EventNodeImpl.Graph("child", "Event", 0, List.of())
|
||||
)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void childrenPriority() {
|
||||
{
|
||||
EventNode<Event> node = EventNode.all("main");
|
||||
node.addChild(EventNode.all("child1").setPriority(5));
|
||||
node.addChild(EventNode.all("child2").setPriority(10));
|
||||
verifyGraph(node, new EventNodeImpl.Graph("main", "Event", 0,
|
||||
List.of(new EventNodeImpl.Graph("child1", "Event", 5, List.of()),
|
||||
new EventNodeImpl.Graph("child2", "Event", 10, List.of())
|
||||
)));
|
||||
}
|
||||
{
|
||||
EventNode<Event> node = EventNode.all("main");
|
||||
node.addChild(EventNode.all("child2").setPriority(10));
|
||||
node.addChild(EventNode.all("child1").setPriority(5));
|
||||
verifyGraph(node, new EventNodeImpl.Graph("main", "Event", 0,
|
||||
List.of(new EventNodeImpl.Graph("child1", "Event", 5, List.of()),
|
||||
new EventNodeImpl.Graph("child2", "Event", 10, List.of())
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
void verifyGraph(EventNode<?> n, EventNodeImpl.Graph graph) {
|
||||
EventNodeImpl<?> node = (EventNodeImpl<?>) n;
|
||||
var nodeGraph = node.createGraph();
|
||||
assertEquals(graph, nodeGraph, "Graphs are not equals");
|
||||
assertEquals(EventNodeImpl.createStringGraph(graph), EventNodeImpl.createStringGraph(nodeGraph), "String graphs are not equals");
|
||||
assertEquals(n.toString(), EventNodeImpl.createStringGraph(nodeGraph), "The node does not use createStringGraph");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user