From f1c41dc9ed13da51cbb1953c3dce3f972406830e Mon Sep 17 00:00:00 2001 From: Rollczi Date: Thu, 16 Nov 2023 13:44:41 +0100 Subject: [PATCH] Add boolean argument. Add javadocs for InvalidUsage.Cause. --- .../standard/BooleanArgumentTest.java | 73 +++++++++++++++++++ .../argument/parser/ParseResult.java | 1 + .../standard/BooleanArgumentResolver.java | 62 ++++++++++++++++ .../invalidusage/InvalidUsage.java | 40 ++++++++++ .../litecommands/message/LiteMessages.java | 6 +- .../litecommands/shared/FailedReason.java | 1 + .../litecommands/LiteCommandsFactory.java | 3 + .../litecommands/unit/AssertExecute.java | 6 +- 8 files changed, 188 insertions(+), 4 deletions(-) create mode 100644 litecommands-annotations/test/dev/rollczi/litecommands/annotations/argument/resolver/standard/BooleanArgumentTest.java create mode 100644 litecommands-core/src/dev/rollczi/litecommands/argument/resolver/standard/BooleanArgumentResolver.java diff --git a/litecommands-annotations/test/dev/rollczi/litecommands/annotations/argument/resolver/standard/BooleanArgumentTest.java b/litecommands-annotations/test/dev/rollczi/litecommands/annotations/argument/resolver/standard/BooleanArgumentTest.java new file mode 100644 index 000000000..c2bcae43e --- /dev/null +++ b/litecommands-annotations/test/dev/rollczi/litecommands/annotations/argument/resolver/standard/BooleanArgumentTest.java @@ -0,0 +1,73 @@ +package dev.rollczi.litecommands.annotations.argument.resolver.standard; + +import dev.rollczi.litecommands.annotations.LiteTestSpec; +import dev.rollczi.litecommands.annotations.argument.Arg; +import dev.rollczi.litecommands.annotations.command.Command; +import dev.rollczi.litecommands.annotations.execute.Execute; +import org.junit.jupiter.api.Test; + +class BooleanArgumentTest extends LiteTestSpec { + + @Command(name = "test") + static class TestCommand { + + @Execute(name = "primitive") + boolean test(@Arg boolean value) { + return value; + } + + @Execute(name = "object") + Boolean test(@Arg Boolean value) { + return value; + } + } + + @Test + void testPrimitive() { + platform.execute("test primitive true") + .assertSuccess(true); + + platform.execute("test primitive false") + .assertSuccess(false); + } + + @Test + void testObject() { + platform.execute("test object true") + .assertSuccess(true); + + platform.execute("test object false") + .assertSuccess(false); + } + + @Test + void testInvalid() { + platform.execute("test primitive invalid") + .assertFailure(); + + platform.execute("test object invalid") + .assertFailure(); + } + + @Test + void testSuggestions() { + platform.suggest("test primitive ") + .assertSuggest("true", "false"); + + platform.suggest("test primitive t") + .assertSuggest("true"); + + platform.suggest("test primitive f") + .assertSuggest("false"); + + platform.suggest("test object ") + .assertSuggest("true", "false"); + + platform.suggest("test object t") + .assertSuggest("true"); + + platform.suggest("test object f") + .assertSuggest("false"); + } + +} \ No newline at end of file diff --git a/litecommands-core/src/dev/rollczi/litecommands/argument/parser/ParseResult.java b/litecommands-core/src/dev/rollczi/litecommands/argument/parser/ParseResult.java index ddfd94ece..0abd9a95d 100644 --- a/litecommands-core/src/dev/rollczi/litecommands/argument/parser/ParseResult.java +++ b/litecommands-core/src/dev/rollczi/litecommands/argument/parser/ParseResult.java @@ -65,6 +65,7 @@ public static ParseResult failure(Object failedReason) { return new ParseResult<>(null, FailedReason.of(failedReason)); } + @Deprecated public static ParseResult failure() { return new ParseResult<>(null, FailedReason.empty()); } diff --git a/litecommands-core/src/dev/rollczi/litecommands/argument/resolver/standard/BooleanArgumentResolver.java b/litecommands-core/src/dev/rollczi/litecommands/argument/resolver/standard/BooleanArgumentResolver.java new file mode 100644 index 000000000..3f7856a35 --- /dev/null +++ b/litecommands-core/src/dev/rollczi/litecommands/argument/resolver/standard/BooleanArgumentResolver.java @@ -0,0 +1,62 @@ +package dev.rollczi.litecommands.argument.resolver.standard; + +import dev.rollczi.litecommands.argument.Argument; +import dev.rollczi.litecommands.argument.parser.ParseResult; +import dev.rollczi.litecommands.argument.resolver.ArgumentResolver; +import dev.rollczi.litecommands.invalidusage.InvalidUsage; +import dev.rollczi.litecommands.invocation.Invocation; +import dev.rollczi.litecommands.suggestion.Suggestion; +import dev.rollczi.litecommands.suggestion.SuggestionContext; +import dev.rollczi.litecommands.suggestion.SuggestionResult; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +public class BooleanArgumentResolver extends ArgumentResolver { + + private final Set trueValues; + private final Set falseValues; + private final SuggestionResult suggestions; + + public BooleanArgumentResolver() { + this(Collections.singletonList("true"), Collections.singletonList("false")); + } + + public BooleanArgumentResolver(Collection trueValues, Collection falseValues) { + this.trueValues = new HashSet<>(trueValues); + this.falseValues = new HashSet<>(falseValues); + SuggestionResult suggestionResult = SuggestionResult.empty(); + + for (String trueValue : trueValues) { + suggestionResult.add(Suggestion.of(trueValue)); + } + + for (String falseValue : falseValues) { + suggestionResult.add(Suggestion.of(falseValue)); + } + + this.suggestions = suggestionResult; + } + + + @Override + protected ParseResult parse(Invocation invocation, Argument context, String argument) { + if (trueValues.contains(argument)) { + return ParseResult.success(true); + } + + if (falseValues.contains(argument)) { + return ParseResult.success(false); + } + + return ParseResult.failure(InvalidUsage.Cause.INVALID_ARGUMENT); + } + + @Override + public SuggestionResult suggest(Invocation invocation, Argument argument, SuggestionContext context) { + return suggestions; + } + +} diff --git a/litecommands-core/src/dev/rollczi/litecommands/invalidusage/InvalidUsage.java b/litecommands-core/src/dev/rollczi/litecommands/invalidusage/InvalidUsage.java index f887dc981..1337ad6f3 100644 --- a/litecommands-core/src/dev/rollczi/litecommands/invalidusage/InvalidUsage.java +++ b/litecommands-core/src/dev/rollczi/litecommands/invalidusage/InvalidUsage.java @@ -28,10 +28,50 @@ public CommandRoute getLastCommand() { } public enum Cause { + + /** + * When command is not found + */ UNKNOWN_COMMAND, + + /** + * When input is invalid + * e.g. + *
+         * command: /command [int]
+         * input: /command text
+         * 
+ */ INVALID_ARGUMENT, + + /** + * When input is valid but not enough + * e.g. + *
+         * command: /command [text] [x y z]
+         * input: /command text
+         * 
+ */ MISSING_ARGUMENT, + + /** + * When input is valid but not enough (part of argument is missing) + * e.g. + *
+         * command: /command [text] [x y z]
+         * input: /command text 10 20
+         * 
+ */ MISSING_PART_OF_ARGUMENT, + + /** + * When input is valid but too much + * e.g. + *
+         * command: /command [text] [x y z]
+         * input: /command text 10 20 30 40
+         * 
+ */ TOO_MANY_ARGUMENTS } diff --git a/litecommands-core/src/dev/rollczi/litecommands/message/LiteMessages.java b/litecommands-core/src/dev/rollczi/litecommands/message/LiteMessages.java index 4f9ca9f3b..fba47e257 100644 --- a/litecommands-core/src/dev/rollczi/litecommands/message/LiteMessages.java +++ b/litecommands-core/src/dev/rollczi/litecommands/message/LiteMessages.java @@ -12,20 +12,20 @@ public class LiteMessages { */ public static final MessageKey MISSING_PERMISSIONS = MessageKey.of( "missing-permission", - missingPermissions -> String.format("You don't have permission to execute this command! (%s)", missingPermissions.asJoinedText()) + missingPermissions -> String.format("You don't have permission to execute this command! (%s) (MISSING_PERMISSIONS)", missingPermissions.asJoinedText()) ); /** * Default message key for invalid number. * It's used in {@link dev.rollczi.litecommands.argument.resolver.standard.NumberArgumentResolver} */ - public static final MessageKey INVALID_NUMBER = MessageKey.of("invalid-number", input -> String.format("'%s' is not a number!", input)); + public static final MessageKey INVALID_NUMBER = MessageKey.of("invalid-number", input -> String.format("'%s' is not a number! (INVALID_NUMBER)", input)); /** * Default message key for invalid usage. * It's used in {@link dev.rollczi.litecommands.invalidusage.InvalidUsageHandlerImpl} */ - public static final MessageKey> INVALID_USAGE = MessageKey.of("invalid-usage", invalidUsage -> "Invalid usage of command!"); + public static final MessageKey> INVALID_USAGE = MessageKey.of("invalid-usage", invalidUsage -> "Invalid usage of command! (INVALID_USAGE)"); /** * Default message key for invalid Instant format. diff --git a/litecommands-core/src/dev/rollczi/litecommands/shared/FailedReason.java b/litecommands-core/src/dev/rollczi/litecommands/shared/FailedReason.java index 8496d9370..cd28bf695 100644 --- a/litecommands-core/src/dev/rollczi/litecommands/shared/FailedReason.java +++ b/litecommands-core/src/dev/rollczi/litecommands/shared/FailedReason.java @@ -32,6 +32,7 @@ public static FailedReason of(Object reason) { return new FailedReason(reason, false); } + @Deprecated public static FailedReason empty() { return new FailedReason(null, true); } diff --git a/litecommands-framework/src/dev/rollczi/litecommands/LiteCommandsFactory.java b/litecommands-framework/src/dev/rollczi/litecommands/LiteCommandsFactory.java index fb42b9bc7..8eef51eff 100644 --- a/litecommands-framework/src/dev/rollczi/litecommands/LiteCommandsFactory.java +++ b/litecommands-framework/src/dev/rollczi/litecommands/LiteCommandsFactory.java @@ -2,6 +2,7 @@ import dev.rollczi.litecommands.argument.resolver.standard.BigDecimalArgumentResolver; import dev.rollczi.litecommands.argument.resolver.standard.BigIntegerArgumentResolver; +import dev.rollczi.litecommands.argument.resolver.standard.BooleanArgumentResolver; import dev.rollczi.litecommands.argument.resolver.standard.DurationArgumentResolver; import dev.rollczi.litecommands.argument.resolver.standard.EnumArgumentResolver; import dev.rollczi.litecommands.argument.resolver.standard.InstantArgumentResolver; @@ -67,6 +68,8 @@ public static ()) .argument(String.class, new StringArgumentResolver<>()) + .argument(Boolean.class, new BooleanArgumentResolver<>()) + .argument(boolean.class, new BooleanArgumentResolver<>()) .argument(Long.class, NumberArgumentResolver.ofLong()) .argument(long.class, NumberArgumentResolver.ofLong()) .argument(Integer.class, NumberArgumentResolver.ofInteger()) diff --git a/litecommands-unit/src/dev/rollczi/litecommands/unit/AssertExecute.java b/litecommands-unit/src/dev/rollczi/litecommands/unit/AssertExecute.java index acd7b18e4..8518d66aa 100644 --- a/litecommands-unit/src/dev/rollczi/litecommands/unit/AssertExecute.java +++ b/litecommands-unit/src/dev/rollczi/litecommands/unit/AssertExecute.java @@ -76,7 +76,11 @@ public AssertExecute assertThrows(Class exception) { public AssertExecute assertFailure() { if (!result.isFailed()) { - throw new AssertionError("Command was not failed."); + if (result.isThrown()) { + throw new AssertionError("Command was thrown", result.getThrowable()); + } + + throw new AssertionError("Command was not failed. Result: " + result.getResult()); } return this;