13
0
geforkt von Mirrors/Velocity

Test suggestions

Dieser Commit ist enthalten in:
Hugo Manrique 2021-06-07 15:15:48 +02:00
Ursprung ea716c15c6
Commit 46b1bee83a
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: A60730A4A4ACE782
5 geänderte Dateien mit 395 neuen und 7 gelöschten Zeilen

Datei anzeigen

@ -2,6 +2,7 @@ package com.velocitypowered.proxy.command;
import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger; import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.arguments.StringArgumentType.word;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
@ -91,8 +92,7 @@ public class BrigadierCommandTests extends CommandTestSuite {
assertEquals(2, context.getNodes().size()); assertEquals(2, context.getNodes().size());
callCount.incrementAndGet(); callCount.incrementAndGet();
return 1; return 1;
}) }))
)
.build(); .build();
manager.register(new BrigadierCommand(node)); manager.register(new BrigadierCommand(node));
@ -113,12 +113,73 @@ public class BrigadierCommandTests extends CommandTestSuite {
.requires(source -> { .requires(source -> {
callCount.incrementAndGet(); callCount.incrementAndGet();
return false; return false;
}) }))
)
.build(); .build();
manager.register(new BrigadierCommand(node)); manager.register(new BrigadierCommand(node));
assertHandled("hello world"); assertHandled("hello world");
assertEquals(1, callCount.get()); assertEquals(1, callCount.get());
} }
// Suggestions
@Test
void testArgumentSuggestions() {
final var node = LiteralArgumentBuilder
.<CommandSource>literal("hello")
.then(RequiredArgumentBuilder
.<CommandSource, String>argument("argument", word())
.suggests((context, builder) -> builder
.suggest("foo")
.suggest("bar")
.suggest("baz")
.buildFuture()))
.build();
manager.register(new BrigadierCommand(node));
assertSuggestions("hello ", "bar", "baz", "foo");
assertSuggestions("hello ba", "bar", "baz", "foo");
assertSuggestions("hello bar", "baz", "foo");
}
// The following 2 tests ensure we strictly follow Brigadier's behavior, even
// if it doesn't make much sense.
@Test
void testSuggestsEvenIfImpermissible() {
final var node = LiteralArgumentBuilder
.<CommandSource>literal("parent")
.then(LiteralArgumentBuilder
.<CommandSource>literal("child")
.requiresWithContext((context, reader) -> fail()))
.build();
manager.register(new BrigadierCommand(node));
assertSuggestions("parent ", "child");
assertSuggestions("parent chi", "child");
}
@Test
void testDoesNotSuggestIfImpermissibleDuringParse() {
final var callCount = new AtomicInteger();
final var node = LiteralArgumentBuilder
.<CommandSource>literal("parent")
.then(LiteralArgumentBuilder
.<CommandSource>literal("child")
.requiresWithContext((context, reader) -> {
// CommandDispatcher#parseNodes checks whether the child node can be added
// to the context object. CommandDispatcher#getCompletionSuggestions then
// considers a suggestion context with "parent" as the parent, and considers
// the suggestions of relevant children, which includes "child".
assertEquals(2, context.getNodes().size());
callCount.incrementAndGet();
return false;
}))
.build();
manager.register(new BrigadierCommand(node));
assertSuggestions("parent child");
assertEquals(1, callCount.get());
}
} }

Datei anzeigen

@ -157,10 +157,12 @@ public class CommandManagerTests {
assertTrue(manager.hasCommand("foo")); assertTrue(manager.hasCommand("foo"));
} }
static class DummyCommand implements SimpleCommand { static final class DummyCommand implements SimpleCommand {
static final DummyCommand INSTANCE = new DummyCommand(); static final DummyCommand INSTANCE = new DummyCommand();
private DummyCommand() {}
@Override @Override
public void execute(final Invocation invocation) { public void execute(final Invocation invocation) {
fail(); fail();

Datei anzeigen

@ -3,7 +3,10 @@ package com.velocitypowered.proxy.command;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
import com.google.common.collect.ImmutableList;
import com.velocitypowered.api.command.RawCommand; import com.velocitypowered.api.command.RawCommand;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -90,4 +93,103 @@ public class RawCommandTests extends CommandTestSuite {
assertHandled("color red"); assertHandled("color red");
assertEquals(1, callCount.get()); assertEquals(1, callCount.get());
} }
// Suggestions
@Test
void testSuggestsArgumentsAfterAlias() {
final var meta = manager.metaBuilder("hello").build();
manager.register(meta, new RawCommand() {
@Override
public void execute(final Invocation invocation) {
fail();
}
@Override
public List<String> suggest(final Invocation invocation) {
assertEquals("hello", invocation.alias());
assertEquals("", invocation.arguments());
return ImmutableList.of("world", "people"); // ensures we don't mutate the user's list
}
});
assertSuggestions("hello ", "people", "world"); // in alphabetical order
}
@Test
void testSuggestsArgumentsAfterPartialArguments() {
final var meta = manager.metaBuilder("numbers").build();
manager.register(meta, new RawCommand() {
@Override
public void execute(final Invocation invocation) {
fail();
}
@Override
public List<String> suggest(final Invocation invocation) {
assertEquals("12345678", invocation.arguments());
return Collections.singletonList("9");
}
});
assertSuggestions("numbers 12345678", "9");
}
@Test
void testDoesNotSuggestFirstArgumentIfImpermissibleAlias() {
final var callCount = new AtomicInteger();
final var meta = manager.metaBuilder("hello").build();
manager.register(meta, new RawCommand() {
@Override
public void execute(final Invocation invocation) {
fail();
}
@Override
public boolean hasPermission(final Invocation invocation) {
assertEquals("hello", invocation.alias());
assertEquals("", invocation.arguments());
callCount.incrementAndGet();
return false;
}
@Override
public List<String> suggest(final Invocation invocation) {
return fail();
}
});
assertSuggestions("hello ");
assertEquals(1, callCount.get());
}
@Test
void testDoesNotSuggestArgumentsAfterPartialImpermissibleArguments() {
final var callCount = new AtomicInteger();
final var meta = manager.metaBuilder("foo").build();
manager.register(meta, new RawCommand() {
@Override
public void execute(final Invocation invocation) {
fail();
}
@Override
public boolean hasPermission(final Invocation invocation) {
assertEquals("foo", invocation.alias());
assertEquals("bar baz ", invocation.arguments());
callCount.incrementAndGet();
return false;
}
@Override
public List<String> suggest(final Invocation invocation) {
return fail();
}
});
assertSuggestions("foo bar baz ");
assertEquals(1, callCount.get());
}
} }

Datei anzeigen

@ -4,7 +4,10 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
import com.google.common.collect.ImmutableList;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -91,4 +94,103 @@ public class SimpleCommandTests extends CommandTestSuite {
assertHandled("color red"); assertHandled("color red");
assertEquals(1, callCount.get()); assertEquals(1, callCount.get());
} }
// Suggestions
@Test
void testSuggestsArgumentsAfterAlias() {
final var meta = manager.metaBuilder("hello").build();
manager.register(meta, new SimpleCommand() {
@Override
public void execute(final Invocation invocation) {
fail();
}
@Override
public List<String> suggest(final Invocation invocation) {
assertEquals("hello", invocation.alias());
assertArrayEquals(new String[0], invocation.arguments());
return ImmutableList.of("world", "people"); // ensures we don't mutate the user's list
}
});
assertSuggestions("hello ", "people", "world"); // in alphabetical order
}
@Test
void testSuggestsArgumentsAfterPartialArguments() {
final var meta = manager.metaBuilder("numbers").build();
manager.register(meta, new SimpleCommand() {
@Override
public void execute(final Invocation invocation) {
fail();
}
@Override
public List<String> suggest(final Invocation invocation) {
assertArrayEquals(new String[] { "12345678" }, invocation.arguments());
return Collections.singletonList("9");
}
});
assertSuggestions("numbers 12345678", "9");
}
@Test
void testDoesNotSuggestFirstArgumentIfImpermissibleAlias() {
final var callCount = new AtomicInteger();
final var meta = manager.metaBuilder("hello").build();
manager.register(meta, new SimpleCommand() {
@Override
public void execute(final Invocation invocation) {
fail();
}
@Override
public boolean hasPermission(final Invocation invocation) {
assertEquals("hello", invocation.alias());
assertArrayEquals(new String[0], invocation.arguments());
callCount.incrementAndGet();
return false;
}
@Override
public List<String> suggest(final Invocation invocation) {
return fail();
}
});
assertSuggestions("hello ");
assertEquals(1, callCount.get());
}
@Test
void testDoesNotSuggestArgumentsAfterPartialImpermissibleArguments() {
final var callCount = new AtomicInteger();
final var meta = manager.metaBuilder("foo").build();
manager.register(meta, new SimpleCommand() {
@Override
public void execute(final Invocation invocation) {
fail();
}
@Override
public boolean hasPermission(final Invocation invocation) {
assertEquals("foo", invocation.alias());
assertArrayEquals(new String[] { "bar", "baz", "" }, invocation.arguments());
callCount.incrementAndGet();
return false;
}
@Override
public List<String> suggest(final Invocation invocation) {
return fail();
}
});
assertSuggestions("foo bar baz ");
assertEquals(1, callCount.get());
}
} }

Datei anzeigen

@ -17,6 +17,127 @@
package com.velocitypowered.proxy.command; package com.velocitypowered.proxy.command;
public class SuggestionsProviderTests { import static com.mojang.brigadier.arguments.StringArgumentType.word;
// TODO import static org.junit.jupiter.api.Assertions.fail;
import com.google.common.collect.ImmutableList;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.velocitypowered.api.command.Command;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.RawCommand;
import java.util.List;
import org.junit.jupiter.api.Test;
/**
* Tests {@link Command} implementation-independent suggestion methods of
* {@link SuggestionsProvider}.
*/
public class SuggestionsProviderTests extends CommandTestSuite {
@Test
void testSuggestsAliasForEmptyInput() {
final var meta = manager.metaBuilder("foo")
.aliases("bar", "baz")
.build();
manager.register(meta, NoSuggestionsCommand.INSTANCE);
assertSuggestions("", "bar", "baz", "foo");
}
@Test
void testDoesNotSuggestForLeadingWhitespace() {
final var meta = manager.metaBuilder("hello").build();
manager.register(meta, NoSuggestionsCommand.INSTANCE);
assertSuggestions(" ");
}
@Test
void testSuggestsAliasesForPartialAlias() {
final var meta = manager.metaBuilder("foo")
.aliases("bar", "baz")
.build();
manager.register(meta, NoSuggestionsCommand.INSTANCE);
assertSuggestions("ba", "bar", "baz");
assertSuggestions("fo", "foo");
assertSuggestions("bar");
}
@Test
void testSuggestsHintLiteral() {
final var hint = LiteralArgumentBuilder
.<CommandSource>literal("hint")
.build();
final var meta = manager.metaBuilder("hello")
.hint(hint)
.build();
manager.register(meta, NoSuggestionsCommand.INSTANCE);
assertSuggestions("hello ", "hint");
assertSuggestions("hello hin", "hint");
assertSuggestions("hello hint");
}
@Test
void testSuggestsHintCustomSuggestions() {
final var hint = RequiredArgumentBuilder
.<CommandSource, String>argument("hint", word())
.suggests((context, builder) -> builder
.suggest("one")
.suggest("two")
.suggest("three")
.buildFuture())
.build();
final var meta = manager.metaBuilder("hello")
.hint(hint)
.build();
manager.register(meta, NoSuggestionsCommand.INSTANCE);
assertSuggestions("hello ", "one", "three", "two");
assertSuggestions("hello two", "one", "three");
}
@Test
void testSuggestsMergesArgumentsSuggestionsWithHintSuggestions() {
final var hint = LiteralArgumentBuilder
.<CommandSource>literal("bar")
.build();
final var meta = manager.metaBuilder("foo")
.hint(hint)
.build();
manager.register(meta, new RawCommand() {
@Override
public void execute(final Invocation invocation) {
fail();
}
@Override
public List<String> suggest(final Invocation invocation) {
return ImmutableList.of("baz", "qux");
}
});
// TODO Fix merging
assertSuggestions("hello ", "bar", "baz", "qux");
assertSuggestions("hello bar", "baz", "qux");
}
static final class NoSuggestionsCommand implements RawCommand {
static final NoSuggestionsCommand INSTANCE = new NoSuggestionsCommand();
private NoSuggestionsCommand() {}
@Override
public void execute(final Invocation invocation) {
fail();
}
@Override
public List<String> suggest(final Invocation invocation) {
return ImmutableList.of();
}
}
} }