|
23 | 23 | // |
24 | 24 | package org.incendo.cloud.bukkit.parser; |
25 | 25 |
|
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.List; |
| 29 | +import java.util.function.Function; |
| 30 | +import java.util.stream.Collectors; |
26 | 31 | import java.util.stream.Stream; |
27 | 32 | import org.bukkit.Location; |
28 | 33 | import org.bukkit.command.CommandSender; |
29 | 34 | import org.checkerframework.checker.nullness.qual.NonNull; |
| 35 | +import org.incendo.cloud.CommandManager; |
30 | 36 | import org.incendo.cloud.bukkit.parser.rotation.Rotation; |
31 | 37 | import org.incendo.cloud.bukkit.parser.rotation.RotationParser; |
32 | 38 | import org.incendo.cloud.bukkit.util.ServerTest; |
33 | 39 | import org.incendo.cloud.context.CommandInput; |
| 40 | +import org.incendo.cloud.execution.ExecutionCoordinator; |
| 41 | +import org.incendo.cloud.internal.CommandRegistrationHandler; |
34 | 42 | import org.incendo.cloud.parser.ArgumentParseResult; |
| 43 | +import org.incendo.cloud.parser.flag.CommandFlag; |
| 44 | +import org.incendo.cloud.suggestion.Suggestion; |
35 | 45 | import org.junit.jupiter.params.ParameterizedTest; |
36 | 46 | import org.junit.jupiter.params.provider.Arguments; |
37 | 47 | import org.junit.jupiter.params.provider.MethodSource; |
38 | 48 | import org.junit.jupiter.params.provider.ValueSource; |
39 | 49 |
|
40 | 50 | import static com.google.common.truth.Truth.assertThat; |
| 51 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
41 | 52 | import static org.junit.jupiter.params.provider.Arguments.arguments; |
42 | 53 |
|
43 | 54 | public class RotationArgumentTest extends ServerTest { |
@@ -95,4 +106,75 @@ void Parse_InvalidAngle_Failure(final @NonNull String input) { |
95 | 106 | assertThat(result.parsedValue()).isEmpty(); |
96 | 107 | } |
97 | 108 |
|
| 109 | + @ParameterizedTest |
| 110 | + @MethodSource |
| 111 | + void suggestions(final String input, final List<String> expectedSuggestions) { |
| 112 | + final CommandManager<CommandSender> manager = new CommandManager<CommandSender>( |
| 113 | + ExecutionCoordinator.simpleCoordinator(), CommandRegistrationHandler.nullCommandRegistrationHandler() |
| 114 | + ) { |
| 115 | + @Override |
| 116 | + public boolean hasPermission(@NonNull CommandSender sender, @NonNull String permission) { |
| 117 | + return true; |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + manager.command( |
| 122 | + manager.commandBuilder("rotation") |
| 123 | + .required("rotation", RotationParser.rotationParser()) |
| 124 | + ); |
| 125 | + |
| 126 | + final Function<String, String> suggestion = s -> "rotation " + s; |
| 127 | + assertEquals( |
| 128 | + expectedSuggestions, |
| 129 | + manager.suggestionFactory().suggestImmediately(this.commandContext().sender(), suggestion.apply(input)) |
| 130 | + .list().stream().map(Suggestion::suggestion).collect(Collectors.toList()) |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + @ParameterizedTest |
| 135 | + @MethodSource("suggestions") |
| 136 | + void suggestionsFlag(final String input, final List<String> expectedSuggestions) { |
| 137 | + final CommandManager<CommandSender> manager = new CommandManager<CommandSender>( |
| 138 | + ExecutionCoordinator.simpleCoordinator(), CommandRegistrationHandler.nullCommandRegistrationHandler() |
| 139 | + ) { |
| 140 | + @Override |
| 141 | + public boolean hasPermission(@NonNull CommandSender sender, @NonNull String permission) { |
| 142 | + return true; |
| 143 | + } |
| 144 | + }; |
| 145 | + |
| 146 | + manager.command( |
| 147 | + manager.commandBuilder("flag") |
| 148 | + .flag(CommandFlag.builder("rot").withComponent(RotationParser.rotationParser()).build()) |
| 149 | + ); |
| 150 | + |
| 151 | + final Function<String, String> flagSuggestion = s -> "flag --rot " + s; |
| 152 | + assertEquals( |
| 153 | + expectedSuggestions, |
| 154 | + manager.suggestionFactory().suggestImmediately(this.commandContext().sender(), flagSuggestion.apply(input)) |
| 155 | + .list().stream().map(Suggestion::suggestion).collect(Collectors.toList()) |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + static Stream<Arguments> suggestions() { |
| 160 | + return Stream.of( |
| 161 | + arguments("", Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")), |
| 162 | + arguments("1", Arrays.asList("1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19")), |
| 163 | + arguments("1 ", Arrays.asList("1 0", "1 1", "1 2", "1 3", "1 4", "1 5", "1 6", "1 7", "1 8", "1 9")), |
| 164 | + arguments("1 1", Arrays.asList("1 1", "1 10", "1 11", "1 12", "1 13", "1 14", "1 15", "1 16", "1 17", "1 18", "1 19")), |
| 165 | + arguments("1 0", Collections.singletonList("1 0")), |
| 166 | + arguments("1 1 ", Collections.emptyList()), |
| 167 | + arguments("0", Collections.singletonList("0")), |
| 168 | + arguments("0 ", Arrays.asList("0 0", "0 1", "0 2", "0 3", "0 4", "0 5", "0 6", "0 7", "0 8", "0 9")), |
| 169 | + arguments("0 1", Arrays.asList("0 1", "0 10", "0 11", "0 12", "0 13", "0 14", "0 15", "0 16", "0 17", "0 18", "0 19")), |
| 170 | + arguments("0 0", Collections.singletonList("0 0")), |
| 171 | + arguments("0 0 ", Collections.emptyList()), |
| 172 | + arguments("~", Arrays.asList("~0", "~1", "~2", "~3", "~4", "~5", "~6", "~7", "~8", "~9")), |
| 173 | + arguments("~ ~", Arrays.asList("~ ~0", "~ ~1", "~ ~2", "~ ~3", "~ ~4", "~ ~5", "~ ~6", "~ ~7", "~ ~8", "~ ~9")), |
| 174 | + arguments("~0", Collections.singletonList("~0")), |
| 175 | + arguments("~1", Arrays.asList("~1", "~10", "~11", "~12", "~13", "~14", "~15", "~16", "~17", "~18", "~19")), |
| 176 | + arguments("~1 ", Arrays.asList("~1 0", "~1 1", "~1 2", "~1 3", "~1 4", "~1 5", "~1 6", "~1 7", "~1 8", "~1 9")) |
| 177 | + ); |
| 178 | + } |
| 179 | + |
98 | 180 | } |
0 commit comments