Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-288 Add boolean argument. Add javadocs for InvalidUsage.Cause. #288

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public static <EXPECTED> ParseResult<EXPECTED> failure(Object failedReason) {
return new ParseResult<>(null, FailedReason.of(failedReason));
}

@Deprecated
public static <EXPECTED> ParseResult<EXPECTED> failure() {
return new ParseResult<>(null, FailedReason.empty());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SENDER> extends ArgumentResolver<SENDER, Boolean> {

private final Set<String> trueValues;
private final Set<String> falseValues;
private final SuggestionResult suggestions;

public BooleanArgumentResolver() {
this(Collections.singletonList("true"), Collections.singletonList("false"));
}

public BooleanArgumentResolver(Collection<String> trueValues, Collection<String> 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<Boolean> parse(Invocation<SENDER> invocation, Argument<Boolean> 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<SENDER> invocation, Argument<Boolean> argument, SuggestionContext context) {
return suggestions;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,50 @@ public CommandRoute<SENDER> getLastCommand() {
}

public enum Cause {

/**
* When command is not found
*/
UNKNOWN_COMMAND,

/**
* When input is invalid
* e.g.
* <pre>
* command: /command [int]
* input: /command text
* </pre>
*/
INVALID_ARGUMENT,

/**
* When input is valid but not enough
* e.g.
* <pre>
* command: /command [text] [x y z]
* input: /command text
* </pre>
*/
MISSING_ARGUMENT,

/**
* When input is valid but not enough (part of argument is missing)
* e.g.
* <pre>
* command: /command [text] [x y z]
* input: /command text 10 20
* </pre>
*/
MISSING_PART_OF_ARGUMENT,

/**
* When input is valid but too much
* e.g.
* <pre>
* command: /command [text] [x y z]
* input: /command text 10 20 30 40
* </pre>
*/
TOO_MANY_ARGUMENTS
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ public class LiteMessages {
*/
public static final MessageKey<MissingPermissions> 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<String> INVALID_NUMBER = MessageKey.of("invalid-number", input -> String.format("'%s' is not a number!", input));
public static final MessageKey<String> 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<InvalidUsage<?>> INVALID_USAGE = MessageKey.of("invalid-usage", invalidUsage -> "Invalid usage of command!");
public static final MessageKey<InvalidUsage<?>> INVALID_USAGE = MessageKey.of("invalid-usage", invalidUsage -> "Invalid usage of command! (INVALID_USAGE)");

/**
* Default message key for invalid Instant format.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,6 +68,8 @@ public static <SENDER, C extends PlatformSettings, B extends LiteCommandsBaseBui
.validator(Scope.global(), new MissingPermissionValidator<>())

.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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ public AssertExecute assertThrows(Class<? extends Throwable> 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;
Expand Down