Test the syntax parsing method
This commit is contained in:
parent
e43a3c7194
commit
da68a6bc87
4 changed files with 141 additions and 4 deletions
|
|
@ -50,11 +50,11 @@ import java.util.regex.Pattern;
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
* @param <M> Command meta type
|
* @param <M> Command meta type
|
||||||
*/
|
*/
|
||||||
public class AnnotationParser<C, M extends CommandMeta> {
|
public final class AnnotationParser<C, M extends CommandMeta> {
|
||||||
|
|
||||||
private static final Predicate<String> PATTERN_ARGUMENT_LITERAL = Pattern.compile("([A-Za-z0-9]+)(|([A-Za-z0-9]+))*")
|
private static final Predicate<String> PATTERN_ARGUMENT_LITERAL = Pattern.compile("([A-Za-z0-9]+)(|([A-Za-z0-9]+))*")
|
||||||
.asPredicate();
|
.asPredicate();
|
||||||
private static final Predicate<String> PATTERN_ARGUMENT_REQUIRED = Pattern.compile("<(A-Za-z0-9]+)>").asPredicate();
|
private static final Predicate<String> PATTERN_ARGUMENT_REQUIRED = Pattern.compile("<([A-Za-z0-9]+)>").asPredicate();
|
||||||
private static final Predicate<String> PATTERN_ARGUMENT_OPTIONAL = Pattern.compile("\\[([A-Za-z0-9]+)\\]").asPredicate();
|
private static final Predicate<String> PATTERN_ARGUMENT_OPTIONAL = Pattern.compile("\\[([A-Za-z0-9]+)\\]").asPredicate();
|
||||||
|
|
||||||
private final CommandManager<C, M> manager;
|
private final CommandManager<C, M> manager;
|
||||||
|
|
@ -126,7 +126,7 @@ public class AnnotationParser<C, M extends CommandMeta> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private LinkedHashMap<String, ArgumentMode> parseSyntax(@Nonnull final String syntax) {
|
LinkedHashMap<String, ArgumentMode> parseSyntax(@Nonnull final String syntax) {
|
||||||
final StringTokenizer stringTokenizer = new StringTokenizer(syntax, " ");
|
final StringTokenizer stringTokenizer = new StringTokenizer(syntax, " ");
|
||||||
final LinkedHashMap<String, ArgumentMode> map = new LinkedHashMap<>();
|
final LinkedHashMap<String, ArgumentMode> map = new LinkedHashMap<>();
|
||||||
while (stringTokenizer.hasMoreTokens()) {
|
while (stringTokenizer.hasMoreTokens()) {
|
||||||
|
|
@ -203,7 +203,7 @@ public class AnnotationParser<C, M extends CommandMeta> {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private enum ArgumentMode {
|
enum ArgumentMode {
|
||||||
LITERAL, OPTIONAL, REQUIRED
|
LITERAL, OPTIONAL, REQUIRED
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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<TestCommandSender, SimpleCommandMeta> manager;
|
||||||
|
private static AnnotationParser<TestCommandSender, SimpleCommandMeta> annotationParser;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void setup() {
|
||||||
|
manager = new TestCommandManager();
|
||||||
|
annotationParser = new AnnotationParser<>(manager);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSyntaxParsing() {
|
||||||
|
final String text = "literal <required> [optional]";
|
||||||
|
final Map<String, AnnotationParser.ArgumentMode> arguments = annotationParser.parseSyntax(text);
|
||||||
|
final Map<String, AnnotationParser.ArgumentMode> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -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<TestCommandSender, SimpleCommandMeta> {
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue