Added bool argument

This commit is contained in:
Nathan Adams 2017-07-20 13:57:48 +02:00
parent eaf4c6175d
commit 74f7c0e6b0
2 changed files with 136 additions and 0 deletions

View file

@ -0,0 +1,46 @@
package com.mojang.brigadier.arguments;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.FixedParsedArgument;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
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() {
}
public static BoolArgumentType bool() {
return new BoolArgumentType();
}
public static boolean getBool(CommandContext<?> context, String name) {
return context.getArgument(name, Boolean.class);
}
@Override
public <S> ParsedArgument<S, Boolean> parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException {
int end = command.indexOf(CommandDispatcher.ARGUMENT_SEPARATOR);
String raw = command;
if (end > -1) {
raw = command.substring(0, end);
}
if (raw.equals("true")) {
return new FixedParsedArgument<>(raw, true);
} else if (raw.equals("false")) {
return new FixedParsedArgument<>(raw, false);
} else {
throw ERROR_INVALID.create();
}
}
@Override
public String getUsageText() {
return "bool";
}
}

View file

@ -0,0 +1,90 @@
package com.mojang.brigadier.arguments;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
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 org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
@RunWith(MockitoJUnitRunner.class)
public class BoolArgumentTypeTest {
private BoolArgumentType type;
@Mock
private Object source;
@Mock
private CommandContextBuilder<Object> context;
@Before
public void setUp() throws Exception {
type = bool();
}
@Test
public void parse_true() throws Exception {
ParsedArgument<Object, Boolean> parse = type.parse("true", context);
assertThat(parse.getResult(source), is(true));
assertThat(parse.getRaw(), equalTo("true"));
}
@Test
public void parse_false() throws Exception {
ParsedArgument<Object, Boolean> parse = type.parse("false", context);
assertThat(parse.getResult(source), is(false));
assertThat(parse.getRaw(), equalTo("false"));
}
@Test
public void parse_trailing() throws Exception {
ParsedArgument<Object, Boolean> parse = type.parse("false hello world", context);
assertThat(parse.getResult(source), is(false));
assertThat(parse.getRaw(), equalTo("false"));
}
@Test
public void parse_invalid() throws Exception {
try {
type.parse("tuesday", context);
} catch (CommandException ex) {
assertThat(ex.getType(), is(ERROR_INVALID));
assertThat(ex.getData(), equalTo(Collections.emptyMap()));
}
}
@Test
public void parse_empty() throws Exception {
try {
type.parse("", context);
} catch (CommandException ex) {
assertThat(ex.getType(), is(ERROR_INVALID));
assertThat(ex.getData(), equalTo(Collections.emptyMap()));
}
}
@Test
public void parse_empty_remaining() throws Exception {
try {
type.parse(" true", context);
} catch (CommandException ex) {
assertThat(ex.getType(), is(ERROR_INVALID));
assertThat(ex.getData(), equalTo(Collections.emptyMap()));
}
}
@Test
public void usageText() throws Exception {
assertThat(type.getUsageText(), equalTo("bool"));
}
}