-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
32b6591
commit 4fbdd29
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...est/dev/rollczi/litecommands/annotations/argument/resolver/standard/UUIDArgumentTest.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,50 @@ | ||
package dev.rollczi.litecommands.annotations.argument.resolver.standard; | ||
|
||
import dev.rollczi.litecommands.annotations.LiteTestSpec; | ||
import dev.rollczi.litecommands.annotations.argument.Arg; | ||
import dev.rollczi.litecommands.annotations.command.Command; | ||
import dev.rollczi.litecommands.annotations.execute.Execute; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneOffset; | ||
import java.util.UUID; | ||
|
||
class UUIDArgumentTest extends LiteTestSpec { | ||
|
||
@Command(name = "test") | ||
static class TestCommand { | ||
|
||
@Execute | ||
UUID test(@Arg UUID uuid) { | ||
return uuid; | ||
} | ||
} | ||
|
||
@Test | ||
void test() { | ||
UUID uuid = UUID.fromString("e44d9727-9da7-4f68-b608-7018a085fc6b"); | ||
|
||
// with dashes | ||
platform.execute("test " + uuid) | ||
.assertSuccess(uuid); | ||
|
||
// without dashes | ||
platform.execute("test " + uuid.toString().replace("-", "")) | ||
.assertSuccess(uuid); | ||
} | ||
|
||
@Test | ||
void testInvalid() { | ||
platform.execute("test invalid") | ||
.assertFailure(); | ||
} | ||
|
||
@Test | ||
void testSuggestions() { | ||
platform.suggest("test ") | ||
.assertNotEmpty() | ||
.assertCorrect(suggestion -> platform.execute("test " + suggestion.multilevel()).assertSuccess()); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...ds-core/src/dev/rollczi/litecommands/argument/resolver/standard/UUIDArgumentResolver.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,66 @@ | ||
package dev.rollczi.litecommands.argument.resolver.standard; | ||
|
||
import dev.rollczi.litecommands.argument.Argument; | ||
import dev.rollczi.litecommands.argument.parser.ParseResult; | ||
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver; | ||
import dev.rollczi.litecommands.invalidusage.InvalidUsage; | ||
import dev.rollczi.litecommands.invocation.Invocation; | ||
import dev.rollczi.litecommands.message.LiteMessages; | ||
import dev.rollczi.litecommands.message.MessageRegistry; | ||
import dev.rollczi.litecommands.suggestion.SuggestionContext; | ||
import dev.rollczi.litecommands.suggestion.SuggestionResult; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class UUIDArgumentResolver<SENDER> extends ArgumentResolver<SENDER, UUID> { | ||
|
||
private final MessageRegistry<SENDER> messageRegistry; | ||
private final SuggestionResult suggestions; | ||
|
||
public UUIDArgumentResolver(MessageRegistry<SENDER> messageRegistry) { | ||
this.messageRegistry = messageRegistry; | ||
|
||
// We want to show 10 random UUIDs in both with-dashes and without-dashes formats | ||
suggestions = Stream | ||
.generate(() -> UUID.randomUUID().toString()) | ||
.limit(10) | ||
.map(str -> ThreadLocalRandom.current().nextBoolean() ? str.replace("-", "") : str) | ||
.collect(SuggestionResult.collector()); | ||
} | ||
|
||
@Override | ||
protected ParseResult<UUID> parse(Invocation<SENDER> invocation, Argument<UUID> context, String argument) { | ||
try { | ||
// Format with dashes | ||
if (argument.length() == 36) { | ||
return ParseResult.success(UUID.fromString(argument)); | ||
} | ||
|
||
// Format without dashes | ||
if (argument.length() == 32) { | ||
return ParseResult.success(UUID.fromString( | ||
new StringBuilder(argument) | ||
.insert(8, '-') | ||
.insert(13, '-') | ||
.insert(18, '-') | ||
.insert(23, '-') | ||
.toString() | ||
)); | ||
} | ||
} catch (IllegalArgumentException ignored) { | ||
} | ||
|
||
return ParseResult.failure(messageRegistry.getInvoked(LiteMessages.UUID_INVALID_FORMAT, invocation, argument)); | ||
} | ||
|
||
@Override | ||
public SuggestionResult suggest(Invocation<SENDER> invocation, Argument<UUID> argument, SuggestionContext context) { | ||
return suggestions; | ||
} | ||
|
||
} |
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