mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
More tests
This commit is contained in:
parent
2a1abdbaf4
commit
6be30d803c
47
src/test/java/net/minestom/server/command/NodeGraphTest.java
Normal file
47
src/test/java/net/minestom/server/command/NodeGraphTest.java
Normal file
@ -0,0 +1,47 @@
|
||||
package net.minestom.server.command;
|
||||
|
||||
import net.minestom.server.command.builder.Command;
|
||||
import net.minestom.server.command.builder.CommandContext;
|
||||
import net.minestom.server.command.builder.arguments.ArgumentType;
|
||||
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
||||
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket.NodeType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class NodeGraphTest {
|
||||
|
||||
@Test
|
||||
public void singleCommandWithOneSyntax() {
|
||||
final Command foo = new Command("foo");
|
||||
foo.addSyntax(NodeGraphTest::dummyExecutor, ArgumentType.Integer("bar"));
|
||||
|
||||
final DeclareCommandsPacket packet = GraphBuilder.forServer(Set.of(foo)).createPacket();
|
||||
assertEquals(3, packet.nodes().size());
|
||||
final DeclareCommandsPacket.Node root = packet.nodes().get(packet.rootIndex());
|
||||
assertNotNull(root);
|
||||
assertNodeType(NodeType.ROOT, root.flags);
|
||||
assertEquals(1, root.children.length);
|
||||
final DeclareCommandsPacket.Node cmd = packet.nodes().get(root.children[0]);
|
||||
assertNotNull(cmd);
|
||||
assertNodeType(NodeType.LITERAL, cmd.flags);
|
||||
assertEquals(1, cmd.children.length);
|
||||
assertEquals("foo", cmd.name);
|
||||
final DeclareCommandsPacket.Node arg = packet.nodes().get(cmd.children[0]);
|
||||
assertNotNull(arg);
|
||||
assertNodeType(NodeType.ARGUMENT, arg.flags);
|
||||
assertEquals(0, arg.children.length);
|
||||
assertEquals("bar", arg.name);
|
||||
}
|
||||
|
||||
private static void assertNodeType(NodeType expected, byte flags) {
|
||||
assertEquals(expected, NodeType.values()[flags & 0x03]);
|
||||
}
|
||||
|
||||
private static void dummyExecutor(CommandSender sender, CommandContext context) {
|
||||
|
||||
}
|
||||
}
|
@ -1,12 +1,14 @@
|
||||
package net.minestom.server.command;
|
||||
|
||||
import net.minestom.server.command.builder.Command;
|
||||
import net.minestom.server.command.builder.exception.IllegalCommandStructureException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class SubcommandTest {
|
||||
|
||||
@ -66,4 +68,16 @@ public class SubcommandTest {
|
||||
assertFalse(childExecuted.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecursionDetection() {
|
||||
final Command foo = new Command("foo");
|
||||
final Command bar = new Command("bar");
|
||||
bar.addSubcommand(foo);
|
||||
assertDoesNotThrow(() -> GraphBuilder.forServer(Set.of(foo, bar)));
|
||||
foo.addSubcommand(bar);
|
||||
assertTimeout(Duration.ofSeconds(5), () -> assertThrows(IllegalCommandStructureException.class,
|
||||
() -> GraphBuilder.forServer(Set.of(foo, bar)), "Builder didn't detect infinite recursion."),
|
||||
"Is your stack fine?!");
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user