diff --git a/src/main/java/com/mojang/brigadier/arguments/BoolArgumentType.java b/src/main/java/com/mojang/brigadier/arguments/BoolArgumentType.java new file mode 100644 index 0000000..2bc6d02 --- /dev/null +++ b/src/main/java/com/mojang/brigadier/arguments/BoolArgumentType.java @@ -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 { + 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 ParsedArgument parse(String command, CommandContextBuilder 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"; + } +} diff --git a/src/test/java/com/mojang/brigadier/arguments/BoolArgumentTypeTest.java b/src/test/java/com/mojang/brigadier/arguments/BoolArgumentTypeTest.java new file mode 100644 index 0000000..57dc6e0 --- /dev/null +++ b/src/test/java/com/mojang/brigadier/arguments/BoolArgumentTypeTest.java @@ -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 context; + + @Before + public void setUp() throws Exception { + type = bool(); + } + + @Test + public void parse_true() throws Exception { + ParsedArgument parse = type.parse("true", context); + assertThat(parse.getResult(source), is(true)); + assertThat(parse.getRaw(), equalTo("true")); + } + + @Test + public void parse_false() throws Exception { + ParsedArgument parse = type.parse("false", context); + assertThat(parse.getResult(source), is(false)); + assertThat(parse.getRaw(), equalTo("false")); + } + + @Test + public void parse_trailing() throws Exception { + ParsedArgument 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")); + } +} \ No newline at end of file