Add some tests and make the permission check work properly
This commit is contained in:
parent
cbe7771b63
commit
c5498e26b9
8 changed files with 228 additions and 10 deletions
|
|
@ -60,6 +60,9 @@ public class Command<C extends CommandSender> {
|
||||||
// Enforce ordering of command components
|
// Enforce ordering of command components
|
||||||
boolean foundOptional = false;
|
boolean foundOptional = false;
|
||||||
for (final CommandComponent<C, ?> component : this.components) {
|
for (final CommandComponent<C, ?> component : this.components) {
|
||||||
|
if (component.getName().isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("Component names may not be empty");
|
||||||
|
}
|
||||||
if (foundOptional && component.isRequired()) {
|
if (foundOptional && component.isRequired()) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
String.format("Command component '%s' cannot be placed after an optional component",
|
String.format("Command component '%s' cannot be placed after an optional component",
|
||||||
|
|
|
||||||
|
|
@ -147,4 +147,14 @@ public abstract class CommandManager<C extends CommandSender> {
|
||||||
return Command.newBuilder(name);
|
return Command.newBuilder(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the internal command tree. This should not be accessed unless you know what you
|
||||||
|
* are doing
|
||||||
|
*
|
||||||
|
* @return Command tree
|
||||||
|
*/
|
||||||
|
@Nonnull CommandTree<C> getCommandTree() {
|
||||||
|
return this.commandTree;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -310,8 +310,8 @@ public class CommandTree<C extends CommandSender> {
|
||||||
return sender.hasPermission(permission) ? null : permission;
|
return sender.hasPermission(permission) ? null : permission;
|
||||||
}
|
}
|
||||||
if (node.isLeaf()) {
|
if (node.isLeaf()) {
|
||||||
// noinspection all
|
return sender.hasPermission(Objects.requireNonNull(Objects.requireNonNull(node.value, "node.value").getOwningCommand(),
|
||||||
return sender.hasPermission(node.value.getOwningCommand().getCommandPermission())
|
"owning command").getCommandPermission())
|
||||||
? null : node.value.getOwningCommand().getCommandPermission();
|
? null : node.value.getOwningCommand().getCommandPermission();
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
|
|
@ -351,6 +351,7 @@ public class CommandTree<C extends CommandSender> {
|
||||||
this.commandRegistrationHandler.registerCommand(leaf.getOwningCommand());
|
this.commandRegistrationHandler.registerCommand(leaf.getOwningCommand());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Register command permissions
|
// Register command permissions
|
||||||
this.getLeavesRaw(this.internalTree).forEach(node -> {
|
this.getLeavesRaw(this.internalTree).forEach(node -> {
|
||||||
/* All leaves must necessarily have an owning command */
|
/* All leaves must necessarily have an owning command */
|
||||||
|
|
@ -362,11 +363,12 @@ public class CommandTree<C extends CommandSender> {
|
||||||
chain = chain.subList(1, chain.size());
|
chain = chain.subList(1, chain.size());
|
||||||
// Go through all nodes from the tail upwards until a collision occurs
|
// Go through all nodes from the tail upwards until a collision occurs
|
||||||
for (final Node<CommandComponent<C, ?>> commandComponentNode : chain) {
|
for (final Node<CommandComponent<C, ?>> commandComponentNode : chain) {
|
||||||
if (commandComponentNode.nodeMeta.containsKey("permission")) {
|
if (commandComponentNode.nodeMeta.containsKey("permission") && !commandComponentNode.nodeMeta.get("permission")
|
||||||
commandComponentNode.nodeMeta.remove("permission");
|
.equalsIgnoreCase(node.nodeMeta.get("permission"))) {
|
||||||
break;
|
commandComponentNode.nodeMeta.put("permission", "");
|
||||||
|
} else {
|
||||||
|
commandComponentNode.nodeMeta.put("permission", node.nodeMeta.get("permission"));
|
||||||
}
|
}
|
||||||
commandComponentNode.nodeMeta.put("permission", node.nodeMeta.get("permission"));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
/* TODO: Figure out a way to register all combinations along a command component path */
|
/* TODO: Figure out a way to register all combinations along a command component path */
|
||||||
|
|
@ -477,13 +479,12 @@ public class CommandTree<C extends CommandSender> {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
final Node<?> node = (Node<?>) o;
|
final Node<?> node = (Node<?>) o;
|
||||||
return Objects.equals(getChildren(), node.getChildren()) &&
|
return Objects.equals(getValue(), node.getValue());
|
||||||
Objects.equals(getValue(), node.getValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(getChildren(), getValue());
|
return Objects.hash(getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|
@ -495,6 +496,10 @@ public class CommandTree<C extends CommandSender> {
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Node{value=" + value + '}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,5 +54,4 @@ public class NoPermissionException extends CommandParseException {
|
||||||
return this.missingPermission;
|
return this.missingPermission;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
//
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Copyright (c) 2020 IntellectualSites
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
package com.intellectualsites.commands;
|
||||||
|
|
||||||
|
import com.intellectualsites.commands.components.StaticComponent;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class CommandTest {
|
||||||
|
|
||||||
|
@Test()
|
||||||
|
void noComponents() {
|
||||||
|
Assertions.assertEquals(1, Command.newBuilder("test").build().getComponents().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void ensureOrdering() {
|
||||||
|
Assertions.assertThrows(IllegalArgumentException.class, () ->
|
||||||
|
Command.newBuilder("test").withComponent(StaticComponent.optional("something"))
|
||||||
|
.withComponent(StaticComponent.required("somethingelse")).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,78 @@
|
||||||
|
//
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Copyright (c) 2020 IntellectualSites
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
package com.intellectualsites.commands;
|
||||||
|
|
||||||
|
import com.intellectualsites.commands.components.StaticComponent;
|
||||||
|
import com.intellectualsites.commands.context.CommandContext;
|
||||||
|
import com.intellectualsites.commands.exceptions.NoPermissionException;
|
||||||
|
import com.intellectualsites.commands.sender.CommandSender;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class CommandTreeTest {
|
||||||
|
|
||||||
|
private static CommandManager<CommandSender> commandManager;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void newTree() {
|
||||||
|
commandManager = new TestCommandManager();
|
||||||
|
commandManager.registerCommand(commandManager.commandBuilder("test")
|
||||||
|
.withComponent(StaticComponent.required("one")).build())
|
||||||
|
.registerCommand(commandManager.commandBuilder("test")
|
||||||
|
.withComponent(StaticComponent.required("two")).withPermission("no").build());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void parse() {
|
||||||
|
final Optional<Command<CommandSender>> command = commandManager.getCommandTree().parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(
|
||||||
|
Arrays.asList("test", "one")));
|
||||||
|
Assertions.assertTrue(command.isPresent());
|
||||||
|
Assertions.assertThrows(NoPermissionException.class, () -> commandManager.getCommandTree().parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(
|
||||||
|
Arrays.asList("test", "two"))));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getSuggestions() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetSuggestions() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void insertCommand() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void verifyAndRegister() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
//
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Copyright (c) 2020 IntellectualSites
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
package com.intellectualsites.commands;
|
||||||
|
|
||||||
|
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
|
||||||
|
import com.intellectualsites.commands.internal.CommandRegistrationHandler;
|
||||||
|
import com.intellectualsites.commands.sender.CommandSender;
|
||||||
|
|
||||||
|
public class TestCommandManager extends CommandManager<CommandSender> {
|
||||||
|
|
||||||
|
protected TestCommandManager() {
|
||||||
|
super(CommandExecutionCoordinator.simpleCoordinator(), CommandRegistrationHandler.NULL_COMMAND_REGISTRATION_HANDLER);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
//
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Copyright (c) 2020 IntellectualSites
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
package com.intellectualsites.commands;
|
||||||
|
|
||||||
|
import com.intellectualsites.commands.sender.CommandSender;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
public class TestCommandSender implements CommandSender {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasPermission(@Nonnull final String permission) {
|
||||||
|
if (permission.equalsIgnoreCase("no")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue