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-499 Fix argument range validation while parsing. #499

Merged
merged 1 commit into from
Dec 16, 2024
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
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Versions {
// Tests
const val JUNIT_JUPITER = "5.11.3"
const val ASSERTJ = "3.26.3"
const val MOCKITO = "5.14.2"
const val MOCKITO = "4.11.0"
const val AWAITILITY = "4.2.2"

// Bukkit
Expand Down
12 changes: 6 additions & 6 deletions buildSrc/src/main/kotlin/litecommands-unit-test.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ dependencies {
testImplementation(project(":litecommands-unit"))
testImplementation(kotlin("stdlib-jdk8"))

testImplementation("org.mockito:mockito-core:5.14.2")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.3")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.11.3")
testImplementation("org.assertj:assertj-core:3.26.3")
testImplementation("org.awaitility:awaitility:4.2.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.11.3")
testImplementation("org.mockito:mockito-core:${Versions.MOCKITO}")
testImplementation("org.junit.jupiter:junit-jupiter-api:${Versions.JUNIT_JUPITER}")
testImplementation("org.junit.jupiter:junit-jupiter-params:${Versions.JUNIT_JUPITER}")
testImplementation("org.assertj:assertj-core:${Versions.ASSERTJ}")
testImplementation("org.awaitility:awaitility:${Versions.AWAITILITY}")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${Versions.JUNIT_JUPITER}")
}

tasks.getByName<Test>("test") {
Expand Down
4 changes: 2 additions & 2 deletions litecommands-bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
`litecommands-java`
`litecommands-java-8`
`litecommands-unit-test`
`litecommands-repositories`
`litecommands-publish`
}
Expand All @@ -11,10 +12,9 @@ dependencies {
compileOnly("org.spigotmc:spigot-api:${Versions.SPIGOT_API}")
compileOnly("org.spigotmc:spigot:${Versions.SPIGOT}")
compileOnly("com.comphenix.protocol:ProtocolLib:${Versions.PROTOCOL_LIB}")
testImplementation("org.spigotmc:spigot-api:${Versions.SPIGOT_API}")
}

val bukkitArtifact: String by extra

litecommandsPublish {
artifactId = "litecommands-bukkit"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.rollczi.litecommands.bukkit.argument;

import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.unit.TestUtil;
import org.bukkit.command.CommandSender;
import org.mockito.Mockito;

public class BukkitTestSpec {

protected static Invocation<CommandSender> invocation(String command, String... args) {
CommandSender sender = Mockito.mock(CommandSender.class);
return TestUtil.invocation(sender, command, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package dev.rollczi.litecommands.bukkit.argument;

import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.ArgumentKey;
import dev.rollczi.litecommands.argument.parser.ParserRegistryImpl;
import dev.rollczi.litecommands.argument.resolver.optional.OptionalArgumentResolver;
import dev.rollczi.litecommands.input.raw.RawInput;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.message.MessageRegistry;
import dev.rollczi.litecommands.reflect.type.TypeRange;
import dev.rollczi.litecommands.reflect.type.TypeToken;
import dev.rollczi.litecommands.requirement.RequirementResult;
import java.util.Optional;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class LocationArgumentTest extends BukkitTestSpec {


public static final Argument<Optional<Location>> ARGUMENT = Argument.of("location", new TypeToken<Optional<Location>>() {});
private ParserRegistryImpl<CommandSender> parsers = new ParserRegistryImpl<>();

@BeforeEach
void before() {
parsers.registerParser(Location.class, ArgumentKey.DEFAULT, new LocationArgument(new MessageRegistry<>()));
parsers.registerParser(TypeRange.same(Optional.class), ArgumentKey.DEFAULT, new OptionalArgumentResolver<>());
}

@Test
void test() {
Invocation<CommandSender> invocation = invocation("test", "pos1");
RequirementResult<Optional<Location>> result = parsers.parse(invocation, ARGUMENT, RawInput.of("1", "2", "3"))
.await();

assertTrue(result.isSuccessful());
assertThat(result.getSuccess())
.hasValue(new Location(null, 1, 2, 3));
}

@Test
void testFail() {
Invocation<CommandSender> invocation = invocation("test", "pos1");
RequirementResult<Optional<Location>> result = parsers.parse(invocation, ARGUMENT, RawInput.of("pos1"))
.await();

assertTrue(result.isFailed());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.ArgumentKey;
import dev.rollczi.litecommands.input.raw.RawInput;
import dev.rollczi.litecommands.invalidusage.InvalidUsage;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.range.Range;
import dev.rollczi.litecommands.reflect.type.TypeRange;
import dev.rollczi.litecommands.reflect.type.TypeIndex;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.Optional;
import org.jetbrains.annotations.NotNull;

public class ParserRegistryImpl<SENDER> implements ParserRegistry<SENDER>, ParserChainAccessor<SENDER> {
Expand Down Expand Up @@ -94,8 +97,20 @@ public <T> Parser<SENDER, T> getParserOrNull(Argument<T> argument) {
@Override
public <T> ParseResult<T> parse(Invocation<SENDER> invocation, Argument<T> argument, RawInput input) {
Parser<SENDER, T> parser = getParser(argument);
Range range = parser.getRange(argument);

return parser.parse(invocation, argument, input);
if (range.isInRangeOrAbove(input.size())) {
return parser.parse(invocation, argument, input);
}

if (!input.hasNext()) {
Optional<ParseResult<T>> optional = argument.getDefaultValue();

return optional
.orElseGet(() -> ParseResult.failure(InvalidUsage.Cause.MISSING_ARGUMENT));
}

return ParseResult.failure(InvalidUsage.Cause.MISSING_PART_OF_ARGUMENT);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ public static Invocation<TestSender> invocation(String command, String... args)
return new Invocation<>(new TestSender(), new TestPlatformSender(), command, command, ParseableInput.raw(args));
}

public static <SENDER> Invocation<SENDER> invocation(SENDER sender, String command, String... args) {
return new Invocation<>(sender, new TestPlatformSender(), command, command, ParseableInput.raw(args));
}

}
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ include(":litecommands-jakarta", VERSION_11)
// platforms
include(":litecommands-velocity", VERSION_11, tests = false)
include(":litecommands-bungee", tests = false)
include(":litecommands-bukkit", tests = false)
include(":litecommands-bukkit")
include(":litecommands-minestom", VERSION_21)
include("litecommands-jda", VERSION_11)
include(":litecommands-sponge", VERSION_21, tests = false)
Expand Down
Loading