|
| 1 | +// |
| 2 | +// MIT License |
| 3 | +// |
| 4 | +// Copyright (c) 2024 Incendo |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in all |
| 14 | +// copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +// SOFTWARE. |
| 23 | +// |
| 24 | +package org.incendo.cloud.feature; |
| 25 | + |
| 26 | +import java.util.concurrent.CompletionException; |
| 27 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 28 | +import org.incendo.cloud.CommandManager; |
| 29 | +import org.incendo.cloud.TestCommandSender; |
| 30 | +import org.incendo.cloud.component.CommandComponent; |
| 31 | +import org.incendo.cloud.component.preprocessor.ComponentPreprocessor; |
| 32 | +import org.incendo.cloud.context.CommandContext; |
| 33 | +import org.incendo.cloud.context.CommandInput; |
| 34 | +import org.incendo.cloud.exception.ArgumentParseException; |
| 35 | +import org.incendo.cloud.execution.CommandResult; |
| 36 | +import org.incendo.cloud.parser.ArgumentParseResult; |
| 37 | +import org.junit.jupiter.api.BeforeEach; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | + |
| 40 | +import static com.google.common.truth.Truth.assertThat; |
| 41 | +import static org.incendo.cloud.parser.standard.StringParser.stringParser; |
| 42 | +import static org.incendo.cloud.util.TestUtils.createManager; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 44 | + |
| 45 | +class ComponentPreprocessorTest { |
| 46 | + |
| 47 | + private CommandManager<TestCommandSender> commandManager; |
| 48 | + |
| 49 | + @BeforeEach void setup() { |
| 50 | + this.commandManager = createManager(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void Parse_HappyFlow_Success() throws Exception { |
| 55 | + // Arrange |
| 56 | + final ComponentPreprocessor<TestCommandSender> preprocessor = new TestPreprocessor(true); |
| 57 | + this.commandManager.command( |
| 58 | + this.commandManager.commandBuilder("test") |
| 59 | + .required(CommandComponent.<TestCommandSender, String>builder("arg", |
| 60 | + stringParser()).preprocessor(preprocessor)) |
| 61 | + ); |
| 62 | + |
| 63 | + // Act |
| 64 | + final CommandResult<TestCommandSender> result = |
| 65 | + this.commandManager.commandExecutor().executeCommand(new TestCommandSender(), "test abc").join(); |
| 66 | + |
| 67 | + // Assert |
| 68 | + assertThat(result.commandContext().<String>get("arg")).isEqualTo("abc"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void Parse_Failing_ThrowsParseException() throws Exception { |
| 73 | + // Arrange |
| 74 | + final ComponentPreprocessor<TestCommandSender> preprocessor = new TestPreprocessor(false); |
| 75 | + this.commandManager.command( |
| 76 | + this.commandManager.commandBuilder("test") |
| 77 | + .required(CommandComponent.<TestCommandSender, String>builder("arg", |
| 78 | + stringParser()).preprocessor(preprocessor)) |
| 79 | + ); |
| 80 | + |
| 81 | + // Act & Assert |
| 82 | + final CompletionException failure = assertThrows(CompletionException.class, () -> |
| 83 | + this.commandManager.commandExecutor().executeCommand(new TestCommandSender(), "test abc").join()); |
| 84 | + assertThat(failure).hasCauseThat().isInstanceOf(ArgumentParseException.class); |
| 85 | + } |
| 86 | + |
| 87 | + private static class TestPreprocessor implements ComponentPreprocessor<TestCommandSender> { |
| 88 | + |
| 89 | + private final boolean result; |
| 90 | + |
| 91 | + TestPreprocessor(final boolean result) { |
| 92 | + this.result = result; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public @NonNull ArgumentParseResult<Boolean> preprocess( |
| 97 | + final @NonNull CommandContext<TestCommandSender> context, |
| 98 | + final @NonNull CommandInput commandInput |
| 99 | + ) { |
| 100 | + if (this.result) { |
| 101 | + return ArgumentParseResult.success(true); |
| 102 | + } |
| 103 | + return ArgumentParseResult.failure(new RuntimeException("no")); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments