Code cleanup

This commit is contained in:
Nathan Adams
2017-07-25 16:07:03 +02:00
parent 3bb1a888fb
commit d0be598676
38 changed files with 221 additions and 252 deletions
@@ -36,22 +36,22 @@ public class CommandDispatcher<S> {
private final RootCommandNode<S> root = new RootCommandNode<>(); private final RootCommandNode<S> root = new RootCommandNode<>();
private final Predicate<CommandNode<S>> hasCommand = new Predicate<CommandNode<S>>() { private final Predicate<CommandNode<S>> hasCommand = new Predicate<CommandNode<S>>() {
@Override @Override
public boolean test(CommandNode<S> input) { public boolean test(final CommandNode<S> input) {
return input != null && (input.getCommand() != null || input.getChildren().stream().anyMatch(hasCommand)); return input != null && (input.getCommand() != null || input.getChildren().stream().anyMatch(hasCommand));
} }
}; };
public void register(LiteralArgumentBuilder<S> command) { public void register(final LiteralArgumentBuilder<S> command) {
final LiteralCommandNode<S> build = command.build(); final LiteralCommandNode<S> build = command.build();
root.addChild(build); root.addChild(build);
} }
public int execute(String input, S source) throws CommandException { public int execute(final String input, final S source) throws CommandException {
final ParseResults<S> parse = parse(input, source); final ParseResults<S> parse = parse(input, source);
return execute(parse); return execute(parse);
} }
public int execute(ParseResults<S> parse) throws CommandException { public int execute(final ParseResults<S> parse) throws CommandException {
if (parse.getRemaining().length() > 0) { if (parse.getRemaining().length() > 0) {
if (parse.getExceptions().size() == 1) { if (parse.getExceptions().size() == 1) {
throw parse.getExceptions().values().iterator().next(); throw parse.getExceptions().values().iterator().next();
@@ -61,32 +61,32 @@ public class CommandDispatcher<S> {
throw ERROR_UNKNOWN_ARGUMENT.create(parse.getRemaining()); throw ERROR_UNKNOWN_ARGUMENT.create(parse.getRemaining());
} }
} }
CommandContext<S> context = parse.getContext().build(); final CommandContext<S> context = parse.getContext().build();
Command<S> command = context.getCommand(); final Command<S> command = context.getCommand();
if (command == null) { if (command == null) {
throw ERROR_UNKNOWN_COMMAND.create(); throw ERROR_UNKNOWN_COMMAND.create();
} }
return command.run(context); return command.run(context);
} }
public ParseResults<S> parse(String command, S source) throws CommandException { public ParseResults<S> parse(final String command, final S source) throws CommandException {
StringReader reader = new StringReader(command); final StringReader reader = new StringReader(command);
return parseNodes(root, reader, new CommandContextBuilder<>(this, source)); return parseNodes(root, reader, new CommandContextBuilder<>(this, source));
} }
private ParseResults<S> parseNodes(CommandNode<S> node, StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException { private ParseResults<S> parseNodes(final CommandNode<S> node, final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
final S source = contextBuilder.getSource(); final S source = contextBuilder.getSource();
Map<CommandNode<S>, CommandException> errors = Maps.newHashMap(); final Map<CommandNode<S>, CommandException> errors = Maps.newHashMap();
for (CommandNode<S> child : node.getChildren()) { for (final CommandNode<S> child : node.getChildren()) {
if (!child.canUse(source)) { if (!child.canUse(source)) {
continue; continue;
} }
CommandContextBuilder<S> context = contextBuilder.copy(); final CommandContextBuilder<S> context = contextBuilder.copy();
int cursor = reader.getCursor(); final int cursor = reader.getCursor();
try { try {
child.parse(reader, context); child.parse(reader, context);
} catch (CommandException ex) { } catch (final CommandException ex) {
errors.put(child, ex); errors.put(child, ex);
reader.setCursor(cursor); reader.setCursor(cursor);
continue; continue;
@@ -107,13 +107,13 @@ public class CommandDispatcher<S> {
return new ParseResults<>(contextBuilder, reader.getRemaining(), errors); return new ParseResults<>(contextBuilder, reader.getRemaining(), errors);
} }
public String[] getAllUsage(CommandNode<S> node, S source) { public String[] getAllUsage(final CommandNode<S> node, final S source) {
final ArrayList<String> result = Lists.newArrayList(); final ArrayList<String> result = Lists.newArrayList();
getAllUsage(node, source, result, ""); getAllUsage(node, source, result, "");
return result.toArray(new String[result.size()]); return result.toArray(new String[result.size()]);
} }
private void getAllUsage(CommandNode<S> node, S source, ArrayList<String> result, String prefix) { private void getAllUsage(final CommandNode<S> node, final S source, final ArrayList<String> result, final String prefix) {
if (!node.canUse(source)) { if (!node.canUse(source)) {
return; return;
} }
@@ -129,12 +129,12 @@ public class CommandDispatcher<S> {
} }
} }
public Map<CommandNode<S>, String> getSmartUsage(CommandNode<S> node, S source) { public Map<CommandNode<S>, String> getSmartUsage(final CommandNode<S> node, final S source) {
Map<CommandNode<S>, String> result = Maps.newLinkedHashMap(); final Map<CommandNode<S>, String> result = Maps.newLinkedHashMap();
final boolean optional = node.getCommand() != null; final boolean optional = node.getCommand() != null;
for (CommandNode<S> child : node.getChildren()) { for (final CommandNode<S> child : node.getChildren()) {
String usage = getSmartUsage(child, source, optional, false); final String usage = getSmartUsage(child, source, optional, false);
if (usage != null) { if (usage != null) {
result.put(child, usage); result.put(child, usage);
} }
@@ -142,15 +142,15 @@ public class CommandDispatcher<S> {
return result; return result;
} }
private String getSmartUsage(CommandNode<S> node, S source, boolean optional, boolean deep) { private String getSmartUsage(final CommandNode<S> node, final S source, final boolean optional, final boolean deep) {
if (!node.canUse(source)) { if (!node.canUse(source)) {
return null; return null;
} }
String self = optional ? USAGE_OPTIONAL_OPEN + node.getUsageText() + USAGE_OPTIONAL_CLOSE : node.getUsageText(); final String self = optional ? USAGE_OPTIONAL_OPEN + node.getUsageText() + USAGE_OPTIONAL_CLOSE : node.getUsageText();
boolean childOptional = node.getCommand() != null; final boolean childOptional = node.getCommand() != null;
String open = childOptional ? USAGE_OPTIONAL_OPEN : USAGE_REQUIRED_OPEN; final String open = childOptional ? USAGE_OPTIONAL_OPEN : USAGE_REQUIRED_OPEN;
String close = childOptional ? USAGE_OPTIONAL_CLOSE : USAGE_REQUIRED_CLOSE; final String close = childOptional ? USAGE_OPTIONAL_CLOSE : USAGE_REQUIRED_CLOSE;
if (!deep) { if (!deep) {
final Collection<CommandNode<S>> children = node.getChildren().stream().filter(c -> c.canUse(source)).collect(Collectors.toList()); final Collection<CommandNode<S>> children = node.getChildren().stream().filter(c -> c.canUse(source)).collect(Collectors.toList());
@@ -160,7 +160,7 @@ public class CommandDispatcher<S> {
return self + ARGUMENT_SEPARATOR + usage; return self + ARGUMENT_SEPARATOR + usage;
} }
} else if (children.size() > 1) { } else if (children.size() > 1) {
Set<String> childUsage = Sets.newLinkedHashSet(); final Set<String> childUsage = Sets.newLinkedHashSet();
for (final CommandNode<S> child : children) { for (final CommandNode<S> child : children) {
final String usage = getSmartUsage(child, source, childOptional, true); final String usage = getSmartUsage(child, source, childOptional, true);
if (usage != null) { if (usage != null) {
@@ -171,7 +171,7 @@ public class CommandDispatcher<S> {
final String usage = childUsage.iterator().next(); final String usage = childUsage.iterator().next();
return self + ARGUMENT_SEPARATOR + (childOptional ? USAGE_OPTIONAL_OPEN + usage + USAGE_OPTIONAL_CLOSE : usage); return self + ARGUMENT_SEPARATOR + (childOptional ? USAGE_OPTIONAL_OPEN + usage + USAGE_OPTIONAL_CLOSE : usage);
} else if (childUsage.size() > 1) { } else if (childUsage.size() > 1) {
StringBuilder builder = new StringBuilder(open); final StringBuilder builder = new StringBuilder(open);
int count = 0; int count = 0;
for (final CommandNode<S> child : children) { for (final CommandNode<S> child : children) {
if (count > 0) { if (count > 0) {
@@ -191,14 +191,14 @@ public class CommandDispatcher<S> {
return self; return self;
} }
private Set<String> findSuggestions(CommandNode<S> node, StringReader reader, CommandContextBuilder<S> contextBuilder, Set<String> result) { private Set<String> findSuggestions(final CommandNode<S> node, final StringReader reader, final CommandContextBuilder<S> contextBuilder, final Set<String> result) {
final S source = contextBuilder.getSource(); final S source = contextBuilder.getSource();
for (CommandNode<S> child : node.getChildren()) { for (final CommandNode<S> child : node.getChildren()) {
if (!child.canUse(source)) { if (!child.canUse(source)) {
continue; continue;
} }
CommandContextBuilder<S> context = contextBuilder.copy(); final CommandContextBuilder<S> context = contextBuilder.copy();
int cursor = reader.getCursor(); final int cursor = reader.getCursor();
try { try {
child.parse(reader, context); child.parse(reader, context);
if (reader.canRead()) { if (reader.canRead()) {
@@ -210,7 +210,7 @@ public class CommandDispatcher<S> {
reader.setCursor(cursor); reader.setCursor(cursor);
child.listSuggestions(reader.getRemaining(), result, context); child.listSuggestions(reader.getRemaining(), result, context);
} }
} catch (CommandException e) { } catch (final CommandException e) {
reader.setCursor(cursor); reader.setCursor(cursor);
child.listSuggestions(reader.getRemaining(), result, context); child.listSuggestions(reader.getRemaining(), result, context);
} }
@@ -219,8 +219,8 @@ public class CommandDispatcher<S> {
return result; return result;
} }
public String[] getCompletionSuggestions(String command, S source) { public String[] getCompletionSuggestions(final String command, final S source) {
StringReader reader = new StringReader(command); final StringReader reader = new StringReader(command);
final Set<String> nodes = findSuggestions(root, reader, new CommandContextBuilder<>(this, source), Sets.newLinkedHashSet()); final Set<String> nodes = findSuggestions(root, reader, new CommandContextBuilder<>(this, source), Sets.newLinkedHashSet());
return nodes.toArray(new String[nodes.size()]); return nodes.toArray(new String[nodes.size()]);
@@ -12,13 +12,13 @@ public class ParseResults<S> {
private final String remaining; private final String remaining;
private final Map<CommandNode<S>, CommandException> exceptions; private final Map<CommandNode<S>, CommandException> exceptions;
public ParseResults(CommandContextBuilder<S> context, String remaining, Map<CommandNode<S>, CommandException> exceptions) { public ParseResults(final CommandContextBuilder<S> context, final String remaining, final Map<CommandNode<S>, CommandException> exceptions) {
this.context = context; this.context = context;
this.remaining = remaining; this.remaining = remaining;
this.exceptions = exceptions; this.exceptions = exceptions;
} }
public ParseResults(CommandContextBuilder<S> context) { public ParseResults(final CommandContextBuilder<S> context) {
this(context, "", Collections.emptyMap()); this(context, "", Collections.emptyMap());
} }
@@ -20,7 +20,7 @@ public class StringReader {
private final String string; private final String string;
private int cursor; private int cursor;
public StringReader(String string) { public StringReader(final String string) {
this.string = string; this.string = string;
} }
@@ -28,7 +28,7 @@ public class StringReader {
return string; return string;
} }
public void setCursor(int cursor) { public void setCursor(final int cursor) {
this.cursor = cursor; this.cursor = cursor;
} }
@@ -116,7 +116,7 @@ public class StringReader {
|| c == '.' || c == '+'; || c == '.' || c == '+';
} }
public String readUnquotedString() throws CommandException { public String readUnquotedString() {
final int start = cursor; final int start = cursor;
while (canRead() && isAllowedInUnquotedString(peek())) { while (canRead() && isAllowedInUnquotedString(peek())) {
skip(); skip();
@@ -163,7 +163,7 @@ public class StringReader {
} }
public boolean readBoolean() throws CommandException { public boolean readBoolean() throws CommandException {
String value = readString(); final String value = readString();
if (value.equals("true")) { if (value.equals("true")) {
return true; return true;
} else if (value.equals("false")) { } else if (value.equals("false")) {
@@ -2,7 +2,6 @@ package com.mojang.brigadier.arguments;
import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.CommandException;
import java.util.Set; import java.util.Set;
@@ -10,7 +9,8 @@ import java.util.Set;
public interface ArgumentType<T> { public interface ArgumentType<T> {
<S> T parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException; <S> T parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException;
default <S> void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) {} default <S> void listSuggestions(final String command, final Set<String> output, final CommandContextBuilder<S> contextBuilder) {
}
default String getUsageSuffix() { default String getUsageSuffix() {
return null; return null;
@@ -1,16 +1,12 @@
package com.mojang.brigadier.arguments; package com.mojang.brigadier.arguments;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
public class BoolArgumentType implements ArgumentType<Boolean> { public class BoolArgumentType implements ArgumentType<Boolean> {
public static final SimpleCommandExceptionType ERROR_INVALID = new SimpleCommandExceptionType("argument.bool.invalid", "Value must be true or false");
private BoolArgumentType() { private BoolArgumentType() {
} }
@@ -18,12 +14,12 @@ public class BoolArgumentType implements ArgumentType<Boolean> {
return new BoolArgumentType(); return new BoolArgumentType();
} }
public static boolean getBool(CommandContext<?> context, String name) { public static boolean getBool(final CommandContext<?> context, final String name) {
return context.getArgument(name, Boolean.class); return context.getArgument(name, Boolean.class);
} }
@Override @Override
public <S> Boolean parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException { public <S> Boolean parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
return reader.readBoolean(); return reader.readBoolean();
} }
@@ -1,10 +1,8 @@
package com.mojang.brigadier.arguments; package com.mojang.brigadier.arguments;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType; import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
@@ -19,7 +17,7 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
private final int maximum; private final int maximum;
private final String suffix; private final String suffix;
private IntegerArgumentType(int minimum, int maximum, String suffix) { private IntegerArgumentType(final int minimum, final int maximum, final String suffix) {
this.minimum = minimum; this.minimum = minimum;
this.maximum = maximum; this.maximum = maximum;
this.suffix = suffix; this.suffix = suffix;
@@ -29,25 +27,25 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
return integer(Integer.MIN_VALUE); return integer(Integer.MIN_VALUE);
} }
public static IntegerArgumentType integer(int min) { public static IntegerArgumentType integer(final int min) {
return integer(min, Integer.MAX_VALUE); return integer(min, Integer.MAX_VALUE);
} }
public static IntegerArgumentType integer(int min, int max) { public static IntegerArgumentType integer(final int min, final int max) {
return integer(min, max, ""); return integer(min, max, "");
} }
public static IntegerArgumentType integer(int min, int max, String suffix) { public static IntegerArgumentType integer(final int min, final int max, final String suffix) {
return new IntegerArgumentType(min, max, suffix); return new IntegerArgumentType(min, max, suffix);
} }
public static int getInteger(CommandContext<?> context, String name) { public static int getInteger(final CommandContext<?> context, final String name) {
return context.getArgument(name, int.class); return context.getArgument(name, int.class);
} }
@Override @Override
public <S> Integer parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException { public <S> Integer parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
int result = reader.readInt(); final int result = reader.readInt();
for (int i = 0; i < suffix.length(); i++) { for (int i = 0; i < suffix.length(); i++) {
if (reader.canRead() && reader.peek() == suffix.charAt(i)) { if (reader.canRead() && reader.peek() == suffix.charAt(i)) {
reader.skip(); reader.skip();
@@ -65,11 +63,11 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(final Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof IntegerArgumentType)) return false; if (!(o instanceof IntegerArgumentType)) return false;
IntegerArgumentType that = (IntegerArgumentType) o; final IntegerArgumentType that = (IntegerArgumentType) o;
return maximum == that.maximum && minimum == that.minimum && Objects.equals(suffix, that.suffix); return maximum == that.maximum && minimum == that.minimum && Objects.equals(suffix, that.suffix);
} }
@@ -4,15 +4,12 @@ import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
public class StringArgumentType implements ArgumentType<String> { public class StringArgumentType implements ArgumentType<String> {
private final StringType type; private final StringType type;
private StringArgumentType(StringType type) { private StringArgumentType(final StringType type) {
this.type = type; this.type = type;
} }
@@ -28,14 +25,14 @@ public class StringArgumentType implements ArgumentType<String> {
return new StringArgumentType(StringType.GREEDY_PHRASE); return new StringArgumentType(StringType.GREEDY_PHRASE);
} }
public static String getString(CommandContext<?> context, String name) { public static String getString(final CommandContext<?> context, final String name) {
return context.getArgument(name, String.class); return context.getArgument(name, String.class);
} }
@Override @Override
public <S> String parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException { public <S> String parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
if (type == StringType.GREEDY_PHRASE) { if (type == StringType.GREEDY_PHRASE) {
String text = reader.getRemaining(); final String text = reader.getRemaining();
reader.setCursor(reader.getTotalLength()); reader.setCursor(reader.getTotalLength());
return text; return text;
} else if (type == StringType.SINGLE_WORD) { } else if (type == StringType.SINGLE_WORD) {
@@ -50,15 +47,15 @@ public class StringArgumentType implements ArgumentType<String> {
return "string()"; return "string()";
} }
public static String escapeIfRequired(String input) { public static String escapeIfRequired(final String input) {
if (input.contains("\\") || input.contains("\"") || input.contains(CommandDispatcher.ARGUMENT_SEPARATOR)) { if (input.contains("\\") || input.contains("\"") || input.contains(CommandDispatcher.ARGUMENT_SEPARATOR)) {
return escape(input); return escape(input);
} }
return input; return input;
} }
private static String escape(String input) { private static String escape(final String input) {
StringBuilder result = new StringBuilder("\""); final StringBuilder result = new StringBuilder("\"");
for (int i = 0; i < input.length(); i++) { for (int i = 0; i < input.length(); i++) {
final char c = input.charAt(i); final char c = input.charAt(i);
@@ -14,7 +14,7 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
protected abstract T getThis(); protected abstract T getThis();
public T then(ArgumentBuilder<S, ?> argument) { public T then(final ArgumentBuilder<S, ?> argument) {
arguments.addChild(argument.build()); arguments.addChild(argument.build());
return getThis(); return getThis();
} }
@@ -23,7 +23,7 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
return arguments.getChildren(); return arguments.getChildren();
} }
public T executes(Command<S> command) { public T executes(final Command<S> command) {
this.command = command; this.command = command;
return getThis(); return getThis();
} }
@@ -32,7 +32,7 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
return command; return command;
} }
public T requires(Predicate<S> requirement) { public T requires(final Predicate<S> requirement) {
this.requirement = requirement; this.requirement = requirement;
return getThis(); return getThis();
} }
@@ -6,11 +6,11 @@ import com.mojang.brigadier.tree.LiteralCommandNode;
public class LiteralArgumentBuilder<S> extends ArgumentBuilder<S, LiteralArgumentBuilder<S>> { public class LiteralArgumentBuilder<S> extends ArgumentBuilder<S, LiteralArgumentBuilder<S>> {
private final String literal; private final String literal;
protected LiteralArgumentBuilder(String literal) { protected LiteralArgumentBuilder(final String literal) {
this.literal = literal; this.literal = literal;
} }
public static <S> LiteralArgumentBuilder<S> literal(String name) { public static <S> LiteralArgumentBuilder<S> literal(final String name) {
return new LiteralArgumentBuilder<>(name); return new LiteralArgumentBuilder<>(name);
} }
@@ -25,9 +25,9 @@ public class LiteralArgumentBuilder<S> extends ArgumentBuilder<S, LiteralArgumen
@Override @Override
public LiteralCommandNode<S> build() { public LiteralCommandNode<S> build() {
LiteralCommandNode<S> result = new LiteralCommandNode<>(getLiteral(), getCommand(), getRequirement()); final LiteralCommandNode<S> result = new LiteralCommandNode<>(getLiteral(), getCommand(), getRequirement());
for (CommandNode<S> argument : getArguments()) { for (final CommandNode<S> argument : getArguments()) {
result.addChild(argument); result.addChild(argument);
} }
@@ -8,12 +8,12 @@ public class RequiredArgumentBuilder<S, T> extends ArgumentBuilder<S, RequiredAr
private final String name; private final String name;
private final ArgumentType<T> type; private final ArgumentType<T> type;
private RequiredArgumentBuilder(String name, ArgumentType<T> type) { private RequiredArgumentBuilder(final String name, final ArgumentType<T> type) {
this.name = name; this.name = name;
this.type = type; this.type = type;
} }
public static <S, T> RequiredArgumentBuilder<S, T> argument(String name, ArgumentType<T> type) { public static <S, T> RequiredArgumentBuilder<S, T> argument(final String name, final ArgumentType<T> type) {
return new RequiredArgumentBuilder<>(name, type); return new RequiredArgumentBuilder<>(name, type);
} }
@@ -31,9 +31,9 @@ public class RequiredArgumentBuilder<S, T> extends ArgumentBuilder<S, RequiredAr
} }
public ArgumentCommandNode<S, T> build() { public ArgumentCommandNode<S, T> build() {
ArgumentCommandNode<S, T> result = new ArgumentCommandNode<>(getName(), getType(), getCommand(), getRequirement()); final ArgumentCommandNode<S, T> result = new ArgumentCommandNode<>(getName(), getType(), getCommand(), getRequirement());
for (CommandNode<S> argument : getArguments()) { for (final CommandNode<S> argument : getArguments()) {
result.addChild(argument); result.addChild(argument);
} }
@@ -15,7 +15,7 @@ public class CommandContext<S> {
private final Map<CommandNode<S>, String> nodes; private final Map<CommandNode<S>, String> nodes;
private final String input; private final String input;
public CommandContext(S source, Map<String, ParsedArgument<S, ?>> arguments, Command<S> command, Map<CommandNode<S>, String> nodes, String input) { public CommandContext(final S source, final Map<String, ParsedArgument<S, ?>> arguments, final Command<S> command, final Map<CommandNode<S>, String> nodes, final String input) {
this.source = source; this.source = source;
this.arguments = arguments; this.arguments = arguments;
this.command = command; this.command = command;
@@ -32,8 +32,8 @@ public class CommandContext<S> {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <V> V getArgument(String name, Class<V> clazz) { public <V> V getArgument(final String name, final Class<V> clazz) {
ParsedArgument<S, ?> argument = arguments.get(name); final ParsedArgument<S, ?> argument = arguments.get(name);
if (argument == null) { if (argument == null) {
throw new IllegalArgumentException("No such argument '" + name + "' exists on this command"); throw new IllegalArgumentException("No such argument '" + name + "' exists on this command");
@@ -48,11 +48,11 @@ public class CommandContext<S> {
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(final Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof CommandContext)) return false; if (!(o instanceof CommandContext)) return false;
CommandContext that = (CommandContext) o; final CommandContext that = (CommandContext) o;
if (!arguments.equals(that.arguments)) return false; if (!arguments.equals(that.arguments)) return false;
if (!Iterables.elementsEqual(nodes.entrySet(), that.nodes.entrySet())) return false; if (!Iterables.elementsEqual(nodes.entrySet(), that.nodes.entrySet())) return false;
@@ -80,7 +80,7 @@ public class CommandContext<S> {
} }
public CommandContext<S> copy() { public CommandContext<S> copy() {
Map<String, ParsedArgument<S, ?>> arguments = Maps.newLinkedHashMap(); final Map<String, ParsedArgument<S, ?>> arguments = Maps.newLinkedHashMap();
this.arguments.forEach((k, v) -> arguments.put(k, v.copy())); this.arguments.forEach((k, v) -> arguments.put(k, v.copy()));
return new CommandContext<>(source, arguments, command, nodes, input); return new CommandContext<>(source, arguments, command, nodes, input);
} }
@@ -15,12 +15,12 @@ public class CommandContextBuilder<S> {
private S source; private S source;
private Command<S> command; private Command<S> command;
public CommandContextBuilder(CommandDispatcher<S> dispatcher, S source) { public CommandContextBuilder(final CommandDispatcher<S> dispatcher, final S source) {
this.dispatcher = dispatcher; this.dispatcher = dispatcher;
this.source = source; this.source = source;
} }
public CommandContextBuilder<S> withSource(S source) { public CommandContextBuilder<S> withSource(final S source) {
this.source = source; this.source = source;
return this; return this;
} }
@@ -29,7 +29,7 @@ public class CommandContextBuilder<S> {
return source; return source;
} }
public CommandContextBuilder<S> withArgument(String name, ParsedArgument<S, ?> argument) { public CommandContextBuilder<S> withArgument(final String name, final ParsedArgument<S, ?> argument) {
this.arguments.put(name, argument); this.arguments.put(name, argument);
return this; return this;
} }
@@ -38,12 +38,12 @@ public class CommandContextBuilder<S> {
return arguments; return arguments;
} }
public CommandContextBuilder<S> withCommand(Command<S> command) { public CommandContextBuilder<S> withCommand(final Command<S> command) {
this.command = command; this.command = command;
return this; return this;
} }
public CommandContextBuilder<S> withNode(CommandNode<S> node, String raw) { public CommandContextBuilder<S> withNode(final CommandNode<S> node, final String raw) {
if (!nodes.isEmpty()) { if (!nodes.isEmpty()) {
input.append(CommandDispatcher.ARGUMENT_SEPARATOR); input.append(CommandDispatcher.ARGUMENT_SEPARATOR);
} }
@@ -53,7 +53,7 @@ public class CommandContextBuilder<S> {
} }
public CommandContextBuilder<S> copy() { public CommandContextBuilder<S> copy() {
CommandContextBuilder<S> copy = new CommandContextBuilder<>(dispatcher, source); final CommandContextBuilder<S> copy = new CommandContextBuilder<>(dispatcher, source);
copy.command = this.command; copy.command = this.command;
arguments.forEach((k, v) -> copy.arguments.put(k, v.copy())); arguments.forEach((k, v) -> copy.arguments.put(k, v.copy()));
copy.nodes.putAll(this.nodes); copy.nodes.putAll(this.nodes);
@@ -4,7 +4,7 @@ public class ParsedArgument<S, T> {
private final String raw; private final String raw;
private final T result; private final T result;
public ParsedArgument(String raw, T result) { public ParsedArgument(final String raw, final T result) {
this.raw = raw; this.raw = raw;
this.result = result; this.result = result;
} }
@@ -18,11 +18,11 @@ public class ParsedArgument<S, T> {
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(final Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof ParsedArgument)) return false; if (!(o instanceof ParsedArgument)) return false;
ParsedArgument that = (ParsedArgument) o; final ParsedArgument that = (ParsedArgument) o;
if (!raw.equals(that.raw)) return false; if (!raw.equals(that.raw)) return false;
if (!result.equals(that.result)) return false; if (!result.equals(that.result)) return false;
@@ -6,7 +6,7 @@ public class CommandException extends Exception {
private final CommandExceptionType type; private final CommandExceptionType type;
private final Map<String, Object> data; private final Map<String, Object> data;
public CommandException(CommandExceptionType type, Map<String, Object> data) { public CommandException(final CommandExceptionType type, final Map<String, Object> data) {
this.type = type; this.type = type;
this.data = data; this.data = data;
} }
@@ -2,5 +2,6 @@ package com.mojang.brigadier.exceptions;
public interface CommandExceptionType { public interface CommandExceptionType {
String getTypeName(); String getTypeName();
String getErrorMessage(CommandException exception); String getErrorMessage(CommandException exception);
} }
@@ -7,14 +7,14 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class ParameterizedCommandExceptionType implements CommandExceptionType { public class ParameterizedCommandExceptionType implements CommandExceptionType {
private static Pattern PATTERN = Pattern.compile("\\$\\{(\\w+)}"); private static final Pattern PATTERN = Pattern.compile("\\$\\{(\\w+)}");
private static final Joiner JOINER = Joiner.on(", "); private static final Joiner JOINER = Joiner.on(", ");
private final String name; private final String name;
private final String message; private final String message;
private final String[] keys; private final String[] keys;
public ParameterizedCommandExceptionType(String name, String message, String... keys) { public ParameterizedCommandExceptionType(final String name, final String message, final String... keys) {
this.name = name; this.name = name;
this.message = message; this.message = message;
this.keys = keys; this.keys = keys;
@@ -26,7 +26,7 @@ public class ParameterizedCommandExceptionType implements CommandExceptionType {
} }
@Override @Override
public String getErrorMessage(CommandException exception) { public String getErrorMessage(final CommandException exception) {
final Matcher matcher = PATTERN.matcher(message); final Matcher matcher = PATTERN.matcher(message);
final StringBuffer result = new StringBuffer(); final StringBuffer result = new StringBuffer();
while (matcher.find()) { while (matcher.find()) {
@@ -36,7 +36,7 @@ public class ParameterizedCommandExceptionType implements CommandExceptionType {
return result.toString(); return result.toString();
} }
public CommandException create(Object... values) { public CommandException create(final Object... values) {
if (values.length != keys.length) { if (values.length != keys.length) {
throw new IllegalArgumentException("Invalid values! (Expected: " + JOINER.join(keys) + ")"); throw new IllegalArgumentException("Invalid values! (Expected: " + JOINER.join(keys) + ")");
} }
@@ -51,11 +51,11 @@ public class ParameterizedCommandExceptionType implements CommandExceptionType {
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(final Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof CommandExceptionType)) return false; if (!(o instanceof CommandExceptionType)) return false;
CommandExceptionType that = (CommandExceptionType) o; final CommandExceptionType that = (CommandExceptionType) o;
return getTypeName().equals(that.getTypeName()); return getTypeName().equals(that.getTypeName());
} }
@@ -6,7 +6,7 @@ public class SimpleCommandExceptionType implements CommandExceptionType {
private final String name; private final String name;
private final String message; private final String message;
public SimpleCommandExceptionType(String name, String message) { public SimpleCommandExceptionType(final String name, final String message) {
this.name = name; this.name = name;
this.message = message; this.message = message;
} }
@@ -17,7 +17,7 @@ public class SimpleCommandExceptionType implements CommandExceptionType {
} }
@Override @Override
public String getErrorMessage(CommandException exception) { public String getErrorMessage(final CommandException exception) {
return message; return message;
} }
@@ -26,11 +26,11 @@ public class SimpleCommandExceptionType implements CommandExceptionType {
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(final Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof CommandExceptionType)) return false; if (!(o instanceof CommandExceptionType)) return false;
CommandExceptionType that = (CommandExceptionType) o; final CommandExceptionType that = (CommandExceptionType) o;
return getTypeName().equals(that.getTypeName()); return getTypeName().equals(that.getTypeName());
} }
@@ -18,7 +18,7 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
private final String name; private final String name;
private final ArgumentType<T> type; private final ArgumentType<T> type;
public ArgumentCommandNode(String name, ArgumentType<T> type, Command<S> command, Predicate<S> requirement) { public ArgumentCommandNode(final String name, final ArgumentType<T> type, final Command<S> command, final Predicate<S> requirement) {
super(command, requirement); super(command, requirement);
this.name = name; this.name = name;
this.type = type; this.type = type;
@@ -51,17 +51,17 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
} }
@Override @Override
public void parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException { public void parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
int start = reader.getCursor(); final int start = reader.getCursor();
T result = type.parse(reader, contextBuilder); final T result = type.parse(reader, contextBuilder);
ParsedArgument<S, T> parsed = new ParsedArgument<>(reader.getString().substring(start, reader.getCursor()), result); final ParsedArgument<S, T> parsed = new ParsedArgument<>(reader.getString().substring(start, reader.getCursor()), result);
contextBuilder.withArgument(name, parsed); contextBuilder.withArgument(name, parsed);
contextBuilder.withNode(this, parsed.getRaw()); contextBuilder.withNode(this, parsed.getRaw());
} }
@Override @Override
public void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) { public void listSuggestions(final String command, final Set<String> output, final CommandContextBuilder<S> contextBuilder) {
type.listSuggestions(command, output, contextBuilder); type.listSuggestions(command, output, contextBuilder);
} }
@@ -76,11 +76,11 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(final Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof ArgumentCommandNode)) return false; if (!(o instanceof ArgumentCommandNode)) return false;
ArgumentCommandNode that = (ArgumentCommandNode) o; final ArgumentCommandNode that = (ArgumentCommandNode) o;
if (!name.equals(that.name)) return false; if (!name.equals(that.name)) return false;
if (!type.equals(that.type)) return false; if (!type.equals(that.type)) return false;
@@ -17,10 +17,10 @@ import java.util.stream.Collectors;
public abstract class CommandNode<S> implements Comparable<CommandNode<S>> { public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
private Map<Object, CommandNode<S>> children = Maps.newLinkedHashMap(); private Map<Object, CommandNode<S>> children = Maps.newLinkedHashMap();
private final Predicate<S> requirement;
private Command<S> command; private Command<S> command;
private Predicate<S> requirement;
protected CommandNode(Command<S> command, Predicate<S> requirement) { protected CommandNode(final Command<S> command, final Predicate<S> requirement) {
this.command = command; this.command = command;
this.requirement = requirement; this.requirement = requirement;
} }
@@ -33,18 +33,18 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
return children.values(); return children.values();
} }
public boolean canUse(S source) { public boolean canUse(final S source) {
return requirement.test(source); return requirement.test(source);
} }
public void addChild(CommandNode<S> node) { public void addChild(final CommandNode<S> node) {
CommandNode<S> child = children.get(node.getMergeKey()); final CommandNode<S> child = children.get(node.getMergeKey());
if (child != null) { if (child != null) {
// We've found something to merge onto // We've found something to merge onto
if (node.getCommand() != null) { if (node.getCommand() != null) {
child.command = node.getCommand(); child.command = node.getCommand();
} }
for (CommandNode<S> grandchild : node.getChildren()) { for (final CommandNode<S> grandchild : node.getChildren()) {
child.addChild(grandchild); child.addChild(grandchild);
} }
} else { } else {
@@ -55,11 +55,11 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(final Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof CommandNode)) return false; if (!(o instanceof CommandNode)) return false;
CommandNode<S> that = (CommandNode<S>) o; final CommandNode<S> that = (CommandNode<S>) o;
if (!children.equals(that.children)) return false; if (!children.equals(that.children)) return false;
if (command != null ? !command.equals(that.command) : that.command != null) return false; if (command != null ? !command.equals(that.command) : that.command != null) return false;
@@ -89,7 +89,7 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
protected abstract String getSortedKey(); protected abstract String getSortedKey();
@Override @Override
public int compareTo(CommandNode<S> o) { public int compareTo(final CommandNode<S> o) {
return ComparisonChain return ComparisonChain
.start() .start()
.compareTrueFirst(this instanceof LiteralCommandNode, o instanceof LiteralCommandNode) .compareTrueFirst(this instanceof LiteralCommandNode, o instanceof LiteralCommandNode)
@@ -1,7 +1,6 @@
package com.mojang.brigadier.tree; package com.mojang.brigadier.tree;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
@@ -16,7 +15,7 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
private final String literal; private final String literal;
public LiteralCommandNode(String literal, Command<S> command, Predicate<S> requirement) { public LiteralCommandNode(final String literal, final Command<S> command, final Predicate<S> requirement) {
super(command, requirement); super(command, requirement);
this.literal = literal; this.literal = literal;
} }
@@ -31,7 +30,7 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
} }
@Override @Override
public void parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException { public void parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
for (int i = 0; i < literal.length(); i++) { for (int i = 0; i < literal.length(); i++) {
if (reader.canRead() && reader.peek() == literal.charAt(i)) { if (reader.canRead() && reader.peek() == literal.charAt(i)) {
reader.skip(); reader.skip();
@@ -44,18 +43,18 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
} }
@Override @Override
public void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) { public void listSuggestions(final String command, final Set<String> output, final CommandContextBuilder<S> contextBuilder) {
if (literal.startsWith(command)) { if (literal.startsWith(command)) {
output.add(literal); output.add(literal);
} }
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(final Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof LiteralCommandNode)) return false; if (!(o instanceof LiteralCommandNode)) return false;
LiteralCommandNode that = (LiteralCommandNode) o; final LiteralCommandNode that = (LiteralCommandNode) o;
if (!literal.equals(that.literal)) return false; if (!literal.equals(that.literal)) return false;
return super.equals(o); return super.equals(o);
@@ -23,15 +23,15 @@ public class RootCommandNode<S> extends CommandNode<S> {
} }
@Override @Override
public void parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException { public void parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
} }
@Override @Override
public void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) { public void listSuggestions(final String command, final Set<String> output, final CommandContextBuilder<S> contextBuilder) {
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(final Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof RootCommandNode)) return false; if (!(o instanceof RootCommandNode)) return false;
return super.equals(o); return super.equals(o);
@@ -6,14 +6,11 @@ import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal; import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
import static org.hamcrest.Matchers.emptyArray; import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class CommandDispatcherCompletionsTest { public class CommandDispatcherCompletionsTest {
@@ -36,9 +33,9 @@ public class CommandDispatcherCompletionsTest {
subject.register(literal("foo")); subject.register(literal("foo"));
subject.register(literal("bar")); subject.register(literal("bar"));
subject.register(literal("baz").requires(s -> false)); subject.register(literal("baz").requires(s -> false));
assertThat(subject.getCompletionSuggestions("", source), equalTo(new String[] {"bar", "foo"})); assertThat(subject.getCompletionSuggestions("", source), equalTo(new String[]{"bar", "foo"}));
assertThat(subject.getCompletionSuggestions("f", source), equalTo(new String[] {"foo"})); assertThat(subject.getCompletionSuggestions("f", source), equalTo(new String[]{"foo"}));
assertThat(subject.getCompletionSuggestions("b", source), equalTo(new String[] {"bar"})); assertThat(subject.getCompletionSuggestions("b", source), equalTo(new String[]{"bar"}));
assertThat(subject.getCompletionSuggestions("q", source), is(emptyArray())); assertThat(subject.getCompletionSuggestions("q", source), is(emptyArray()));
} }
@@ -46,12 +43,12 @@ public class CommandDispatcherCompletionsTest {
public void testSubCommand() throws Exception { public void testSubCommand() throws Exception {
subject.register(literal("foo").then(literal("abc")).then(literal("def")).then(literal("ghi").requires(s -> false))); subject.register(literal("foo").then(literal("abc")).then(literal("def")).then(literal("ghi").requires(s -> false)));
subject.register(literal("bar")); subject.register(literal("bar"));
assertThat(subject.getCompletionSuggestions("", source), equalTo(new String[] {"bar", "foo"})); assertThat(subject.getCompletionSuggestions("", source), equalTo(new String[]{"bar", "foo"}));
assertThat(subject.getCompletionSuggestions("f", source), equalTo(new String[] {"foo"})); assertThat(subject.getCompletionSuggestions("f", source), equalTo(new String[]{"foo"}));
assertThat(subject.getCompletionSuggestions("foo", source), equalTo(new String[] {"foo"})); assertThat(subject.getCompletionSuggestions("foo", source), equalTo(new String[]{"foo"}));
assertThat(subject.getCompletionSuggestions("foo ", source), equalTo(new String[] {"abc", "def"})); assertThat(subject.getCompletionSuggestions("foo ", source), equalTo(new String[]{"abc", "def"}));
assertThat(subject.getCompletionSuggestions("foo a", source), equalTo(new String[] {"abc"})); assertThat(subject.getCompletionSuggestions("foo a", source), equalTo(new String[]{"abc"}));
assertThat(subject.getCompletionSuggestions("foo d", source), equalTo(new String[] {"def"})); assertThat(subject.getCompletionSuggestions("foo d", source), equalTo(new String[]{"def"}));
assertThat(subject.getCompletionSuggestions("foo g", source), is(emptyArray())); assertThat(subject.getCompletionSuggestions("foo g", source), is(emptyArray()));
} }
} }
@@ -1,10 +1,7 @@
package com.mojang.brigadier; package com.mojang.brigadier;
import com.google.common.collect.ImmutableMap;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.CommandExceptionType;
import com.mojang.brigadier.tree.LiteralCommandNode; import com.mojang.brigadier.tree.LiteralCommandNode;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -13,7 +10,6 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collections; import java.util.Collections;
import java.util.Map;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal; import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
@@ -21,7 +17,11 @@ import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class CommandDispatcherTest { public class CommandDispatcherTest {
@@ -65,7 +65,7 @@ public class CommandDispatcherTest {
try { try {
subject.execute("foo", source); subject.execute("foo", source);
fail(); fail();
} catch (CommandException ex) { } catch (final CommandException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND)); assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND));
assertThat(ex.getData(), is(Collections.<String, Object>emptyMap())); assertThat(ex.getData(), is(Collections.<String, Object>emptyMap()));
} }
@@ -78,7 +78,7 @@ public class CommandDispatcherTest {
try { try {
subject.execute("foo", source); subject.execute("foo", source);
fail(); fail();
} catch (CommandException ex) { } catch (final CommandException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND)); assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND));
assertThat(ex.getData(), is(Collections.<String, Object>emptyMap())); assertThat(ex.getData(), is(Collections.<String, Object>emptyMap()));
} }
@@ -91,7 +91,7 @@ public class CommandDispatcherTest {
try { try {
subject.execute("", source); subject.execute("", source);
fail(); fail();
} catch (CommandException ex) { } catch (final CommandException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND)); assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND));
assertThat(ex.getData(), is(Collections.<String, Object>emptyMap())); assertThat(ex.getData(), is(Collections.<String, Object>emptyMap()));
} }
@@ -104,7 +104,7 @@ public class CommandDispatcherTest {
try { try {
subject.execute("foo bar", source); subject.execute("foo bar", source);
fail(); fail();
} catch (CommandException ex) { } catch (final CommandException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_ARGUMENT)); assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_ARGUMENT));
assertThat(ex.getData(), is(Collections.singletonMap("argument", "bar"))); assertThat(ex.getData(), is(Collections.singletonMap("argument", "bar")));
} }
@@ -117,7 +117,7 @@ public class CommandDispatcherTest {
try { try {
subject.execute("foo baz", source); subject.execute("foo baz", source);
fail(); fail();
} catch (CommandException ex) { } catch (final CommandException ex) {
assertThat(ex.getType(), is(LiteralCommandNode.ERROR_INCORRECT_LITERAL)); assertThat(ex.getType(), is(LiteralCommandNode.ERROR_INCORRECT_LITERAL));
assertThat(ex.getData(), is(Collections.singletonMap("expected", "bar"))); assertThat(ex.getData(), is(Collections.singletonMap("expected", "bar")));
} }
@@ -134,7 +134,7 @@ public class CommandDispatcherTest {
try { try {
subject.execute("foo unknown", source); subject.execute("foo unknown", source);
fail(); fail();
} catch (CommandException ex) { } catch (final CommandException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_ARGUMENT)); assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_ARGUMENT));
assertThat(ex.getData(), is(Collections.singletonMap("argument", "unknown"))); assertThat(ex.getData(), is(Collections.singletonMap("argument", "unknown")));
} }
@@ -143,7 +143,7 @@ public class CommandDispatcherTest {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testExecuteSubcommand() throws Exception { public void testExecuteSubcommand() throws Exception {
Command<Object> subCommand = mock(Command.class); final Command<Object> subCommand = mock(Command.class);
when(subCommand.run(any())).thenReturn(100); when(subCommand.run(any())).thenReturn(100);
subject.register(literal("foo").then( subject.register(literal("foo").then(
@@ -167,7 +167,7 @@ public class CommandDispatcherTest {
try { try {
subject.execute("foo bar", source); subject.execute("foo bar", source);
fail(); fail();
} catch (CommandException ex) { } catch (final CommandException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_INT)); assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_INT));
assertThat(ex.getData(), is(Collections.emptyMap())); assertThat(ex.getData(), is(Collections.emptyMap()));
} }
@@ -2,7 +2,6 @@ package com.mojang.brigadier;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterators; import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.CommandNode;
import org.junit.Before; import org.junit.Before;
@@ -13,15 +12,12 @@ import org.mockito.runners.MockitoJUnitRunner;
import java.util.Map; import java.util.Map;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal; import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyArray; import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class CommandDispatcherUsagesTest { public class CommandDispatcherUsagesTest {
@@ -93,10 +89,10 @@ public class CommandDispatcherUsagesTest {
); );
} }
private CommandNode<Object> get(String command) { private CommandNode<Object> get(final String command) {
try { try {
return Iterators.getLast(subject.parse(command, source).getContext().getNodes().keySet().iterator()); return Iterators.getLast(subject.parse(command, source).getContext().getNodes().keySet().iterator());
} catch (CommandException e) { } catch (final CommandException e) {
throw new AssertionError("get() failed unexpectedly", e); throw new AssertionError("get() failed unexpectedly", e);
} }
} }
@@ -118,7 +114,7 @@ public class CommandDispatcherUsagesTest {
@Test @Test
public void testAllUsage_root() throws Exception { public void testAllUsage_root() throws Exception {
final String[] results = subject.getAllUsage(subject.getRoot(), source); final String[] results = subject.getAllUsage(subject.getRoot(), source);
assertThat(results, equalTo(new String[] { assertThat(results, equalTo(new String[]{
"a 1 i", "a 1 i",
"a 1 ii", "a 1 ii",
"a 2 i", "a 2 i",
@@ -1,9 +1,7 @@
package com.mojang.brigadier; package com.mojang.brigadier;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.CommandException;
import org.hamcrest.CoreMatchers;
import org.junit.Test; import org.junit.Test;
import java.util.Collections; import java.util.Collections;
@@ -15,7 +13,7 @@ import static org.junit.Assert.assertThat;
public class StringReaderTest { public class StringReaderTest {
@Test @Test
public void canRead() throws Exception { public void canRead() throws Exception {
StringReader reader = new StringReader("abc"); final StringReader reader = new StringReader("abc");
assertThat(reader.canRead(), is(true)); assertThat(reader.canRead(), is(true));
reader.skip(); // 'a' reader.skip(); // 'a'
assertThat(reader.canRead(), is(true)); assertThat(reader.canRead(), is(true));
@@ -27,7 +25,7 @@ public class StringReaderTest {
@Test @Test
public void getRemainingLength() throws Exception { public void getRemainingLength() throws Exception {
StringReader reader = new StringReader("abc"); final StringReader reader = new StringReader("abc");
assertThat(reader.getRemainingLength(), is(3)); assertThat(reader.getRemainingLength(), is(3));
reader.setCursor(1); reader.setCursor(1);
assertThat(reader.getRemainingLength(), is(2)); assertThat(reader.getRemainingLength(), is(2));
@@ -39,7 +37,7 @@ public class StringReaderTest {
@Test @Test
public void canRead_length() throws Exception { public void canRead_length() throws Exception {
StringReader reader = new StringReader("abc"); final StringReader reader = new StringReader("abc");
assertThat(reader.canRead(1), is(true)); assertThat(reader.canRead(1), is(true));
assertThat(reader.canRead(2), is(true)); assertThat(reader.canRead(2), is(true));
assertThat(reader.canRead(3), is(true)); assertThat(reader.canRead(3), is(true));
@@ -49,7 +47,7 @@ public class StringReaderTest {
@Test @Test
public void peek() throws Exception { public void peek() throws Exception {
StringReader reader = new StringReader("abc"); final StringReader reader = new StringReader("abc");
assertThat(reader.peek(), is('a')); assertThat(reader.peek(), is('a'));
assertThat(reader.getCursor(), is(0)); assertThat(reader.getCursor(), is(0));
reader.setCursor(2); reader.setCursor(2);
@@ -59,7 +57,7 @@ public class StringReaderTest {
@Test @Test
public void read() throws Exception { public void read() throws Exception {
StringReader reader = new StringReader("abc"); final StringReader reader = new StringReader("abc");
assertThat(reader.read(), is('a')); assertThat(reader.read(), is('a'));
assertThat(reader.read(), is('b')); assertThat(reader.read(), is('b'));
assertThat(reader.read(), is('c')); assertThat(reader.read(), is('c'));
@@ -68,14 +66,14 @@ public class StringReaderTest {
@Test @Test
public void skip() throws Exception { public void skip() throws Exception {
StringReader reader = new StringReader("abc"); final StringReader reader = new StringReader("abc");
reader.skip(); reader.skip();
assertThat(reader.getCursor(), is(1)); assertThat(reader.getCursor(), is(1));
} }
@Test @Test
public void getRemaining() throws Exception { public void getRemaining() throws Exception {
StringReader reader = new StringReader("Hello!"); final StringReader reader = new StringReader("Hello!");
assertThat(reader.getRemaining(), equalTo("Hello!")); assertThat(reader.getRemaining(), equalTo("Hello!"));
reader.setCursor(3); reader.setCursor(3);
assertThat(reader.getRemaining(), equalTo("lo!")); assertThat(reader.getRemaining(), equalTo("lo!"));
@@ -85,7 +83,7 @@ public class StringReaderTest {
@Test @Test
public void getRead() throws Exception { public void getRead() throws Exception {
StringReader reader = new StringReader("Hello!"); final StringReader reader = new StringReader("Hello!");
assertThat(reader.getRead(), equalTo("")); assertThat(reader.getRead(), equalTo(""));
reader.setCursor(3); reader.setCursor(3);
assertThat(reader.getRead(), equalTo("Hel")); assertThat(reader.getRead(), equalTo("Hel"));
@@ -2,22 +2,16 @@ package com.mojang.brigadier.arguments;
import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collections;
import static com.mojang.brigadier.arguments.BoolArgumentType.ERROR_INVALID;
import static com.mojang.brigadier.arguments.BoolArgumentType.bool; import static com.mojang.brigadier.arguments.BoolArgumentType.bool;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*; import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -35,7 +29,7 @@ public class BoolArgumentTypeTest {
@Test @Test
public void parse() throws Exception { public void parse() throws Exception {
StringReader reader = mock(StringReader.class); final StringReader reader = mock(StringReader.class);
when(reader.readBoolean()).thenReturn(true); when(reader.readBoolean()).thenReturn(true);
assertThat(type.parse(reader, context), is(true)); assertThat(type.parse(reader, context), is(true));
verify(reader).readBoolean(); verify(reader).readBoolean();
@@ -36,25 +36,25 @@ public class IntegerArgumentTypeTest {
@Test @Test
public void parse_noSuffix() throws Exception { public void parse_noSuffix() throws Exception {
StringReader reader = new StringReader("15"); final StringReader reader = new StringReader("15");
assertThat(integer().parse(reader, context), is(15)); assertThat(integer().parse(reader, context), is(15));
assertThat(reader.canRead(), is(false)); assertThat(reader.canRead(), is(false));
} }
@Test @Test
public void parse_suffix() throws Exception { public void parse_suffix() throws Exception {
StringReader reader = new StringReader("15L"); final StringReader reader = new StringReader("15L");
assertThat(integer(0, 100, "L").parse(reader, context), is(15)); assertThat(integer(0, 100, "L").parse(reader, context), is(15));
assertThat(reader.canRead(), is(false)); assertThat(reader.canRead(), is(false));
} }
@Test @Test
public void parse_suffix_incorrect() throws Exception { public void parse_suffix_incorrect() throws Exception {
StringReader reader = new StringReader("15W"); final StringReader reader = new StringReader("15W");
try { try {
integer(0, 100, "L").parse(reader, context); integer(0, 100, "L").parse(reader, context);
fail(); fail();
} catch (CommandException ex) { } catch (final CommandException ex) {
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_WRONG_SUFFIX)); assertThat(ex.getType(), is(IntegerArgumentType.ERROR_WRONG_SUFFIX));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("suffix", "L"))); assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("suffix", "L")));
} }
@@ -62,11 +62,11 @@ public class IntegerArgumentTypeTest {
@Test @Test
public void parse_suffix_missing() throws Exception { public void parse_suffix_missing() throws Exception {
StringReader reader = new StringReader("15"); final StringReader reader = new StringReader("15");
try { try {
integer(0, 100, "L").parse(reader, context); integer(0, 100, "L").parse(reader, context);
fail(); fail();
} catch (CommandException ex) { } catch (final CommandException ex) {
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_WRONG_SUFFIX)); assertThat(ex.getType(), is(IntegerArgumentType.ERROR_WRONG_SUFFIX));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("suffix", "L"))); assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("suffix", "L")));
} }
@@ -74,11 +74,11 @@ public class IntegerArgumentTypeTest {
@Test @Test
public void parse_tooSmall() throws Exception { public void parse_tooSmall() throws Exception {
StringReader reader = new StringReader("-5"); final StringReader reader = new StringReader("-5");
try { try {
integer(0, 100).parse(reader, context); integer(0, 100).parse(reader, context);
fail(); fail();
} catch (CommandException ex) { } catch (final CommandException ex) {
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_SMALL)); assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_SMALL));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("found", -5, "minimum", 0))); assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("found", -5, "minimum", 0)));
} }
@@ -86,11 +86,11 @@ public class IntegerArgumentTypeTest {
@Test @Test
public void parse_tooBig() throws Exception { public void parse_tooBig() throws Exception {
StringReader reader = new StringReader("5"); final StringReader reader = new StringReader("5");
try { try {
integer(-100, 0).parse(reader, context); integer(-100, 0).parse(reader, context);
fail(); fail();
} catch (CommandException ex) { } catch (final CommandException ex) {
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_BIG)); assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_BIG));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("found", 5, "maximum", 0))); assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("found", 5, "maximum", 0)));
} }
@@ -98,7 +98,7 @@ public class IntegerArgumentTypeTest {
@Test @Test
public void testSuggestions() throws Exception { public void testSuggestions() throws Exception {
Set<String> set = Sets.newHashSet(); final Set<String> set = Sets.newHashSet();
@SuppressWarnings("unchecked") final CommandContextBuilder<Object> context = Mockito.mock(CommandContextBuilder.class); @SuppressWarnings("unchecked") final CommandContextBuilder<Object> context = Mockito.mock(CommandContextBuilder.class);
type.listSuggestions("", set, context); type.listSuggestions("", set, context);
assertThat(set, is(empty())); assertThat(set, is(empty()));
@@ -30,7 +30,7 @@ public class StringArgumentTypeTest {
@Test @Test
public void testParseWord() throws Exception { public void testParseWord() throws Exception {
StringReader reader = mock(StringReader.class); final StringReader reader = mock(StringReader.class);
when(reader.readUnquotedString()).thenReturn("hello"); when(reader.readUnquotedString()).thenReturn("hello");
assertThat(word().parse(reader, context), equalTo("hello")); assertThat(word().parse(reader, context), equalTo("hello"));
verify(reader).readUnquotedString(); verify(reader).readUnquotedString();
@@ -38,7 +38,7 @@ public class StringArgumentTypeTest {
@Test @Test
public void testParseString() throws Exception { public void testParseString() throws Exception {
StringReader reader = mock(StringReader.class); final StringReader reader = mock(StringReader.class);
when(reader.readString()).thenReturn("hello world"); when(reader.readString()).thenReturn("hello world");
assertThat(string().parse(reader, context), equalTo("hello world")); assertThat(string().parse(reader, context), equalTo("hello world"));
verify(reader).readString(); verify(reader).readString();
@@ -46,14 +46,14 @@ public class StringArgumentTypeTest {
@Test @Test
public void testParseGreedyString() throws Exception { public void testParseGreedyString() throws Exception {
StringReader reader = new StringReader("Hello world! This is a test."); final StringReader reader = new StringReader("Hello world! This is a test.");
assertThat(greedyString().parse(reader, context), equalTo("Hello world! This is a test.")); assertThat(greedyString().parse(reader, context), equalTo("Hello world! This is a test."));
assertThat(reader.canRead(), is(false)); assertThat(reader.canRead(), is(false));
} }
@Test @Test
public void testSuggestions() throws Exception { public void testSuggestions() throws Exception {
Set<String> set = Sets.newHashSet(); final Set<String> set = Sets.newHashSet();
string().listSuggestions("", set, context); string().listSuggestions("", set, context);
assertThat(set, is(empty())); assertThat(set, is(empty()));
} }
@@ -6,7 +6,6 @@ import org.junit.Test;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument; import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.IsCollectionContaining.hasItem; import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@@ -21,7 +20,7 @@ public class ArgumentBuilderTest {
@Test @Test
public void testArguments() throws Exception { public void testArguments() throws Exception {
RequiredArgumentBuilder<Object, ?> argument = argument("bar", integer()); final RequiredArgumentBuilder<Object, ?> argument = argument("bar", integer());
builder.then(argument); builder.then(argument);
@@ -25,14 +25,14 @@ public class LiteralArgumentBuilderTest {
@Test @Test
public void testBuild() throws Exception { public void testBuild() throws Exception {
LiteralCommandNode<Object> node = builder.build(); final LiteralCommandNode<Object> node = builder.build();
assertThat(node.getLiteral(), is("foo")); assertThat(node.getLiteral(), is("foo"));
} }
@Test @Test
public void testBuildWithExecutor() throws Exception { public void testBuildWithExecutor() throws Exception {
LiteralCommandNode<Object> node = builder.executes(command).build(); final LiteralCommandNode<Object> node = builder.executes(command).build();
assertThat(node.getLiteral(), is("foo")); assertThat(node.getLiteral(), is("foo"));
assertThat(node.getCommand(), is(command)); assertThat(node.getCommand(), is(command));
@@ -42,7 +42,7 @@ public class LiteralArgumentBuilderTest {
public void testBuildWithChildren() throws Exception { public void testBuildWithChildren() throws Exception {
builder.then(argument("bar", integer())); builder.then(argument("bar", integer()));
builder.then(argument("baz", integer())); builder.then(argument("baz", integer()));
LiteralCommandNode<Object> node = builder.build(); final LiteralCommandNode<Object> node = builder.build();
assertThat(node.getChildren(), hasSize(2)); assertThat(node.getChildren(), hasSize(2));
} }
@@ -28,7 +28,7 @@ public class RequiredArgumentBuilderTest {
@Test @Test
public void testBuild() throws Exception { public void testBuild() throws Exception {
ArgumentCommandNode<Object, Integer> node = builder.build(); final ArgumentCommandNode<Object, Integer> node = builder.build();
assertThat(node.getName(), is("foo")); assertThat(node.getName(), is("foo"));
assertThat(node.getType(), is(type)); assertThat(node.getType(), is(type));
@@ -36,7 +36,7 @@ public class RequiredArgumentBuilderTest {
@Test @Test
public void testBuildWithExecutor() throws Exception { public void testBuildWithExecutor() throws Exception {
ArgumentCommandNode<Object, Integer> node = builder.executes(command).build(); final ArgumentCommandNode<Object, Integer> node = builder.executes(command).build();
assertThat(node.getName(), is("foo")); assertThat(node.getName(), is("foo"));
assertThat(node.getType(), is(type)); assertThat(node.getType(), is(type));
@@ -47,7 +47,7 @@ public class RequiredArgumentBuilderTest {
public void testBuildWithChildren() throws Exception { public void testBuildWithChildren() throws Exception {
builder.then(argument("bar", integer())); builder.then(argument("bar", integer()));
builder.then(argument("baz", integer())); builder.then(argument("baz", integer()));
ArgumentCommandNode<Object, Integer> node = builder.build(); final ArgumentCommandNode<Object, Integer> node = builder.build();
assertThat(node.getChildren(), hasSize(2)); assertThat(node.getChildren(), hasSize(2));
} }
@@ -37,13 +37,13 @@ public class CommandContextTest {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testGetArgument_wrongType() throws Exception { public void testGetArgument_wrongType() throws Exception {
CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>("123", 123)).build(); final CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>("123", 123)).build();
context.getArgument("foo", String.class); context.getArgument("foo", String.class);
} }
@Test @Test
public void testGetArgument() throws Exception { public void testGetArgument() throws Exception {
CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>("123", 123)).build(); final CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>("123", 123)).build();
assertThat(context.getArgument("foo", int.class), is(123)); assertThat(context.getArgument("foo", int.class), is(123));
} }
@@ -55,11 +55,11 @@ public class CommandContextTest {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testEquals() throws Exception { public void testEquals() throws Exception {
Object otherSource = new Object(); final Object otherSource = new Object();
Command<Object> command = mock(Command.class); final Command<Object> command = mock(Command.class);
Command<Object> otherCommand = mock(Command.class); final Command<Object> otherCommand = mock(Command.class);
CommandNode<Object> node = mock(CommandNode.class); final CommandNode<Object> node = mock(CommandNode.class);
CommandNode<Object> otherNode = mock(CommandNode.class); final CommandNode<Object> otherNode = mock(CommandNode.class);
new EqualsTester() new EqualsTester()
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).build(), new CommandContextBuilder<>(dispatcher, source).build()) .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).build(), new CommandContextBuilder<>(dispatcher, source).build())
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, otherSource).build(), new CommandContextBuilder<>(dispatcher, otherSource).build()) .addEqualityGroup(new CommandContextBuilder<>(dispatcher, otherSource).build(), new CommandContextBuilder<>(dispatcher, otherSource).build())
@@ -73,7 +73,7 @@ public class CommandContextTest {
@Test @Test
public void testGetInput() throws Exception { public void testGetInput() throws Exception {
CommandContext<Object> context = builder.withNode(literal("foo").build(), "foo").withNode(argument("bar", integer()).build(), "100").withNode(literal("baz").build(), "baz").build(); final CommandContext<Object> context = builder.withNode(literal("foo").build(), "foo").withNode(argument("bar", integer()).build(), "100").withNode(literal("baz").build(), "baz").build();
assertThat(context.getInput(), is("foo 100 baz")); assertThat(context.getInput(), is("foo 100 baz"));
} }
@@ -5,8 +5,6 @@ import com.google.common.testing.EqualsTester;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.Map;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@@ -31,7 +29,7 @@ public class ParameterizedCommandExceptionTypeTest {
@Test @Test
public void testCreate() throws Exception { public void testCreate() throws Exception {
CommandException exception = type.create("World"); final CommandException exception = type.create("World");
assertThat(exception.getType(), is(type)); assertThat(exception.getType(), is(type));
assertThat(exception.getData(), is(ImmutableMap.<String, Object>of("name", "World"))); assertThat(exception.getData(), is(ImmutableMap.<String, Object>of("name", "World")));
assertThat(exception.getMessage(), is("Hello, World!")); assertThat(exception.getMessage(), is("Hello, World!"));
@@ -11,8 +11,8 @@ import static org.junit.Assert.assertThat;
public class SimpleCommandExceptionTypeTest { public class SimpleCommandExceptionTypeTest {
@Test @Test
public void testCreate() throws Exception { public void testCreate() throws Exception {
SimpleCommandExceptionType type = new SimpleCommandExceptionType("foo", "bar"); final SimpleCommandExceptionType type = new SimpleCommandExceptionType("foo", "bar");
CommandException exception = type.create(); final CommandException exception = type.create();
assertThat(exception.getType(), is(type)); assertThat(exception.getType(), is(type));
assertThat(exception.getMessage(), is("bar")); assertThat(exception.getMessage(), is("bar"));
assertThat(exception.getData().values(), empty()); assertThat(exception.getData().values(), empty());
@@ -13,13 +13,14 @@ import static org.junit.Assert.assertThat;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public abstract class AbstractCommandNodeTest { public abstract class AbstractCommandNodeTest {
@Mock private Command command; @Mock
private Command command;
protected abstract CommandNode<Object> getCommandNode(); protected abstract CommandNode<Object> getCommandNode();
@Test @Test
public void testAddChild() throws Exception { public void testAddChild() throws Exception {
CommandNode<Object> node = getCommandNode(); final CommandNode<Object> node = getCommandNode();
node.addChild(literal("child1").build()); node.addChild(literal("child1").build());
node.addChild(literal("child2").build()); node.addChild(literal("child2").build());
@@ -30,7 +31,7 @@ public abstract class AbstractCommandNodeTest {
@Test @Test
public void testAddChildMergesGrandchildren() throws Exception { public void testAddChildMergesGrandchildren() throws Exception {
CommandNode<Object> node = getCommandNode(); final CommandNode<Object> node = getCommandNode();
node.addChild(literal("child").then( node.addChild(literal("child").then(
literal("grandchild1") literal("grandchild1")
@@ -46,7 +47,7 @@ public abstract class AbstractCommandNodeTest {
@Test @Test
public void testAddChildPreservesCommand() throws Exception { public void testAddChildPreservesCommand() throws Exception {
CommandNode<Object> node = getCommandNode(); final CommandNode<Object> node = getCommandNode();
node.addChild(literal("child").executes(command).build()); node.addChild(literal("child").executes(command).build());
node.addChild(literal("child").build()); node.addChild(literal("child").build());
@@ -56,7 +57,7 @@ public abstract class AbstractCommandNodeTest {
@Test @Test
public void testAddChildOverwritesCommand() throws Exception { public void testAddChildOverwritesCommand() throws Exception {
CommandNode<Object> node = getCommandNode(); final CommandNode<Object> node = getCommandNode();
node.addChild(literal("child").build()); node.addChild(literal("child").build());
node.addChild(literal("child").executes(command).build()); node.addChild(literal("child").executes(command).build());
@@ -1,16 +1,13 @@
package com.mojang.brigadier.tree; package com.mojang.brigadier.tree;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.common.testing.EqualsTester; import com.google.common.testing.EqualsTester;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandException;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
@@ -22,14 +19,12 @@ import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
public class ArgumentCommandNodeTest extends AbstractCommandNodeTest { public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
private ArgumentCommandNode<Object, Integer> node; private ArgumentCommandNode<Object, Integer> node;
private CommandContextBuilder<Object> contextBuilder; private CommandContextBuilder<Object> contextBuilder;
private Object source = new Object();
@Override @Override
protected CommandNode<Object> getCommandNode() { protected CommandNode<Object> getCommandNode() {
@@ -44,7 +39,7 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
@Test @Test
public void testParse() throws Exception { public void testParse() throws Exception {
StringReader reader = new StringReader("123 456"); final StringReader reader = new StringReader("123 456");
node.parse(reader, contextBuilder); node.parse(reader, contextBuilder);
assertThat(contextBuilder.getArguments().containsKey("foo"), is(true)); assertThat(contextBuilder.getArguments().containsKey("foo"), is(true));
@@ -64,15 +59,15 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
@Test @Test
public void testUsage_empty() throws Exception { public void testUsage_empty() throws Exception {
@SuppressWarnings("unchecked") ArgumentType<String> type = mock(ArgumentType.class); @SuppressWarnings("unchecked") final ArgumentType<String> type = mock(ArgumentType.class);
when(type.getUsageText()).thenReturn(null); when(type.getUsageText()).thenReturn(null);
ArgumentCommandNode<Object, String> node = argument("foo", type).build(); final ArgumentCommandNode<Object, String> node = argument("foo", type).build();
assertThat(node.getUsageText(), is("<foo>")); assertThat(node.getUsageText(), is("<foo>"));
} }
@Test @Test
public void testSuggestions() throws Exception { public void testSuggestions() throws Exception {
Set<String> set = Sets.newHashSet(); final Set<String> set = Sets.newHashSet();
@SuppressWarnings("unchecked") final CommandContextBuilder<Object> context = Mockito.mock(CommandContextBuilder.class); @SuppressWarnings("unchecked") final CommandContextBuilder<Object> context = Mockito.mock(CommandContextBuilder.class);
node.listSuggestions("", set, context); node.listSuggestions("", set, context);
assertThat(set, is(empty())); assertThat(set, is(empty()));
@@ -80,7 +75,7 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
@Test @Test
public void testEquals() throws Exception { public void testEquals() throws Exception {
@SuppressWarnings("unchecked") Command<Object> command = (Command<Object>) mock(Command.class); @SuppressWarnings("unchecked") final Command<Object> command = (Command<Object>) mock(Command.class);
new EqualsTester() new EqualsTester()
.addEqualityGroup( .addEqualityGroup(
@@ -40,21 +40,21 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
@Test @Test
public void testParse() throws Exception { public void testParse() throws Exception {
StringReader reader = new StringReader("foo bar"); final StringReader reader = new StringReader("foo bar");
node.parse(reader, contextBuilder); node.parse(reader, contextBuilder);
assertThat(reader.getRemaining(), equalTo(" bar")); assertThat(reader.getRemaining(), equalTo(" bar"));
} }
@Test @Test
public void testParseExact() throws Exception { public void testParseExact() throws Exception {
StringReader reader = new StringReader("foo"); final StringReader reader = new StringReader("foo");
node.parse(reader, contextBuilder); node.parse(reader, contextBuilder);
assertThat(reader.getRemaining(), equalTo("")); assertThat(reader.getRemaining(), equalTo(""));
} }
@Test @Test
public void testParseSimilar() throws Exception { public void testParseSimilar() throws Exception {
StringReader reader = new StringReader("foobar"); final StringReader reader = new StringReader("foobar");
node.parse(reader, contextBuilder); node.parse(reader, contextBuilder);
assertThat(reader.getRemaining(), equalTo("bar")); assertThat(reader.getRemaining(), equalTo("bar"));
// This should succeed, because it's the responsibility of the dispatcher to realize there's trailing text // This should succeed, because it's the responsibility of the dispatcher to realize there's trailing text
@@ -62,11 +62,11 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
@Test @Test
public void testParseInvalid() throws Exception { public void testParseInvalid() throws Exception {
StringReader reader = new StringReader("bar"); final StringReader reader = new StringReader("bar");
try { try {
node.parse(reader, contextBuilder); node.parse(reader, contextBuilder);
fail(); fail();
} catch (CommandException ex) { } catch (final CommandException ex) {
assertThat(ex.getType(), is(LiteralCommandNode.ERROR_INCORRECT_LITERAL)); assertThat(ex.getType(), is(LiteralCommandNode.ERROR_INCORRECT_LITERAL));
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("expected", "foo"))); assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("expected", "foo")));
} }
@@ -79,7 +79,7 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
@Test @Test
public void testSuggestions() throws Exception { public void testSuggestions() throws Exception {
Set<String> set = Sets.newHashSet(); final Set<String> set = Sets.newHashSet();
@SuppressWarnings("unchecked") final CommandContextBuilder<Object> context = Mockito.mock(CommandContextBuilder.class); @SuppressWarnings("unchecked") final CommandContextBuilder<Object> context = Mockito.mock(CommandContextBuilder.class);
node.listSuggestions("", set, context); node.listSuggestions("", set, context);
@@ -100,7 +100,7 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
@Test @Test
public void testEquals() throws Exception { public void testEquals() throws Exception {
@SuppressWarnings("unchecked") Command<Object> command = mock(Command.class); @SuppressWarnings("unchecked") final Command<Object> command = mock(Command.class);
new EqualsTester() new EqualsTester()
.addEqualityGroup( .addEqualityGroup(
@@ -31,7 +31,7 @@ public class RootCommandNodeTest extends AbstractCommandNodeTest {
@Test @Test
public void testParse() throws Exception { public void testParse() throws Exception {
StringReader reader = new StringReader("hello world"); final StringReader reader = new StringReader("hello world");
node.parse(reader, new CommandContextBuilder<>(new CommandDispatcher<>(), new Object())); node.parse(reader, new CommandContextBuilder<>(new CommandDispatcher<>(), new Object()));
assertThat(reader.getCursor(), is(0)); assertThat(reader.getCursor(), is(0));
} }
@@ -48,7 +48,7 @@ public class RootCommandNodeTest extends AbstractCommandNodeTest {
@Test @Test
public void testSuggestions() throws Exception { public void testSuggestions() throws Exception {
Set<String> set = Sets.newHashSet(); final Set<String> set = Sets.newHashSet();
@SuppressWarnings("unchecked") final CommandContextBuilder<Object> context = Mockito.mock(CommandContextBuilder.class); @SuppressWarnings("unchecked") final CommandContextBuilder<Object> context = Mockito.mock(CommandContextBuilder.class);
node.listSuggestions("", set, context); node.listSuggestions("", set, context);
assertThat(set, is(empty())); assertThat(set, is(empty()));