diff --git a/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/AnnotationParser.java b/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/AnnotationParser.java index 12af8e52..badad3d8 100644 --- a/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/AnnotationParser.java +++ b/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/AnnotationParser.java @@ -50,11 +50,11 @@ import java.util.regex.Pattern; * @param Command sender type * @param Command meta type */ -public class AnnotationParser { +public final class AnnotationParser { private static final Predicate PATTERN_ARGUMENT_LITERAL = Pattern.compile("([A-Za-z0-9]+)(|([A-Za-z0-9]+))*") .asPredicate(); - private static final Predicate PATTERN_ARGUMENT_REQUIRED = Pattern.compile("<(A-Za-z0-9]+)>").asPredicate(); + private static final Predicate PATTERN_ARGUMENT_REQUIRED = Pattern.compile("<([A-Za-z0-9]+)>").asPredicate(); private static final Predicate PATTERN_ARGUMENT_OPTIONAL = Pattern.compile("\\[([A-Za-z0-9]+)\\]").asPredicate(); private final CommandManager manager; @@ -126,7 +126,7 @@ public class AnnotationParser { } @Nonnull - private LinkedHashMap parseSyntax(@Nonnull final String syntax) { + LinkedHashMap parseSyntax(@Nonnull final String syntax) { final StringTokenizer stringTokenizer = new StringTokenizer(syntax, " "); final LinkedHashMap map = new LinkedHashMap<>(); while (stringTokenizer.hasMoreTokens()) { @@ -203,7 +203,7 @@ public class AnnotationParser { } - private enum ArgumentMode { + enum ArgumentMode { LITERAL, OPTIONAL, REQUIRED } diff --git a/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/AnnotationParserTest.java b/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/AnnotationParserTest.java new file mode 100644 index 00000000..6a6db351 --- /dev/null +++ b/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/AnnotationParserTest.java @@ -0,0 +1,57 @@ +// +// MIT License +// +// Copyright (c) 2020 Alexander Söderberg +// +// 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.annotations; + +import com.google.common.collect.Maps; +import com.intellectualsites.commands.CommandManager; +import com.intellectualsites.commands.meta.SimpleCommandMeta; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +class AnnotationParserTest { + + private static CommandManager manager; + private static AnnotationParser annotationParser; + + @BeforeAll + static void setup() { + manager = new TestCommandManager(); + annotationParser = new AnnotationParser<>(manager); + } + + @Test + void testSyntaxParsing() { + final String text = "literal [optional]"; + final Map arguments = annotationParser.parseSyntax(text); + final Map map = Maps.newLinkedHashMap(); + map.put("literal", AnnotationParser.ArgumentMode.LITERAL); + map.put("required", AnnotationParser.ArgumentMode.REQUIRED); + map.put("optional", AnnotationParser.ArgumentMode.OPTIONAL); + Assertions.assertEquals(map, arguments); + } + +} diff --git a/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/TestCommandManager.java b/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/TestCommandManager.java new file mode 100644 index 00000000..3750d77c --- /dev/null +++ b/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/TestCommandManager.java @@ -0,0 +1,52 @@ +// +// MIT License +// +// Copyright (c) 2020 Alexander Söderberg +// +// 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.annotations; + +import com.intellectualsites.commands.CommandManager; +import com.intellectualsites.commands.execution.CommandExecutionCoordinator; +import com.intellectualsites.commands.internal.CommandRegistrationHandler; +import com.intellectualsites.commands.meta.SimpleCommandMeta; + +import javax.annotation.Nonnull; + +public class TestCommandManager extends CommandManager { + + protected TestCommandManager() { + super(CommandExecutionCoordinator.simpleCoordinator(), CommandRegistrationHandler.nullCommandRegistrationHandler()); + } + + @Nonnull + @Override + public final SimpleCommandMeta createDefaultCommandMeta() { + return SimpleCommandMeta.empty(); + } + + @Override + public final boolean hasPermission(@Nonnull final TestCommandSender sender, + @Nonnull final String permission) { + return !permission.equalsIgnoreCase("no"); + } + +} + diff --git a/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/TestCommandSender.java b/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/TestCommandSender.java new file mode 100644 index 00000000..949098a7 --- /dev/null +++ b/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/TestCommandSender.java @@ -0,0 +1,28 @@ +// +// MIT License +// +// Copyright (c) 2020 Alexander Söderberg +// +// 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.annotations; + +public class TestCommandSender { + +}