-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create text instruction parameters
- Loading branch information
Showing
6 changed files
with
156 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
extensions/rimor-params/src/main/java/st/networkers/rimor/params/parse/support/Text.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package st.networkers.rimor.params.parse.support; | ||
|
||
import st.networkers.rimor.annotation.RimorQualifier; | ||
import st.networkers.rimor.params.InstructionParam; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Indicates that a {@code String} {@link InstructionParam parameter} is a text parameter, i.e., the following command | ||
* parameters will be concatenated into a single String and this parameter won't only contain the first word of the text. | ||
* | ||
* <p>The {@link InstructionParam#index() parameter index} indicates the first parameter that counts as text. If the | ||
* number of parameters in an execution is less than the index, the whole text parameter will be {@code null} (instead | ||
* of an empty string). | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.PARAMETER) | ||
@RimorQualifier | ||
public @interface Text { | ||
} |
38 changes: 38 additions & 0 deletions
38
...ms/src/main/java/st/networkers/rimor/params/parse/support/TextInstructionParamParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package st.networkers.rimor.params.parse.support; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import st.networkers.rimor.annotation.RequireQualifiers; | ||
import st.networkers.rimor.context.ExecutionContext; | ||
import st.networkers.rimor.context.Token; | ||
import st.networkers.rimor.params.InstructionParam; | ||
import st.networkers.rimor.params.parse.AbstractInstructionParamParser; | ||
|
||
import java.util.List; | ||
|
||
@RequireQualifiers({InstructionParam.class, Text.class}) | ||
public class TextInstructionParamParser extends AbstractInstructionParamParser<String> { | ||
|
||
public TextInstructionParamParser() { | ||
super(String.class); | ||
} | ||
|
||
@Override | ||
public String get(Token<String> token, ExecutionContext context) { | ||
int index = getIndex(token, context); | ||
if (index < 0) | ||
return null; | ||
|
||
List<Object> commandParameters = context.get(PARAMS_TOKEN) | ||
.orElseThrow(() -> new IllegalArgumentException("there is no @InstructionParams annotated List<String> in the execution context!")); | ||
|
||
return index < commandParameters.size() | ||
? StringUtils.join(commandParameters.subList(index, commandParameters.size()), " ") | ||
: null; | ||
} | ||
|
||
@Override | ||
public String parse(Object parameter, Token<String> token, ExecutionContext context) { | ||
// unused, overriding #get(Token, ExecutionContext) | ||
return null; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...rc/test/java/st/networkers/rimor/params/parse/support/TextInstructionParamParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package st.networkers.rimor.params.parse.support; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import st.networkers.rimor.context.ExecutionContext; | ||
import st.networkers.rimor.context.Token; | ||
import st.networkers.rimor.params.InstructionParamImpl; | ||
import st.networkers.rimor.params.parse.AbstractInstructionParamParser; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class TextInstructionParamParserTest { | ||
|
||
@Test | ||
void givenTextParameterOfIndex0_whenGettingText_containsAllParams() { | ||
Token<String> token = Token.of(String.class) | ||
.annotatedWith(new InstructionParamImpl(null, null, 0)) | ||
.annotatedWith(Text.class); | ||
|
||
ExecutionContext context = ExecutionContext.builder() | ||
.bind(AbstractInstructionParamParser.PARAMS_TOKEN, Arrays.asList("foo", "bar", "baz", "qux", "quux")) | ||
.build(); | ||
|
||
assertThat(new TextInstructionParamParser().get(token, context)).isEqualTo("foo bar baz qux quux"); | ||
} | ||
|
||
@Test | ||
void givenTextParameterOfIndex2_whenGettingText_containsAllButFirstTwoParams() { | ||
Token<String> token = Token.of(String.class) | ||
.annotatedWith(new InstructionParamImpl(null, null, 2)) | ||
.annotatedWith(Text.class); | ||
|
||
ExecutionContext context = ExecutionContext.builder() | ||
.bind(AbstractInstructionParamParser.PARAMS_TOKEN, Arrays.asList("foo", "bar", "baz", "qux", "quux")) | ||
.build(); | ||
|
||
assertThat(new TextInstructionParamParser().get(token, context)).isEqualTo("baz qux quux"); | ||
} | ||
|
||
@Test | ||
void givenTextParameterOfIndexOutOfBounds_whenGettingText_returnsNull() { | ||
Token<String> token = Token.of(String.class) | ||
.annotatedWith(new InstructionParamImpl(null, null, 2)) | ||
.annotatedWith(Text.class); | ||
|
||
ExecutionContext context = ExecutionContext.builder() | ||
.bind(AbstractInstructionParamParser.PARAMS_TOKEN, Arrays.asList("foo", "bar")) | ||
.build(); | ||
|
||
assertThat(new TextInstructionParamParser().get(token, context)).isNull(); | ||
} | ||
|
||
@Test | ||
void givenTextParameter_whenGettingText_stringRepresentationOfEachParamIsUsed() { | ||
Object mockedObject = mock(Object.class); | ||
when(mockedObject.toString()).thenReturn("baz"); | ||
|
||
Token<String> token = Token.of(String.class) | ||
.annotatedWith(new InstructionParamImpl(null, null, 0)) | ||
.annotatedWith(Text.class); | ||
|
||
ExecutionContext context = ExecutionContext.builder() | ||
.bind(AbstractInstructionParamParser.PARAMS_TOKEN, Arrays.asList("foo", "bar", mockedObject)) | ||
.build(); | ||
|
||
assertThat(new TextInstructionParamParser().get(token, context)).isEqualTo("foo bar baz"); | ||
} | ||
} |